|
// Helper to convert 1D numpy array to Omega_h::Read |
|
template <typename T> |
|
Omega_h::Read<T> numpy_to_omega_h_read(py::array_t<T> arr) |
|
{ |
|
py::buffer_info buf = arr.request(); |
|
if (buf.ndim != 1) { |
|
throw std::runtime_error("Number of dimensions must be 1"); |
|
} |
|
Kokkos::View<T*, Kokkos::DefaultExecutionSpace::memory_space, |
|
Kokkos::MemoryTraits<Kokkos::Unmanaged>> |
|
view(reinterpret_cast<T*>(buf.ptr), buf.shape[0]); |
|
Omega_h::Write<T> write_view(view); |
|
Omega_h::Read<T> read_view(write_view); |
|
return read_view; |
|
} |
|
|
|
// Helper to convert Omega_h::Read to numpy array (creates a copy) |
|
template <typename T> |
|
py::array_t<T> omega_h_read_to_numpy(Omega_h::Read<T> read_view) |
|
{ |
|
auto read_view_host = Omega_h::HostRead<T>(read_view); |
|
py::array_t<T> result(read_view_host.size()); |
|
py::buffer_info buf = result.request(); |
|
T* ptr = static_cast<T*>(buf.ptr); |
|
for (Omega_h::LO i = 0; i < read_view_host.size(); ++i) { |
|
ptr[i] = read_view_host[i]; |
|
} |
|
return result; |
|
} |
|
|
|
// Helper to convert 1D numpy array to Omega_h::Write |
|
template <typename T> |
|
Omega_h::Write<T> numpy_to_omega_h_write(py::array_t<T> arr) |
|
{ |
|
py::buffer_info buf = arr.request(); |
|
if (buf.ndim != 1) { |
|
throw std::runtime_error("Number of dimensions must be 1"); |
|
} |
|
// Get host mirror and copy data |
|
auto write_view_host = Omega_h::HostWrite<T>(buf.shape[0]); |
|
T* ptr = static_cast<T*>(buf.ptr); |
|
for (Omega_h::LO i = 0; i < buf.shape[0]; ++i) { |
|
write_view_host[i] = ptr[i]; |
|
} |
|
auto write_view = Omega_h::Write<T>(write_view_host); |
|
return write_view; |
|
} |
|
|
|
// Helper to convert Omega_h::Write to numpy array (creates a reference) |
|
template <typename T> |
|
py::array_t<T> omega_h_write_to_numpy(Omega_h::Write<T> write_view) |
|
{ |
|
return py::array_t<T>({static_cast<py::ssize_t>(write_view.size())}, // shape |
|
{sizeof(T)}, // strides |
|
write_view.data(), // data pointer |
|
py::cast(write_view) // base object to manage lifetime |
|
); |
|
} |
|
|
pcms/src/pcms/pythonapi/numpy_array_transform.h
Lines 182 to 240 in 7b5ff9f
These functions are already defined in Omega_h:
https://github.com/SCOREC/omega_h/blob/cb4fabbf6f00a02a8df29c179d22212daeb32ea0/src/PyOmega_h_numpy_transform.hpp#L13-L69