jon*_*jon 5 c++ opencv cuda gpu
我正在尝试使用GpuMat数据编写自定义内核来查找图像像素的反余弦.当GPU上传数据时,我可以上传,下载和更改值,CV_8UC1但字符不能用于计算反余弦值.但是,当我尝试将GPU转换为CV_32FC1类型(浮点数)时,在下载部分期间出现非法内存访问错误.这是我的代码:
//.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;
}
Run Code Online (Sandbox Code Playgroud)
当我运行程序时,我的编译器告诉我:
OpenCV错误:副本中的Gpu API调用(遇到非法内存访问),文件/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)非法内存访问是在函数副本中遇到
小智 14
您可以使用cv::cuda::PtrStp<>或cv::cuda::PtrStpSz<>编写自己的内核(因此您不必使用GpuMat的step-Parameter,它可以简化您的代码: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);
}
}
Run Code Online (Sandbox Code Playgroud)
注意::
cv::cuda::PtrStep<>没有大小信息
cv::cuda::PtrStepSz<>:带有大小信息
cv::cuda::PtrStepSzb:对于unsigned char Mats(CV_8U)
cv::cuda::PtrStepSzf:对于float Mats(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());
}
Run Code Online (Sandbox Code Playgroud)
您可以使用cv::cuda::GpuMat以下方法调用此函数
callKernel(d_frame, d_output, cv::cuda::Stream());
Run Code Online (Sandbox Code Playgroud)
您将图像step视为float偏移.它是从一行到下一行的字节偏移量.
尝试这样的事情:
const float* rowsrcptr= (const float *)(((char *)srcptr)+rowInd*srcstep);
float* rowdstPtr= (float *) (((char *)dstptr)+rowInd*dststep);
Run Code Online (Sandbox Code Playgroud)
从文件:
step - 每个矩阵行占用的字节数.
为代码添加适当的cuda错误检查也是一个好主意(例如func).您可以运行代码cuda-memcheck来查看实际的内核故障,从而产生无效的读/写.
| 归档时间: |
|
| 查看次数: |
4409 次 |
| 最近记录: |