Skip to content

Instantly share code, notes, and snippets.

@mickeyouyou
Created January 3, 2020 07:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mickeyouyou/c943b351f9375afb34f97079e8ea7604 to your computer and use it in GitHub Desktop.
Save mickeyouyou/c943b351f9375afb34f97079e8ea7604 to your computer and use it in GitHub Desktop.
unfied memroy cuda operation codes
#include <stdio.h>
inline cudaError_t checkCuda(cudaError_t result) {
if (result != cudaSuccess) {
printf("CUDA Runtime Error: %s:%d, ", __FILE__, __LINE__);
printf("Code: %d, message: %s\n", result, cudaGetErrorString(result));
}
return result;
}
__global__ void set_kernel(int* g, int* in, const unsigned int size) {
int index = threadIdx.x + blockIdx.x * blockDim.x;
// printf("set_kernel index : %d \n", index);
if (index < size) {
// printf("set_kernel index : %d , size: %d \n", index, size);
g[index] = index;
printf("set_kernel index : %d, in element: %d \n", index, in[index]);
}
}
int main() {
unsigned int size = 10;
size_t sizes = size * sizeof(int);
int* gg;
int* origin;
cudaMallocManaged(&gg, sizes);
cudaMallocManaged(&origin, sizes);
for (int i = 0; i < size; i++) {
origin[i] = i;
}
set_kernel<<<1, 10>>>(gg, origin, size);
checkCuda(cudaDeviceSynchronize());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment