0

I encountered a problem on GpuMat.

I wrote the following code to change the value in GpuMat in a kernel function and then failed.

__global__ void testKer(uint8_t* src, int size)
{
    int x = blockIdx.x;
    int offset = x ;
    if (offset >= size) return;

    src[offset] = offset;
}
void test(GpuMat src)
{
    int size = src.cols * src.rows;

    testKer << <size, 1 >> > (src.data, total);
}
void main()
{
    GpuMat src(10, 10, CV_8U, Scalar::all(0));
    test(src);
    Mat host;
    src.download(host);
    cout << host;
}

Then I got the following output

[  0,   1,   2,   3,   4,   5,   6,   7,   8,   9;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0;
   0,   0,   0,   0,   0,   0,   0,   0,   0,   0]

only the first row data changed.

What's the problem? Thank you all.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
fanfan
  • 1
  • 2
  • Your kernel is incorrect because your assumption about the layout of the GpuMat in memory is wrong. See the linked duplicate -- it is a pitched allocation, and the answers to the duplicate show two different ways to access it correctly – talonmies Mar 09 '20 at 10:52
  • 2
    Moreover i would recommend to pass a cv::cuda::PtrStepSz to your kernel. By using this object you can access your data using operator(int row, int col) without worry about how your memory is aligned. – X3liF Mar 09 '20 at 12:02

0 Answers0