zed-python-api icon indicating copy to clipboard operation
zed-python-api copied to clipboard

Could I share my CUDA context with a PyZEDCamera?

Open oha08 opened this issue 7 years ago • 13 comments

Though the ZED SDK provides a way to share my CUDA context with a ZED camera, it seems that the current version of its python wrapper lacks it:

  • InitParameters::sdk_cuda_ctx
  • Camera::getCUDAContext

Is there any way to share my context with a PyZEDCamera? I'm grad if ZED cameras could be used in CUDA applications developed on python.

oha08 avatar Apr 09 '18 07:04 oha08

Hi, Yes, it's a missing feature, you currently can't (without modifying the plugin). It is planned though. It will probably require PyCUDA.

adujardin avatar Apr 09 '18 09:04 adujardin

Hi, thank you for clarification. For now, I will try to modify the plugin to run it with PyCUDA.

oha08 avatar Apr 10 '18 00:04 oha08

Hi, I've noticed that I can use CUDA context switching (push & pop) to run my CUDA programs with PyZED rather than trying to share the contexts, which is sufficient for my purpose. Yet it'd be convenient if context sharing will be implemented on PyZED in future :)

oha08 avatar Jun 01 '18 02:06 oha08

@oha08 I'm new to this and trying to figure out how to do the same (I need to run a DNN and query the ZED In the same process). Any code you can share? How exactly does one switch cuda contexts? Thanks!

frk2 avatar Jul 01 '18 00:07 frk2

I made a tiny cython module to do this and it works! Thanks for the tip guys!

https://github.com/frk2/cuda-fun

frk2 avatar Jul 01 '18 18:07 frk2

@frk2 Hi, Your module looks pretty nice and simple enough for working! In my case, I'm using PyCUDA to create and push/pop my context. Because PyCUDA doesn't provide any interface to get the current context, I've selected to keep the context made by ZED stacked and push my context on top of it:

  1. open ZED Camera where a context is created
  2. create my context, which is automatically stacked on top of the previous context
  3. pop my context when calling ZED functions
  4. push my context when calling my CUDA kernels (if it is popped before) and so on

Your module enables more flexible control than this :) Thanks a lot!

oha08 avatar Jul 02 '18 02:07 oha08

I was thinking of doing the same - but the bummer is that you can't pass a created context to either ZED or Pytorch (they both create one) and hence pycuda was useless since it doesn't let me get the current context.

Thanks for the tip though definitely guided me in the right direction!

frk2 avatar Jul 02 '18 02:07 frk2

@frk2 By default the ZED SDK creates a new context, but with the C++ SDK you can pass your own context to the ZED with InitParameters::sdk_cuda_ctx. As mentioned in the original post, this feature is missing from the python wrapper though.

adujardin avatar Jul 04 '18 08:07 adujardin

@adujardin yup totally am aware - since im trying to use pytorch in the same process the simplest approach was to juggle contexts using pop and push from within python. Temporary approach till you guys add context passing through pycuda or similar. Thanks!

frk2 avatar Jul 04 '18 20:07 frk2

Any update whether context sharing is possible with ZED camera and the python API? I'm trying to work with the ZED camera an CuPy an have probably encountered an issue related to this.

yuvalg1987 avatar Oct 03 '21 10:10 yuvalg1987

Just to update on my solution for this issue. The ZED runs its own context internally and therefore processing images using CuPy should be handled in a different thread. My best solution relied on the ZED-TensorFlow example which worked pretty good.

yuvalg1987 avatar Oct 04 '21 13:10 yuvalg1987

你好。我用的是zed相机,cuda是11.7, 运行get_python_api.py文件时,提示PermissionError: [Errno 13] Permission denied: 'C:\Program Files (x86)\ZED SDK\pyzed-3.8-cp39-cp39-win_amd64.whl',但是安装whl文件之后,继续重复出现这个错误,是什么原因导致的呢

zcy1113 avatar Mar 11 '23 02:03 zcy1113

Hello. I use a zed camera, and the cuda is 11.7. When I run the get_python_api.py file, I am prompted with permission error: [errno13] Permission denied:' C: \ \ Program files (x86) \ \ ZED SDK \ \ PYZED-3.8-CP39-CP39-Win-amd64.whl', but after installing the whl file, what causes this error to appear repeatedly?

zcy1113 avatar Mar 11 '23 02:03 zcy1113