Mapping data to a texure How can I do that?

Hi. i’m trying to map an array to a texture.
The size of my array is [20][20], and i’ll be accesing it from all my blocks and kernels, so i think that the best way (from what i’ve read) is to use a texture. I read the texture example that comes in the sdk, but it loads an image, how can I load an array of integers/floats into a texture?

Thanks a lot.


The simpleTexture example in the SDK does load an image, but the image is simply a two dimensional array of floats.

To load a 20 x 20 array of floats to a texture, you’d do something like this:

// declare 2D texture reference

texture<float, 2, cudaReadModeElementType> tex;

// allocate 20 x 20 float array

cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);

cudaArray* cu_array;

cudaMallocArray( &cu_array, &channelDesc, 20, 20);

cudaMemcpyToArray( cu_array, 0, 0, h_data, size, cudaMemcpyHostToDevice);

// bind array to the texture

cudaBindTexture( tex, cu_array, channelDesc)

Thanks a lot!
In that code, i would put my data in an array called “h_data” ?
Should I define h_data as 1D (h_data[400]) ou 2D(h_data[20][20]) array?
and in my case “size” would be 2020sizeof(float)??

Again, thanks!