0

I have the following code snippet:

// main.cu

#define thread 16

dim3 blocks( ( width + thread - 1 ) / thread, ( height + thread - 1 ) / thread );

dim3 threads( thread, thread );

kernel<<<blocks, threads>>>( dev_data, width, height );

// kernel function

__global__ void kernel( uchar *data, int width, int height ) {

// map from threadIdx/blockIdx to pixel position

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

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

int offset = x + y * blockDim.x * gridDim.x;

data[offset * 3 + 0] = 255;

data[offset * 3 + 1] = 0;

data[offset * 3 + 2] = 0;
}

When i execute it only near half of the pixels getting blue. What am i doing wrong? I think its about indexing.

Ian Decks
  • 241
  • 1
  • 5
  • 14
  • i want all pixels to be blue by the way – Ian Decks Mar 11 '13 at 10:46
  • You also have to pass `widthStep` or `step` field of the OpenCV image as a kernel argument, to correctly calculate the offset of the pixel. [See this answer](http://stackoverflow.com/questions/12483321/unknown-error-when-inverting-image-using-cuda/12488854#12488854). – sgarizvi Mar 11 '13 at 11:48
  • `int offset = y * widthStep + (3 * x)`; `blue = data[offset];`, `green = data[offset + 1]`, `red = data[offset + 2];` – sgarizvi Mar 11 '13 at 11:52
  • i tried int offset = x * 3 + y * width * 3; but nothing changed – Ian Decks Mar 11 '13 at 14:09
  • http://stackoverflow.com/a/2327958/2055810 – Ian Decks Mar 11 '13 at 14:18
  • i just forgot to multiply with 3 in this code cudaMemcpy( data, dev_data, 3 * width * height * sizeof( uchar ), cudaMemcpyDeviceToHost ); sorry about that. – Ian Decks Mar 11 '13 at 14:35
  • 1
    Believe me you have to use widthStep to calculate correct index. I have gone through the trouble caused by the step of the image. See [this question](http://stackoverflow.com/q/8309258/1231073), you get this problem without widthStep. `width * 3` works with specific cases but not with all. – sgarizvi Mar 11 '13 at 15:46

0 Answers0