Skip to content

Commit 8df11b6

Browse files
ax3latmyers
andauthored
Named SoA Support (#4163)
## Summary Add support to optionally name SoA Real and Int components, both for compile-time and runtime components. - [x] define and defaults - [x] single tile `Get*Data(std::string)` - [x] refactor out default name generation for reuse, finalize defaults in `AddReal/IntComp` - [x] test ## Additional background Close #3614 ## Checklist The proposed changes: - [ ] fix a bug or incorrect behavior in AMReX - [x] add new capabilities to AMReX - [ ] changes answers in the test suite to more than roundoff level - [ ] are likely to significantly affect the results of downstream AMReX users - [ ] include documentation in the code and/or rst files, if appropriate --------- Co-authored-by: Andrew Myers <[email protected]>
1 parent bd56c53 commit 8df11b6

File tree

10 files changed

+367
-60
lines changed

10 files changed

+367
-60
lines changed

Src/Particle/AMReX_ParticleContainer.H

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
#include <memory>
6161
#include <numeric>
6262
#include <random>
63+
#include <string>
6364
#include <tuple>
6465
#include <type_traits>
6566
#include <utility>
@@ -1144,7 +1145,8 @@ public:
11441145
*/
11451146
ParticleTileType& DefineAndReturnParticleTile (int lev, int grid, int tile)
11461147
{
1147-
m_particles[lev][std::make_pair(grid, tile)].define(NumRuntimeRealComps(), NumRuntimeIntComps());
1148+
m_particles[lev][std::make_pair(grid, tile)].define(NumRuntimeRealComps(), NumRuntimeIntComps(), &m_soa_rdata_names, &m_soa_idata_names);
1149+
11481150
return ParticlesAt(lev, grid, tile);
11491151
}
11501152

@@ -1247,10 +1249,10 @@ public:
12471249

12481250
Long superParticleSize() const { return superparticle_size; }
12491251

1250-
template <typename T,
1251-
std::enable_if_t<std::is_same_v<T,bool>,int> = 0>
1252-
void AddRealComp (T communicate=true)
1252+
void AddRealComp (std::string const & name, int communicate=1)
12531253
{
1254+
m_soa_rdata_names.push_back(name);
1255+
12541256
m_runtime_comps_defined = true;
12551257
m_num_runtime_real++;
12561258
h_redistribute_real_comp.push_back(communicate);
@@ -1270,10 +1272,15 @@ public:
12701272
}
12711273
}
12721274

1273-
template <typename T,
1274-
std::enable_if_t<std::is_same_v<T,bool>,int> = 0>
1275-
void AddIntComp (T communicate=true)
1275+
void AddRealComp (int communicate=1)
12761276
{
1277+
AddRealComp(getDefaultCompNameReal<ParticleType>(NArrayReal+m_num_runtime_real), communicate);
1278+
}
1279+
1280+
void AddIntComp (std::string const & name, int communicate=1)
1281+
{
1282+
m_soa_idata_names.push_back(name);
1283+
12771284
m_runtime_comps_defined = true;
12781285
m_num_runtime_int++;
12791286
h_redistribute_int_comp.push_back(communicate);
@@ -1293,6 +1300,11 @@ public:
12931300
}
12941301
}
12951302

1303+
void AddIntComp (int communicate=1)
1304+
{
1305+
AddIntComp(getDefaultCompNameInt<ParticleType>(NArrayInt+m_num_runtime_int), communicate);
1306+
}
1307+
12961308
int NumRuntimeRealComps () const { return m_num_runtime_real; }
12971309
int NumRuntimeIntComps () const { return m_num_runtime_int; }
12981310

@@ -1403,6 +1415,15 @@ public:
14031415
#include "AMReX_ParticlesHDF5.H"
14041416
#endif
14051417

1418+
/** Overwrite the default names for the compile-time SoA components */
1419+
void SetSoACompileTimeNames (std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name);
1420+
1421+
/** Get the names for the real SoA components **/
1422+
std::vector<std::string> GetRealSoANames () const {return m_soa_rdata_names;}
1423+
1424+
/** Get the names for the int SoA components **/
1425+
std::vector<std::string> GetIntSoANames () const {return m_soa_idata_names;}
1426+
14061427
protected:
14071428

