images and cuda array types. which cuda array type is appropriate for representing images.


I read some of the posts here but none seem to clearly answer my (simple) questions.

  1. To represent an image with 3 channels I can use either a 1d array of float3 allocated with cudaMalloc, or an 2d array of float3 allocated with cudaMalloc3D with a size of Width x Height x 1 or a 3d float array allocated with cudaMalloc3D of size Width x Height x 3. With the fact in mind that I want to access rows and columns: Is there one way which I should prefer?

  2. The whole topic about cudaMalloc3D, cudaMalloc3DArray, … and copying memory is quite confusing to me, to put it mildly. Is there anywhere some good post, tutorial or howto which I have maybe missed about how to allocate and fill cuda arrays. The programming guide e.g. says nothing about cudaMalloc3D. My input is an 1d float array like {r1,g1,b1,r2,g2,b2,…} where r are the red values and so on. My hope is, that I don’t have to iterate over the whole array an put the values in float3 variable. Maybe there is a shorter way.

I hope this wasn’t too confusing.


Don’t confuse yourself with cudaMalloc*3D if you are working with images. Use cudaMallocPitch, see section of the programming guide (and not that float3’s cannot be read coalesced easily so you probably want to pad out to float4).

For a 2D image in array memory for reading from a texture, use cudaMallocArray(). And here you must use float4 as textures cannot read the float3 data type. Section includes examples of allocating and copying this memory.

I followed your advice and used the pitched version.

Thank you for your advice.