Hi Folks,
I am trying to declare “Unified Memory” and copy a buffer into it. After doing so I am trying to display the image using cv::imshow available in OpenCV. I am successful in getting one copy of the image. However, the program crashes with Segmentation fault if I try to create 2 copies and try to display the second copy. The code snippet is as follows :
// Acquire a Frame of size 1920x1080
UniqueObj<Frame> frame(iFrameConsumer->acquireFrame());
IFrame *iFrame = interface_cast<IFrame>(frame);
if (!iFrame)
break;
// Get the Frame's Image.
Image *image = iFrame->getImage();
EGLStream::NV::IImageNativeBuffer *iImageNativeBuffer
= interface_cast<EGLStream::NV::IImageNativeBuffer>(image);
TEST_ERROR_RETURN(!iImageNativeBuffer, "Failed to create an IImageNativeBuffer");
int fd = iImageNativeBuffer->createNvBuffer(Argus::Size {m_framesize.width, m_framesize.height},
NvBufferColorFormat_YUV420, NvBufferLayout_Pitch, &status);
if (status != STATUS_OK)
TEST_ERROR_RETURN(status != STATUS_OK, "Failed to create a native buffer");
#if 1
cudaSetDeviceFlags(cudaDeviceMapHost);
NvBufferParams params;
NvBufferGetParams(fd, ¶ms);
char *data_mem = NULL;
int size = m_framesize.width* m_framesize.height;
int fsize = params.pitch[0] * m_framesize.height ;
data_mem = (char*)mmap(NULL, fsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, params.offset[0]);
char *d_myimagen = NULL;
char* h_cudaout = NULL;
int read1 = cudaMallocManaged(&h_cudaout, ( m_framesize.height * m_framesize.width)*sizeof(char));
cout<<"read1 : " <<read1 <<endl;
cout<<"h_cudaout :" <<h_cudaout <<endl;
int read2 = cudaMallocManaged(&d_myimagen, ( m_framesize.height * m_framesize.width)*sizeof(char));
cout<<"read2 : " <<read2 <<endl;
cout<<"d_myimagen :" <<d_myimagen <<endl;
int copy1 = cudaMemcpy (h_cudaout,data_mem,m_framesize.width*m_framesize.height,cudaMemcpyHostToDevice) ;
cout<<"copy1 : " <<copy1 <<endl;
int copy2 = cudaMemcpy (d_myimagen,data_mem,m_framesize.width*m_framesize.height,cudaMemcpyHostToDevice) ;
cout<<"copy2 : " <<copy2 <<endl;
cudaDeviceSynchronize();
cv::Mat CudaOUTimgbuf1 = cv::Mat(m_framesize.height, m_framesize.width, CV_8UC1, (void *) d_myimagen , params.pitch[0]);
cv::imshow("CudaOUTimgbuf1", CudaOUTimgbuf1);
cv::waitKey(1);
The code works fine if I try to display “h_cudaout” instead of “d_myimagen”. Please help me figure out where I am going wrong.
Thanks