-
Notifications
You must be signed in to change notification settings - Fork 109
/
prk_cuda.h
208 lines (178 loc) · 7.2 KB
/
prk_cuda.h
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
#ifndef PRK_CUDA_HPP
#define PRK_CUDA_HPP
//#include <cstdio>
//#include <cstdlib>
#include <iostream>
#include <vector>
#include <array>
#include <cuda.h>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>
#include <cuda_device_runtime_api.h>
#include <cublas_v2.h>
typedef double prk_float;
namespace prk
{
namespace CUDA
{
void check(cudaError_t rc)
{
if (rc==cudaSuccess) {
return;
} else {
std::cerr << "PRK CUDA error: " << cudaGetErrorString(rc) << std::endl;
std::abort();
}
}
void check(cublasStatus_t rc)
{
if (rc==CUBLAS_STATUS_SUCCESS) {
return;
} else {
std::cerr << "PRK CUBLAS error: " << rc << std::endl;
std::abort();
}
}
class info {
private:
int nDevices;
std::vector<cudaDeviceProp> vDevices;
public:
int maxThreadsPerBlock;
std::array<unsigned,3> maxThreadsDim;
std::array<unsigned,3> maxGridSize;
info() {
prk::CUDA::check( cudaGetDeviceCount(&nDevices) );
vDevices.resize(nDevices);
for (int i=0; i<nDevices; ++i) {
cudaGetDeviceProperties(&(vDevices[i]), i);
if (i==0) {
maxThreadsPerBlock = vDevices[i].maxThreadsPerBlock;
for (int j=0; j<3; ++j) {
maxThreadsDim[j] = vDevices[i].maxThreadsDim[j];
maxGridSize[j] = vDevices[i].maxGridSize[j];
}
}
}
}
// do not use cached value as a hedge against weird stuff happening
int num_gpus() {
int g;
prk::CUDA::check( cudaGetDeviceCount(&g) );
return g;
}
int get_gpu() {
int g;
prk::CUDA::check( cudaGetDevice(&g) );
return g;
}
void set_gpu(int g) {
prk::CUDA::check( cudaSetDevice(g) );
}
void print() {
for (int i=0; i<nDevices; ++i) {
std::cout << "device name: " << vDevices[i].name << "\n";
std::cout << "total global memory: " << vDevices[i].totalGlobalMem << "\n";
std::cout << "max threads per block: " << vDevices[i].maxThreadsPerBlock << "\n";
std::cout << "max threads dim: " << vDevices[i].maxThreadsDim[0] << ","
<< vDevices[i].maxThreadsDim[1] << ","
<< vDevices[i].maxThreadsDim[2] << "\n";
std::cout << "max grid size: " << vDevices[i].maxGridSize[0] << ","
<< vDevices[i].maxGridSize[1] << ","
<< vDevices[i].maxGridSize[2] << "\n";
std::cout << "memory clock rate (KHz): " << vDevices[i].memoryClockRate << "\n";
std::cout << "memory bus width (bits): " << vDevices[i].memoryBusWidth << "\n";
}
}
bool checkDims(dim3 dimBlock, dim3 dimGrid) {
if (dimBlock.x > maxThreadsDim[0]) {
std::cout << "dimBlock.x too large" << std::endl;
return false;
}
if (dimBlock.y > maxThreadsDim[1]) {
std::cout << "dimBlock.y too large" << std::endl;
return false;
}
if (dimBlock.z > maxThreadsDim[2]) {
std::cout << "dimBlock.z too large" << std::endl;
return false;
}
if (dimGrid.x > maxGridSize[0]) {
std::cout << "dimGrid.x too large" << std::endl;
return false;
}
if (dimGrid.y > maxGridSize[1]) {
std::cout << "dimGrid.y too large" << std::endl;
return false;
}
if (dimGrid.z > maxGridSize[2]) {
std::cout << "dimGrid.z too large" << std::endl;
return false;
}
return true;
}
};
template <typename T>
T * malloc_device(size_t n) {
T * ptr;
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMalloc((void**)&ptr, bytes) );
return ptr;
}
template <typename T>
T * malloc_host(size_t n) {
T * ptr;
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMallocHost((void**)&ptr, bytes) );
return ptr;
}
template <typename T>
T * malloc_managed(size_t n) {
T * ptr;
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMallocManaged((void**)&ptr, bytes) );
return ptr;
}
template <typename T>
void free(T * ptr) {
prk::CUDA::check( cudaFree((void*)ptr) );
}
template <typename T>
void free_host(T * ptr) {
prk::CUDA::check( cudaFreeHost((void*)ptr) );
}
template <typename T>
void copyD2H(T * output, T * const input, size_t n) {
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMemcpy(output, input, bytes, cudaMemcpyDeviceToHost) );
}
template <typename T>
void copyH2D(T * output, T * const input, size_t n) {
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMemcpy(output, input, bytes, cudaMemcpyHostToDevice) );
}
template <typename T>
void copyD2Hasync(T * output, T * const input, size_t n) {
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMemcpyAsync(output, input, bytes, cudaMemcpyDeviceToHost) );
}
template <typename T>
void copyH2Dasync(T * output, T * const input, size_t n) {
size_t bytes = n * sizeof(T);
prk::CUDA::check( cudaMemcpyAsync(output, input, bytes, cudaMemcpyHostToDevice) );
}
template <typename T>
void prefetch(T * ptr, size_t n, int device = 0) {
size_t bytes = n * sizeof(T);
//std::cout << "device=" << device << "\n";
prk::CUDA::check( cudaMemPrefetchAsync(ptr, bytes, device) );
}
void sync(void) {
prk::CUDA::check( cudaDeviceSynchronize() );
}
void set_device(int i) {
prk::CUDA::check( cudaSetDevice(i) );
}
} // CUDA namespace
} // prk namespace
#endif // PRK_CUDA_HPP