Is there any function to create cuda context in opencv similar to what cuCtxCreate does from cuda library?

asked 2019-05-27 11:36:49 -0500

vchill gravatar image

updated 2019-12-09 08:05:33 -0500

Akhil Patel gravatar image

I am developing an app which requires to use cuda functions like cuInit, cuCtxCreate etc. If i use these functions from cuda library then i get the following error which i mentioned in this issue

I got this solved by modifying CMakeLists.txt and linking CUDA_CUDA_LIBRARY which is path to cuda.lib. My question here is Are there any alternatives available in opencv for doing the same? Like i see cuSetDevice cuGetDevice which sets and gets cuda devices but i am not able to find a function in opencv for creating a cuda context similar to what cuCtrCreate does.

edit retag flag offensive close merge delete


Are you are getting errors when you link to both opencv_worldxxx.lib and cuda.lib? And because of this you can't directly call cuCtxCreate() so you want to see if there is an OpenCV function which does the same thing?

cudawarped gravatar imagecudawarped ( 2019-05-27 14:31:23 -0500 )edit

Yes. I get link errors "unresolved external symbol cuCtxCreate" .So in windows, since the opencv solution file is created, i opened solution file and in opencv_world project->projectProperties->linker->input i added cuda.lib and it worked. But i was not able to compile with linux using cmake and hence i found that solution which is mentioned on git.

Suppose i dont want to have that solution. And still want to use cuCtxCreate. So i want an alternative function (may be present in opencv) which does exact same thing.

The reason of searching for alternative is there are cuda based sample apps which doesn't have cuInit or cuCtxCreate but still they work. But i believe internally they somewhere might be having a cuda context created.

vchill gravatar imagevchill ( 2019-05-27 23:49:18 -0500 )edit

I don't understand why needing to include calls to the cuda driver api in your own code has anything to do with the compilation of the OpenCV library itself? As you said in windows you just need to link to cuda.lib. In linux you should just be able to pass -lcuda to nvcc when you compile your own source.

Regarding the context, OpenCV uses the cuda runtime and not the driver api so the contexts are automatically created if they do not exist, as per the documentaion.

cudawarped gravatar imagecudawarped ( 2019-05-28 03:39:51 -0500 )edit

If i push my code into opencv repo then cuda.lib needs to be automatically linked rather than i adding -lcuda or adding to project properties. Regarding the context, How can i get that context which is automatically created?

vchill gravatar imagevchill ( 2019-05-28 05:07:26 -0500 )edit

Why are you pushing to the OpenCV repo, are you adding functionality to the library?

Why do you need the context?

cudawarped gravatar imagecudawarped ( 2019-05-28 05:24:16 -0500 )edit

Yes. i am adding a functionality in opencv_contrib in optical flow section. For my code to work, i need the cuda context and hence i am using cuCtxCreate for creating one and now i need an alternative for the same

vchill gravatar imagevchill ( 2019-05-28 05:41:49 -0500 )edit

Interesting what are you trying to do that requires a CUDA context?

Are you going to call cuCtxCreate() before calling any runtime api functions which require a context? If its after you should just be able to grab the context that has been created with cuCtxGetCurrent().

cudawarped gravatar imagecudawarped ( 2019-05-28 07:03:28 -0500 )edit

I can initially call runtime API's and then call cuCtxGetCurrent() But cuCtxGetCurrent() is again a driver API which will lead to unresolved symbol error.

vchill gravatar imagevchill ( 2019-05-29 04:03:37 -0500 )edit

Hi, I understand that, I am suggesting that it would be better once you have the driver api available to you to grab the current context instead of creating a new one. Because if you do create a new context when one exists for the runtime api and then apply it, your runtime api calls and driver api could get confused. But this depends what you are trying to achieve, why do you nee the context, is there an alternative?

cudawarped gravatar imagecudawarped ( 2019-05-29 06:49:34 -0500 )edit

Sounds fine. But at the end, anyway i need to link cuda.lib. I dont have any other choice.

Thanks @cudawarped for helping. Appreciated :-)

vchill gravatar imagevchill ( 2019-05-29 11:41:12 -0500 )edit