Skip to content

Work-Around: pybind11 type aliases on Win #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Particle/ArrayOfStructs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include <AMReX_Config.H>
#include <AMReX_ArrayOfStructs.H>
#include <AMReX_GpuAllocators.H>

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
Expand Down Expand Up @@ -124,8 +125,20 @@ template <int NReal, int NInt>
void make_ArrayOfStructs(py::module &m)
{
// see Src/Base/AMReX_GpuContainers.H
// !AMREX_USE_GPU: DefaultAllocator = std::allocator
// AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator

// work-around for https://github.com/pybind/pybind11/pull/4581
//make_ArrayOfStructs<NReal, NInt, std::allocator> (m, "std");
//make_ArrayOfStructs<NReal, NInt, amrex::ArenaAllocator> (m, "arena");
Comment on lines +131 to +133

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
#ifdef AMREX_USE_GPU
make_ArrayOfStructs<NReal, NInt, std::allocator> (m, "std");
make_ArrayOfStructs<NReal, NInt, amrex::DefaultAllocator> (m, "default"); // amrex::ArenaAllocator
#else
make_ArrayOfStructs<NReal, NInt, amrex::DefaultAllocator> (m, "default"); // std::allocator
make_ArrayOfStructs<NReal, NInt, amrex::ArenaAllocator> (m, "arena");
#endif
// end work-around
make_ArrayOfStructs<NReal, NInt, amrex::PinnedArenaAllocator> (m, "pinned");
#ifdef AMREX_USE_GPU
make_ArrayOfStructs<NReal, NInt, amrex::DeviceArenaAllocator> (m, "device");
Expand Down
28 changes: 23 additions & 5 deletions src/Particle/ParticleContainer.H
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Base/Iterator.H"

#include <AMReX_BoxArray.H>
#include <AMReX_GpuAllocators.H>
#include <AMReX_IntVect.H>
#include <AMReX_ParIter.H>
#include <AMReX_Particles.H>
Expand Down Expand Up @@ -35,11 +36,12 @@ void make_Base_Iterators (py::module &m, std::string allocstr)
constexpr int NArrayReal = container::NArrayReal;
constexpr int NArrayInt = container::NArrayInt;

std::string particle_it_base_name = std::string("ParIterBase_") +
std::to_string(NStructReal) + "_" + std::to_string(NStructInt) + "_" +
std::to_string(NArrayReal) + "_" + std::to_string(NArrayInt) + "_" +
allocstr;
if (is_const) particle_it_base_name = "Const" + particle_it_base_name;
std::string particle_it_base_name = std::string("Par");
if (is_const) particle_it_base_name += "Const";
particle_it_base_name += "IterBase_" +
std::to_string(NStructReal) + "_" + std::to_string(NStructInt) + "_" +
std::to_string(NArrayReal) + "_" + std::to_string(NArrayInt) + "_" +
allocstr;
py::class_<iterator_base, MFIter>(m, particle_it_base_name.c_str())
.def(py::init<container&, int>(),
py::arg("particle_container"), py::arg("level"))
Expand Down Expand Up @@ -350,10 +352,26 @@ void make_ParticleContainer_and_Iterators (py::module &m)
make_ParticleInitData<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt>(m);

// see Src/Base/AMReX_GpuContainers.H
// !AMREX_USE_GPU: DefaultAllocator = std::allocator
// AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator

// work-around for https://github.com/pybind/pybind11/pull/4581
//make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
// std::allocator>(m, "std"); // CPU DefaultAllocator
//make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
// amrex::ArenaAllocator>(m, "arena"); // GPU DefaultAllocator
#ifdef AMREX_USE_GPU
make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
std::allocator>(m, "std");
make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
amrex::DefaultAllocator>(m, "default"); // amrex::ArenaAllocator
#else
make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
amrex::DefaultAllocator>(m, "default"); // std::allocator
make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
amrex::ArenaAllocator>(m, "arena");
#endif
// end work-around
make_ParticleContainer_and_Iterators<T_NStructReal, T_NStructInt, T_NArrayReal, T_NArrayInt,
amrex::PinnedArenaAllocator>(m, "pinned");
#ifdef AMREX_USE_GPU
Expand Down
17 changes: 17 additions & 0 deletions src/Particle/ParticleTile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
#include <AMReX_Config.H>
#include <AMReX_BoxArray.H>
#include <AMReX_GpuAllocators.H>
#include <AMReX_IntVect.H>
#include <AMReX_ParticleTile.H>

Expand Down Expand Up @@ -112,10 +113,26 @@ void make_ParticleTile(py::module &m)
make_ParticleTileData<NStructReal, NStructInt, NArrayReal, NArrayInt>(m);

// see Src/Base/AMReX_GpuContainers.H
// !AMREX_USE_GPU: DefaultAllocator = std::allocator
// AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator

// work-around for https://github.com/pybind/pybind11/pull/4581
//make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
// std::allocator>(m, "std");
//make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
// amrex::ArenaAllocator>(m, "arena");
#ifdef AMREX_USE_GPU
make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
std::allocator>(m, "std");
make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
amrex::DefaultAllocator>(m, "default"); // amrex::ArenaAllocator
#else
make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
amrex::DefaultAllocator>(m, "default"); // std::allocator
make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
amrex::ArenaAllocator>(m, "arena");
#endif
// end work-around
make_ParticleTile<NStructReal, NStructInt, NArrayReal, NArrayInt,
amrex::PinnedArenaAllocator>(m, "pinned");
#ifdef AMREX_USE_GPU
Expand Down
13 changes: 13 additions & 0 deletions src/Particle/StructOfArrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* License: BSD-3-Clause-LBNL
*/
#include <AMReX_Config.H>
#include <AMReX_GpuAllocators.H>
#include <AMReX_StructOfArrays.H>

#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -46,8 +47,20 @@ template <int NReal, int NInt>
void make_StructOfArrays(py::module &m)
{
// see Src/Base/AMReX_GpuContainers.H
// !AMREX_USE_GPU: DefaultAllocator = std::allocator
// AMREX_USE_GPU: DefaultAllocator = amrex::ArenaAllocator

// work-around for https://github.com/pybind/pybind11/pull/4581
//make_StructOfArrays<NReal, NInt, std::allocator>(m, "std");
//make_StructOfArrays<NReal, NInt, amrex::ArenaAllocator>(m, "arena");
Comment on lines +53 to +55

Check notice

Code scanning / CodeQL

Commented-out code

This comment appears to contain commented-out code.
#ifdef AMREX_USE_GPU
make_StructOfArrays<NReal, NInt, std::allocator>(m, "std");
make_StructOfArrays<NReal, NInt, amrex::DefaultAllocator> (m, "default"); // amrex::ArenaAllocator
#else
make_StructOfArrays<NReal, NInt, amrex::DefaultAllocator> (m, "default"); // std::allocator
make_StructOfArrays<NReal, NInt, amrex::ArenaAllocator>(m, "arena");
#endif
// end work-around
make_StructOfArrays<NReal, NInt, amrex::PinnedArenaAllocator>(m, "pinned");
#ifdef AMREX_USE_GPU
make_StructOfArrays<NReal, NInt, amrex::DeviceArenaAllocator>(m, "device");
Expand Down
6 changes: 3 additions & 3 deletions tests/test_aos.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@


def test_aos_init():
aos = amrex.ArrayOfStructs_2_1_std()
aos = amrex.ArrayOfStructs_2_1_default()

assert aos.numParticles() == 0
assert aos.numTotalParticles() == aos.numRealParticles() == 0
assert aos.empty()


def test_aos_push_pop():
aos = amrex.ArrayOfStructs_2_1_std()
aos = amrex.ArrayOfStructs_2_1_default()
p1 = amrex.Particle_2_1()
p1.set_rdata([1.5, 2.2])
p1.set_idata([3])
Expand Down Expand Up @@ -50,7 +50,7 @@ def test_aos_push_pop():


def test_array_interface():
aos = amrex.ArrayOfStructs_2_1_std()
aos = amrex.ArrayOfStructs_2_1_default()
p1 = amrex.Particle_2_1()
p1.setPos([1, 2, 3])
p1.set_rdata([4.5, 5.2])
Expand Down
26 changes: 13 additions & 13 deletions tests/test_particleContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def Npart():

@pytest.fixture(scope="function")
def empty_particle_container(std_geometry, distmap, boxarr):
pc = amrex.ParticleContainer_1_1_2_1_std(std_geometry, distmap, boxarr)
pc = amrex.ParticleContainer_1_1_2_1_default(std_geometry, distmap, boxarr)
return pc


Expand All @@ -29,7 +29,7 @@ def std_particle():

@pytest.fixture(scope="function")
def particle_container(Npart, std_geometry, distmap, boxarr, std_real_box):
pc = amrex.ParticleContainer_1_1_2_1_std(std_geometry, distmap, boxarr)
pc = amrex.ParticleContainer_1_1_2_1_default(std_geometry, distmap, boxarr)
myt = amrex.ParticleInitType_1_1_2_1()
myt.real_struct_data = [0.5]
myt.int_struct_data = [5]
Expand Down Expand Up @@ -62,17 +62,17 @@ def test_particleInitType():
def test_n_particles(particle_container, Npart):
pc = particle_container
assert pc.OK()
assert pc.NStructReal == amrex.ParticleContainer_1_1_2_1_std.NStructReal == 1
assert pc.NStructInt == amrex.ParticleContainer_1_1_2_1_std.NStructInt == 1
assert pc.NArrayReal == amrex.ParticleContainer_1_1_2_1_std.NArrayReal == 2
assert pc.NArrayInt == amrex.ParticleContainer_1_1_2_1_std.NArrayInt == 1
assert pc.NStructReal == amrex.ParticleContainer_1_1_2_1_default.NStructReal == 1
assert pc.NStructInt == amrex.ParticleContainer_1_1_2_1_default.NStructInt == 1
assert pc.NArrayReal == amrex.ParticleContainer_1_1_2_1_default.NArrayReal == 2
assert pc.NArrayInt == amrex.ParticleContainer_1_1_2_1_default.NArrayInt == 1
assert (
pc.NumberOfParticlesAtLevel(0) == np.sum(pc.NumberOfParticlesInGrid(0)) == Npart
)


def test_pc_init():
pc = amrex.ParticleContainer_1_1_2_1_std()
pc = amrex.ParticleContainer_1_1_2_1_default()

print("bytespread", pc.ByteSpread())
print("capacity", pc.PrintCapacity())
Expand All @@ -93,10 +93,10 @@ def test_pc_init():
print("define particle container")
pc.Define(gm, dm, ba)
assert pc.OK()
assert pc.NStructReal == amrex.ParticleContainer_1_1_2_1_std.NStructReal == 1
assert pc.NStructInt == amrex.ParticleContainer_1_1_2_1_std.NStructInt == 1
assert pc.NArrayReal == amrex.ParticleContainer_1_1_2_1_std.NArrayReal == 2
assert pc.NArrayInt == amrex.ParticleContainer_1_1_2_1_std.NArrayInt == 1
assert pc.NStructReal == amrex.ParticleContainer_1_1_2_1_default.NStructReal == 1
assert pc.NStructInt == amrex.ParticleContainer_1_1_2_1_default.NStructInt == 1
assert pc.NArrayReal == amrex.ParticleContainer_1_1_2_1_default.NArrayReal == 2
assert pc.NArrayInt == amrex.ParticleContainer_1_1_2_1_default.NArrayInt == 1

print("bytespread", pc.ByteSpread())
print("capacity", pc.PrintCapacity())
Expand All @@ -123,7 +123,7 @@ def test_pc_init():

print("Iterate particle boxes & set values")
lvl = 0
for pti in amrex.ParIter_1_1_2_1_std(pc, level=lvl):
for pti in amrex.ParIter_1_1_2_1_default(pc, level=lvl):
print("...")
assert pti.num_particles == 1
assert pti.num_real_particles == 1
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_pc_init():
assert np.allclose(int_arrays[0], np.array([2]))

# read-only
for pti in amrex.ParConstIter_1_1_2_1_std(pc, level=lvl):
for pti in amrex.ParConstIter_1_1_2_1_default(pc, level=lvl):
assert pti.num_particles == 1
assert pti.num_real_particles == 1
assert pti.num_neighbor_particles == 0
Expand Down
10 changes: 5 additions & 5 deletions tests/test_particleTile.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_ptile_data():


def test_ptile_funs():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()

assert pt.empty() and pt.size() == 0
assert pt.numParticles() == pt.numRealParticles() == pt.numNeighborParticles() == 0
Expand All @@ -35,7 +35,7 @@ def test_ptile_funs():

################
def test_ptile_pushback_ptiledata():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()
p = amrex.Particle_1_1(1.0, 2.0, 3, 4.0, 5)
sp = amrex.Particle_3_2(5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11, 12)
pt.push_back(p)
Expand All @@ -62,7 +62,7 @@ def test_ptile_pushback_ptiledata():

@pytest.mark.skipif(amrex.Config.spacedim != 3, reason="Requires AMREX_SPACEDIM = 3")
def test_ptile_access():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()
sp1 = amrex.Particle_3_2()
pt.push_back(sp1)
pt.push_back(sp1)
Expand All @@ -81,7 +81,7 @@ def test_ptile_access():


def test_ptile_soa():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()

pt.push_back_real(1, 2.1)
pt.push_back_real([1.1, 1.3])
Expand Down Expand Up @@ -120,7 +120,7 @@ def test_ptile_soa():

@pytest.mark.skipif(amrex.Config.spacedim != 3, reason="Requires AMREX_SPACEDIM = 3")
def test_ptile_aos():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()
p1 = amrex.Particle_1_1()
p2 = amrex.Particle_1_1()
p1.x = 3.0
Expand Down
4 changes: 2 additions & 2 deletions tests/test_soa.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@


def test_soa_init():
soa = amrex.StructOfArrays_2_1_std()
soa = amrex.StructOfArrays_2_1_default()
print("--test init --")
print("num real components", soa.NumRealComps())
print("num int components", soa.NumIntComps())
Expand Down Expand Up @@ -51,7 +51,7 @@ def test_soa_init():


def test_soa_from_tile():
pt = amrex.ParticleTile_1_1_2_1_std()
pt = amrex.ParticleTile_1_1_2_1_default()
p = amrex.Particle_1_1(1.0, 2.0, 3, rdata_0=4.0, idata_1=5)
sp = amrex.Particle_3_2(
5.0, 6.0, 7.0, rdata_0=8.0, rdata_1=9.0, rdata_2=10.0, idata_0=11, idata_1=12
Expand Down