Skip to content

Commit e5bc881

Browse files
authored
Vectorized functions for unpacking the 64-bit amrex particle ids (#165)
* vectorized functions for unpacking the 64-bit amrex particle ids * remove C++ from Python file * remove more C++
1 parent f561142 commit e5bc881

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/Particle/ArrayOfStructs.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@ namespace
1515
{
1616
using namespace amrex;
1717

18+
// Note - this function MUST be consistent with AMReX_Particle.H
19+
Long unpack_id (uint64_t cpuid) {
20+
Long r = 0;
21+
22+
uint64_t sign = cpuid >> 63; // extract leftmost sign bit
23+
uint64_t val = ((cpuid >> 24) & 0x7FFFFFFFFF); // extract next 39 id bits
24+
25+
Long lval = static_cast<Long>(val); // bc we take -
26+
r = (sign) ? lval : -lval;
27+
return r;
28+
}
29+
30+
// Note - this function MUST be consistent with AMReX_Particle.H
31+
int unpack_cpu (uint64_t cpuid) {
32+
return static_cast<int>(cpuid & 0x00FFFFFF);
33+
}
34+
1835
/** CPU: __array_interface__ v3
1936
*
2037
* https://numpy.org/doc/stable/reference/arrays.interface.html
@@ -156,4 +173,7 @@ void init_ArrayOfStructs(py::module& m) {
156173
make_ArrayOfStructs<0, 0> (m); // WarpX 22.07, ImpactX 22.07, HiPACE++ 22.07
157174
make_ArrayOfStructs<1, 1> (m); // test in ParticleContainer
158175
make_ArrayOfStructs<2, 1> (m); // test
176+
177+
m.def("unpack_ids", py::vectorize(unpack_id));
178+
m.def("unpack_cpus", py::vectorize(unpack_cpu));
159179
}

tests/test_particleTile.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,11 @@ def test_ptile_aos():
137137
print(aos)
138138
assert np.isclose(aos[0]["x"], 3.0) and np.isclose(aos[0]["y"], 0)
139139
assert np.isclose(aos[2][0], 0.0) and np.isclose(aos[2][2], 20)
140+
141+
142+
def test_ptile_aos():
143+
cpuids = np.array([100, 100, 100, 100, 100], dtype=np.uint64)
144+
ids = amr.unpack_ids(cpuids)
145+
cpus = amr.unpack_cpus(cpuids)
146+
assert np.array_equal(ids, np.array([0, 0, 0, 0, 0]))
147+
assert np.array_equal(cpus, np.array([100, 100, 100, 100, 100]))

0 commit comments

Comments
 (0)