Ask Your Question
0

Passing an array of cv::GpuMat to a cuda kernel?

asked 2016-03-01 22:41:15 -0600

liquidmetal gravatar image

updated 2016-03-02 08:10:36 -0600

I'm trying to write my own cuda kernel but I can't find a way to pass an array of cv::GpuMat to the cuda kernel.

Here is what I have until now:

__global__ void somename(cv::cuda::PtrStepSz<float> *array_of_images) {
    ...
    }
...
int main(...) {
    cv::cuda::GpuMat *mats = new cv::cuda::GpuMat[180]();
    // Initialize eachof the mats
    somename<<<...>>>(mats);           // Doesn't work
}

The compile error that the above snippet produces is:

filane.cu(89): error: argument of type "cv::cuda::GpuMat *" is incompatible with parameter of type "cv::cuda::PtrStepSz<float> *"

What's the correct way to pass an array of images to a cuda kernel?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
5

answered 2016-03-03 01:01:56 -0600

Vladislav Vinogradov gravatar image

You need to manually convert array of GpuMat to array of PtrStepSz:

cv::cuda::GpuMat *mats = new cv::cuda::GpuMat[180];
// Initialize eachof the mats

cv::cuda::PtrStepSz<float> h_ptrs = new cv::cuda::PtrStepSz<float>[180];
for (int i = 0; i < 180; ++i)
    h_ptrs[i] = mats[i];

Then you need to copy h_ptrs to GPU memory:

cv::cuda::PtrStepSz<float> *d_ptrs = NULL;
cudaMalloc(&d_ptrs, 180 * sizeof(cv::cuda::PtrStepSz<float>));

cudaMemcpy(d_ptrs, h_ptrs, 180 * sizeof(cv::cuda::PtrStepSz<float>), cudaMemcpyHostToDevice);

Then you can pass d_ptrs to kernel:

 somename<<<...>>>(d_ptrs);
edit flag offensive delete link more
0

answered 2017-05-26 00:28:19 -0600

josh97 gravatar image

updated 2020-06-13 11:18:58 -0600

HYPEREGO gravatar image

You can directly pass the array of GpuMats to kernels, but use them as an array of PtrStepSz in the kernel. You don't have to copy it to GPU memory.

somename<<<...>>>(mats);

In the kernel, access the array in this way:

__global__ void somename(PtrStepSz<float> *mats)
{
    x=threadIdx.x;
    y=threadIdx.y;
    mats[0] (y,x)=3;
    ..
    ...
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-03-01 22:41:15 -0600

Seen: 5,175 times

Last updated: Jun 13 '20