Skip to content

Commit 805bfbe

Browse files
authored
PODVector: Add _default Platform Aliases (#540)
Register `PODVector_{real,int,uint64}_default` as aliases that resolve to `_std` on CPU builds and `_arena` on GPU builds, matching the C++ `Gpu::DeviceVector<T>` type per platform. For other containers like PC & MF we an just use the default arena, but PODVector is implemented a bit special, relying on a `std::allocator` instead of the default arena on CPU. This helps to write CPU/GPU agnostic interfaces, e.g., in BLAST-ImpactX/impactx#1305
1 parent 5fabb44 commit 805bfbe

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/Base/PODVector.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ namespace
3232
d["version"] = 3;
3333
return d;
3434
}
35+
36+
std::string
37+
str_PODVector(std::string typestr, std::string allocstr)
38+
{
39+
auto const podv_name = std::string("PODVector_")
40+
.append(typestr)
41+
.append("_")
42+
.append(allocstr);
43+
return podv_name;
44+
}
3545
}
3646

3747
template <class T, class Allocator = std::allocator<T> >
@@ -40,10 +50,16 @@ void make_PODVector(py::module &m, std::string typestr, std::string allocstr)
4050
using namespace amrex;
4151

4252
using PODVector_type = PODVector<T, Allocator>;
43-
auto const podv_name = std::string("PODVector_").append(typestr)
44-
.append("_").append(allocstr);
53+
auto const podv_name = str_PODVector(typestr, allocstr);
54+
55+
auto const podv_doc = std::string(
56+
"A plain-old-data (POD) vector of '")
57+
.append(typestr)
58+
.append("' elements with '")
59+
.append(allocstr)
60+
.append("' allocation.");
4561

46-
py::class_<PODVector_type>(m, podv_name.c_str())
62+
py::class_<PODVector_type>(m, podv_name.c_str(), podv_doc.c_str())
4763
.def("__repr__",
4864
[typestr](PODVector_type const & pv) {
4965
std::stringstream s, rs;
@@ -151,6 +167,16 @@ void make_PODVector(py::module &m, std::string typestr)
151167
make_PODVector<T, amrex::AsyncArenaAllocator<T>> (m, typestr, "async");
152168
#endif
153169
make_PODVector<T, amrex::PolymorphicArenaAllocator<T>> (m, typestr, "polymorphic");
170+
171+
// Alias matching Gpu::DeviceVector<T> — resolves per platform:
172+
// CPU: PODVector_<type>_std, GPU: PODVector_<type>_arena
173+
auto const default_name = str_PODVector(typestr, "default");
174+
m.attr(default_name.c_str()) =
175+
#ifdef AMREX_USE_GPU
176+
m.attr(str_PODVector(typestr, "arena").c_str());
177+
#else
178+
m.attr(str_PODVector(typestr, "std").c_str());
179+
#endif
154180
}
155181

156182
void init_PODVector(py::module& m)

0 commit comments

Comments
 (0)