14081429
template <class RTYPE>
@@ -1435,6 +1456,10 @@ private:
14351456
size_t particle_size, superparticle_size;
14361457
int num_real_comm_comps, num_int_comm_comps;
14371458
Vector<ParticleLevel> m_particles;
1459+
1460+
// names of both compile-time and runtime Real and Int SoA data
1461+
std::vector<std::string> m_soa_rdata_names;
1462+
std::vector<std::string> m_soa_idata_names;
14381463
};
14391464

14401465
template <int T_NStructReal, int T_NStructInt, int T_NArrayReal, int T_NArrayInt, template<class> class Allocator, class CellAssignor>

Src/Particle/AMReX_ParticleContainerI.H

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
#include <type_traits>
21
#include <AMReX_MakeParticle.H>
32

3+
#include <string>
4+
#include <type_traits>
5+
#include <vector>
6+
7+
48
template <typename ParticleType, int NArrayReal, int NArrayInt,
59
template<class> class Allocator, class CellAssignor>
610
void
@@ -60,10 +64,40 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
6064
pp.query("do_unlink", doUnlink);
6165
pp.queryAdd("do_mem_efficient_sort", memEfficientSort);
6266

67+
// add default names for SoA Real and Int compile-time arguments
68+
for (int i=0; i<NArrayReal; ++i)
69+
{
70+
m_soa_rdata_names.push_back(getDefaultCompNameReal<ParticleType>(i));
71+
}
72+
for (int i=0; i<NArrayInt; ++i)
73+
{
74+
m_soa_idata_names.push_back(getDefaultCompNameInt<ParticleType>(i));
75+
}
76+
6377
initialized = true;
6478
}
6579
}
6680

81+
template <typename ParticleType, int NArrayReal, int NArrayInt,
82+
template<class> class Allocator, class CellAssignor>
83+
void
84+
ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssignor> :: SetSoACompileTimeNames (
85+
std::vector<std::string> const & rdata_name, std::vector<std::string> const & idata_name
86+
)
87+
{
88+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(rdata_name.size() == NArrayReal, "rdata_name must be equal to NArrayReal");
89+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(idata_name.size() == NArrayInt, "idata_name must be equal to NArrayInt");
90+
91+
for (int i=0; i<NArrayReal; ++i)
92+
{
93+
m_soa_rdata_names.at(i) = rdata_name.at(i);
94+
}
95+
for (int i=0; i<NArrayInt; ++i)
96+
{
97+
m_soa_idata_names.at(i) = idata_name.at(i);
98+
}
99+
}
100+
67101
template <typename ParticleType, int NArrayReal, int NArrayInt,
68102
template<class> class Allocator, class CellAssignor>
69103
template <typename P, typename Assignor>
@@ -1161,7 +1195,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
11611195
}
11621196
} else {
11631197
ParticleTileType ptile_tmp;
1164-
ptile_tmp.define(m_num_runtime_real, m_num_runtime_int);
1198+
ptile_tmp.define(m_num_runtime_real, m_num_runtime_int, &m_soa_rdata_names, &m_soa_idata_names);
11651199
ptile_tmp.resize(np_total);
11661200
// copy re-ordered particles
11671201
gatherParticles(ptile_tmp, ptile, np, permutations);
@@ -1498,7 +1532,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
14981532
tmp_local[lev][index].resize(num_threads);
14991533
soa_local[lev][index].resize(num_threads);
15001534
for (int t = 0; t < num_threads; ++t) {
1501-
soa_local[lev][index][t].define(m_num_runtime_real, m_num_runtime_int);
1535+
soa_local[lev][index][t].define(m_num_runtime_real, m_num_runtime_int, &m_soa_rdata_names, &m_soa_idata_names);
15021536
}
15031537
}
15041538
}

