pyAMReX
StructOfArrays.H
Go to the documentation of this file.
1 /* Copyright 2022 The AMReX Community
2  *
3  * Authors: Ryan Sandberg, Axel Huebl
4  * License: BSD-3-Clause-LBNL
5  */
6 #pragma once
7 
8 #include "pyAMReX.H"
9 
10 #include <AMReX_GpuAllocators.H>
11 #include <AMReX_StructOfArrays.H>
12 
13 #include <sstream>
14 
15 
16 template <int NReal, int NInt,
17  template<class> class Allocator=amrex::DefaultAllocator,
18  bool use64BitIdCpu=false>
19 void make_StructOfArrays(py::module &m, std::string allocstr)
20 {
21  using namespace amrex;
22 
24 
25  auto soa_name = std::string("StructOfArrays_") + std::to_string(NReal) + "_" +
26  std::to_string(NInt);
27  if (use64BitIdCpu)
28  soa_name += "_idcpu";
29  soa_name += "_" + allocstr;
30 
31  py::class_<SOAType> py_SoA(m, soa_name.c_str(), py::dynamic_attr());
32  py_SoA
33  .def(py::init())
34  .def("define", &SOAType::define)
35  .def_property_readonly("num_real_comps", &SOAType::NumRealComps,
36  "Get the number of compile-time + runtime Real components")
37  .def_property_readonly("num_int_comps", &SOAType::NumIntComps,
38  "Get the number of compile-time + runtime Int components")
39  .def_property_readonly("has_idcpu", [](const py::object&){ return use64BitIdCpu; },
40  "In pure SoA particle layout, idcpu is an array in the SoA")
41 
42  // compile-time components
43  .def("get_real_data", py::overload_cast<>(&SOAType::GetRealData),
44  py::return_value_policy::reference_internal,
45  "Get access to the particle Real Arrays (only compile-time components)")
46  .def("get_int_data", py::overload_cast<>(&SOAType::GetIntData),
47  py::return_value_policy::reference_internal,
48  "Get access to the particle Int Arrays (only compile-time components)")
49  // compile-time and runtime components
50  .def("get_real_data", py::overload_cast<const int>(&SOAType::GetRealData),
51  py::return_value_policy::reference_internal,
52  py::arg("index"),
53  "Get access to a particle Real component Array (compile-time and runtime component)")
54  .def("get_int_data", py::overload_cast<const int>(&SOAType::GetIntData),
55  py::return_value_policy::reference_internal,
56  py::arg("index"),
57  "Get access to a particle Real component Array (compile-time and runtime component)")
58 
59  .def("__len__", &SOAType::size,
60  "Get the number of particles")
61  .def_property_readonly("size", &SOAType::size,
62  "Get the number of particles")
63  .def_property_readonly("num_particles", &SOAType::numParticles)
64  .def_property_readonly("num_real_particles", &SOAType::numRealParticles)
65  .def_property_readonly("num_total_particles", &SOAType::numTotalParticles)
66 
67  .def("set_num_neighbors", &SOAType::setNumNeighbors)
68  .def("get_num_neighbors", &SOAType::getNumNeighbors)
69  .def("resize", &SOAType::resize)
70  ;
71  if (use64BitIdCpu)
72  py_SoA.def("get_idcpu_data", py::overload_cast<>(&SOAType::GetIdCPUData),
73  py::return_value_policy::reference_internal,
74  "Get access to a particle IdCPU component Array");
75 }
76 
77 template <int NReal, int NInt, bool use64BitIdCpu=false>
78 void make_StructOfArrays(py::module &m)
79 {
80  // first, because used as copy target in methods in containers with other allocators
81  make_StructOfArrays<NReal, NInt, amrex::PinnedArenaAllocator, use64BitIdCpu>(m, "pinned");
82 
83  // see Src/Base/AMReX_GpuContainers.H
84  // !AMREX_USE_GPU: DefaultAllocator = std::allocator
85  // AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator
86 
87  // work-around for https://github.com/pybind/pybind11/pull/4581
88  //make_StructOfArrays<NReal, NInt, std::allocator, use64BitIdCpu>(m, "std");
89  //make_StructOfArrays<NReal, NInt, amrex::ArenaAllocator, use64BitIdCpu>(m, "arena");
90 #ifdef AMREX_USE_GPU
91  make_StructOfArrays<NReal, NInt, std::allocator, use64BitIdCpu>(m, "std");
92  make_StructOfArrays<NReal, NInt, amrex::DefaultAllocator, use64BitIdCpu> (m, "default"); // amrex::ArenaAllocator
93 #else
94  make_StructOfArrays<NReal, NInt, amrex::DefaultAllocator, use64BitIdCpu> (m, "default"); // std::allocator
95  make_StructOfArrays<NReal, NInt, amrex::ArenaAllocator, use64BitIdCpu>(m, "arena");
96 #endif
97  // end work-around
98 #ifdef AMREX_USE_GPU
99  make_StructOfArrays<NReal, NInt, amrex::DeviceArenaAllocator, use64BitIdCpu>(m, "device");
100  make_StructOfArrays<NReal, NInt, amrex::ManagedArenaAllocator, use64BitIdCpu>(m, "managed");
101  make_StructOfArrays<NReal, NInt, amrex::AsyncArenaAllocator, use64BitIdCpu>(m, "async");
102 #endif
103 }
void make_StructOfArrays(py::module &m, std::string allocstr)
Definition: StructOfArrays.H:19
amrex::ArenaAllocator< T > DefaultAllocator