Skip to content

Instantly share code, notes, and snippets.

@HanatoK
Last active June 11, 2020 23:28
Show Gist options
  • Save HanatoK/f3ad0b248d70142993424d3c4bb307af to your computer and use it in GitHub Desktop.
Save HanatoK/f3ad0b248d70142993424d3c4bb307af to your computer and use it in GitHub Desktop.
cuda copy to symbol, device to device
#include <cstdio>
#define N 3
__device__ int d_array[N] = {1, 2, 3};
__device__ __constant__ int d_array_constant[N];
static const char *_cudaGetErrorEnum(cudaError_t error) {
return cudaGetErrorName(error);
}
template <typename T>
void check(T result, char const *const func, const char *const file,
int const line) {
if (result) {
fprintf(stderr, "CUDA error at %s:%d code=%d(%s) \"%s\" \n", file, line,
static_cast<unsigned int>(result), _cudaGetErrorEnum(result), func);
exit(EXIT_FAILURE);
}
}
#define checkCudaErrors(val) check((val), #val, __FILE__, __LINE__)
int main() {
int *d_array_tmp;
cudaMalloc(&d_array_tmp, N*sizeof(int));
checkCudaErrors(cudaMemcpyFromSymbol(d_array_tmp, d_array, N*sizeof(int), 0, cudaMemcpyDeviceToDevice));
checkCudaErrors(cudaMemcpyToSymbol(d_array_constant, d_array_tmp, N*sizeof(int), 0, cudaMemcpyDeviceToDevice));
cudaFree(d_array_tmp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment