Need help for get data from thread

I am new programmer for CUDA and my problem is

in global function, if I set up index like this

const int x = blockDim.x * blockIdx.x + threadIdx.x;
const int y = blockDim.y * blockIdx.y + threadIdx.y;
const int gpuIdx = y * width + x;

and each thread is GPUdata[gpuIdx]

How can I get x value and y value from any GPUdata[gpuIdx] ?

Thank you for all answer.

Is GPUdata a structure ?

What do you mean get value ? Read it inside a thread or read it on host side ?

I presume you want to do smth like that:

GPUdata_struct                                         // create structure which holds your x's and y's

{

  float x;

  float y;

};

__global__ void function(GPUdata_struct *GPUdata)

{

  const int x = blockDim.x * blockIdx.x + threadIdx.x;

  const int y = blockDim.y * blockIdx.y + threadIdx.y;

  const int gpuIdx = y * width + x;

  GPUdata[gpuIdx].x = 5.0f * cos(gpuIdx/4.0);          // set some values to GPUdata_struct array

  GPUdata[gpuIdx].y = 3.0f * sin(gpuIdx);

}

int main()

{

  const int size = 32;

  GPUdata_structure *data_d;                           // data on the device

  GPUdata_structure *data;                             // data on the host

  cudaMalloc((void **)&(data_d), size * sizeof(GPUdata_structure)));   // resserve memory on GPU

  function<<< 1, size >>>(data_d);                     // fill in the data on the GPU

  cudaMemcpy(data, data_d, size * sizeof(GPUdata_structure), cudaMemcpyDeviceToHost)); // copy data to host

  for(int i = 0; i < size; ++i)                        // print data

    printf("data[%d]:\tx:%f\ty:%f\n", i, data[i].x, data[i].y);

  return 0;

}

Ofcourse you can do it the other way: initialize data on host, copy it to GPU, use it there.

Or even change it, and copy it back to the host.

Thank you for your answer.

In my case, I need to calculate in some [gpuIdx] index not all gpu thread.

So, I need to known which index that pass my condition and I will calculate on some gpu block.

const int x = blockDim.x * blockIdx.x + threadIdx.x;
const int y = blockDim.y * blockIdx.y + threadIdx.y;
const int gpuIdx = y * widthp2 + x; 

if (GPUdata[gpuIdx] > 0.15) <----- for selecting gpu block 
       {
             do something 1
       }

   if (GPUdata[x] > 1)               <----- for calculate in some block that choose from do something 1
       {
             do something 2
       }

from example, how can I get x position of gpuIdx in global function?

Thank you for all answer.

I’m confused. You calculate x in each thread. Why can’t you use it?

Or do you mean, how to retrieve x and y from gpuIdx?

Dear Dittoaway,

Yes, I need to retrieve x and y value from gpuIdx.

Because I need some gpu thread to calculate not all gpu thread to calculate.

x = gpuIdx % width;

y = gpuIdx / width;

Or, a tiny bit faster

y = gpuIdx / width;

x = gpuIdx - y*width

I’m also confused. But isn’t threadIdx.x and threadIdx.y what you want ?

See Programming Guide on page 9,

and maybe then try to explain which x’s and y’s you need to get.

Thank you tera (I’m not sure you are THAI people or not?)
Your answer is my point.

Dear Michał Skrzypkowski,
Sorry for my question that make you confuse and the answer of tera is my point.

Thank you for all answer that make me clear for my problem.

No, I’m not Thai. My nickname derives from the order of magnitude of FLOP/s achievable on current GPUs.