-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxtensor_halo3d.hpp
516 lines (429 loc) · 14.1 KB
/
xtensor_halo3d.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// -*- C++ -*-
#ifndef _XTENSOR_HALO3D_HPP_
#define _XTENSOR_HALO3D_HPP_
#include "halo3d.hpp"
#include "nix.hpp"
#include "xtensor_particle.hpp"
#include "xtensorall.hpp"
NIX_NAMESPACE_BEGIN
///
/// @brief Boundary Halo3D class for field
///
template <typename Chunk>
class XtensorHaloField3D : public Halo3D<xt::xtensor<float64, 4>, Chunk, true>
{
public:
using Base = Halo3D<xt::xtensor<float64, 4>, Chunk, true>;
using Base::Base; // constructor
using Base::data;
using Base::chunk;
template <typename BufferPtr>
bool pack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
// packing
auto Iz = xt::range(send_bound[0][0], send_bound[0][1] + 1);
auto Iy = xt::range(send_bound[1][0], send_bound[1][1] + 1);
auto Ix = xt::range(send_bound[2][0], send_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_send_buffer(iz, iy, ix));
std::copy(view.begin(), view.end(), ptr);
// datatype
mpibuf->sendtype(iz, iy, ix) = MPI_BYTE;
mpibuf->recvtype(iz, iy, ix) = MPI_BYTE;
return true;
}
template <typename BufferPtr>
bool unpack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
if (chunk->get_nb_rank(iz - 1, iy - 1, ix - 1) == MPI_PROC_NULL)
return false;
// unpacking
auto Iz = xt::range(recv_bound[0][0], recv_bound[0][1] + 1);
auto Iy = xt::range(recv_bound[1][0], recv_bound[1][1] + 1);
auto Ix = xt::range(recv_bound[2][0], recv_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_recv_buffer(iz, iy, ix));
std::copy(ptr, ptr + view.size(), view.begin());
return true;
}
};
///
/// @brief Boundary Halo3D class for current
///
template <typename Chunk>
class XtensorHaloCurrent3D : public Halo3D<xt::xtensor<float64, 4>, Chunk, true>
{
public:
using Base = Halo3D<xt::xtensor<float64, 4>, Chunk, true>;
using Base::Base; // constructor
using Base::data;
using Base::chunk;
template <typename BufferPtr>
bool pack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
// packing
auto Iz = xt::range(recv_bound[0][0], recv_bound[0][1] + 1);
auto Iy = xt::range(recv_bound[1][0], recv_bound[1][1] + 1);
auto Ix = xt::range(recv_bound[2][0], recv_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_send_buffer(iz, iy, ix));
std::copy(view.begin(), view.end(), ptr);
// datatype
mpibuf->sendtype(iz, iy, ix) = MPI_BYTE;
mpibuf->recvtype(iz, iy, ix) = MPI_BYTE;
return true;
}
template <typename BufferPtr>
bool unpack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
if (chunk->get_nb_rank(iz - 1, iy - 1, ix - 1) == MPI_PROC_NULL)
return false;
// unpacking
auto Iz = xt::range(send_bound[0][0], send_bound[0][1] + 1);
auto Iy = xt::range(send_bound[1][0], send_bound[1][1] + 1);
auto Ix = xt::range(send_bound[2][0], send_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_recv_buffer(iz, iy, ix));
std::transform(ptr, ptr + view.size(), view.begin(), view.begin(), std::plus<float64>());
return true;
}
};
///
/// @brief Boundary Halo3D class for moment
///
template <typename Chunk>
class XtensorHaloMoment3D : public Halo3D<xt::xtensor<float64, 5>, Chunk, true>
{
public:
using Base = Halo3D<xt::xtensor<float64, 5>, Chunk, true>;
using Base::Base; // constructor
using Base::data;
using Base::chunk;
template <typename BufferPtr>
bool pack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
// packing
auto Iz = xt::range(recv_bound[0][0], recv_bound[0][1] + 1);
auto Iy = xt::range(recv_bound[1][0], recv_bound[1][1] + 1);
auto Ix = xt::range(recv_bound[2][0], recv_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_send_buffer(iz, iy, ix));
std::copy(view.begin(), view.end(), ptr);
// datatype
mpibuf->sendtype(iz, iy, ix) = MPI_BYTE;
mpibuf->recvtype(iz, iy, ix) = MPI_BYTE;
return true;
}
template <typename BufferPtr>
bool unpack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
if (chunk->get_nb_rank(iz - 1, iy - 1, ix - 1) == MPI_PROC_NULL)
return false;
// unpacking
auto Iz = xt::range(send_bound[0][0], send_bound[0][1] + 1);
auto Iy = xt::range(send_bound[1][0], send_bound[1][1] + 1);
auto Ix = xt::range(send_bound[2][0], send_bound[2][1] + 1);
auto view = xt::strided_view(*data, {Iz, Iy, Ix, xt::ellipsis()});
float64* ptr = static_cast<float64*>(mpibuf->get_recv_buffer(iz, iy, ix));
std::transform(ptr, ptr + view.size(), view.begin(), view.begin(), std::plus<float64>());
return true;
}
};
///
/// @brief Boundary Halo3D class for particle
///
template <typename Chunk>
class XtensorHaloParticle3D : public Halo3D<ParticleVec, Chunk, false>
{
public:
using Base = Halo3D<ParticleVec, Chunk, false>;
using Base::data;
using Base::chunk;
static constexpr int32_t head_byte = sizeof(int32_t);
static constexpr int32_t elem_byte = ParticlePtr::element_type::get_particle_size();
int32_t Ns;
ParticleVec particle;
std::vector<int32_t> num_unpacked;
XtensorHaloParticle3D(ParticleVec& data, Chunk& chunk)
: Halo3D<ParticleVec, Chunk, false>(data, chunk)
{
Ns = data.size();
particle = data;
}
template <typename BufferPtr>
void pre_pack(BufferPtr& mpibuf)
{
const float64 xmin = chunk->get_xmin();
const float64 xmax = chunk->get_xmax();
const float64 ymin = chunk->get_ymin();
const float64 ymax = chunk->get_ymax();
const float64 zmin = chunk->get_zmin();
const float64 zmax = chunk->get_zmax();
std::array<size_t, 4> shape = {static_cast<size_t>(Ns + 1), 3ul, 3ul, 3ul};
xt::xtensor<int32_t, 4> send_count(shape);
// initialize
send_count.fill(0);
//
// count out-of-bounds particles
//
for (int is = 0; is < Ns; is++) {
auto& xu = particle[is]->xu;
for (int ip = 0; ip < particle[is]->Np; ip++) {
int iz = (xu(ip, 2) >= zmax) - (xu(ip, 2) < zmin) + 1;
int iy = (xu(ip, 1) >= ymax) - (xu(ip, 1) < ymin) + 1;
int ix = (xu(ip, 0) >= xmax) - (xu(ip, 0) < xmin) + 1;
// skip
if (ix == 1 && iy == 1 && iz == 1)
continue;
send_count(is, iz, iy, ix)++;
send_count(Ns, iz, iy, ix)++; // total number of send particles
}
}
//
// allocate buffer
//
{
int bufsize = 0;
mpibuf->bufsize.fill(0);
mpibuf->bufaddr.fill(0);
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
// skip
if (iz == 1 && iy == 1 && ix == 1)
continue;
mpibuf->bufsize(iz, iy, ix) = elem_byte * send_count(Ns, iz, iy, ix) + head_byte * Ns;
mpibuf->bufaddr(iz, iy, ix) = bufsize;
bufsize += mpibuf->bufsize(iz, iy, ix);
}
}
}
mpibuf->sendbuf.resize(bufsize);
}
//
// pack header
//
{
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
// skip
if (iz == 1 && iy == 1 && ix == 1)
continue;
int addr = mpibuf->bufaddr(iz, iy, ix);
for (int is = 0; is < Ns; is++) {
std::memcpy(mpibuf->sendbuf.get(addr), &send_count(is, iz, iy, ix), head_byte);
addr += head_byte + elem_byte * send_count(is, iz, iy, ix);
}
}
}
}
}
//
// pack out-of-bounds particles
//
{
auto addr = mpibuf->bufaddr;
for (int is = 0; is < Ns; is++) {
// skip header
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
addr(iz, iy, ix) += head_byte;
}
}
}
// pack particles
auto& xu = particle[is]->xu;
for (int ip = 0; ip < particle[is]->Np; ip++) {
int iz = (xu(ip, 2) >= zmax) - (xu(ip, 2) < zmin) + 1;
int iy = (xu(ip, 1) >= ymax) - (xu(ip, 1) < ymin) + 1;
int ix = (xu(ip, 0) >= xmax) - (xu(ip, 0) < xmin) + 1;
// skip
if (ix == 1 && iy == 1 && iz == 1)
continue;
// pack
std::memcpy(mpibuf->sendbuf.get(addr(iz, iy, ix)), &xu(ip, 0), elem_byte);
addr(iz, iy, ix) += elem_byte;
send_count(is, iz, iy, ix)--;
}
}
}
//
// check if all particles are packed
//
{
bool is_all_packed = true;
for (int is = 0; is < Ns; is++) {
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
is_all_packed = is_all_packed && (send_count(is, iz, iy, ix) == 0);
}
}
}
}
if (is_all_packed == false) {
ERROR << tfm::format("Some particles are not properly packed!");
}
}
}
template <typename BufferPtr>
bool pack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
// datatype
mpibuf->sendtype(iz, iy, ix) = MPI_BYTE;
mpibuf->recvtype(iz, iy, ix) = MPI_BYTE;
return true;
}
template <typename BufferPtr>
void post_pack(BufferPtr& mpibuf)
{
// do nothing
}
template <typename BufferPtr>
void pre_unpack(BufferPtr& mpibuf)
{
std::array<size_t, 4> shape = {static_cast<size_t>(Ns + 1), 3ul, 3ul, 3ul};
xt::xtensor<int32_t, 4> recv_count(shape);
// initialize
recv_count.fill(0);
//
// unpack header
//
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
// skip
if (iz == 1 && iy == 1 && ix == 1)
continue;
// skip null message
if (chunk->get_nb_rank(iz - 1, iy - 1, ix - 1) == MPI_PROC_NULL)
continue;
int rcnt = 0;
int addr = mpibuf->bufaddr(iz, iy, ix);
for (int is = 0; is < Ns; is++) {
std::memcpy(&rcnt, mpibuf->recvbuf.get(addr), head_byte);
addr += head_byte + elem_byte * rcnt;
recv_count(is, iz, iy, ix) = rcnt;
recv_count(Ns, iz, iy, ix) += rcnt;
}
}
}
}
//
// resize particle buffer if needed
//
{
const float64 target = 1 + chunk->get_buffer_ratio();
for (int is = 0; is < Ns; is++) {
int np_next = particle[is]->Np;
for (int iz = 0; iz < 3; iz++) {
for (int iy = 0; iy < 3; iy++) {
for (int ix = 0; ix < 3; ix++) {
np_next += recv_count(is, iz, iy, ix);
}
}
}
if (np_next > particle[is]->Np_total) {
// expand
particle[is]->resize(target * np_next);
} else if (target * np_next < particle[is]->Np_total) {
// shrink
particle[is]->resize(target * np_next);
}
}
}
// number of unpacked particles
num_unpacked.resize(Ns, 0);
}
template <typename BufferPtr>
bool unpack(BufferPtr& mpibuf, int iz, int iy, int ix, int send_bound[3][2], int recv_bound[3][2])
{
// skip
if (iz == 1 && iy == 1 && ix == 1)
return false;
if (chunk->get_nb_rank(iz - 1, iy - 1, ix - 1) == MPI_PROC_NULL)
return false;
//
// copy to the end of particle array
//
uint8_t* recvptr = mpibuf->recvbuf.get(mpibuf->bufaddr(iz, iy, ix));
int recvcnt = mpibuf->bufsize(iz, iy, ix);
// check message size
if (recvcnt < Ns * head_byte) {
ERROR << tfm::format("Received message smaller than the header size: %d", recvcnt);
return false;
}
// unpack
for (int is = 0; is < Ns; is++) {
int Np = particle[is]->Np;
// header
int rcnt;
std::memcpy(&rcnt, recvptr, head_byte);
recvptr += head_byte;
recvcnt -= head_byte;
// particles
float64* ptcl = &particle[is]->xu(Np + num_unpacked[is], 0);
std::memcpy(ptcl, recvptr, elem_byte * rcnt);
recvptr += rcnt * elem_byte;
recvcnt -= rcnt * elem_byte;
// increment number of unpacked particles
num_unpacked[is] += rcnt;
}
// check consistency
if (recvcnt != 0) {
ERROR << tfm::format("Unexpected message perhaps with wrong headers?");
return false;
}
return true;
}
template <typename BufferPtr>
void post_unpack(BufferPtr& mpibuf)
{
//
// set boundary condition and append count for received particles
//
for (int is = 0; is < Ns; is++) {
int np_prev = particle[is]->Np;
int np_next = particle[is]->Np + num_unpacked[is];
chunk->set_boundary_particle_after_sendrecv(particle[is], np_prev, np_next - 1, is);
chunk->count_particle(particle[is], np_prev, np_next - 1, false);
// now update number of particles
particle[is]->Np = np_next;
}
//
// sort particle array and discard out-of-range particles
//
for (int is = 0; is < Ns; is++) {
particle[is]->sort();
}
}
};
NIX_NAMESPACE_END
// Local Variables:
// c-file-style : "gnu"
// c-file-offsets : ((innamespace . 0) (inline-open . 0))
// End:
#endif