Take the 2-minute tour ×

I am new to OpenCL programming and my input is a 3D array. I am calculating the index as:

    int gidX = get_global_id(0)?1:get_global_id(0);
    int gidY = get_global_id(1)?1:get_global_id(1);
    int gidZ = get_global_id(2)?1:get_global_id(2);

    int index = gidX + (gidY*SizeX) + (gidZ*SizeY*SizeZ);

Is this the right way to do it? How do I use the local thread ids with 3d arrays? I had used it with 2d arrays as:

 int tid = get_local_id(0);
 int gid = get_global_id(0);
 int index = tid + gid*width;

And, is there a way I could use image3d_t type for my 3D volume?


share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

It depends how you have your 3D array linearized to memory.. but Rick's answer coded as an inline function would work fine. The other optimization you may want are prefetching to local memory when possible.

/* Visualize as a cube. You are looking at the front in x,y coordinates. Z is depth. You have stored it by starting at (x=0, y=0) and taking the depth z lists of elements one by one and placing them in a contiguous array.*/

//Inline this
int matrix3D_lookup(int x, int y, int z, int sizeZ, int sizeX){
       return          z+ sizeZ*x +(sizeZ*sizeX*y);
share|improve this answer
add comment

What you seem to need is some basic information about the functionality and working principles of OpenCL. Please have a look to the following links:

  1. http://developer.download.nvidia.com/compute/cuda/3_2_prod/toolkit/docs/OpenCL_Programming_Guide.pdf
  2. http://www.nvidia.com/object/cuda_opencl_new.html
  3. http://developer.download.nvidia.com/compute/cuda/3_0/sdk/website/OpenCL/website/samples.html

You code samples for getting gidX, gidY and gidZ do not make much sense and the calculation of the index is wrong, too. The calculation depends on the ordering of your 3D matrix. It should look something like:

int index = x + y * sizeX + z * sizeX * sizeY;

But you should check the documentation first. Especially the working principle of the local ids are not explained quickly.

share|improve this answer
add comment

Your Answer