Src/Particle/AMReX_ParticleIO.H

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,18 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
5151
{
5252
Vector<int> write_real_comp;
5353
Vector<std::string> tmp_real_comp_names;
54-
int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps();
5554

56-
for (int i = 0; i < nrc; ++i )
55+
int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0;
56+
for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i )
5757
{
5858
write_real_comp.push_back(1);
5959
if (real_comp_names.empty())
6060
{
61-
std::stringstream ss;
62-
ss << "real_comp" << i;
63-
tmp_real_comp_names.push_back(ss.str());
61+
tmp_real_comp_names.push_back(getDefaultCompNameReal<ParticleType>(i));
6462
}
6563
else
6664
{
67-
tmp_real_comp_names.push_back(real_comp_names[i]);
65+
tmp_real_comp_names.push_back(real_comp_names[i-first_rcomp]);
6866
}
6967
}
7068

@@ -75,9 +73,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
7573
write_int_comp.push_back(1);
7674
if (int_comp_names.empty())
7775
{
78-
std::stringstream ss;
79-
ss << "int_comp" << i;
80-
tmp_int_comp_names.push_back(ss.str());
76+
tmp_int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
8177
}
8278
else
8379
{
@@ -98,24 +94,20 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
9894
{
9995
Vector<int> write_real_comp;
10096
Vector<std::string> real_comp_names;
101-
int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps();
10297

103-
for (int i = 0; i < nrc; ++i )
98+
int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0;
99+
for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i )
104100
{
105101
write_real_comp.push_back(1);
106-
std::stringstream ss;
107-
ss << "real_comp" << i;
108-
real_comp_names.push_back(ss.str());
102+
real_comp_names.push_back(getDefaultCompNameReal<ParticleType>(i));
109103
}
110104

111105
Vector<int> write_int_comp;
112106
Vector<std::string> int_comp_names;
113107
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
114108
{
115109
write_int_comp.push_back(1);
116-
std::stringstream ss;
117-
ss << "int_comp" << i;
118-
int_comp_names.push_back(ss.str());
110+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
119111
}
120112

121113
WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
@@ -182,9 +174,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
182174
Vector<std::string> int_comp_names;
183175
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
184176
{
185-
std::stringstream ss;
186-
ss << "int_comp" << i;
187-
int_comp_names.push_back(ss.str());
177+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
188178
}
189179

190180
WriteBinaryParticleData(dir, name,
@@ -211,20 +201,16 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
211201
AMREX_ASSERT(write_int_comp.size() == NStructInt + NArrayInt );
212202

213203
Vector<std::string> real_comp_names;
214-
int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps();
215-
for (int i = 0; i < nrc; ++i )
204+
int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0;
205+
for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i )
216206
{
217-
std::stringstream ss;
218-
ss << "real_comp" << i;
219-
real_comp_names.push_back(ss.str());
207+
real_comp_names.push_back(getDefaultCompNameReal<ParticleType>(i));
220208
}
221209

222210
Vector<std::string> int_comp_names;
223211
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
224212
{
225-
std::stringstream ss;
226-
ss << "int_comp" << i;
227-
int_comp_names.push_back(ss.str());
213+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
228214
}
229215

230216
WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
@@ -259,24 +245,20 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
259245
{
260246
Vector<int> write_real_comp;
261247
Vector<std::string> real_comp_names;
262-
int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps();
263248

264-
for (int i = 0; i < nrc; ++i )
249+
int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0;
250+
for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i )
265251
{
266252
write_real_comp.push_back(1);
267-
std::stringstream ss;
268-
ss << "real_comp" << i;
269-
real_comp_names.push_back(ss.str());
253+
real_comp_names.push_back(getDefaultCompNameReal<ParticleType>(i));
270254
}
271255

272256
Vector<int> write_int_comp;
273257
Vector<std::string> int_comp_names;
274258
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
275259
{
276260
write_int_comp.push_back(1);
277-
std::stringstream ss;
278-
ss << "int_comp" << i;
279-
int_comp_names.push_back(ss.str());
261+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
280262
}
281263

282264
WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,
@@ -345,9 +327,7 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
345327
Vector<std::string> int_comp_names;
346328
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
347329
{
348-
std::stringstream ss;
349-
ss << "int_comp" << i;
350-
int_comp_names.push_back(ss.str());
330+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
351331
}
352332

353333
WriteBinaryParticleData(dir, name,
@@ -374,20 +354,16 @@ ParticleContainer_impl<ParticleType, NArrayReal, NArrayInt, Allocator, CellAssig
374354
AMREX_ASSERT(write_int_comp.size() == NStructInt + NumIntComps() );
375355

376356
Vector<std::string> real_comp_names;
377-
int nrc = ParticleType::is_soa_particle ? NStructReal + NumRealComps() - AMREX_SPACEDIM : NStructReal + NumRealComps();
378-
for (int i = 0; i < nrc; ++i )
357+
int first_rcomp = ParticleType::is_soa_particle ? AMREX_SPACEDIM : 0;
358+
for (int i = first_rcomp; i < NStructReal + NumRealComps(); ++i )
379359
{
380-
std::stringstream ss;
381-
ss << "real_comp" << i;
382-
real_comp_names.push_back(ss.str());
360+
real_comp_names.push_back(getDefaultCompNameReal<ParticleType>(i));
383361
}
384362

385363
Vector<std::string> int_comp_names;
386364
for (int i = 0; i < NStructInt + NumIntComps(); ++i )
387365
{
388-
std::stringstream ss;
389-
ss << "int_comp" << i;
390-
int_comp_names.push_back(ss.str());
366+
int_comp_names.push_back(getDefaultCompNameInt<ParticleType>(i));
391367
}
392368

393369
WriteBinaryParticleData(dir, name, write_real_comp, write_int_comp,

Src/Particle/AMReX_ParticleTile.H

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
#include <AMReX_RealVect.H>
1212

1313
#include <array>
14+
#include <string>
1415
#include <type_traits>
16+
#include <vector>
17+
1518

1619
namespace amrex {
1720

@@ -730,10 +733,15 @@ struct ParticleTile
730733
ParticleTile& operator= (ParticleTile &&) noexcept = default;
731734
#endif
732735

733-
void define (int a_num_runtime_real, int a_num_runtime_int)
736+
void define (
737+
int a_num_runtime_real,
738+
int a_num_runtime_int,
739+
std::vector<std::string>* soa_rdata_names=nullptr,
740+
std::vector<std::string>* soa_idata_names=nullptr
741+
)
734742
{
735743
m_defined = true;
736-
GetStructOfArrays().define(a_num_runtime_real, a_num_runtime_int);
744+
GetStructOfArrays().define(a_num_runtime_real, a_num_runtime_int, soa_rdata_names, soa_idata_names);
737745
m_runtime_r_ptrs.resize(a_num_runtime_real);
738746
m_runtime_i_ptrs.resize(a_num_runtime_int);
739747
m_runtime_r_cptrs.resize(a_num_runtime_real);

Src/Particle/AMReX_ParticleUtil.H

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,26 @@ void PermutationForDeposition (Gpu::DeviceVector<index_type>& perm, index_type n
883883
});
884884
}
885885

886+
template <typename P>
887+
std::string getDefaultCompNameReal (const int i) {
888+
int first_r_name = 0;
889+
if constexpr (P::is_soa_particle) {
890+
if (i < AMREX_SPACEDIM) {
891+
constexpr int x_in_ascii = 120;
892+
std::string const name{char(x_in_ascii+i)};
893+
return name;
894+
}
895+
first_r_name = AMREX_SPACEDIM;
896+
}
897+
std::string const name{("real_comp" + std::to_string(i-first_r_name))};
898+
return name;
899+
}
900+
901+
template <typename P>
902+
std::string getDefaultCompNameInt (const int i) {
903+
std::string const name{("int_comp" + std::to_string(i))};
904+
return name;
905+
}
886906

887907
#ifdef AMREX_USE_HDF5_ASYNC
888908
void async_vol_es_wait_particle();

0 commit comments

Comments
 (0)