Is device pointer arithmetic supported?

Does anybody know whether device pointer arithmetic is supported? I have some weird errors, I am suspecting it is not supported.

Yes, it works. You can do device pointer arithmetic on the device. You can do it on the host too, as long as there is no pointer dereferencing involved.

1 Like

Well… Can you see an error from the following code I have been working on? I get 'Cuda error: invalid device pointer" in emulation. The constants are valid positive number, so I can’t see anything would have gone wrong other than pointer arithmetic.

[codebox]

int *T_all_Device;

if (cudaMalloc((void **) &T_all_Device, N_MARKET_SCENARIO * N_ALL * sizeof(int)) != cudaSuccess)

{

printf("Error in memory allocation");

}

for (int i = 0; i < N_MARKET_SCENARIO; i++)

{

cudaMemcpy(T_all_Device + (i * N_ALL * sizeof(int)), &T_all[i][0], N_ALL * sizeof(int), cudaMemcpyHostToDevice);

}

cudaError_t err2 = cudaGetLastError();

if (err2 != cudaSuccess)

{

printf("Cuda error: %s\n", cudaGetErrorString(err2));

}

}[/codebox]

If I change

cudaMemcpy(T_all_Device + (i * N_ALL * sizeof(int)), &T_all[i][0], N_ALL * sizeof(int), cudaMemcpyHostToDevice);

to

cudaMemcpy(T_all_Device + (0 * N_ALL * sizeof(int)), &T_all[i][0], N_ALL * sizeof(int), cudaMemcpyHostToDevice);

I get no error… So the error should has something to do with T_all_Device device pointer.

What is the sizeof(int) doing inside the pointer arithmetic expression? My guess is that it shouldn’t be there. Pointer arithmetic works on word offsets, not byte offsets.

I think you’re right. Problem solved. Thank you ^^