diff --git a/src/pcms/localization/point_search.cpp b/src/pcms/localization/point_search.cpp index f71481074..7dae84237 100644 --- a/src/pcms/localization/point_search.cpp +++ b/src/pcms/localization/point_search.cpp @@ -65,8 +65,8 @@ LO GetOwningElementId(Omega_h::Mesh& mesh, int mesh_dim, int entity_dim, KOKKOS_INLINE_FUNCTION AABBox<2> triangle_bbox(const Omega_h::Matrix<2, 3>& coords) { - std::array max{coords(0, 0), coords(1, 0)}; - std::array min{coords(0, 0), coords(1, 0)}; + Kokkos::Array max{coords(0, 0), coords(1, 0)}; + Kokkos::Array min{coords(0, 0), coords(1, 0)}; for (int i = 1; i < 3; ++i) { max[0] = std::fmax(max[0], coords(0, i)); max[1] = std::fmax(max[1], coords(1, i)); @@ -185,7 +185,7 @@ template // each dimension has a pair of opposing "walls" // 2D: { [left, right], [top, bottom] } -> { left, right, top, bottom } // 3D: { [left, right], [top, bottom], [front, back] } -> { left, ..., back } - std::array bbox_walls{}; + Kokkos::Array bbox_walls{}; for (int i = 0; i < dim; i++) { bbox_walls[i * 2] = bbox.center[i] - bbox.half_width[i]; bbox_walls[i * 2 + 1] = bbox.center[i] + bbox.half_width[i]; @@ -751,8 +751,8 @@ GridPointSearch3D::GridPointSearch3D(Omega_h::Mesh& mesh, LO Nx, LO Ny, LO Nz, auto mesh_bbox = Omega_h::get_bounding_box<3>(&mesh); auto grid_h = Kokkos::create_mirror_view(grid_); - std::array edge_lengths{}; - std::array bot_left{}; + Kokkos::Array edge_lengths{}; + Kokkos::Array bot_left{}; for (int i = 0; i < DIM; ++i) { edge_lengths[i] = mesh_bbox.max[i] - mesh_bbox.min[i]; diff --git a/src/pcms/pythonapi/bind_uniform_grid_field_layout.cpp b/src/pcms/pythonapi/bind_uniform_grid_field_layout.cpp index 96d0286ab..a7c0be552 100644 --- a/src/pcms/pythonapi/bind_uniform_grid_field_layout.cpp +++ b/src/pcms/pythonapi/bind_uniform_grid_field_layout.cpp @@ -1,6 +1,7 @@ #include #include #include +#include "numpy_array_transform.h" #include "pcms/field/layout/uniform_grid.h" #include "pcms/utility/uniform_grid.h" diff --git a/src/pcms/pythonapi/numpy_array_transform.h b/src/pcms/pythonapi/numpy_array_transform.h index ccf498fae..2d891c0f3 100644 --- a/src/pcms/pythonapi/numpy_array_transform.h +++ b/src/pcms/pythonapi/numpy_array_transform.h @@ -7,6 +7,48 @@ namespace py = pybind11; +// Mirror pybind11's built-in std::array type caster for Kokkos::Array so that +// def_readwrite / def_property members work with Python sequences. +namespace pybind11::detail +{ +template +struct type_caster> +{ + using ArrayType = Kokkos::Array; + using value_conv = make_caster; + + bool load(handle src, bool convert) + { + if (!isinstance(src)) + return false; + auto seq = reinterpret_borrow(src); + if (seq.size() != Size) + return false; + size_t i = 0; + for (auto it : seq) { + value_conv conv; + if (!conv.load(it, convert)) + return false; + value[i++] = cast_op(conv); + } + return true; + } + + template + static handle cast(T&& src, return_value_policy policy, handle parent) + { + list l(Size); + size_t i = 0; + for (auto& v : src) + l[i++] = make_caster::cast(v, policy, parent); + return l.release(); + } + + PYBIND11_TYPE_CASTER(ArrayType, const_name("List[") + + make_caster::name + const_name("]")); +}; +} // namespace pybind11::detail + namespace pcms { diff --git a/src/pcms/utility/arrays.h b/src/pcms/utility/arrays.h index 71aa6d19c..b8582f988 100644 --- a/src/pcms/utility/arrays.h +++ b/src/pcms/utility/arrays.h @@ -70,6 +70,12 @@ struct arr_trait> using type = T; }; +template +struct arr_trait> +{ + using type = T; +}; + template using element_type_t = typename arr_trait::value>::type; diff --git a/src/pcms/utility/uniform_grid.h b/src/pcms/utility/uniform_grid.h index 1a478fec3..07a394d4c 100644 --- a/src/pcms/utility/uniform_grid.h +++ b/src/pcms/utility/uniform_grid.h @@ -4,7 +4,8 @@ #include "Omega_h_vector.hpp" #include "Omega_h_bbox.hpp" #include "Omega_h_mesh.hpp" -#include +#include +#include namespace pcms { @@ -12,15 +13,17 @@ template struct UniformGrid { // Make private? - std::array edge_length; - std::array bot_left; - std::array divisions; + Kokkos::Array edge_length; + Kokkos::Array bot_left; + Kokkos::Array divisions; public: [[nodiscard]] LO GetNumCells() const { - return std::accumulate(divisions.begin(), divisions.end(), 1, - std::multiplies{}); + LO total = 1; + for (std::size_t i = 0; i < dim; ++i) + total *= divisions[i]; + return total; } /// return the grid cell ID that the input point is inside or closest to if /// the point lies outside @@ -30,13 +33,13 @@ struct UniformGrid [[nodiscard]] KOKKOS_INLINE_FUNCTION LO ClosestCellID(const Omega_h::Vector& point) const { - std::array distance_within_grid; + Kokkos::Array distance_within_grid; for (size_t i = 0; i < dim; ++i) { distance_within_grid[i] = point[i] - bot_left[i]; } - std::array indexes; + Kokkos::Array indexes; for (auto& index : indexes) { index = -1; @@ -86,14 +89,14 @@ struct UniformGrid return true; } - [[nodiscard]] KOKKOS_INLINE_FUNCTION std::array GetDimensionedIndex( - LO idx) const + [[nodiscard]] KOKKOS_INLINE_FUNCTION Kokkos::Array + GetDimensionedIndex(LO idx) const { LO stride = 1; - for (std::size_t i = 0; i < divisions.size() - 1; ++i) { + for (std::size_t i = 0; i < dim - 1; ++i) { stride *= divisions[i]; } - std::array result; + Kokkos::Array result; for (size_t i = 0; i < dim; ++i) { result[i] = idx / stride; @@ -105,7 +108,7 @@ struct UniformGrid } [[nodiscard]] KOKKOS_INLINE_FUNCTION LO - GetCellIndex(std::array dimensionedIndex) const + GetCellIndex(Kokkos::Array dimensionedIndex) const { // note that the indexes refer to row/columns which have the opposite order // of the coordinates i.e. x,y @@ -124,9 +127,9 @@ struct UniformGrid private: template - KOKKOS_INLINE_FUNCTION static void reverse(std::array& arr) + KOKKOS_INLINE_FUNCTION static void reverse(Kokkos::Array& arr) { - for (size_t i = 0, j = arr.size() - 1; i < j; ++i, --j) { + for (size_t i = 0, j = N - 1; i < j; ++i, --j) { auto temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; @@ -158,16 +161,18 @@ UniformGrid CreateUniformGridFromMesh(Omega_h::Mesh& mesh, auto bbox = Omega_h::get_bounding_box(&mesh); // Calculate edge lengths and bottom-left corner - std::array edge_length; - std::array bot_left; + Kokkos::Array edge_length; + Kokkos::Array bot_left; + Kokkos::Array divs; for (unsigned i = 0; i < dim; ++i) { bot_left[i] = bbox.min[i]; edge_length[i] = bbox.max[i] - bbox.min[i]; + divs[i] = divisions[i]; } return UniformGrid{ - .edge_length = edge_length, .bot_left = bot_left, .divisions = divisions}; + .edge_length = edge_length, .bot_left = bot_left, .divisions = divs}; } /** @@ -187,7 +192,8 @@ UniformGrid CreateUniformGridFromMesh(Omega_h::Mesh& mesh, LO cells_per_dim) { std::array divisions; - divisions.fill(cells_per_dim); + for (unsigned i = 0; i < dim; ++i) + divisions[i] = cells_per_dim; return CreateUniformGridFromMesh(mesh, divisions); }