Created
October 11, 2014 12:49
-
-
Save alphaville/be2c6865eab86dd491a6 to your computer and use it in GitHub Desktop.
Source code of the first example posted at http://codeofhonour.blogspot.it/2014/10/memories-from-cuda-constant-memory-ii.html
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> | |
#include <cuda_runtime.h> | |
#include "helper_cuda.h" | |
int host_x[4] = {1, 2, 3, 4}; | |
__constant__ int dev_x[4]; | |
__global__ void kernel(int *d_var) { d_var[threadIdx.x] += 10; } | |
int main(void) | |
{ | |
int data_size = 4 * sizeof(int); | |
int *address; | |
checkCudaErrors(cudaMallocHost((void**) &dev_x, data_size, cudaHostAllocWriteCombined)); | |
checkCudaErrors(cudaMemcpyToSymbol(dev_x, host_x, data_size,0, cudaMemcpyHostToDevice)); | |
checkCudaErrors(cudaGetSymbolAddress((void**)&address, dev_x)); | |
kernel<<<1,4>>>(address); | |
checkCudaErrors(cudaDeviceSynchronize()); | |
getLastCudaError("wtf!"); | |
checkCudaErrors(cudaMemcpyFromSymbol(host_x, dev_x, data_size, 0, cudaMemcpyDeviceToHost)); | |
for (int i=0; i< 4; i++){ | |
printf("%d\n", host_x[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment