-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunkmap.hpp
491 lines (414 loc) · 11.5 KB
/
chunkmap.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
// -*- C++ -*-
#ifndef _CHUNKMAP_HPP_
#define _CHUNKMAP_HPP_
#include "nix.hpp"
#include "sfc.hpp"
#include "xtensorall.hpp"
NIX_NAMESPACE_BEGIN
///
/// @brief ChunkMap class
///
/// The chunk ID is defined with row-major ordering of chunks in cartesian
/// coordinate. Mapping between chunk ID and cartesian coordinate may be
/// calculated via get_chunk() and get_coordinate() methods.
///
template <int Dimension>
class ChunkMap
{
protected:
using IntArray1D = xt::xtensor<int, 1>;
using IntArray2D = xt::xtensor<int, 2>;
using IntArrayND = xt::xtensor<int, Dimension>;
int size; ///< number of total chunks
int dims[3]; ///< chunk dimension
int periodicity[3]; ///< periodicity in each direction
std::vector<int> boundary; ///< rank boundary
IntArray2D coord; ///< chunk ID to coordinate map
IntArrayND chunkid; ///< coordinate to chunk ID map
public:
///
/// @brief constructor for 1D map
/// @param Cx number of chunk in x direction
///
ChunkMap(int Cx);
///
/// @brief constructor for 2D map
/// @param Cy number of chunk in y direction
/// @param Cx number of chunk in x direction
///
ChunkMap(int Cy, int Cx);
///
/// @brief constructor for 3D map
/// @param Cz number of chunk in z direction
/// @param Cy number of chunk in y direction
/// @param Cx number of chunk in x direction
///
ChunkMap(int Cz, int Cy, int Cx);
///
/// @brief constructor
/// @param dims number of chunk in each direction
///
ChunkMap(const int dims[Dimension]);
///
/// @brief check the validity of map
/// @return true if it is valid map, false otherwise
///
virtual bool validate();
///
/// @brief get map information as json object
/// @return obj json object
///
virtual json to_json();
///
/// @brief restore map information from json object
/// @param obj json object
///
virtual void from_json(json& obj);
///
/// @brief set periodicity in each direction
/// @param pz periodicity in z direction
/// @param py periodicity in y direction
/// @param px periodicity in x direction
///
virtual void set_periodicity(int pz, int py, int px)
{
periodicity[0] = pz;
periodicity[1] = py;
periodicity[2] = px;
}
///
/// @brief return neighbor coordinate for a specific direction `dir`
/// @param coord index of coordinate
/// @param delta difference of index of coordinate from `coord`
/// @param dir direction of coordinate
/// @return `coord + delta` if not at boundary, otherwise boundary condition dependent
///
virtual int get_neighbor_coord(int coord, int delta, int dir)
{
int cdir = coord + delta;
if (periodicity[dir] == 1) {
cdir = cdir >= 0 ? cdir : dims[dir] - 1;
cdir = cdir < dims[dir] ? cdir : 0;
} else {
cdir = (cdir >= 0 && cdir < dims[dir]) ? cdir : -1;
}
return cdir;
}
///
/// @brief get process rank associated with chunk ID
/// @param id chunk ID
/// @return rank
///
int get_rank(int id)
{
if (id >= 0 && id < size) {
auto it = std::upper_bound(boundary.begin(), boundary.end(), id);
return std::distance(boundary.begin(), it) - 1;
} else {
return MPI_PROC_NULL;
}
}
///
/// @brief set process rank boundary
/// @param boundary array of rank boundary to set
///
virtual void set_rank_boundary(std::vector<int>& boundary)
{
this->boundary = boundary;
}
///
/// @brief get process rank boundary
/// @return array of rank boundary
///
std::vector<int> get_rank_boundary()
{
return boundary;
}
///
/// @brief get coordinate of chunk for 1D map
/// @param id chunk ID
/// @param cx x coordinate of chunk will be stored
///
void get_coordinate(int id, int& cx);
///
/// @brief get coordinate of chunk for 2D map
/// @param id chunk ID
/// @param cy y coordinate of chunk will be stored
/// @param cx x coordinate of chunk will be stored
///
void get_coordinate(int id, int& cy, int& cx);
///
/// @brief get coordinate of chunk for 3D map
/// @param id chunk ID
/// @param cz z coordinate of chunk will be stored
/// @param cy y coordinate of chunk will be stored
/// @param cx x coordinate of chunk will be stored
///
void get_coordinate(int id, int& cz, int& cy, int& cx);
///
/// @brief get chunk ID for 1D map
/// @param cx x coordinate of chunk
/// @return chunk ID
///
int get_chunkid(int cx);
///
/// @brief get chunk ID for 2D map
/// @param cy y coordinate of chunk
/// @param cx x coordinate of chunk
/// @return chunk ID
///
int get_chunkid(int cy, int cx);
///
/// @brief get chunk ID for 3D map
/// @param cz z coordinate of chunk
/// @param cy y coordinate of chunk
/// @param cx x coordinate of chunk
/// @return chunk ID
///
int get_chunkid(int cz, int cy, int cx);
};
//
// implementation follows
//
#define DEFINE_MEMBER1(type, name) \
template <> \
inline type ChunkMap<1>::name
#define DEFINE_MEMBER2(type, name) \
template <> \
inline type ChunkMap<2>::name
#define DEFINE_MEMBER3(type, name) \
template <> \
inline type ChunkMap<3>::name
DEFINE_MEMBER1(, ChunkMap)(int Cx) : periodicity{1, 1, 1}
{
size = Cx;
dims[0] = Cx;
dims[1] = 1;
dims[2] = 1;
// memory allocation
{
std::vector<size_t> dims1 = {static_cast<size_t>(size)};
std::vector<size_t> dims2 = {static_cast<size_t>(size), 1};
std::vector<size_t> dims3 = {static_cast<size_t>(Cx)};
coord.resize(dims2);
chunkid.resize(dims3);
coord.fill(0);
chunkid.fill(0);
}
// build mapping
sfc::get_map1d(Cx, chunkid, coord);
}
DEFINE_MEMBER2(, ChunkMap)(int Cy, int Cx) : periodicity{1, 1, 1}
{
size = Cy * Cx;
dims[0] = Cy;
dims[1] = Cx;
dims[2] = 1;
// memory allocation
{
std::vector<size_t> dims1 = {static_cast<size_t>(size)};
std::vector<size_t> dims2 = {static_cast<size_t>(size), 2};
std::vector<size_t> dims3 = {static_cast<size_t>(Cy), static_cast<size_t>(Cx)};
coord.resize(dims2);
chunkid.resize(dims3);
coord.fill(0);
chunkid.fill(0);
}
// build mapping
sfc::get_map2d(Cy, Cx, chunkid, coord);
}
DEFINE_MEMBER3(, ChunkMap)(int Cz, int Cy, int Cx) : periodicity{1, 1, 1}
{
size = Cz * Cy * Cx;
dims[0] = Cz;
dims[1] = Cy;
dims[2] = Cx;
// memory allocation
{
std::vector<size_t> dims1 = {static_cast<size_t>(size)};
std::vector<size_t> dims2 = {static_cast<size_t>(size), 3};
std::vector<size_t> dims3 = {static_cast<size_t>(Cz), static_cast<size_t>(Cy),
static_cast<size_t>(Cx)};
coord.resize(dims2);
chunkid.resize(dims3);
coord.fill(0);
chunkid.fill(0);
}
// build mapping
sfc::get_map3d(Cz, Cy, Cx, chunkid, coord);
}
DEFINE_MEMBER1(, ChunkMap)(const int dims[1]) : ChunkMap<1>(dims[0])
{
}
DEFINE_MEMBER2(, ChunkMap)(const int dims[2]) : ChunkMap<2>(dims[0], dims[1])
{
}
DEFINE_MEMBER3(, ChunkMap)(const int dims[3]) : ChunkMap<3>(dims[0], dims[1], dims[2])
{
}
DEFINE_MEMBER1(bool, validate)()
{
return sfc::check_index(chunkid);
}
DEFINE_MEMBER2(bool, validate)()
{
return sfc::check_index(chunkid) & sfc::check_locality2d(coord);
}
DEFINE_MEMBER3(bool, validate)()
{
return sfc::check_index(chunkid) & sfc::check_locality3d(coord);
}
DEFINE_MEMBER1(json, to_json)()
{
json obj;
// meta data
obj["size"] = size;
obj["ndim"] = 1;
obj["shape"] = {dims[0]};
obj["periodicity"] = {periodicity[0]};
// map
obj["chunkid"] = chunkid;
obj["coord"] = coord;
obj["boundary"] = boundary;
return obj;
}
DEFINE_MEMBER2(json, to_json)()
{
json obj;
// meta data
obj["size"] = size;
obj["ndim"] = 2;
obj["shape"] = {dims[0], dims[1]};
obj["periodicity"] = {periodicity[0], periodicity[1]};
// map
obj["chunkid"] = chunkid;
obj["coord"] = coord;
obj["boundary"] = boundary;
return obj;
}
DEFINE_MEMBER3(json, to_json)()
{
json obj;
// meta data
obj["size"] = size;
obj["ndim"] = 3;
obj["shape"] = {dims[0], dims[1], dims[2]};
obj["periodicity"] = {periodicity[0], periodicity[1], periodicity[2]};
// map
obj["chunkid"] = chunkid;
obj["coord"] = coord;
obj["boundary"] = boundary;
return obj;
}
DEFINE_MEMBER1(void, from_json)(json& obj)
{
if (obj["ndim"].get<int>() != 1) {
ERROR << tfm::format("Invalid input to ChunkMap<1>::load_json");
}
// meta data
size = obj["size"].get<int>();
dims[0] = obj["shape"][0].get<int>();
periodicity[0] = obj["periodicity"][0].get<int>();
// map
chunkid = obj["chunkid"];
coord = obj["coord"];
boundary = obj["boundary"].get<std::vector<int>>();
}
DEFINE_MEMBER2(void, from_json)(json& obj)
{
if (obj["ndim"].get<int>() != 2) {
ERROR << tfm::format("Invalid input to ChunkMap<2>::load_json");
}
// meta data
size = obj["size"].get<int>();
dims[0] = obj["shape"][0].get<int>();
dims[1] = obj["shape"][1].get<int>();
periodicity[0] = obj["periodicity"][0].get<int>();
periodicity[1] = obj["periodicity"][1].get<int>();
// map
chunkid = obj["chunkid"];
coord = obj["coord"];
boundary = obj["boundary"].get<std::vector<int>>();
}
DEFINE_MEMBER3(void, from_json)(json& obj)
{
if (obj["ndim"].get<int>() != 3) {
ERROR << tfm::format("Invalid input to ChunkMap<3>::load_json");
}
// meta data
size = obj["size"].get<int>();
dims[0] = obj["shape"][0].get<int>();
dims[1] = obj["shape"][1].get<int>();
dims[2] = obj["shape"][2].get<int>();
periodicity[0] = obj["periodicity"][0].get<int>();
periodicity[1] = obj["periodicity"][1].get<int>();
periodicity[2] = obj["periodicity"][2].get<int>();
// map
chunkid = obj["chunkid"];
coord = obj["coord"];
boundary = obj["boundary"].get<std::vector<int>>();
}
DEFINE_MEMBER1(void, get_coordinate)(int id, int& cx)
{
if (id >= 0 && id < size) {
cx = coord(id, 0);
} else {
cx = -1;
}
}
DEFINE_MEMBER1(int, get_chunkid)(int cx)
{
if (cx >= 0 && cx < dims[0]) {
return chunkid(cx);
} else {
return -1;
}
}
DEFINE_MEMBER2(void, get_coordinate)(int id, int& cy, int& cx)
{
if (id >= 0 && id < size) {
cx = coord(id, 0);
cy = coord(id, 1);
} else {
cy = -1;
cx = -1;
}
}
DEFINE_MEMBER2(int, get_chunkid)(int cy, int cx)
{
if ((cy >= 0 && cy < dims[0]) && (cx >= 0 && cx < dims[1])) {
return chunkid(cy, cx);
} else {
return -1;
}
}
DEFINE_MEMBER3(void, get_coordinate)(int id, int& cz, int& cy, int& cx)
{
if (id >= 0 && id < size) {
cx = coord(id, 0);
cy = coord(id, 1);
cz = coord(id, 2);
} else {
cz = -1;
cy = -1;
cx = -1;
}
}
DEFINE_MEMBER3(int, get_chunkid)(int cz, int cy, int cx)
{
if ((cz >= 0 && cz < dims[0]) && (cy >= 0 && cy < dims[1]) && (cx >= 0 && cx < dims[2])) {
return chunkid(cz, cy, cx);
} else {
return -1;
}
}
#undef DEFINE_MEMBER
#undef DEFINE_MEMBER1
#undef DEFINE_MEMBER2
#undef DEFINE_MEMBER3
NIX_NAMESPACE_END
// Local Variables:
// c-file-style : "gnu"
// c-file-offsets : ((innamespace . 0) (inline-open . 0))
// End:
#endif