Brick Library 0.1
Performance-portable stencil datalayout & codegen
Loading...
Searching...
No Matches
brick-gpu.h
Go to the documentation of this file.
1
6#ifndef BRICK_GPU_FUNCS_H
7#define BRICK_GPU_FUNCS_H
8
9#if !(defined(BRICK_BRICK_CUDA_H) || defined(BRICK_BRICK_HIP_H))
10#error "Include either brick-cuda.h or brick-hip.h for generic GPU defines"
11#endif
12
13#include <cassert>
14#include <brick.h>
15
16#ifndef NDEBUG
17
18#define gpuCheck(x) x
19
20#else // defined(NDEBUG)
21
22#include <cstdio>
23#define gpuCheck(x) _gpuCheck(x, #x, __FILE__, __LINE__)
24
25#endif // NDEBUG
26
27template<typename T>
28void _gpuCheck(T e, const char *func, const char *call, const int line) {
29 if (e != gpuSuccess) {
30 printf("\"%s\" at %d in %s\n\treturned %d\n-> %s\n", func, line, call, (int) e, gpuGetErrorString(e));
31 exit(EXIT_FAILURE);
32 }
33}
34
42template<unsigned dims>
44 assert(kind == gpuMemcpyHostToDevice || kind == gpuMemcpyDeviceToHost);
45
46 BrickInfo<dims> ret = bInfo;
47 unsigned size = bInfo.nbricks * static_power<3, dims>::value * sizeof(unsigned);
48 if (kind == gpuMemcpyHostToDevice) {
49 gpuCheck(gpuMalloc(&ret.adj, size));
50 } else {
51 ret.adj = (unsigned (*)[(static_power<3, dims>::value)]) malloc(size);
52 }
53 gpuCheck(gpuMemcpy(ret.adj, bInfo.adj, size, kind));
54 return ret;
55}
56
64template<unsigned dims>
66 BrickInfo<dims> *ret;
67 BrickInfo<dims> temp = movBrickInfo(bInfo, kind);
68 unsigned size = sizeof(BrickInfo<dims>);
69 if (kind == gpuMemcpyHostToDevice) {
70 gpuMalloc(&ret, size);
71 } else {
72 ret = (BrickInfo<dims> *) malloc(size);
73 }
74 gpuMemcpy(ret, &temp, size, kind);
75 return ret;
76}
77
85 assert(kind == gpuMemcpyHostToDevice || kind == gpuMemcpyDeviceToHost);
86
87 bool isToDevice = (kind == gpuMemcpyHostToDevice);
88 BrickStorage ret = bStorage;
89 unsigned size = bStorage.step * bStorage.chunks * sizeof(bElem);
90 bElem *datptr;
91 if (isToDevice) {
92 gpuCheck(gpuMalloc(&datptr, size));
93 } else {
94 datptr = (bElem *) malloc(size);
95 }
96 gpuCheck(gpuMemcpy(datptr, bStorage.dat.get(), size, kind));
97 if (isToDevice) {
98 ret.dat = std::shared_ptr<bElem>(datptr, [](bElem *p) { gpuFree(p); });
99 } else {
100 ret.dat = std::shared_ptr<bElem>(datptr, [](bElem *p) { free(p); });
101 }
102 return ret;
103}
104
105#include "dev_shl.h"
106
107#endif // BRICK_GPU_FUNCS_H
#define gpuMalloc(p, s)
Definition: brick-cuda.h:12
#define gpuMemcpy(d, p, s, k)
Definition: brick-cuda.h:13
#define gpuMemcpyHostToDevice
Definition: brick-cuda.h:15
#define gpuMemcpyDeviceToHost
Definition: brick-cuda.h:16
#define gpuSuccess
Definition: brick-cuda.h:19
#define gpuGetErrorString(e)
Definition: brick-cuda.h:18
#define gpuMemcpyKind
Definition: brick-cuda.h:14
#define gpuFree(p)
Definition: brick-cuda.h:17
#define gpuCheck(x)
Definition: brick-gpu.h:18
void _gpuCheck(T e, const char *func, const char *call, const int line)
Definition: brick-gpu.h:28
BrickStorage movBrickStorage(BrickStorage &bStorage, gpuMemcpyKind kind)
Moving BrickStorage to or from GPU (allocate new)
Definition: brick-gpu.h:84
BrickInfo< dims > * movBrickInfoDeep(BrickInfo< dims > &bInfo, gpuMemcpyKind kind)
Moving the full BrickInfo to or from GPU, including the adjacency list and other elements.
Definition: brick-gpu.h:65
BrickInfo< dims > movBrickInfo(BrickInfo< dims > &bInfo, gpuMemcpyKind kind)
Moving BrickInfo to or from GPU.
Definition: brick-gpu.h:43
Main header for bricks.
Implementation for various shuffle implementations.
Metadata related to bricks.
Definition: brick.h:97
unsigned nbricks
Number of bricks in this list.
Definition: brick.h:103
adjlist adj
Adjacency list.
Definition: brick.h:101
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
size_t step
Size of a chunk in number of elements.
Definition: brick.h:63
Compute Statically compute exponentials.
Definition: brick.h:35
#define bElem
Basic datatype for all brick elements.
Definition: vecscatter.h:13