Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_NeighborParticlesGPUImpl.H
Go to the documentation of this file.
1#ifndef AMREX_NEIGHBORPARTICLESGPUIMPL_H_
2#define AMREX_NEIGHBORPARTICLESGPUIMPL_H_
3#include <AMReX_Config.H>
4
5namespace amrex {
6
8namespace detail
9{
10 inline Vector<Box> getBoundaryBoxes(const Box& box, int ncells)
11 {
12 AMREX_ASSERT_WITH_MESSAGE(box.size() > 2*IntVect(AMREX_D_DECL(ncells, ncells, ncells)),
13 "Too many cells requested in getBoundaryBoxes");
14
15 AMREX_ASSERT_WITH_MESSAGE(box.ixType().cellCentered(),
16 "Box must be cell-centered");
17
18 Vector<Box> bl;
19 for (int i = 0; i < AMREX_SPACEDIM; ++i) {
20 BoxList face_boxes;
21 Box hi_face_box = adjCellHi(box, i, ncells);
22 Box lo_face_box = adjCellLo(box, i, ncells);
23 face_boxes.push_back(hi_face_box); bl.push_back(hi_face_box);
24 face_boxes.push_back(lo_face_box); bl.push_back(lo_face_box);
25 for (auto face_box : face_boxes) {
26 for (int j = 0; j < AMREX_SPACEDIM; ++j) {
27 if (i == j) { continue; }
28 BoxList edge_boxes;
29 Box hi_edge_box = adjCellHi(face_box, j, ncells);
30 Box lo_edge_box = adjCellLo(face_box, j, ncells);
31 edge_boxes.push_back(hi_edge_box); bl.push_back(hi_edge_box);
32 edge_boxes.push_back(lo_edge_box); bl.push_back(lo_edge_box);
33 for (auto edge_box : edge_boxes) {
34 for (int k = 0; k < AMREX_SPACEDIM; ++k) {
35 if ((j == k) || (i == k)) { continue; }
36 Box hi_corner_box = adjCellHi(edge_box, k, ncells);
37 Box lo_corner_box = adjCellLo(edge_box, k, ncells);
38 bl.push_back(hi_corner_box);
39 bl.push_back(lo_corner_box);
40 }
41 }
42 }
43 }
44 }
45
47 return bl;
48 }
49}
51
52template <int NStructReal, int NStructInt, int NArrayReal, int NArrayInt>
53void
56{
57 BL_PROFILE("NeighborParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt>::buildNeighborMask");
58 m_neighbor_mask_initialized = true;
59 const int lev = 0;
60 const Geometry& geom = this->Geom(lev);
61 const BoxArray& ba = this->ParticleBoxArray(lev);
62 const DistributionMapping& dmap = this->ParticleDistributionMap(lev);
63
64 if (ba.size() == 1 && (! geom.isAnyPeriodic()) ) { return; }
65
66 if (m_neighbor_mask_ptr == nullptr ||
67 ! BoxArray::SameRefs(m_neighbor_mask_ptr->boxArray(), ba) ||
68 ! DistributionMapping::SameRefs(m_neighbor_mask_ptr->DistributionMap(), dmap))
69 {
70 const Periodicity& periodicity = geom.periodicity();
71 const std::vector<IntVect>& pshifts = periodicity.shiftIntVect();
72
73 for (MFIter mfi(ba, dmap); mfi.isValid(); ++mfi)
74 {
75 int grid = mfi.index();
76
77 std::set<NeighborTask> neighbor_grids;
78 for (auto pshift : pshifts)
79 {
80 const Box box = ba[mfi] + pshift;
81
82 const bool first_only = false;
83 auto isecs = ba.intersections(box, first_only, m_num_neighbor_cells);
84
85 for (auto& isec : isecs)
86 {
87 int nbor_grid = isec.first;
88 const Box isec_box = isec.second - pshift;
89 if ( (grid == nbor_grid) && (pshift == 0)) { continue; }
90 neighbor_grids.insert(NeighborTask(nbor_grid, isec_box, pshift));
91 const int global_rank = dmap[nbor_grid];
92 neighbor_procs.push_back(ParallelContext::global_to_local_rank(global_rank));
93 }
94 }
95
96 Gpu::HostVector<Box> h_isec_boxes;
98 for (auto nbor_grid : neighbor_grids)
99 {
100 NeighborCode code;
101 code.grid_id = nbor_grid.grid_id;
102 code.periodic_shift = nbor_grid.periodic_shift;
103 h_code_arr.push_back(code);
104 h_isec_boxes.push_back(nbor_grid.box);
105 }
106
107 m_code_array[grid].resize(h_code_arr.size());
108 Gpu::copyAsync(Gpu::hostToDevice, h_code_arr.begin(), h_code_arr.end(),
109 m_code_array[grid].begin());
110 m_isec_boxes[grid].resize(h_isec_boxes.size());
111 Gpu::copyAsync(Gpu::hostToDevice, h_isec_boxes.begin(), h_isec_boxes.end(),
112 m_isec_boxes[grid].begin());
113
115 }
116
117 RemoveDuplicates(neighbor_procs);
118 }
119}
120
121template <int NStructReal, int NStructInt, int NArrayReal, int NArrayInt>
122void
124buildNeighborCopyOp (bool use_boundary_neighbor)
125{
126 BL_PROFILE("NeighborParticleContainer<NStructReal, NStructInt, NArrayReal, NArrayInt>::buildNeighborCopyOp()");
127
128 AMREX_ASSERT(!hasNeighbors() || use_boundary_neighbor);
129
130 const int lev = 0;
131 const auto& geom = this->Geom(lev);
132 const auto dxi = this->Geom(lev).InvCellSizeArray();
133 const auto plo = this->Geom(lev).ProbLoArray();
134 const auto domain = this->Geom(lev).Domain();
135 auto& plev = this->GetParticles(lev);
136 auto& ba = this->ParticleBoxArray(lev);
137
138 if (ba.size() == 1 && (! geom.isAnyPeriodic()) ) { return; }
139
140 for(MFIter mfi = this->MakeMFIter(lev); mfi.isValid(); ++mfi)
141 {
142 int gid = mfi.index();
143 int tid = mfi.LocalTileIndex();
144 auto index = std::make_pair(gid, tid);
145
146 auto& src_tile = plev[index];
147 auto& aos = src_tile.GetArrayOfStructs();
148 const size_t np_real = aos.numParticles();
149
150 size_t np = np_real;
151 if (use_boundary_neighbor) {
152 np = m_boundary_particle_ids[lev][index].size();
153 }
154 else {
155 m_boundary_particle_ids.resize(1);
156 m_boundary_particle_ids[lev][index];
157 }
158
159 const auto* p_bndry_pid = m_boundary_particle_ids[lev][index].dataPtr();
160
161 Gpu::DeviceVector<int> counts(np + 1, 0);
162 Gpu::DeviceVector<int> offsets(np + 1);
163 auto p_counts = counts.dataPtr();
164 auto p_offsets = offsets.dataPtr();
165
166 ParticleType* p_ptr = aos.data();
167 auto p_code_array = m_code_array[gid].dataPtr();
168 auto p_isec_boxes = m_isec_boxes[gid].dataPtr();
169 const int nisec_box = m_isec_boxes[gid].size();
170 // auto p_code_offsets = m_code_offsets[gid].dataPtr();
171
172 AMREX_FOR_1D ( np, i,
173 {
174 // note that cannot use ternary statement here because p_bndry is not
175 // properly allocated when use_boundary_neighbor=false
176 int pid = i;
177 if (use_boundary_neighbor) {
178 pid = p_bndry_pid[i];
179 }
180 IntVect iv = getParticleCell(p_ptr[pid], plo, dxi, domain);
181 for (int j=0; j<nisec_box; ++j) {
182 if (p_isec_boxes[j].contains(iv)) {
183 ++p_counts[i];
184 }
185 }
186 });
187
188 amrex::Gpu::exclusive_scan(counts.begin(), counts.end(), offsets.begin());
189
190 int num_copies;
191 Gpu::dtoh_memcpy_async(&num_copies, offsets.data()+np, sizeof(int));
193
194 neighbor_copy_op.resize(gid, lev, num_copies);
195
196 auto p_boxes = neighbor_copy_op.m_boxes[lev][gid].dataPtr();
197 auto p_levs = neighbor_copy_op.m_levels[lev][gid].dataPtr();
198 auto p_src_indices = neighbor_copy_op.m_src_indices[lev][gid].dataPtr();
199 auto p_periodic_shift = neighbor_copy_op.m_periodic_shift[lev][gid].dataPtr();
200
202 AMREX_FOR_1D ( np, i,
203 {
204 int pid = i;
205 if (use_boundary_neighbor) {
206 pid = p_bndry_pid[i];
207 }
208 IntVect iv = getParticleCell(p_ptr[pid], plo, dxi, domain);
209 int k = p_offsets[i];
210 for (int j=0; j<nisec_box; ++j) {
211 if (p_isec_boxes[j].contains(iv)) {
212 p_boxes[k] = p_code_array[j].grid_id;
213 p_levs[k] = 0;
214 p_periodic_shift[k] = p_code_array[j].periodic_shift;
215 p_src_indices[k] = pid;
216 ++k;
217 }
218 }
219 AMREX_ALWAYS_ASSERT(k == p_offsets[i+1]);
220 });
221
223 }
224}
225
226template <int NStructReal, int NStructInt, int NArrayReal, int NArrayInt>
227void
230{
231 BL_PROFILE("NeighborParticleContainer::fillNeighbors");
232
233 AMREX_ASSERT(numParticlesOutOfRange(*this, 0) == 0);
234
235 buildNeighborMask();
236 this->defineBufferMap();
237
238 neighbor_copy_op.clear();
239 neighbor_copy_plan.clear();
240 buildNeighborCopyOp();
241 neighbor_copy_plan.build(*this, neighbor_copy_op, ghost_int_comp,
242 ghost_real_comp, true);
243 updateNeighborsGPU(false);
244}
245
246template <int NStructReal, int NStructInt, int NArrayReal, int NArrayInt>
247void
249updateNeighborsGPU (bool boundary_neighbors_only)
250{
251 BL_PROFILE("NeighborParticleContainer::updateNeighborsGPU");
252
253 if (boundary_neighbors_only) {
254 neighbor_copy_op.clear();
255 neighbor_copy_plan.clear();
256 buildNeighborCopyOp(true);
257 neighbor_copy_plan.build(*this, neighbor_copy_op, ghost_int_comp,
258 ghost_real_comp, true);
259 }
260
261 clearNeighbors();
262
263 if (this->use_comms_arena) {
264 snd_buffer.setArena(The_Comms_Arena());
265 rcv_buffer.setArena(The_Comms_Arena());
266 }
267
268 packBuffer(*this, neighbor_copy_op, neighbor_copy_plan, snd_buffer);
270 {
271 neighbor_copy_plan.buildMPIFinish(this->BufferMap());
272 communicateParticlesStart(*this, neighbor_copy_plan, snd_buffer, rcv_buffer);
273 unpackBuffer(*this, neighbor_copy_plan, snd_buffer, NeighborUnpackPolicy());
274 communicateParticlesFinish(neighbor_copy_plan);
275 unpackRemotes(*this, neighbor_copy_plan, rcv_buffer, NeighborUnpackPolicy());
276 }
277 else
278 {
280 if (snd_buffer.arena()->isPinned()) {
281 neighbor_copy_plan.buildMPIFinish(this->BufferMap());
283 communicateParticlesStart(*this, neighbor_copy_plan, snd_buffer, pinned_rcv_buffer);
284 } else {
285 pinned_snd_buffer.resize(snd_buffer.size());
286 Gpu::dtoh_memcpy_async(pinned_snd_buffer.dataPtr(), snd_buffer.dataPtr(), snd_buffer.size());
287 neighbor_copy_plan.buildMPIFinish(this->BufferMap());
289 communicateParticlesStart(*this, neighbor_copy_plan, pinned_snd_buffer, pinned_rcv_buffer);
290 }
291
292 rcv_buffer.resize(pinned_rcv_buffer.size());
293 unpackBuffer(*this, neighbor_copy_plan, snd_buffer, NeighborUnpackPolicy());
294 communicateParticlesFinish(neighbor_copy_plan);
295 Gpu::htod_memcpy_async(rcv_buffer.dataPtr(), pinned_rcv_buffer.dataPtr(), pinned_rcv_buffer.size());
296 unpackRemotes(*this, neighbor_copy_plan, rcv_buffer, NeighborUnpackPolicy());
297 }
298
300}
301
302template <int NStructReal, int NStructInt, int NArrayReal, int NArrayInt>
303void
306{
307 BL_PROFILE("NeighborParticleContainer::clearNeighborsGPU");
308
309 this->reserveData();
310 this->resizeData();
311 for (int lev = 0; lev < this->numLevels(); ++lev)
312 {
313 for(MFIter mfi = this->MakeMFIter(lev); mfi.isValid(); ++mfi)
314 {
315 auto& ptile = this->DefineAndReturnParticleTile(lev, mfi);
316 ptile.setNumNeighbors(0);
317 }
318 }
319}
320
321}
322
323#endif
#define BL_PROFILE(a)
Definition AMReX_BLProfiler.H:551
#define AMREX_ASSERT_WITH_MESSAGE(EX, MSG)
Definition AMReX_BLassert.H:37
#define AMREX_ASSERT(EX)
Definition AMReX_BLassert.H:38
#define AMREX_ALWAYS_ASSERT(EX)
Definition AMReX_BLassert.H:50
#define AMREX_FOR_1D(...)
Definition AMReX_GpuLaunchMacrosC.nolint.H:97
#define AMREX_D_DECL(a, b, c)
Definition AMReX_SPACE.H:171
A collection of Boxes stored in an Array.
Definition AMReX_BoxArray.H:567
std::vector< std::pair< int, Box > > intersections(const Box &bx) const
Return intersections of Box and BoxArray.
Definition AMReX_BoxArray.cpp:1186
static bool SameRefs(const BoxArray &lhs, const BoxArray &rhs)
whether two BoxArrays share the same data
Definition AMReX_BoxArray.H:840
Long size() const noexcept
Return the number of boxes in the BoxArray.
Definition AMReX_BoxArray.H:614
Calculates the distribution of FABs to MPI processes.
Definition AMReX_DistributionMapping.H:43
static bool SameRefs(const DistributionMapping &lhs, const DistributionMapping &rhs)
Definition AMReX_DistributionMapping.H:189
Rectangular problem domain geometry.
Definition AMReX_Geometry.H:74
bool isAnyPeriodic() const noexcept
Is domain periodic in any direction?
Definition AMReX_Geometry.H:334
Periodicity periodicity() const noexcept
Definition AMReX_Geometry.H:356
Iterator for looping ever tiles and boxes of amrex::FabArray based containers.
Definition AMReX_MFIter.H:85
bool isValid() const noexcept
Is the iterator valid i.e. is it associated with a FAB?
Definition AMReX_MFIter.H:169
void clearNeighborsGPU()
Definition AMReX_NeighborParticlesGPUImpl.H:305
typename ParticleContainerType::ParticleType ParticleType
Definition AMReX_NeighborParticles.H:38
void buildNeighborCopyOp(bool use_boundary_neighbor=false)
Definition AMReX_NeighborParticlesGPUImpl.H:124
void updateNeighborsGPU(bool boundary_neighbors_only=false)
Definition AMReX_NeighborParticlesGPUImpl.H:249
void buildNeighborMask()
Definition AMReX_NeighborParticlesGPUImpl.H:55
void fillNeighborsGPU()
Definition AMReX_NeighborParticlesGPUImpl.H:229
Dynamically allocated vector for trivially copyable data.
Definition AMReX_PODVector.H:308
size_type size() const noexcept
Definition AMReX_PODVector.H:648
iterator begin() noexcept
Definition AMReX_PODVector.H:674
iterator end() noexcept
Definition AMReX_PODVector.H:678
T * dataPtr() noexcept
Definition AMReX_PODVector.H:670
T * data() noexcept
Definition AMReX_PODVector.H:666
void push_back(const T &a_value)
Definition AMReX_PODVector.H:629
This provides length of period for periodic domains. 0 means it is not periodic in that direction....
Definition AMReX_Periodicity.H:17
std::vector< IntVect > shiftIntVect(IntVect const &nghost=IntVect(0)) const
Definition AMReX_Periodicity.cpp:8
OutIter exclusive_scan(InIter begin, InIter end, OutIter result)
Definition AMReX_Scan.H:1440
Arena * The_Comms_Arena()
Definition AMReX_Arena.cpp:843
void copyAsync(HostToDevice, InIter begin, InIter end, OutIter result) noexcept
A host-to-device copy routine. Note this is just a wrapper around memcpy, so it assumes contiguous st...
Definition AMReX_GpuContainers.H:228
static constexpr HostToDevice hostToDevice
Definition AMReX_GpuContainers.H:105
void streamSynchronize() noexcept
Definition AMReX_GpuDevice.H:263
void dtoh_memcpy_async(void *p_h, const void *p_d, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:315
void htod_memcpy_async(void *p_d, const void *p_h, const std::size_t sz) noexcept
Definition AMReX_GpuDevice.H:301
int global_to_local_rank(int rank) noexcept
Definition AMReX_ParallelContext.H:98
bool UseGpuAwareMpi()
Definition AMReX_ParallelDescriptor.H:113
Definition AMReX_Amr.cpp:49
__host__ __device__ BoxND< dim > adjCellHi(const BoxND< dim > &b, int dir, int len=1) noexcept
Similar to adjCellLo but builds an adjacent BoxND on the high end.
Definition AMReX_Box.H:1735
void communicateParticlesStart(const PC &pc, ParticleCopyPlan &plan, const SndBuffer &snd_buffer, RcvBuffer &rcv_buffer)
Definition AMReX_ParticleCommunication.H:500
__host__ __device__ BoxND< dim > adjCellLo(const BoxND< dim > &b, int dir, int len=1) noexcept
Return the cell centered BoxND of length len adjacent to b on the low end along the coordinate direct...
Definition AMReX_Box.H:1714
void unpackRemotes(PC &pc, const ParticleCopyPlan &plan, Buffer &rcv_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:600
BoxND< 3 > Box
Box is an alias for amrex::BoxND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:30
void communicateParticlesFinish(const ParticleCopyPlan &plan)
Definition AMReX_ParticleCommunication.cpp:384
IntVectND< 3 > IntVect
IntVect is an alias for amrex::IntVectND instantiated with AMREX_SPACEDIM.
Definition AMReX_BaseFwd.H:33
int numParticlesOutOfRange(Iterator const &pti, int nGrow)
Returns the number of particles that are more than nGrow cells from the box correspond to the input i...
Definition AMReX_ParticleUtil.H:34
void RemoveDuplicates(Vector< T > &vec)
Definition AMReX_Vector.H:211
void unpackBuffer(PC &pc, const ParticleCopyPlan &plan, const Buffer &snd_buffer, UnpackPolicy const &policy)
Definition AMReX_ParticleCommunication.H:435
void packBuffer(const PC &pc, const ParticleCopyOp &op, const ParticleCopyPlan &plan, Buffer &snd_buffer)
Definition AMReX_ParticleCommunication.H:336
__host__ __device__ IntVect getParticleCell(P const &p, amrex::GpuArray< amrex::Real, 3 > const &plo, amrex::GpuArray< amrex::Real, 3 > const &dxi) noexcept
Returns the cell index for a given particle using the provided lower bounds and cell sizes.
Definition AMReX_ParticleUtil.H:336
Definition AMReX_NeighborParticles.H:16
IntVect periodic_shift
Definition AMReX_NeighborParticles.H:18
int grid_id
Definition AMReX_NeighborParticles.H:17
Definition AMReX_NeighborParticles.H:432
Definition AMReX_ParticleCommunication.H:19