I want to create a pointer in the host memory, add some data to the pointer and load it to the device memory to do calculations afterwards.
An example from the programming guide: __device__ float* devPointer; float* ptr; cudaMalloc(&ptr, 256 * sizeof(float)); cudaMemcpyToSymbol(devPointer, &ptr, sizeof(ptr));
My code:
`#include <stdio.h> include “cuda_runtime.h” include “device_launch_parameters.h”
device float* devPointer;
global void test()
{
printf(“devPointer[%d] = %g\n”,threadIdx.x, devPointer[threadIdx.x]);
}
int main(void)
{
float* ptr;
cudaMalloc(&ptr, 8 * sizeof(float));
//add data to the pointer
for(int s = 0; s < 8 ; s++)
{
ptr[s] = s;
}
cudaMemcpyToSymbol(devPointer, &ptr, sizeof(ptr));
test<<<1,8>>>();
return 0;
}
`
I tried the above suggestion from the programming guide but I cannot get it to function properly. Help is highly appreciated.
Also, I’m pretty sure need to work on your C skills. A pointer is not an array. Mind this:
float* x;
sizeof(x); // Is ALWAYS going to be 4 on 32-bit and 8 on 64-bit systems
float arr[8];
sizeof(arr); //This is going to be 32 (8 * sizeof(float) = 8 * 4 = 32)
The term “invoke” is used in the cuda-c-programming-guide so that’s why I used it. I wrote: “If I copy to the device memory, with the precursor__device__ , it needs to be of a constant value.” I meant a constant size. But you explained that´s not the case.