Skip to content

Instantly share code, notes, and snippets.

@jrussino
Created July 8, 2017 05:29
Show Gist options
  • Save jrussino/c10aece599934797f4cf9e99b6f883aa to your computer and use it in GitHub Desktop.
Save jrussino/c10aece599934797f4cf9e99b6f883aa to your computer and use it in GitHub Desktop.
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
Copy link
Author

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
Copy link

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