forked from torstem/demo-cuda-pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_library.cu
33 lines (28 loc) · 866 Bytes
/
gpu_library.cu
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
#include <sstream>
#include <iostream>
#include <cuda_runtime.h>
__global__ void kernel
(double *vec, double scalar, int num_elements)
{
unsigned int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx < num_elements) {
vec[idx] = vec[idx] * scalar;
}
}
void run_kernel
(double *vec, double scalar, int num_elements)
{
dim3 dimBlock(256, 1, 1);
dim3 dimGrid(ceil((double)num_elements / dimBlock.x));
kernel<<<dimGrid, dimBlock>>>
(vec, scalar, num_elements);
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
std::stringstream strstr;
strstr << "run_kernel launch failed" << std::endl;
strstr << "dimBlock: " << dimBlock.x << ", " << dimBlock.y << std::endl;
strstr << "dimGrid: " << dimGrid.x << ", " << dimGrid.y << std::endl;
strstr << cudaGetErrorString(error);
throw strstr.str();
}
}