Skip to content

Instantly share code, notes, and snippets.

@alphaville
Created October 11, 2014 13:17
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 alphaville/9c6021692a89fdeb39e4 to your computer and use it in GitHub Desktop.
Save alphaville/9c6021692a89fdeb39e4 to your computer and use it in GitHub Desktop.
Source code of the third example that can be found at http://codeofhonour.blogspot.it/2014/10/memories-from-cuda-constant-memory-ii.html
#include <stdio.h>
#include <cuda_runtime.h>
#include "helper_cuda.h"
int host_x[4] = {1, 2, 3, 4};
__device__ int dev_x[4];
__global__ void kernel(int *d_var) { d_var[threadIdx.x] += 10; }
int main(void)
{
/* Declarations */
int data_size = 4 * sizeof(int);
int *address;
/* Allocate memory for `dev_x` using `cudaMallocHost` */
checkCudaErrors(cudaMallocHost((void**) &dev_x, data_size, cudaHostAllocWriteCombined));
/* Get the address of the `__device__` variable `dev_x` */
checkCudaErrors(cudaGetSymbolAddress((void**)&address, dev_x));
/* Transfer data to `address` on the device */
checkCudaErrors(cudaMemcpy(address, host_x, data_size,cudaMemcpyHostToDevice));
/* Launch the kernel for `address` */
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;
}
@WANG-lp
Copy link

WANG-lp commented Sep 14, 2016

why we need line 18? I think if we don't allocate memory on host for dev_x, it still ok.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment