-
Notifications
You must be signed in to change notification settings - Fork 2
/
cuda_tools.h
42 lines (36 loc) · 1012 Bytes
/
cuda_tools.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
// printf
#include "stdio.h"
// cuda
#include <cuda.h>
#include <cuda_runtime.h>
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
// An assert to get information regarding any error throw during execution on
// the GPU
inline void gpuAssert(
cudaError_t code,
const char *file,
int line,
bool abort=true
) {
if (code != cudaSuccess) {
fprintf(
stderr,
"GPUassert: %s %s %d\n",
cudaGetErrorString(code),
file,
line
);
if (abort) {
exit(code);
}
}
}
// Copy the memory content from the GPU (device) to CPU (host)
void sendToDevice(double*& ptr, const double* data, const int n) {
gpuErrchk(cudaMalloc((void**) &ptr, n*sizeof(double)));
gpuErrchk(cudaMemcpy(ptr, data, n*sizeof(double), cudaMemcpyHostToDevice));
}
void sendToDeviceFloat(float*& ptr, const float* data, const int n) {
gpuErrchk(cudaMalloc((void**) &ptr, n*sizeof(float)));
gpuErrchk(cudaMemcpy(ptr, data, n*sizeof(float), cudaMemcpyHostToDevice));
}