Brick Library 0.1
Performance-portable stencil datalayout & codegen
Loading...
Searching...
No Matches
brick.h
Go to the documentation of this file.
1
6#ifndef BRICK_H
7#define BRICK_H
8
9#include <stdlib.h>
10#include <type_traits>
11#include <memory>
12#include "vecscatter.h"
13
15#define ALIGN 2048
16
17#if defined(__HIP__)
18#include <hip/hip_runtime.h>
19#endif
20
22#if defined(__CUDACC__) || defined(__HIP__)
23#define FORCUDA __host__ __device__
24#else
25#define FORCUDA
26#endif
27
34template<unsigned base, unsigned exp>
36 static constexpr unsigned value = base * static_power<base, exp - 1>::value;
37};
38
40template<unsigned base>
41struct static_power<base, 0> {
42 static constexpr unsigned value = 1;
43};
55 std::shared_ptr<bElem> dat;
61 long chunks;
63 size_t step;
65 void *mmap_info = nullptr;
66
68 static BrickStorage allocate(long chunks, size_t step) {
70 b.chunks = chunks;
71 b.step = step;
72 b.dat = std::shared_ptr<bElem>((bElem *)aligned_alloc(ALIGN, chunks * step * sizeof(bElem)),
73 [](bElem *p) { free(p); });
74 return b;
75 }
76
78 static BrickStorage mmap_alloc(long chunks, long step);
79
81 static BrickStorage mmap_alloc(long chunks, long step, void *mmap_fd, size_t offset);
82};
83
96template<unsigned dims>
97struct BrickInfo {
103 unsigned nbricks;
104
109 explicit BrickInfo(unsigned nbricks) : nbricks(nbricks) {
110 adj = (adjlist) malloc(nbricks * static_power<3, dims>::value * sizeof(unsigned));
111 }
112
115 return BrickStorage::allocate(nbricks, step);
116 }
117
120 return BrickStorage::mmap_alloc(nbricks, step);
121 }
122
124 BrickStorage mmap_alloc(long step, void *mmap_fd, size_t offset) {
125 return BrickStorage::mmap_alloc(nbricks, step, mmap_fd, offset);
126 }
127};
128
130template<unsigned ... Ds>
131struct Dim {
132};
133
142template<unsigned ... xs>
143struct cal_size;
144
149template<unsigned x>
150struct cal_size<x> {
151 static constexpr unsigned value = x;
152};
153
159template<unsigned x, unsigned ... xs>
160struct cal_size<x, xs...> {
161 static constexpr unsigned value = x * cal_size<xs ...>::value;
162};
173template<unsigned ... offs>
174struct cal_offs;
175
180template<unsigned off>
181struct cal_offs<1, off> {
182 static constexpr unsigned value = off;
183};
184
191template<unsigned dim, unsigned off, unsigned ...offs>
192struct cal_offs<dim, off, offs...> {
193 static constexpr unsigned value = off * static_power<3, dim - 1>::value + cal_offs<dim - 1, offs...>::value;
194};
214template<typename...>
216
218template<typename T,
219 unsigned D,
220 unsigned F>
221struct _BrickAccessor<T, Dim<D>, Dim<F>, bool> {
222 T *par;
223
224 unsigned b;
225 unsigned pos;
226 unsigned nvec;
227 unsigned wvec;
228
229 FORCUDA
230 _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec) :
231 par(par), b(b), pos(pos), nvec(nvec), wvec(wvec) {
232 }
233
234 FORCUDA
235 inline bElem &operator[](unsigned i) {
236 // change pos
237 unsigned dir = i + D;
238 unsigned d = pos * 3 + dir / D;
239 // new vec position
240 unsigned l = dir % D;
241 unsigned w = wvec * F + l % F;
242 unsigned n = nvec * (D / F) + l / F;
243 unsigned offset = n * par->VECLEN + w;
244
245 return par->dat[par->bInfo->adj[b][d] * par->step + offset];
246 }
247};
248
257template<typename T,
258 unsigned D,
259 unsigned F,
260 unsigned ... BDims,
261 unsigned ... Folds>
262struct _BrickAccessor<T, Dim<D, BDims...>, Dim<F, Folds...>, bool> {
263 T *par;
264
265 unsigned b;
266 unsigned pos;
267 unsigned nvec;
268 unsigned wvec;
269
270 FORCUDA
271 _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec) :
272 par(par), b(b), pos(pos), nvec(nvec), wvec(wvec) {
273 }
274
275 FORCUDA
276 inline _BrickAccessor<T, Dim<BDims...>, Dim<Folds...>, bool> operator[](unsigned i) {
277 // change pos
278 unsigned dir = i + D;
279 unsigned d = pos * 3 + dir / D;
280 // new vec position
281 unsigned l = dir % D;
282 unsigned w = wvec * F + l % F;
283 unsigned n = nvec * (D / F) + l / F;
284 return _BrickAccessor<T, Dim<BDims...>, Dim<Folds...>, bool>(par, b, d, n, w);
285 }
286};
287
296template<typename T,
297 unsigned D,
298 unsigned ... BDims,
299 unsigned ... Folds>
300struct _BrickAccessor<T, Dim<D, BDims...>, Dim<Folds...>, void> {
301 T *par;
302
303 unsigned b;
304 unsigned pos;
305 unsigned nvec;
306 unsigned wvec;
307
308 FORCUDA
309 _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec) :
310 par(par), b(b), pos(pos), nvec(nvec), wvec(wvec) {
311 }
312
313 FORCUDA
314 inline _BrickAccessor<T, Dim<BDims...>, Dim<Folds...>,
315 typename std::conditional<sizeof...(BDims) == sizeof...(Folds), bool, void>::type>
316 operator[](unsigned i) {
317 // change pos
318 unsigned dir = i + D;
319 unsigned d = pos * 3 + dir / D;
320 // new vec position
321 unsigned l = dir % D;
322 unsigned w = wvec;
323 unsigned n = nvec * D + l;
324 return _BrickAccessor<T, Dim<BDims...>, Dim<Folds...>,
325 typename std::conditional<sizeof...(BDims) == sizeof...(Folds), bool, void>::type>(par, b, d, n, w);
326 }
327};
339template<typename...>
340struct Brick;
341
353template<
354 unsigned ... BDims,
355 unsigned ... Folds>
356struct Brick<Dim<BDims...>, Dim<Folds...> > {
357 typedef Brick<Dim<BDims...>, Dim<Folds...> > mytype;
358 typedef BrickInfo<sizeof...(BDims)> myBrickInfo;
359
360 static constexpr unsigned VECLEN = cal_size<Folds...>::value;
361 static constexpr unsigned BRICKSIZE = cal_size<BDims...>::value;
362
364 size_t step;
367
369 FORCUDA
370 inline _BrickAccessor<mytype, Dim<BDims...>, Dim<Folds...>,
371 typename std::conditional<sizeof...(BDims) == sizeof...(Folds), bool, void>::type> operator[](unsigned b) {
372 return _BrickAccessor<mytype, Dim<BDims...>, Dim<Folds...>,
373 typename std::conditional<sizeof...(BDims) == sizeof...(Folds), bool, void>::type>(this, b, 0, 0, 0);
374 }
375
377 template<unsigned ... Offsets>
378 FORCUDA
379 inline bElem *neighbor(unsigned b) {
380 unsigned off = cal_offs<sizeof...(BDims), Offsets...>::value;
381 return &dat[bInfo->adj[b][off] * step];
382 }
383
390 Brick(myBrickInfo *bInfo, const BrickStorage &brickStorage, unsigned offset) : bInfo(bInfo) {
391 bStorage = brickStorage;
392 dat = bStorage.dat.get() + offset;
393 step = (unsigned) bStorage.step;
394 }
395};
398#endif //BRICK_H
#define FORCUDA
Overloaded attributes for potentially GPU-usable functions (in place of host device etc....
Definition: brick.h:25
#define ALIGN
BrickStorage allocation alignment.
Definition: brick.h:15
Metadata related to bricks.
Definition: brick.h:97
unsigned(* adjlist)[static_power< 3, dims >::value]
Adjacency list type.
Definition: brick.h:99
unsigned nbricks
Number of bricks in this list.
Definition: brick.h:103
adjlist adj
Adjacency list.
Definition: brick.h:101
BrickInfo(unsigned nbricks)
Creating an empty metadata consisting of the specified number of bricks.
Definition: brick.h:109
BrickStorage mmap_alloc(long step)
Allocate a new brick storage BrickStorage::mmap_alloc(long, long)
Definition: brick.h:119
BrickStorage allocate(long step)
Allocate a new brick storage BrickStorage::allocate()
Definition: brick.h:114
BrickStorage mmap_alloc(long step, void *mmap_fd, size_t offset)
Allocate a new brick storage BrickStorage::mmap_alloc(long, long, void*, size_t)
Definition: brick.h:124
Initializing and holding the storage of bricks.
Definition: brick.h:53
std::shared_ptr< bElem > dat
Pointer holding brick data.
Definition: brick.h:55
long chunks
Number of chunks.
Definition: brick.h:61
static BrickStorage allocate(long chunks, size_t step)
Allocation using *alloc.
Definition: brick.h:68
void * mmap_info
MMAP data structure when using mmap as allocator.
Definition: brick.h:65
static BrickStorage mmap_alloc(long chunks, long step)
mmap allocator using default (new) file
Definition: memfd.cpp:103
size_t step
Size of a chunk in number of elements.
Definition: brick.h:63
Brick data structure.
Definition: brick.h:356
BrickStorage bStorage
Definition: brick.h:366
Brick< Dim< BDims... >, Dim< Folds... > > mytype
Shorthand for this struct's type.
Definition: brick.h:357
FORCUDA bElem * neighbor(unsigned b)
Return the adjacency list of brick b
Definition: brick.h:379
size_t step
Spacing between bricks in unit of bElem (BrickStorage)
Definition: brick.h:364
myBrickInfo * bInfo
Pointer to (possibly shared) metadata.
Definition: brick.h:363
bElem * dat
Offsetted memory (BrickStorage)
Definition: brick.h:365
FORCUDA _BrickAccessor< mytype, Dim< BDims... >, Dim< Folds... >, typename std::conditional< sizeof...(BDims)==sizeof...(Folds), bool, void >::type > operator[](unsigned b)
Indexing operator returns: Accessing brick elements using [].
Definition: brick.h:371
Brick(myBrickInfo *bInfo, const BrickStorage &brickStorage, unsigned offset)
Initialize a brick data structure.
Definition: brick.h:390
Generic base template, see Brick< Dim< BDims... >, Dim< Folds... > >
Definition: brick.h:340
Empty template to specify an n-D list.
Definition: brick.h:131
unsigned nvec
Which vector.
Definition: brick.h:267
unsigned wvec
Position within a vector.
Definition: brick.h:268
T * par
parent Brick data structure reference
Definition: brick.h:263
unsigned pos
Accumulative position within adjacency list.
Definition: brick.h:266
FORCUDA _BrickAccessor< T, Dim< BDims... >, Dim< Folds... >, bool > operator[](unsigned i)
Definition: brick.h:276
FORCUDA _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec)
Definition: brick.h:271
unsigned b
Reference (center) brick.
Definition: brick.h:265
FORCUDA _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec)
Definition: brick.h:309
unsigned nvec
Which vector.
Definition: brick.h:305
unsigned b
Reference (center) brick.
Definition: brick.h:303
unsigned pos
Accumulative position within adjacency list.
Definition: brick.h:304
T * par
parent Brick data structure reference
Definition: brick.h:301
FORCUDA _BrickAccessor< T, Dim< BDims... >, Dim< Folds... >, typename std::conditional< sizeof...(BDims)==sizeof...(Folds), bool, void >::type > operator[](unsigned i)
Definition: brick.h:316
unsigned wvec
Position within a vector.
Definition: brick.h:306
T * par
parent Brick data structure reference
Definition: brick.h:222
FORCUDA bElem & operator[](unsigned i)
Definition: brick.h:235
unsigned wvec
Position within a vector.
Definition: brick.h:227
FORCUDA _BrickAccessor(T *par, unsigned b, unsigned pos, unsigned nvec, unsigned wvec)
Definition: brick.h:230
unsigned b
Reference (center) brick.
Definition: brick.h:224
unsigned nvec
Which vector.
Definition: brick.h:226
unsigned pos
Accumulative position within adjacency list.
Definition: brick.h:225
Generic base template for Accessing brick elements using [].
Definition: brick.h:215
Generic base template for Calculating the offset within the adjacency list.
Definition: brick.h:174
Generic base template for Calculate the product of n numbers in a template.
Definition: brick.h:143
Compute Statically compute exponentials.
Definition: brick.h:35
static constexpr unsigned value
Definition: brick.h:36
Interface to code generator.
#define bElem
Basic datatype for all brick elements.
Definition: vecscatter.h:13