我使用CUDA的统一内存概念(CudaMallocManaged)创建了两个gpumat。当我尝试调用convertTo函数时,它崩溃了,并出现以下错误,
what():OpenCV(3.4.2)/home/visionteam/revampedtarastack/source/build/opencv-3.4.2/modules/cudev/include/opencv2/cudev/grid/detail/transform.hpp:321:错误:(-217:Gpu API调用)函数'call'中遇到非法的内存访问
我在ubuntu18.04中尝试了cuda 10
Mat input = imread("sample.jpg",0);
uint8_t* h_a;
uint8_t* h_b;
cudaMallocManaged(&h_a, sizeof(uint8_t)*1280*720);
cudaMallocManaged(&h_b, sizeof(float)*1280*720);
cv::Mat left1(input.cols,input.rows,CV_8UC1,h_a);
h_a = (uint8_t*)input.data;
cv::cuda::GpuMat floatGPU(input.rows,input.cols,CV_32FC1,h_b);
cv::cuda::GpuMat leftGPU(input.rows,input.cols,CV_8UC1,h_a);
floatGPU.convertTo(leftGPU,CV_8U);
'
预期:转换成功。
实际值:what():OpenCV(3.4.2) /home/visionteam/revampedtarastack/source/build/opencv-3.4.2/modules/cudev/include/opencv2/cudev/grid/detail/transform.hpp:321:错误:(-217:Gpu API调用)非法内存在中遇到访问 函数“通话”
编辑:
#include "iostream"
#include "stdio"
#include "opencv2/core/core.hpp"
#include "opencv2/cudastereo.hpp"
#include "opencv2/cudaarithm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <cuda_runtime.h>
#include <sys/time.h>
#include <math.h>
using namespace cv;
int main(void)
{
int rows = 480;
int cols = 1280;
float* h_a, *h_b, *h_c;
//Allocate memory for device pointers
cudaMallocManaged(&h_a, sizeof(float)*rows*cols);
cudaMallocManaged(&h_b, sizeof(float)*rows*cols);
cudaMallocManaged(&h_c, sizeof(float)*rows*cols);
//Mats (declaring them using pointers)
Mat hmat_a(Size(cols, rows), CV_32F, h_a);
hmat_a = imread("/home/vishnu/Desktop/color.png", 0);
Mat hmat_b(Size(cols, rows), CV_32F, h_b);
hmat_b = imread("/home/vishnu/Desktop/color.png", 0);
Mat hmat_c(Size(cols, rows), CV_32F, h_c);
//Gpu Mats (declaring with the same pointers!)
cuda::GpuMat dmat_a(Size(cols, rows), CV_32F, h_a);
cuda::GpuMat dmat_b(Size(cols, rows), CV_32F, h_b);
cuda::GpuMat dmat_c(Size(cols, rows), CV_32F, h_c);
cuda::multiply(dmat_a, dmat_b, dmat_c);
std::cout << hmat_c << endl;
return 0;
}
hmat_c也不会产生任何结果。我的代码出了什么问题。