gpu memory address in c

Hi I have a relatively simple question that maybe someone can answer for me.

how can I store a gpu memory address in my .cpp main(). is there any problem with storing gpu memory addresses in the c part of the code?

basically I want to do something like the following. so I want to allocate the memory for some data at startup but keep the same data through the loop where the kernel is executed each time. this is just a shortened example not the actual code.

sorry if I sound like a newbi… I am in many ways ;-)




float3* d_m1,d_m2;

float3* h_m1,h_m2




t1 = getT1();

t2 = getT2();





extern “C” void runTest(float3* d_m1,float3* d_m2,float3 t1,float3 t2)


float3 d_t1;

float3 d_t2;

    float3* d_results;

CUDA_SAFE_CALL( cudaMalloc( (void**) &d_t1, sizeof(float3) ) );

CUDA_SAFE_CALL( cudaMalloc( (void**) &d_t2, sizeof(float3) ) );

CUDA_SAFE_CALL( cudaMemcpy( d_t1, t1,	sizeof(float3),	cudaMemcpyHostToDevice) );

CUDA_SAFE_CALL( cudaMemcpy( d_t2, t2,	sizeof(float3),	cudaMemcpyHostToDevice) );

CUDA_SAFE_CALL( cudaMalloc( (void**) &d_results, sizeof(float3) *s1*s2) );

int gx = (int)s1/12;

int gy = (int)s2/12;



dim3  grid( gx, gy, 1);

dim3  threads( 12, 12, 1); 

kernel<<< grid, threads>>>(d_m1, d_m2, d_t1, d_t2, d_results);


extern “C” void copyM(float3* d_m1,float3* d_m2, float3* h_m1, float3* h_m2,int s1, int s2)


CUDA_SAFE_CALL(cudaMalloc((void**) &d_m1, sizeof(float3)*s1));

CUDA_SAFE_CALL(cudaMalloc((void**) &d_m2, sizeof(float3)*s2));

CUDA_SAFE_CALL(cudaMemcpy(d_m1,h_m1, sizeof(float3)*s1 ,cudaMemcpyHostToDevice));

CUDA_SAFE_CALL(cudaMemcpy(d_m2,h_m2, sizeof(float3)*s2 ,cudaMemcpyHostToDevice));


Umm, how would you not? A pointer is a pointer, whether it points to device memory or host memory. You can copy the pointer wherever you wish with the = operator…

Also, there isn’t anything magical about the CUDA API. Just include “cuda_runtime.h” and you can call cudaMalloc, cudaMemcpy, … all from the C part of the code. Only a few things must be compiled by nvcc (these are documented in the programming guide).

here I was thinking that gpu programming was structured around magic… haha thanks for the reply.

Well, a lot of people come to the forums with the assumption that every cuda* call is special and must be compiled by nvcc. That is my standard response when I think someone is making that assumption.

Every CUDA call is sacred, every CUDA call is great; when a CUDA call is wasted, the driver gets quite irate…

That is one to put in my .signature at work ;)