GpuMat custom kernel with float

I am trying to write my own kernel using the data GpuMatto find the cosine of the arc of the pixels of the image. I can load, load and change values ​​when I load data, when the GPU has data CV_8UC1, but symbols cannot be used to calculate arc cosines. However, when I try to convert my GPU to type CV_32FC1(floats), I get an illegal memory access error at boot time. Here is my code:

//.cu code 
#include <cuda_runtime.h>
#include <stdlib.h>
#include <iostream>
#include <stdio.h>
__global__ void funcKernel(const float* srcptr, float* dstptr, size_t srcstep, const     size_t dststep, int cols, int rows){
    int rowInd = blockIdx.y*blockDim.y+threadIdx.y;
    int colInd = blockIdx.x*blockDim.x+threadIdx.x;
    if(rowInd >= rows || colInd >= cols)
            return;
    const float* rowsrcptr=srcptr+rowInd*srcstep;
    float* rowdstPtr=  dstptr+rowInd*dststep;
    float val = rowsrcptr[colInd];
    if((int) val % 90 == 0)
            rowdstPtr[colInd] = -1 ;
    else{
            float acos_val = acos(val);
            rowdstPtr[colInd] = acos_val;
    }
}

int divUp(int a, int b){
    return (a+b-1)/b;
}

extern "C"
{
void func(const float* srcptr, float* dstptr, size_t srcstep, const size_t dststep, int cols, int rows){
    dim3 blDim(32,8);
    dim3 grDim(divUp(cols, blDim.x), divUp(rows,blDim.y));
    std::cout << "calling kernel from func\n";
    funcKernel<<<grDim,blDim>>>(srcptr,dstptr,srcstep,dststep,cols,rows);
    std::cout << "done with kernel call\n";
     cudaDeviceSynchronize();
}

//.cpp code
void callKernel(const GpuMat &src, GpuMat &dst){
    float* p = (float*)src.data;
    float* p2 =(float*) dst.data;
    func(p,p2,src.step,dst.step,src.cols,src.rows);
}

int main(){
    Mat input = imread("cat.jpg",0);
    Mat float_input;
    input.convertTo(float_input,CV_32FC1);
    GpuMat d_frame,d_output;
    Size size = float_input.size();
    d_frame.upload(float_input);
    d_output.create(size,CV_32FC1);
    callKernel(d_frame,d_output);
    Mat output(d_output);
    return 0;
}

When I run the program, my compiler tells me the following:

OpenCV: API Gpu ( ) , /home/mobile/opencv -2.4.9/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp, 882 'cv:: Exception' what(): /home/mobile/opencv -2.4.9/modules/dynamicuda/include/opencv2/dynamicuda/dynamicuda.hpp:882: : (-217)

+4
2

step, float. .

- :

const float* rowsrcptr= (const float *)(((char *)srcptr)+rowInd*srcstep);
float* rowdstPtr=  (float *) (((char *)dstptr)+rowInd*dststep);

:

step - , .

cuda (, func). cuda-memcheck, , /.

+5

cv::cuda::PtrStp<> cv::cuda::PtrStpSz<> ( step-Parameter GpuMat, : D):

:

    __global__ void myKernel(const cv::cuda::PtrStepSzf input,
                             cv::cuda::PtrStepSzf output)
    {
        int x = blockIdx.x * blockDim.x + threadIdx.x;
        int y = blockIdx.y * blockDim.y + threadIdx.y;

        if (x <= input.cols - 1 && y <= input.rows - 1 && y >= 0 && x >= 0)
        {
           output(y, x) = input(y, x);
        }
    }

:
cv::cuda::PtrStep<>:
      cv::cuda::PtrStepSz<>:
       cv::cuda::PtrStepSzb: unsigned char (CV_8U)
      cv::cuda::PtrStepSzf: (CV_32F)
      cv::cuda::PtrStep<cv::Point2f>:

:

    void callKernel(cv::InputArray _input,
                    cv::OutputArray _output,
                    cv::cuda::Stream _stream)
    {
        const cv::cuda::GpuMat input = _input.getGpuMat();

        _output.create(input.size(), input.type()); 
        cv::cuda::GpuMat output = _output.getGpuMat();

        dim3 cthreads(16, 16);
        dim3 cblocks(
            static_cast<int>(std::ceil(input1.size().width /
                static_cast<double>(cthreads.x))),
            static_cast<int>(std::ceil(input1.size().height / 
                static_cast<double>(cthreads.y))));

        cudaStream_t stream = cv::cuda::StreamAccessor::getStream(_stream);
        myKernel<<<cblocks, cthreads, 0, stream>>>(input, output);

        cudaSafeCall(cudaGetLastError());
    }

cv::cuda::GpuMat:

   callKernel(d_frame, d_output, cv::cuda::Stream());
+8

Source: https://habr.com/ru/post/1547434/


All Articles