-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunk.hpp
481 lines (411 loc) · 10.7 KB
/
chunk.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
// -*- C++ -*-
#ifndef _CHUNK_HPP_
#define _CHUNK_HPP_
#include "nix.hpp"
NIX_NAMESPACE_BEGIN
// template trick to set number of neighbors
template <int N>
struct NbSize;
template <>
struct NbSize<1> {
static constexpr int size = 3;
};
template <>
struct NbSize<2> {
static constexpr int size = 9;
};
template <>
struct NbSize<3> {
static constexpr int size = 27;
};
///
/// @brief Base class for Chunk
/// @tparam Ndim number of dimension
///
template <int Ndim>
class Chunk
{
protected:
static constexpr int nbsize = NbSize<Ndim>::size; ///< number of neighbors
int myid; ///< chunk ID
int nbid[nbsize]; ///< neighboring chunk ID
int nbrank[nbsize]; ///< neighboring chunk MPI rank
int dims[Ndim]; ///< number of grids
std::vector<float64> load; ///< load array of chunk
virtual void initialize(const int dims[Ndim], int id)
{
// set ID
set_id(id);
// set dimensions
for (int i = 0; i < Ndim; i++) {
this->dims[i] = dims[i];
}
load.resize(1);
reset_load();
}
public:
///
/// @brief get maximum Chunk ID allowable
/// @return maximum Chunk ID
///
static int get_max_id()
{
int max_int32_t = std::numeric_limits<int32_t>::max();
return max_int32_t;
}
/// @brief default constructor
Chunk()
{
const int dims[Ndim] = {};
initialize(dims, 0);
}
///
/// @brief constructor
/// @param dims number of grids for each direction
/// @param id Chunk ID
///
Chunk(const int dims[Ndim], int id = 0)
{
initialize(dims, id);
}
///
/// @brief reset load of chunk
///
virtual void reset_load()
{
load.assign(load.size(), 0.0);
}
///
/// @brief return load array with each element representing different types of operation
/// @return load array
///
virtual std::vector<float64> get_load()
{
return load;
}
///
/// @brief return sum of loads for different operations
/// @return total load of Chunk
///
virtual float64 get_total_load()
{
return std::accumulate(load.begin(), load.end(), 0.0);
}
///
/// @brief pack the content of Chunk into given `buffer`
/// @param buffer pointer to buffer to pack
/// @param address first address of buffer to which the data will be packed
/// @return `address` + (number of bytes packed as a result)
///
virtual int pack(void* buffer, int address)
{
int count = address;
count += memcpy_count(buffer, &myid, sizeof(int), count, 0);
count += memcpy_count(buffer, &nbid[0], nbsize * sizeof(int), count, 0);
count += memcpy_count(buffer, &nbrank[0], nbsize * sizeof(int), count, 0);
// load
{
int size = load.size();
count += memcpy_count(buffer, &size, sizeof(int), count, 0);
count += memcpy_count(buffer, load.data(), sizeof(float64) * size, count, 0);
}
return count;
}
///
/// @brief unpack the content of Chunk from given `buffer`
/// @param buffer point to buffer from unpack
/// @param address first address of buffer from which the data will be unpacked
/// @return `address` + (number of bytes unpacked as a result)
///
virtual int unpack(void* buffer, int address)
{
int count = address;
count += memcpy_count(&myid, buffer, sizeof(int), 0, count);
count += memcpy_count(&nbid[0], buffer, nbsize * sizeof(int), 0, count);
count += memcpy_count(&nbrank[0], buffer, nbsize * sizeof(int), 0, count);
// load
{
int size = 0;
count += memcpy_count(&size, buffer, sizeof(int), 0, count);
load.resize(size);
count += memcpy_count(load.data(), buffer, sizeof(float64) * size, 0, count);
}
return count;
}
///
/// @brief query status of boundary exchange
/// @param mode mode of boundary exchange
/// @param sendrecv +1 for send, -1 for recv, 0 for both
/// @return 1 if boundary exchange is finished and 0 otherwise
///
virtual int set_boundary_query(int mode, int sendrecv) = 0;
///
/// @brief pack boundary exchange
/// @param mode mode of boundary exchange
///
virtual void set_boundary_pack(int mode) = 0;
///
/// @brief unpack boundary exchange
/// @param mode mode of boundary exchange
///
virtual void set_boundary_unpack(int mode) = 0;
///
/// @brief begin boundary exchange
/// @param mode mode of boundary exchange
///
virtual void set_boundary_begin(int mode) = 0;
///
/// @brief end boundary exchange
/// @param mode mode of boundary exchange
///
virtual void set_boundary_end(int mode) = 0;
///
/// @brief set Chunk ID
/// @param id ID to be set
///
void set_id(int id)
{
myid = id;
}
///
/// @brief get Chunk ID
/// @return Chunk ID
///
int get_id()
{
return myid;
}
///
/// @brief set neighbor ID for 1D Chunk
/// @param dirx direction in x
/// @param id ID of neighbor Chunk
///
void set_nb_id(int dirx, int id);
///
/// @brief set neighbor ID for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @param id ID of neighbor Chunk
///
void set_nb_id(int diry, int dirx, int id);
///
/// @brief set neighbor ID for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @param id ID of neighbor Chunk
///
void set_nb_id(int dirz, int diry, int dirx, int id);
///
/// @brief get neighbor Chunk ID for 1D Chunk
/// @param dirx direction in x
/// @return neighbor Chunk ID
///
int get_nb_id(int dirx);
///
/// @brief get neighbor Chunk ID for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @return neighbor Chunk ID
///
int get_nb_id(int diry, int dirx);
///
/// @brief get neighbor Chunk ID for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @return neighbor Chunk ID
///
int get_nb_id(int dirz, int diry, int dirx);
///
/// @brief set neighbor Chunk rank for 1D Chunk
/// @param dirx direction in x
/// @param rank neighbor Chunk rank
///
void set_nb_rank(int dirx, int rank);
///
/// @brief set neighbor Chunk rank for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @param rank neighbor Chunk rank
///
void set_nb_rank(int diry, int dirx, int rank);
///
/// @brief set neigbhro Chunk rank for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @param rank neighbor Chunk rank
///
void set_nb_rank(int dirz, int diry, int dirx, int rank);
///
/// @brief get neighbor Chunk rank for 1D Chunk
/// @param dirx direction x
/// @return neighbor Chunk rank
///
int get_nb_rank(int dirx);
///
/// @brief get neighbor Chunk rank for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @return neighbor Chunk rank
///
int get_nb_rank(int diry, int dirx);
///
/// @brief get neighbor Chunk rank for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @return neighbor Chunk rank
///
int get_nb_rank(int dirz, int diry, int dirx);
///
/// @brief get send tag for 1D Chunk
/// @param dirx direction in x
/// @return send tag
///
int get_sndtag(int dirx);
///
/// @brief get send tag for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @return send tag
///
int get_sndtag(int diry, int dirx);
///
/// @brief get send tag for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @return send tag
///
int get_sndtag(int dirz, int diry, int dirx);
///
/// @brief get receive tag for 1D Chunk
/// @param dirx direction in x
/// @return receive tag
///
int get_rcvtag(int dirx);
///
/// @brief get receive tag for 2D Chunk
/// @param diry direction in y
/// @param dirx direction in x
/// @return receive tag
///
int get_rcvtag(int diry, int dirx);
///
/// @brief get receive tag for 3D Chunk
/// @param dirz direction in z
/// @param diry direction in y
/// @param dirx direction in x
/// @return receive tag
///
int get_rcvtag(int dirz, int diry, int dirx);
};
template <int Ndim>
constexpr int Chunk<Ndim>::nbsize;
//
// implementation of small methods follows
//
template <>
inline void Chunk<1>::set_nb_id(int dirx, int id)
{
nbid[(dirx + 1)] = id;
}
template <>
inline void Chunk<2>::set_nb_id(int diry, int dirx, int id)
{
nbid[3 * (diry + 1) + (dirx + 1)] = id;
}
template <>
inline void Chunk<3>::set_nb_id(int dirz, int diry, int dirx, int id)
{
nbid[9 * (dirz + 1) + 3 * (diry + 1) + (dirx + 1)] = id;
}
template <>
inline int Chunk<1>::get_nb_id(int dirx)
{
return nbid[(dirx + 1)];
}
template <>
inline int Chunk<2>::get_nb_id(int diry, int dirx)
{
return nbid[3 * (diry + 1) + (dirx + 1)];
}
template <>
inline int Chunk<3>::get_nb_id(int dirz, int diry, int dirx)
{
return nbid[9 * (dirz + 1) + 3 * (diry + 1) + (dirx + 1)];
}
template <>
inline void Chunk<1>::set_nb_rank(int dirx, int rank)
{
nbrank[(dirx + 1)] = rank;
}
template <>
inline void Chunk<2>::set_nb_rank(int diry, int dirx, int rank)
{
nbrank[3 * (diry + 1) + (dirx + 1)] = rank;
}
template <>
inline void Chunk<3>::set_nb_rank(int dirz, int diry, int dirx, int rank)
{
nbrank[9 * (dirz + 1) + 3 * (diry + 1) + (dirx + 1)] = rank;
}
template <>
inline int Chunk<1>::get_nb_rank(int dirx)
{
return nbrank[(dirx + 1)];
}
template <>
inline int Chunk<2>::get_nb_rank(int diry, int dirx)
{
return nbrank[3 * (diry + 1) + (dirx + 1)];
}
template <>
inline int Chunk<3>::get_nb_rank(int dirz, int diry, int dirx)
{
return nbrank[9 * (dirz + 1) + 3 * (diry + 1) + (dirx + 1)];
}
template <>
inline int Chunk<1>::get_sndtag(int dirx)
{
int dir = (dirx + 1);
// return dummy tag for invalid neighbor
return nbid[dir] < 0 ? myid : nbid[dir] % MAX_CHUNK_PER_RANK;
}
template <>
inline int Chunk<2>::get_sndtag(int diry, int dirx)
{
int dir = 3 * (diry + 1) + (dirx + 1);
// return dummy tag for invalid neighbor
return nbid[dir] < 0 ? myid : nbid[dir] % MAX_CHUNK_PER_RANK;
}
template <>
inline int Chunk<3>::get_sndtag(int dirz, int diry, int dirx)
{
int dir = 9 * (dirz + 1) + 3 * (diry + 1) + (dirx + 1);
// return dummy tag for invalid neighbor
return nbid[dir] < 0 ? myid : nbid[dir] % MAX_CHUNK_PER_RANK;
}
template <>
inline int Chunk<1>::get_rcvtag(int dirx)
{
return myid % MAX_CHUNK_PER_RANK;
}
template <>
inline int Chunk<2>::get_rcvtag(int diry, int dirx)
{
return myid % MAX_CHUNK_PER_RANK;
}
template <>
inline int Chunk<3>::get_rcvtag(int dirz, int diry, int dirx)
{
return myid % MAX_CHUNK_PER_RANK;
}
NIX_NAMESPACE_END
// Local Variables:
// c-file-style : "gnu"
// c-file-offsets : ((innamespace . 0) (inline-open . 0))
// End:
#endif