Use cudaMallocManaged of CUDA in OpenCV 3.2.0

Hello there,
I am trying to use Unified Memory of CUDA on my Quadro P2000 Nvidia with OpenCV 3.2.0 in C++ on Ubuntu but the compiler does not let me compile when I use cudaMallocManaged.
Your help would be very much appreciated.
Victor Wu.

please do not use such an outdated version (that’s from 2016 !)
try with a current release , and if there are errors, show us those !

I would definitely want to the most current version of OpenCV. However, I am using OpenCV with ROS - ROS Melodic which is hard coded to OpenCV 3.2.0 ONLY!

sad to hear that. there seems to be a special hell for ROS users …

however, still noone can help you without seeing your exact code / errors,
please update your question with this


Thank you very much for your reply. I will post the exact error message tomorrow morning. It is just pass midnight here in Hong Kong.


1 Like

Hello berak,

This morning, I downloaded and compiled OpenCV 4.x and installed it. Then as I expected the vision_opencv modules of ROS came back at me with a lot of errors. I searched on the Internet and people encountered the problem with no obvious solutions yet.

That said, what I simply want to do is:

  1. Read from a special camera a frame.
  2. I want to ask CUDA to do CLAHE for me on that frame.
  3. I do not want to use upload and download for they take up alot of time.
  4. I managed to use pinned memory for the download, that is after the cuda::clahe I can then send off the frame – publish it using ROS.
  5. I want to do the same for the input, that is to copy the raw frame from the my special camera, which I cannot use imread on, to a patch of memory that CUDA can handle without explicit upload.

So, I did the following:

#include <opencv2/core/cuda.hpp>
#include <cuda_runtime.h>
#include <cuda_runtime_api.h>

void *raw_ptr;
cudaMallocManaged(&raw_ptr, 1280*1024*32);

When I compile, it gave the following error:

src/driver.cpp:100:18: error: expected constructor, destructor, or type conversion before ‘(’ token
cudaMallocManaged(&raw_ptr, 1280*1024*32);

stereo_nit/CMakeFiles/stereo_nit.dir/build.make:86: recipe for target 'stereo_nit/CMakeFiles/stereo_nit.dir/src/driver.cpp.o' failed
make[2]: *** [stereo_nit/CMakeFiles/stereo_nit.dir/src/driver.cpp.o] Error 1
CMakeFiles/Makefile2:4983: recipe for target 'stereo_nit/CMakeFiles/stereo_nit.dir/all' failed
make[1]: *** [stereo_nit/CMakeFiles/stereo_nit.dir/all] Error 2
Makefile:140: recipe for target 'all' failed

I hope this will make my situation a bit more clear to you if and when you try to help me.

Thank you very much indeed for your help.

Victor Wu.

I don’t personally ever use managed cuda memory but from a quick read through this blog post I don’t think you will see any speed up from using managed memory over manually uploading and downloading the frames.

Just to clarify, without including OpenCV in your project you can compile the below and it is simply the inclusion of #include <opencv2/core/cuda.hpp> which causes the error?

#include <cuda_runtime.h>
#include <cuda_runtime_api.h>

void *raw_ptr;
cudaMallocManaged(&raw_ptr, 1280*1024*32);