cudaMemcpy: Invalid Device Pointer (17)

I’m clueless, I don’t know why I am getting this error. Basically this is what I’m doing:

[codebox]

if(cudaMalloc((void **)&weights_d,2nEsizeof(struct smax)) != cudaSuccess) die(“Error allocating GPU memory”);

if(cudaMalloc((void **)&edges_d[0],nE*sizeof(int)) != cudaSuccess) die(“Error allocating GPU memory”);

if(cudaMalloc((void **)&edges_d[1],nE*sizeof(int)) != cudaSuccess) die(“Error allocating GPU memory”);

(…)

// somekernelthat search for the max of weights.value

// Check for kernel errors

cudaError_t error = cudaGetLastError();

// no error

(…)

// Update values

if(cudaMemcpy(&edges_d[0][m],&mOne,sizeof(int),cudaMemcpyHostToDevice)!= cudaSuccess) die(“edges0: Error Copying From Device”);

if(cudaMemcpy(&edges_d[1][m],&mOne,sizeof(int),cudaMemcpyHostToDevice)!= cudaSuccess) die(“edges1: Error Copying From Device”);

if(cudaMemcpy(&weights_d[m].value,&mInf,sizeof(float),cudaMemcpyHostToDevice)!= cudaSuccess){

		sprintf(szError,"weights_d: Error copying to device. Error: %d", cudaGetLastError());	

		die(szError);

	}

[/codebox]

Output:

weights_d: Error copying to device. Error: 17

My pointers are preserved trough the code:

weights_d: 0x2a20000

edges_d0: 0x34f0000

edges_d1: 0x37b0000

And &weights_d[m].value is: 0x2c2abb0 wich is 0x2a20000 + m*sizeof(struct smax). Of course m < nE so I should be within the video memory range.

Any suggestion or comments will be appreciated.

Regards,

You can’t do either of:

&edges_d[0][m]

&edges_d[1][m]

edges[0] and [1] are device pointers, but the rest of the content of edges clearly isn’t. The GPU and CPU have separate memory spaces, and you can’t use pointers in each space interchangeably, which is what you are trying to to here.

But if edges_d[0] (0x34f0000 ) is a device pointer shouldn’t &edges_d[0][m] (0x34f0000 + m*sizeof(pointedstruff)) still be within the same memory space?..doesn’t work like this with device pointers?

edges_d[0] is supoused to be pointing to an array on the device, how can I properly access one element then…??

The problem here is that &edges_d[0][m] isn’t the same thing as 0x34f0000 + m*sizeof(pointedstruff), even if edges_d[0]=0x34f0000 . You are trying to mix host side and device side pointer arithmetic and you can’t do it.

Why?.. there is not a derreference in the first one…

I tried edges_d[0] + m*sizeof(int) and didn’t work either… both calculations got the same result 0x35f55d8, that is 0x34f0000 + 4 * d’267638.

Do this mean that I can’t do the arithmetic like this?..how will be the proper way to update that element…

Can you recognize that in the original code you posted, edges_d[0] holds the value of device pointer returned by cudaMalloc, and &edges_d[0] is a host pointer?

Yes, I understand your answer. But in my understanding &edges_d[0] doesn’t mean &edges_d[0][m]. While &edges_d[0] is the address where this number (0x34f0000) is stored, &edges[0][m] should be the address of the m element of the array at 0x34f0000. I’ve tested this by coding the edges_d[0] + m*sizeof(int) notation and I get the same pointer. Neither of them worked. :(

Sorry to have wasted your time, the pointer arithmetic was fine, it was a problem in other part of the code where i was actually freeing!! the pointer:S

…All work and no play makes Jack a dull boy…