Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OpenCV multi-threaded CUDA ORB example (fails with "illegal memory access")
#include <iostream>
#include <thread>
#include <opencv2/cudafeatures2d.hpp>
#include <opencv2/cudaimgproc.hpp>
#include <opencv2/highgui.hpp>
void capture_thread(int device_num, int n_cycles)
{
std::cout << "Starting capture thread for device " + std::to_string(device_num) + "\n" << std::flush;
// open VideoCapture for the specified device
cv::VideoCapture cap(device_num);
if (!cap.isOpened())
{
std::cerr << "ERROR - Failed to open VideoCapture for device " + std::to_string(device_num) + "\n" << std::flush;
return;
}
// each thread gets its own CUDA stream
cv::cuda::Stream stream;
std::cout << "Initialization complete for capture thread for device " + std::to_string(device_num) + "\n" << std::flush;
for(int i = 1; i <= n_cycles; ++i)
{
// capture frame
cv::Mat frame;
if(!cap.read(frame))
{
continue;
}
// upload to GPU
cv::cuda::GpuMat frame_d;
frame_d.upload(frame, stream);
// convert to grayscale
cv::cuda::GpuMat frame_gray_d;
cv::cuda::cvtColor(frame_d, frame_gray_d, CV_BGR2GRAY, 0, stream);
// create CUDA ORB feature detector
cv::Ptr<cv::cuda::ORB> p_orb_d = cv::cuda::ORB::create();
p_orb_d->setBlurForDescriptor(true);
// detect and compute features
cv::cuda::GpuMat kp_d;
cv::cuda::GpuMat desc_d;
p_orb_d->detectAndComputeAsync(frame_gray_d, cv::cuda::GpuMat(), kp_d, desc_d, false, stream);
/*
* Do something with the features & descriptors...
*/
}
std::cout << "Capture thread for device " + std::to_string(device_num) + " finished with no errors\n" << std::flush;
}
int main(int argc, const char* argv[])
{
std::cout << "Starting main program" << std::endl;
// Start two capture threads
std::thread capture_thread_0(capture_thread, 0, 1000);
std::thread capture_thread_1(capture_thread, 1, 1000);
// Wait for both threads to return
capture_thread_0.join();
capture_thread_1.join();
std::cout << "Main program completed with no errors" << std::endl;
return 0;
}
@jrussino

This comment has been minimized.

Copy link
Owner Author

@jrussino jrussino commented Jul 8, 2017

This always fails immediately after both threads are initialized, with some variant of an "illegal memory access" error. Here are all of the different versions I observed:

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in nonmaxSuppression_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/fast.cu, line 375
terminate called after throwing an instance of 'thrust::system::system_error'
what(): after cub_::DeviceRadixSort::SortPairs(0): an illegal memory access was encountered

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in IC_Angle_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu, line 239
terminate called after throwing an instance of 'thrust::system::system_error'
what(): after cub_::DeviceRadixSort::SortPairs(1): an illegal memory access was encountered

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in IC_Angle_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu, line 239
OpenCV Error: Gpu API call (an illegal memory access was encountered) in IC_Angle_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu, line 239
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu:239: error: (-217) an illegal memory access was encountered in function IC_Angle_gpu

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (invalid pitch argument) in copyTo, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 284
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu:284: error: (-217) invalid pitch argument in function copyTo

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in setTo, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 387
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu:387: error: (-217) an illegal memory access was encountered in function setTo

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in allocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 116
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu:116: error: (-217) an illegal memory access was encountered in function allocate

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file /home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in IC_Angle_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu, line 239
terminate called after throwing an instance of 'thrust::system::system_error'
what(): an illegal memory access was encountered

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in nonmaxSuppression_gpu, file /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/fast.cu, line 375
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/fast.cu:375: error: (-217) an illegal memory access was encountered in function nonmaxSuppression_gpu

Starting main program
Starting capture thread for device 0
Starting capture thread for device 1
Initialization complete for capture thread for device 1
Initialization complete for capture thread for device 0
OpenCV Error: Gpu API call (an illegal memory access was encountered) in deallocate, file
/home/jrussino/third_party/opencv/opencv/modules/core/src/cuda/gpu_mat.cu, line 81
OpenCV Error: Gpu API call (an illegal memory access was encountered) in HarrisResponses_gpu, file
/home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu, line 157
terminate called after throwing an instance of 'cv::Exception'
what(): /home/jrussino/third_party/opencv/opencv/modules/cudafeatures2d/src/cuda/orb.cu:157: error: (-217) an illegal
memory access was encountered in function HarrisResponses_gpu

@jiawenhao2015

This comment has been minimized.

Copy link

@jiawenhao2015 jiawenhao2015 commented Dec 23, 2020

hello~~~~i met the similar error recentlly when i use opencv gpu download or opencv gpu clone, so how do you solve your issue above???
many many thanks~~~waiting for your reply sincerely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment