Created
January 3, 2020 07:24
-
-
Save mickeyouyou/c943b351f9375afb34f97079e8ea7604 to your computer and use it in GitHub Desktop.
unfied memroy cuda operation codes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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