/
sharedmem.hpp
453 lines (389 loc) · 14 KB
/
sharedmem.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
/** @file
* @copyright University of Warsaw
* @section LICENSE
* GPLv3+ (see the COPYING file or http://www.gnu.org/licenses/)
*/
#pragma once
#include <unordered_map>
#include <boost/ptr_container/ptr_vector.hpp>
#include <libmpdata++/blitz.hpp>
#include <libmpdata++/formulae/arakawa_c.hpp>
#include <array>
namespace libmpdataxx
{
namespace concurr
{
namespace detail
{
template <
typename real_t,
int n_dims,
int n_tlev
>
class sharedmem_common
{
using arr_t = blitz::Array<real_t, n_dims>;
static_assert(n_dims > 0, "n_dims <= 0");
static_assert(n_tlev > 0, "n_tlev <= 0");
std::unique_ptr<blitz::Array<real_t, 1>> xtmtmp;
std::unique_ptr<blitz::Array<double, 1>> sumtmp;
protected:
blitz::TinyVector<int, n_dims> origin;
public:
int n = 0;
const int size;
std::array<rng_t, n_dims> grid_size;
bool panic = false; // for multi-threaded SIGTERM handling
// TODO: these are public because used from outside in alloc - could friendship help?
arrvec_t<arr_t> GC, ndt_GC, ndtt_GC;
std::vector<arrvec_t<arr_t>> psi; // TODO: since n_eqns is known, could make it an std::array!
std::unique_ptr<arr_t> G;
std::unique_ptr<arr_t> vab_coeff; // velocity absorber coefficient
arrvec_t<arr_t> vab_relax; // velocity absorber relaxed state
arrvec_t<arr_t> khn_tmp; // Kahan sum for donor-cell
std::unordered_map<
const char*, // intended for addressing with __FILE__
boost::ptr_vector<arrvec_t<arr_t>>
> tmp;
// list of temporary fields that can be accessed from outside of concurr
std::unordered_map<
std::string,
std::pair<const char*, int>
> avail_tmp;
virtual void barrier()
{
assert(false && "sharedmem_common::barrier() called!");
}
void cycle(const int &rank)
{
barrier();
if (rank == 0) n = (n + 1) % n_tlev - n_tlev; // - n_tlev assures Python-type end-of-array cyclic behaviour works
barrier();
}
// ctors
// TODO: fill reducetmp with NaNs (or use 1-element arrvec_t - it's NaN-filled by default)
sharedmem_common(const std::array<int, n_dims> &grid_size, const int &size)
: n(0), size(size) // TODO: is n(0) needed?
{
for (int d = 0; d < n_dims; ++d)
{
this->grid_size[d] = rng_t(0, grid_size[d]-1);
origin[d] = this->grid_size[d].first();
}
if (size > grid_size[0])
throw std::runtime_error("number of subdomains greater than number of gridpoints");
if (n_dims != 1)
sumtmp.reset(new blitz::Array<double, 1>(grid_size[0]));
xtmtmp.reset(new blitz::Array<real_t, 1>(size));
}
/// @brief concurrency-aware summation of array elements
double sum(const arr_t &arr, const idx_t<n_dims> &ijk, const bool sum_khn)
{
// doing a two-step sum to reduce numerical error
// and make parallel results reproducible
for (int c = ijk[0].first(); c <= ijk[0].last(); ++c) // TODO: optimise for i.count() == 1
{
auto slice_idx = ijk;
slice_idx.lbound(0) = c;
slice_idx.ubound(0) = c;
if (sum_khn)
(*sumtmp)(c) = blitz::kahan_sum(arr(slice_idx));
else
(*sumtmp)(c) = blitz::sum(arr(slice_idx));
}
barrier();
double result;
if (sum_khn)
result = blitz::kahan_sum(*sumtmp);
else
result = blitz::sum(*sumtmp);
barrier();
return result;
}
/// @brief concurrency-aware summation of a (element-wise) product of two arrays
double sum(const arr_t &arr1, const arr_t &arr2, const idx_t<n_dims> &ijk, const bool sum_khn)
{
// doing a two-step sum to reduce numerical error
// and make parallel results reproducible
for (int c = ijk[0].first(); c <= ijk[0].last(); ++c)
{
auto slice_idx = ijk;
slice_idx.lbound(0) = c;
slice_idx.ubound(0) = c;
if (sum_khn)
(*sumtmp)(c) = blitz::kahan_sum(arr1(slice_idx) * arr2(slice_idx));
else
(*sumtmp)(c) = blitz::sum(arr1(slice_idx) * arr2(slice_idx));
}
barrier();
double result;
if (sum_khn)
result = blitz::kahan_sum(*sumtmp);
else
result = blitz::sum(*sumtmp);
barrier();
return result;
}
real_t min(const int &rank, const arr_t &arr)
{
(*xtmtmp)(rank) = blitz::min(arr);
barrier();
real_t result = blitz::min(*xtmtmp);
barrier();
return result;
}
real_t max(const int &rank, const arr_t &arr)
{
(*xtmtmp)(rank) = blitz::max(arr);
barrier();
real_t result = blitz::max(*xtmtmp);
barrier();
return result;
}
// this hack is introduced to allow to use neverDeleteData
// and hence to not use BZ_THREADSAFE
private:
boost::ptr_vector<arr_t> tobefreed;
public:
arr_t *never_delete(arr_t *arg)
{
arr_t *ret = new arr_t(arg->dataFirst(), arg->shape(), blitz::neverDeleteData);
ret->reindexSelf(arg->base());
return ret;
}
arr_t *old(arr_t *arg)
{
tobefreed.push_back(arg);
arr_t *ret = never_delete(arg);
return ret;
}
private:
// helper methods to define subdomain ranges
static int min(const int &span, const int &rank, const int &size)
{
return rank * span / size;
}
static int max(const int &span, const int &rank, const int &size)
{
return min(span, rank + 1, size) - 1;
}
public:
static rng_t slab(
const rng_t &span,
const int &rank = 0,
const int &size = 1
) {
return rng_t(
span.first() + min(span.length(), rank, size),
span.first() + max(span.length(), rank, size)
);
}
};
template<typename real_t, int n_dims, int n_tlev>
class sharedmem
{};
template<typename real_t, int n_tlev>
class sharedmem<real_t, 1, n_tlev> : public sharedmem_common<real_t, 1, n_tlev>
{
using parent_t = sharedmem_common<real_t, 1, n_tlev>;
using parent_t::parent_t; // inheriting ctors
public:
// accessor methods
blitz::Array<real_t, 1> advectee(int e = 0)
{
assert(this->n < n_tlev);
// returning just the domain interior, i.e. without halos
// reindexing so that element 0 is at 0
return this->psi[e][ this->n ](
this->grid_size[0]
).reindex(this->origin);
}
blitz::Array<real_t, 1> advector(int d = 0)
{
using namespace arakawa_c;
assert(d == 0);
// returning just the domain interior, i.e. without halos
// reindexed to make it more intuitive when working with index placeholders
// (i.e. border between cell 0 and cell 1 is indexed with 0)
return this->GC[d](
this->grid_size[0]^(-1)^h
).reindex(this->origin);
}
blitz::Array<real_t, 1> g_factor()
{
// a sanity check
if (this->G.get() == nullptr)
throw std::runtime_error("g_factor() called with nug option unset?");
// the same logic as in advectee() - see above
return (*this->G)(
this->grid_size[0]
).reindex(this->origin);
}
blitz::Array<real_t, 1> vab_coefficient()
{
throw std::logic_error("absorber not yet implemented in 1d");
}
blitz::Array<real_t, 1> vab_relaxed_state(int d = 0)
{
throw std::logic_error("absorber not yet implemented in 1d");
}
blitz::Array<real_t, 1> sclr_array(const std::string& name, int n = 0)
{
return this->tmp.at(this->avail_tmp[name].first)[this->avail_tmp[name].second][n](
this->grid_size[0]
).reindex(this->origin);
}
};
template<typename real_t, int n_tlev>
class sharedmem<real_t, 2, n_tlev> : public sharedmem_common<real_t, 2, n_tlev>
{
using parent_t = sharedmem_common<real_t, 2, n_tlev>;
using parent_t::parent_t; // inheriting ctors
public:
blitz::Array<real_t, 2> advectee(int e = 0)
{
assert(this->n < n_tlev);
return this->psi[e][ this->n ](
this->grid_size[0],
this->grid_size[1]
).reindex(this->origin);
}
blitz::Array<real_t, 2> advector(int d = 0)
{
using namespace arakawa_c;
assert(d == 0 || d== 1);
// returning just the domain interior, i.e. without halos
// reindexed to make it more intuitive when working with index placeholders
switch (d)
{
case 0: return this->GC[d](this->grid_size[0]^(-1)^h, this->grid_size[1]).reindex(this->origin);
case 1: return this->GC[d](this->grid_size[0], this->grid_size[1]^(-1)^h).reindex(this->origin);
default: assert(false); throw;
}
}
blitz::Array<real_t, 2> g_factor()
{
// a sanity check
if (this->G.get() == nullptr)
throw std::runtime_error("g_factor() called with nug option unset?");
// the same logic as in advectee() - see above
return (*this->G)(
this->grid_size[0],
this->grid_size[1]
).reindex(this->origin);
}
blitz::Array<real_t, 2> vab_coefficient()
{
// a sanity check
if (this->vab_coeff.get() == nullptr)
throw std::runtime_error("vab_coeff() called with option vip_vab unset?");
// the same logic as in advectee() - see above
return (*this->vab_coeff)(
this->grid_size[0],
this->grid_size[1]
).reindex(this->origin);
}
blitz::Array<real_t, 2> vab_relaxed_state(int d = 0)
{
assert(d == 0 || d== 1);
// a sanity check
if (this->vab_coeff.get() == nullptr)
throw std::runtime_error("vab_relaxed_state() called with option vip_vab unset?");
// the same logic as in advectee() - see above
return this->vab_relax[d](
this->grid_size[0],
this->grid_size[1]
).reindex(this->origin);
}
blitz::Array<real_t, 2> sclr_array(const std::string& name, int n = 0)
{
return this->tmp.at(this->avail_tmp[name].first)[this->avail_tmp[name].second][n](
this->grid_size[0],
this->grid_size[1]
).reindex(this->origin);
}
};
template<typename real_t, int n_tlev>
class sharedmem<real_t, 3, n_tlev> : public sharedmem_common<real_t, 3, n_tlev>
{
using parent_t = sharedmem_common<real_t, 3, n_tlev>;
using parent_t::parent_t; // inheriting ctors
public:
blitz::Array<real_t, 3> advectee(int e = 0)
{
assert(this->n < n_tlev);
return this->psi[e][ this->n ](
this->grid_size[0],
this->grid_size[1],
this->grid_size[2]
).reindex(this->origin);
}
blitz::Array<real_t, 3> advector(int d = 0)
{
using namespace arakawa_c;
assert(d == 0 || d == 1 || d == 2);
// returning just the domain interior, i.e. without halos
// reindexed to make it more intuitive when working with index placeholders
switch (d)
{
case 0: return this->GC[d](this->grid_size[0]^(-1)^h,
this->grid_size[1],
this->grid_size[2]).reindex(this->origin);
case 1: return this->GC[d](this->grid_size[0],
this->grid_size[1]^(-1)^h,
this->grid_size[2]).reindex(this->origin);
case 2: return this->GC[d](this->grid_size[0],
this->grid_size[1],
this->grid_size[2]^(-1)^h).reindex(this->origin);
default: assert(false); throw;
}
}
blitz::Array<real_t, 3> g_factor()
{
// a sanity check
if (this->G.get() == nullptr)
throw std::runtime_error("g_factor() called with nug option unset?");
// the same logic as in advectee() - see above
return (*this->G)(
this->grid_size[0],
this->grid_size[1],
this->grid_size[2]
).reindex(this->origin);
}
blitz::Array<real_t, 3> vab_coefficient()
{
// a sanity check
if (this->vab_coeff.get() == nullptr)
throw std::runtime_error("vab_coeff() called with option vip_vab unset?");
// the same logic as in advectee() - see above
return (*this->vab_coeff)(
this->grid_size[0],
this->grid_size[1],
this->grid_size[2]
).reindex(this->origin);
}
blitz::Array<real_t, 3> vab_relaxed_state(int d = 0)
{
assert(d == 0 || d == 1 || d == 2);
// a sanity check
if (this->vab_coeff.get() == nullptr)
throw std::runtime_error("vab_relaxed_state() called with option vip_vab unset?");
// the same logic as in advectee() - see above
return this->vab_relax[d](
this->grid_size[0],
this->grid_size[1],
this->grid_size[2]
).reindex(this->origin);
}
blitz::Array<real_t, 3> sclr_array(const std::string& name, int n = 0)
{
return this->tmp.at(this->avail_tmp[name].first)[this->avail_tmp[name].second][n](
this->grid_size[0],
this->grid_size[1],
this->grid_size[2]
).reindex(this->origin);
}
};
} // namespace detail
} // namespace concurr
} // namespace libmpdataxx