Block-Structured AMR Software Framework
AMReX_StructOfArrays.H
Go to the documentation of this file.
1 #ifndef AMREX_STRUCTOFARRAYS_H_
2 #define AMREX_STRUCTOFARRAYS_H_
3 #include <AMReX_Config.H>
4 
5 #include <AMReX_REAL.H>
6 #include <AMReX_Vector.H>
7 #include <AMReX_GpuContainers.H>
8 
9 #include <algorithm>
10 #include <array>
11 #include <string>
12 #include <vector>
13 
14 
15 namespace amrex {
16 
17 template <int NReal, int NInt,
18  template<class> class Allocator=DefaultAllocator,
19  bool use64BitIdCpu=false>
21 
25 
26  void define (
27  int a_num_runtime_real,
28  int a_num_runtime_int,
29  std::vector<std::string>* soa_rdata_names=nullptr,
30  std::vector<std::string>* soa_idata_names=nullptr
31  )
32  {
33  m_defined = true;
34  m_runtime_rdata.resize(a_num_runtime_real);
35  m_runtime_idata.resize(a_num_runtime_int );
36  m_rdata_names = soa_rdata_names;
37  m_idata_names = soa_idata_names;
38  }
39 
40  [[nodiscard]] int NumRealComps () const noexcept { return NReal + m_runtime_rdata.size(); }
41 
42  [[nodiscard]] int NumIntComps () const noexcept { return NInt + m_runtime_idata.size(); }
43 
44  [[nodiscard]] IdCPU& GetIdCPUData () { return m_idcpu; }
45  [[nodiscard]] std::array<RealVector, NReal>& GetRealData () { return m_rdata; }
46  [[nodiscard]] std::array< IntVector, NInt>& GetIntData () { return m_idata; }
47 
49  [[nodiscard]] const IdCPU& GetIdCPUData () const { return m_idcpu; }
51  [[nodiscard]] const std::array<RealVector, NReal>& GetRealData () const { return m_rdata; }
53  [[nodiscard]] const std::array< IntVector, NInt>& GetIntData () const { return m_idata; }
54 
56  [[nodiscard]] std::vector<std::string> GetRealNames () const
57  {
58  if (m_rdata_names) {
59  return *m_rdata_names;
60  }
61  else {
62  return std::vector<std::string>();
63  }
64  }
65 
67  [[nodiscard]] std::vector<std::string> GetIntNames () const
68  {
69  if (m_idata_names) {
70  return *m_idata_names;
71  }
72  else {
73  return std::vector<std::string>();
74  }
75  }
76 
81  [[nodiscard]] RealVector& GetRealData (const int index) {
82  AMREX_ASSERT(index >= 0 && index < NReal + static_cast<int>(m_runtime_rdata.size()));
83  if constexpr (NReal == 0) {
85  return m_runtime_rdata[index];
86  } else {
87  if (index < NReal) {
88  return m_rdata[index];
89  } else {
91  return m_runtime_rdata[index - NReal];
92  }
93  }
94  }
95 
100  [[nodiscard]] const RealVector& GetRealData (const int index) const {
101  AMREX_ASSERT(index >= 0 && index < NReal + static_cast<int>(m_runtime_rdata.size()));
102  if constexpr (NReal == 0) {
104  return m_runtime_rdata[index];
105  } else {
106  if (index < NReal) {
107  return m_rdata[index];
108  } else {
110  return m_runtime_rdata[index - NReal];
111  }
112  }
113  }
114 
119  [[nodiscard]] RealVector& GetRealData (std::string const & name) {
120  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_rdata_names != nullptr, "SoA Real names were not defined.");
121  auto const pos = std::find(m_rdata_names->begin(), m_rdata_names->end(), name);
122  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos != m_rdata_names->end(), "Soa Real name='" + name + "' was not found components");
123 
124  int const index = std::distance(m_rdata_names->begin(), pos);
125  return GetRealData(index);
126  }
127 
132  [[nodiscard]] const RealVector& GetRealData (std::string const & name) const {
133  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_rdata_names != nullptr, "SoA Real names were not defined.");
134  auto const pos = std::find(m_rdata_names->begin(), m_rdata_names->end(), name);
135  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos != m_rdata_names->end(), "Soa Real name='" + name + "' was not found components");
136 
137  int const index = std::distance(m_rdata_names->begin(), pos);
138  return GetRealData(index);
139  }
140 
145  [[nodiscard]] IntVector& GetIntData (const int index) {
146  AMREX_ASSERT(index >= 0 && index < NInt + static_cast<int>(m_runtime_idata.size()));
147  if constexpr (NInt == 0) {
149  return m_runtime_idata[index];
150  } else {
151  if (index < NInt) {
152  return m_idata[index];
153  } else {
155  return m_runtime_idata[index - NInt];
156  }
157  }
158  }
159 
165  [[nodiscard]] const IntVector& GetIntData (const int index) const {
166  AMREX_ASSERT(index >= 0 && index < NInt + static_cast<int>(m_runtime_idata.size()));
167  if constexpr (NInt == 0) {
169  return m_runtime_idata[index];
170  } else {
171  if (index < NInt) {
172  return m_idata[index];
173  } else {
175  return m_runtime_idata[index - NInt];
176  }
177  }
178  }
179 
185  [[nodiscard]] IntVector& GetIntData (std::string const & name) {
186  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_idata_names != nullptr, "SoA Int names were not defined.");
187  auto const pos = std::find(m_idata_names->begin(), m_idata_names->end(), name);
188  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos != m_idata_names->end(), "Soa Int name='" + name + "' was not found components");
189 
190  int const index = std::distance(m_idata_names->begin(), pos);
191  return GetIntData(index);
192  }
193 
199  [[nodiscard]] const IntVector& GetIntData (std::string const & name) const {
200  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(m_idata_names != nullptr, "SoA Int names were not defined.");
201  auto const pos = std::find(m_idata_names->begin(), m_idata_names->end(), name);
202  AMREX_ALWAYS_ASSERT_WITH_MESSAGE(pos != m_idata_names->end(), "Soa Int name='" + name + "' was not found components");
203 
204  int const index = std::distance(m_idata_names->begin(), pos);
205  return GetIntData(index);
206  }
207 
212  [[nodiscard]] std::size_t size () const
213  {
214  if constexpr (use64BitIdCpu == true) {
215  return m_idcpu.size();
216  } else if constexpr (NReal > 0) {
217  return m_rdata[0].size();
218  } else if constexpr (NInt > 0) {
219  return m_idata[0].size();
220  } else {
221  if (!m_runtime_rdata.empty()) {
222  return m_runtime_rdata[0].size();
223  } else if (!m_runtime_idata.empty()) {
224  return m_runtime_idata[0].size();
225  } else {
226  return 0;
227  }
228  }
229  }
230 
235  [[nodiscard]] bool empty () const { return this->size() == 0; }
236 
241  [[nodiscard]] int numParticles () const { return numRealParticles(); }
242 
247  [[nodiscard]] int numRealParticles () const { return numTotalParticles()-m_num_neighbor_particles; }
248 
253  [[nodiscard]] int numNeighborParticles () const { return m_num_neighbor_particles; }
254 
259  [[nodiscard]] int numTotalParticles () const { return size(); }
260 
261  void setNumNeighbors (int num_neighbors)
262  {
263  auto nrp = numRealParticles();
264  m_num_neighbor_particles = num_neighbors;
265  resize(nrp + num_neighbors);
266  }
267 
268  [[nodiscard]] int getNumNeighbors () const { return m_num_neighbor_particles; }
269 
270  void resize (size_t count)
271  {
272  if constexpr (use64BitIdCpu == true) {
273  m_idcpu.resize(count);
274  }
275  if constexpr (NReal > 0) {
276  for (int i = 0; i < NReal; ++i) { m_rdata[i].resize(count); }
277  }
278  if constexpr (NInt > 0) {
279  for (int i = 0; i < NInt; ++i) { m_idata[i].resize(count); }
280  }
281  for (int i = 0; i < int(m_runtime_rdata.size()); ++i) { m_runtime_rdata[i].resize(count); }
282  for (int i = 0; i < int(m_runtime_idata.size()); ++i) { m_runtime_idata[i].resize(count); }
283  }
284 
285  [[nodiscard]] uint64_t* idcpuarray () {
286  if constexpr (use64BitIdCpu == true) {
287  return m_idcpu.dataPtr();
288  } else {
289  return nullptr;
290  }
291  }
292 
294  {
296  for (int i = 0; i < NReal; ++i)
297  {
298  arr[i] = m_rdata[i].dataPtr();
299  }
300  return arr;
301  }
302 
304  {
306  for (int i = 0; i < NInt; ++i)
307  {
308  arr[i] = m_idata[i].dataPtr();
309  }
310  return arr;
311  }
312 
314 
315 private:
316  // compile-time data
318  std::array<RealVector, NReal> m_rdata;
319  std::array< IntVector, NInt> m_idata;
320 
321  // runtime data
322  std::vector<RealVector> m_runtime_rdata;
323  std::vector<IntVector > m_runtime_idata;
324 
325  // names of both compile-time and runtime Real and Int data
326  std::vector<std::string>* m_rdata_names = nullptr;
327  std::vector<std::string>* m_idata_names = nullptr;
328 
330  bool m_defined{false};
331 };
332 
333 } // namespace amrex
334 
335 #endif // AMREX_STRUCTOFARRAYS_H_
#define AMREX_ALWAYS_ASSERT_WITH_MESSAGE(EX, MSG)
Definition: AMReX_BLassert.H:49
#define AMREX_ASSERT(EX)
Definition: AMReX_BLassert.H:38
Definition: AMReX_PODVector.H:246
size_type size() const noexcept
Definition: AMReX_PODVector.H:575
void resize(size_type a_new_size)
Definition: AMReX_PODVector.H:625
T * dataPtr() noexcept
Definition: AMReX_PODVector.H:597
Definition: AMReX_Amr.cpp:49
amrex::ArenaAllocator< T > DefaultAllocator
Definition: AMReX_GpuAllocators.H:194
const int[]
Definition: AMReX_BLProfiler.cpp:1664
Definition: AMReX_Array.H:34
Definition: AMReX_StructOfArrays.H:20
std::array< IntVector, NInt > m_idata
Definition: AMReX_StructOfArrays.H:319
void setNumNeighbors(int num_neighbors)
Definition: AMReX_StructOfArrays.H:261
int getNumNeighbors() const
Definition: AMReX_StructOfArrays.H:268
IdCPU & GetIdCPUData()
Definition: AMReX_StructOfArrays.H:44
std::vector< IntVector > m_runtime_idata
Definition: AMReX_StructOfArrays.H:323
int numTotalParticles() const
Returns the total number of particles (real and neighbor)
Definition: AMReX_StructOfArrays.H:259
const IntVector & GetIntData(const int index) const
Definition: AMReX_StructOfArrays.H:165
std::vector< RealVector > m_runtime_rdata
Definition: AMReX_StructOfArrays.H:322
bool empty() const
Returns whether the SoA is empty (i.e. has size() == 0)
Definition: AMReX_StructOfArrays.H:235
std::array< IntVector, NInt > & GetIntData()
Definition: AMReX_StructOfArrays.H:46
bool m_defined
whether the runtime components are sized correctly
Definition: AMReX_StructOfArrays.H:330
std::vector< std::string > GetIntNames() const
Definition: AMReX_StructOfArrays.H:67
const std::array< IntVector, NInt > & GetIntData() const
Definition: AMReX_StructOfArrays.H:53
const RealVector & GetRealData(std::string const &name) const
Definition: AMReX_StructOfArrays.H:132
std::array< RealVector, NReal > & GetRealData()
Definition: AMReX_StructOfArrays.H:45
void resize(size_t count)
Definition: AMReX_StructOfArrays.H:270
uint64_t * idcpuarray()
Definition: AMReX_StructOfArrays.H:285
GpuArray< ParticleReal *, NReal > realarray()
Definition: AMReX_StructOfArrays.H:293
RealVector & GetRealData(std::string const &name)
Definition: AMReX_StructOfArrays.H:119
IdCPU m_idcpu
Definition: AMReX_StructOfArrays.H:317
int NumRealComps() const noexcept
Definition: AMReX_StructOfArrays.H:40
IntVector & GetIntData(std::string const &name)
Definition: AMReX_StructOfArrays.H:185
std::vector< std::string > GetRealNames() const
Definition: AMReX_StructOfArrays.H:56
const IntVector & GetIntData(std::string const &name) const
Definition: AMReX_StructOfArrays.H:199
int numParticles() const
Returns the number of real particles (excluding neighbors)
Definition: AMReX_StructOfArrays.H:241
const std::array< RealVector, NReal > & GetRealData() const
Definition: AMReX_StructOfArrays.H:51
int m_num_neighbor_particles
Definition: AMReX_StructOfArrays.H:313
GpuArray< int *, NInt > intarray()
Definition: AMReX_StructOfArrays.H:303
std::vector< std::string > * m_rdata_names
Definition: AMReX_StructOfArrays.H:326
const IdCPU & GetIdCPUData() const
Definition: AMReX_StructOfArrays.H:49
std::array< RealVector, NReal > m_rdata
Definition: AMReX_StructOfArrays.H:318
IntVector & GetIntData(const int index)
Definition: AMReX_StructOfArrays.H:145
void define(int a_num_runtime_real, int a_num_runtime_int, std::vector< std::string > *soa_rdata_names=nullptr, std::vector< std::string > *soa_idata_names=nullptr)
Definition: AMReX_StructOfArrays.H:26
std::vector< std::string > * m_idata_names
Definition: AMReX_StructOfArrays.H:327
RealVector & GetRealData(const int index)
Definition: AMReX_StructOfArrays.H:81
const RealVector & GetRealData(const int index) const
Definition: AMReX_StructOfArrays.H:100
int numRealParticles() const
Returns the number of real particles (excluding neighbors)
Definition: AMReX_StructOfArrays.H:247
std::size_t size() const
Returns the total number of particles (real and neighbor)
Definition: AMReX_StructOfArrays.H:212
int NumIntComps() const noexcept
Definition: AMReX_StructOfArrays.H:42
int numNeighborParticles() const
Returns the number of neighbor particles (excluding reals)
Definition: AMReX_StructOfArrays.H:253