Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proof-of-concept for zero-copy transfer to python
Browse files Browse the repository at this point in the history
Olaf Schumann committed Oct 21, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent aa0c1ff commit fc0a400
Showing 3 changed files with 70 additions and 0 deletions.
56 changes: 56 additions & 0 deletions python/src/section/8/FissionYieldData.python.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// system includes
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

// local includes
#include "ENDFtk/section/8/FissionYieldData.hpp"
@@ -134,6 +135,61 @@ void wrap_8_FissionYieldData( python::module& module, python::module& viewmodule
{ return self.Y(); },
"The fission yield values and uncertainties"
)
.def_property_readonly(

"Ypy",
[](const Component& self ) {

// Array shape and stride
std::vector<size_t> shape = { self.NFP(), 2 };
std::vector<size_t> stride = { 4*sizeof(double), sizeof(double) };

// create an array descriptor. If the handle (last parameter) is not void, the data is not copied.
// python::none() is the most simple type to achive this
auto array = python::array(
shape,
stride,
self.listPtr() + 2, // drop(2)
python::none());

// make the array read-only. hack from https://github.com/pybind/pybind11/issues/481
reinterpret_cast<python::detail::PyArray_Proxy*>(array.ptr())->flags &= ~python::detail::npy_api::NPY_ARRAY_WRITEABLE_;

// Rerutn the array
return array;

},
"The fission yield values and uncertainties as numpy array"
)
.def_property_readonly(

"ZAFPpy",
[](const Component& self ) {

// Array shape and stride. The underlying data are still doubles
std::vector<size_t> shape = { self.NFP() };
std::vector<size_t> stride = { 4*sizeof(double) };

// create an array descriptor. If the handle (last parameter) is not void, the data is not copied.
// python::none() is the most simple type to achive this
auto array = python::array(
shape,
stride,
self.listPtr(),
python::none());

// make the array read-only. hack from https://github.com/pybind/pybind11/issues/481
reinterpret_cast<python::detail::PyArray_Proxy*>(array.ptr())->flags &= ~python::detail::npy_api::NPY_ARRAY_WRITEABLE_;

// Function to convert array to ont. This will do a copy and reduce performance.
// Could be a proper function in a real implementation.
auto toInt = [](const python::array_t<long long>& a) -> python::array_t<long long> { return a; };

// Return integer array
return toInt(array);
},
"The fission product ZA identifiers as numpy array"
)
.def_property_readonly(

"fission_yields",
7 changes: 7 additions & 0 deletions src/ENDFtk/ListRecord.hpp
Original file line number Diff line number Diff line change
@@ -127,6 +127,13 @@ namespace ENDFtk {
return std20::views::all( this->data );
}

/**
* @brief Return a pointer to the list of values for zero-copy transfer to python
*/
auto listPtr() const {
return this->data.data();
}

/**
* @brief Return the list of values
*/
7 changes: 7 additions & 0 deletions src/ENDFtk/section/8/FissionYieldData.hpp
Original file line number Diff line number Diff line change
@@ -117,6 +117,13 @@ namespace section{
| std23::views::stride( 2 );
}

/**
* @brief Return a pointer to the list of values for zero-copy transfer to python
*/
auto listPtr() const {
return ListRecord::listPtr(); // ListRecord::listPtr() is protected
}

/**
* @brief Return the fission yield values and uncertainties
*/

0 comments on commit fc0a400

Please sign in to comment.