-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatablockcontainer.h
More file actions
495 lines (411 loc) · 15.6 KB
/
datablockcontainer.h
File metadata and controls
495 lines (411 loc) · 15.6 KB
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
#ifndef DATABLOCKCONTAINER
#define DATABLOCKCONTAINER
#include "datablock.h"
#include<iostream>
template<typename T>
class DataBlock_GPU_Memory_Functions;
template<typename T>
class DataBlock_Host_Memory_Functions;
template<typename T>
class DataBlock_MPI_Functions;
template<typename T>
class DataBlock;
template<typename T>
class In_Kernel_Mathfunctions;
template<typename T>
class Math_Functions;
template<typename T>
class Math_Functions_MPI;
template<typename T>
class GPU_Math_Functions;
template<typename U, typename Container>
class mdspan;
template<typename U, typename Container>
class mdspan_data;
#include <iostream>
#pragma omp begin declare target
template<typename T>
class BlockedDataView:
protected DataBlock<T>
{
public:
friend class DataBlock_GPU_Memory_Functions<T>;
friend class DataBlock_Host_Memory_Functions<T>;
friend class DataBlock_MPI_Functions<T>;
friend class In_Kernel_Mathfunctions<T>;
friend class GPU_Math_Functions<T>;
friend class Math_Functions<T>;
friend class Math_Functions_MPI<T>;
template<typename U, typename Container>
friend class ::mdspan;
template<typename U, typename Container>
friend class ::mdspan_data;
BlockedDataView(const DataBlock<T>& db, const size_t* bshape, bool remove_zeroblocks)
:DataBlock<T>(db)
{
block_shape=new size_t[this->dprank];
#pragma omp simd
for (size_t i=0; i<this->dprank; i++)
block_shape[i]=bshape[i];
offsets_starts_is_devptr=(this->dpdata_is_devptr &&omp_is_initial_device());
if(offsets_starts_is_devptr)
devnum=this->devptr_devicenum;
else
devnum=-1;
switch(this->dprank)
{
case 1:
build_blocks_rank1(bshape[0], remove_zeroblocks);
break;
case 2:
build_blocks_rank2(bshape[0], bshape[1], remove_zeroblocks);
break;
default:
build_blocks_arbitrary_rank(bshape, remove_zeroblocks);
break;
}
}
~BlockedDataView()
{
delete[] block_shape;
if (offsets_starts_is_devptr &&omp_is_initial_device())
{
omp_target_free(pooled_offsets_flat,devnum);
omp_target_free(pooled_offsets_starts, devnum);
}
else
{
delete[] pooled_offsets_flat;
delete[] pooled_offsets_starts;
}
devnum=-1;
}
const DataBlock<T>& get_datablock()const
{
return *this;
}
protected:
size_t* block_shape;
size_t* pooled_offsets_flat; // stores concatenated coordinates
size_t* pooled_offsets_starts;
size_t usedblocks=0;
bool offsets_starts_is_devptr=false;
int devnum=-1;
// --- Rank-1 specialized ---
void build_blocks_rank1(size_t block_size, bool remove_zeroblocks)
{
const size_t nblocks = (this->dpextents[0] + block_size - 1) / block_size;
pooled_offsets_flat = offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * nblocks,devnum)
: new size_t[nblocks];
pooled_offsets_starts = offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * (nblocks + 1),devnum)
: new size_t[nblocks + 1];
size_t count = 0;
size_t ext0=this->dpextents[0] ;
const T* pd=this->dpdata;
if(offsets_starts_is_devptr)
{
#pragma omp target teams distribute parallel for map (tofrom: count)is_device_ptr(pd) device(devnum)
for (size_t bi = 0; bi < nblocks; ++bi)
{
const size_t offset = bi * block_size;
const size_t diff = ext0- offset;
const size_t len = (block_size < diff) ? block_size : diff;
bool keep = true;
if (remove_zeroblocks)
{
keep = false;
for (size_t i = 0; i < len; ++i)
{
if (pd[offset + i] != T(0))
{
keep = true;
goto outofloop1;
}
}
}
outofloop1:
if (keep)
{
size_t slot;
#pragma omp atomic capture
slot = count++;
{
pooled_offsets_starts[slot] = slot;
pooled_offsets_flat[slot] = offset;
}
}
}
omp_target_memcpy(pooled_offsets_starts,&count,sizeof(size_t),sizeof(size_t)*count,0,devnum,omp_get_initial_device()); // sentinel
usedblocks = count;
}
else
{
#pragma omp parallel for
for (size_t bi = 0; bi < nblocks; ++bi)
{
const size_t offset = bi * block_size;
const size_t diff = ext0- offset;
const size_t len = (block_size < diff) ? block_size : diff;
bool keep = true;
if (remove_zeroblocks)
{
keep = false;
for (size_t i = 0; i < len; ++i)
{
if (pd[offset + i] != T(0))
{
keep = true;
goto outofloop2;
}
}
}
outofloop2:
if (keep)
{
size_t slot;
#pragma omp atomic capture
slot = count++;
{
pooled_offsets_starts[slot] = slot;
pooled_offsets_flat[slot] = offset;
}
}
}
pooled_offsets_starts[count] = count; // sentinel
usedblocks = count;
}
}
// --- Rank-2 specialized ---
void build_blocks_rank2(const size_t block_rows,const size_t block_cols,const bool remove_zeroblocks)
{
const size_t nblocks_row = (this->dpextents[0] + block_rows - 1) / block_rows;
const size_t nblocks_col = (this->dpextents[1] + block_cols - 1) / block_cols;
const size_t maxblocks = nblocks_row * nblocks_col;
pooled_offsets_flat = offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * 2 * maxblocks, devnum)
: new size_t[2 * maxblocks];
pooled_offsets_starts = offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * (maxblocks + 1),devnum)
: new size_t[maxblocks + 1];
size_t count = 0; // block count
const size_t ext0=this->dpextents[0];
const size_t ext1=this->dpextents[1];
const size_t str0=this->dpstrides[0];
const size_t str1=this->dpstrides[1];
const T* pd=this->dpdata;
if(offsets_starts_is_devptr)
{
#pragma omp target teams distribute parallel for collapse(2) map(tofrom:count) is_device_ptr(pd) device(devnum)
for (size_t bi = 0; bi < nblocks_row; ++bi)
{
for (size_t bj = 0; bj < nblocks_col; ++bj)
{
const size_t row_off = bi * block_rows;
const size_t diff1 = ext0 - row_off;
const size_t tile_rows = (block_rows < diff1) ? block_rows : diff1;
const size_t col_off = bj * block_cols;
const size_t diff2 = ext1 - col_off;
const size_t tile_cols = (block_cols < diff2) ? block_cols : diff2;
bool keep = true;
if (remove_zeroblocks)
{
keep = false;
for (size_t i = 0; i < tile_rows && !keep; ++i)
{
for (size_t j = 0; j < tile_cols && !keep; ++j)
{
if (pd[(row_off + i) * str0 + (col_off + j) *str1] != T(0))
{
keep = true;
goto outofloop3;
}
}
}
}
outofloop3:
if (keep)
{
size_t slot;
#pragma omp atomic capture
slot = count++;
{
size_t pos = slot * 2;
pooled_offsets_starts[slot] = pos;
pooled_offsets_flat[pos] = row_off;
pooled_offsets_flat[pos+1] = col_off;
}
}
}
}
size_t count2=2*count;
omp_target_memcpy(pooled_offsets_starts,&count2,sizeof(size_t),sizeof(size_t)*count,0,devnum,omp_get_initial_device()); // sentinel
usedblocks = count;
}
else
{
#pragma omp parallel for collapse(2)
for (size_t bi = 0; bi < nblocks_row; ++bi)
{
for (size_t bj = 0; bj < nblocks_col; ++bj)
{
const size_t row_off = bi * block_rows;
const size_t diff1 = ext0 - row_off;
const size_t tile_rows = (block_rows < diff1) ? block_rows : diff1;
bool keep = true;
const size_t col_off = bj * block_cols;
const size_t diff2 = ext1 - col_off;
const size_t tile_cols = (block_cols < diff2) ? block_cols : diff2;
if (remove_zeroblocks)
{
keep = false;
for (size_t i = 0; i < tile_rows && !keep; ++i)
for (size_t j = 0; j < tile_cols && !keep; ++j)
if (pd[(row_off + i) * str0 + (col_off + j) *str1] != T(0))
{
keep = true;
goto outofloop4;
}
}
outofloop4:
if (keep)
{
size_t slot;
#pragma omp atomic capture
slot = count++;
{
const size_t pos = slot * 2;
pooled_offsets_starts[slot] = pos;
pooled_offsets_flat[pos] = row_off;
pooled_offsets_flat[pos+1] = col_off;
}
}
}
}
pooled_offsets_starts[count] = count*2;
usedblocks = count;
}
}
bool is_nonzero_block(const size_t* block_shape,
const size_t* block_idx,
const size_t* tile_extents,
const size_t rank)
{
size_t* idx=new size_t[rank];
for(size_t i=0; i<rank; i++)
idx[i]=0;
bool b=check_nonzero_recursive(block_shape, block_idx, tile_extents, rank, 0, idx);
delete []idx;
return b;
}
bool check_nonzero_recursive(const size_t* block_shape,
const size_t* block_idx,
const size_t* tile_extents,
const size_t rank,
const size_t dim,
size_t* idx)
{
if (dim == rank)
{
// compute linear offset
size_t linear = 0;
for (size_t d = 0; d < rank; ++d)
{
const size_t global_coord = block_idx[d] * block_shape[d] + idx[d];
linear += global_coord * this->dpstrides[d];
}
T d;
if(offsets_starts_is_devptr)
omp_target_memcpy(&d,this->dpdata,sizeof(T),0,sizeof(T)*linear,omp_get_initial_device(),devnum);
else
d=this->dpdata[linear];
return d != T(0);
}
for (size_t i = 0; i < tile_extents[dim]; ++i)
{
idx[dim] = i;
if (check_nonzero_recursive(block_shape, block_idx, tile_extents, rank, dim+1, idx))
return true;
}
return false;
}
void build_blocks_arbitrary_rank(const size_t* bshape,const bool remove_zeroblocks)
{
const size_t r = this->dprank;
size_t* nblocks_dim = new size_t[r];
size_t maxblocks = 1;
for (size_t d = 0; d < r; ++d)
{
nblocks_dim[d] = (this->dpextents[d] + bshape[d] - 1) / bshape[d];
maxblocks *= nblocks_dim[d];
}
pooled_offsets_flat = offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * r * maxblocks, devnum)
: new size_t[r * maxblocks];
pooled_offsets_starts =offsets_starts_is_devptr
? (size_t*)omp_target_alloc(sizeof(size_t) * (maxblocks + 1), devnum)
: new size_t[maxblocks + 1];
size_t* idx = new size_t[r];
for (size_t d = 0; d < r; ++d)
idx[d] = 0;
size_t count = 0;
size_t count2 = 0;
while (true)
{
bool keep = true;
if (remove_zeroblocks)
{
size_t* tile_extents = new size_t[r];
for (size_t d = 0; d < r; ++d)
{
const size_t offset = idx[d] * bshape[d];
const size_t diff = this->dpextents[d] - offset;
tile_extents[d] = (bshape[d] < diff) ? bshape[d] : diff;
}
keep = is_nonzero_block(bshape, idx, tile_extents, r);
delete[] tile_extents;
}
if (keep)
{
if(offsets_starts_is_devptr)
{
omp_target_memcpy(pooled_offsets_starts,&count2,sizeof(size_t),sizeof(size_t)*count,0,devnum,omp_get_initial_device());
for (size_t d = 0; d < r; ++d)
{
size_t u= idx[d] * bshape[d];
omp_target_memcpy(pooled_offsets_flat,&u,sizeof(size_t),sizeof(size_t)*count2,0,devnum,omp_get_initial_device());
}
++count2;
++count;
}
else
{
pooled_offsets_starts[count] = count2;
for (size_t d = 0; d < r; ++d)
pooled_offsets_flat[count2++] = idx[d] * bshape[d];
++count;
}
}
// increment multidim index
size_t dim = 0;
for (; dim < r; ++dim)
{
idx[dim]++;
if (idx[dim] < nblocks_dim[dim])
break;
idx[dim] = 0;
}
if (dim == r) break;
}
if(offsets_starts_is_devptr)
omp_target_memcpy(pooled_offsets_starts,&count2,sizeof(size_t),sizeof(size_t)*count,0,devnum,omp_get_initial_device());
else
pooled_offsets_starts[count] = count2; // sentinel
usedblocks = count;
delete[] idx;
delete[] nblocks_dim;
}
};
#pragma omp end declare target
#endif