Writing to cuda array in kernel?

Hi All,

I am passing in a 2D array as a cuda array into my kernel. I know that you can read cuda arrays only texture fetches, and I am doing this through tex2d(). But if I need to modify this data, how can I write to the 2D array? Just as a simple example, suppose I just want to square each element in the array. How can I write to cuda array?

I can’t find anything on the writing in the programming guide.

Thanks for your help!

Square your data into a normal global memory region and then call cudaMemcpyToArray. It is the only way.

Thanks MisterAnderson, it makes sense.

How can I declare an array in global memory region? I’ve tried doing it outside my functions where I declare all my global variables,

cudaMallocPitch((void**)&devPtr, &pitch, width * sizeof(float), height);

but I get the error

1>cudaArray.cu(99): error: this declaration has no storage class or type specifier

among others.

I know you can use device to declare a variable that resides on the device global memory,

but the following (declared in my kernel file outside of the global function )also doesn’t make much sense:

device cudaMallocPitch((void**)&devPtr, &pitch, width * sizeof(float), height);

Could you give me some pointers?

Thanks very much!

It is not obvious what you are doing to get the error.

You should just need a simple:

float *devPtr;

cudaMallocPitch((void**)&devPtr, &pitch, width * sizeof(float), height);

in your initialization function. Then pass devPtr to the kernel when you call it.

I wouldn’t. device variables are more trouble then they are worth. And you cannot dynamically choose the size, which makes them worthless in 99.999% of all cases.