Using CUDA mex files interoperably with gpuArray

조회 수: 2 (최근 30일)
Matt J
Matt J 2021년 1월 14일
댓글: Matt J 2021년 1월 22일
I have a 3rd party black box CUDA mex file. Every time I use it, it puts my GPU in a state such that gpuArray cannot subsequently use it. This is in R2020a, but I've observed similar problems in previous versions.
>> a=gpuArray(1)
Error using gpuArray
An unexpected error occurred during CUDA execution. The CUDA error was:
CUDA_ERROR_CONTEXT_IS_DESTROYED
The only remedy once this occurs is to restart Matlab. Is there any way gpuArray and my cuda mex can share use of the same GPU without nuclear devastation?
  댓글 수: 3
Edric Ellis
Edric Ellis 2021년 1월 15일
Unfortunately, I suspect there is not. It is definitely intended that you should be able to use CUDA MEX files together with gpuArray. If the CUDA MEX files are putting the GPU into a bad state, then reset(gpuDevice) is indeed the best option. You can use save to save gpuArray data to a file (providing you do this before the GPU is in a bad state).
Matt J
Matt J 2021년 1월 15일
It is definitely intended that you should be able to use CUDA MEX files together with gpuArray
Thanks, Edric, but can you speculate what basic mistake the MEX might be making that would put the GPU in a bad state, and how it could be fixed? How exactly is a CUDA MEX file supposed to be aware of and operate within the same CUDA context as gpuArray code?

댓글을 달려면 로그인하십시오.

채택된 답변

Joss Knight
Joss Knight 2021년 1월 16일
편집: Joss Knight 2021년 1월 16일
A third party CUDA MEX file could be built with a different version of the CUDA toolkit, and thus a different version of the CUDA runtime. You will not be able to safely pass gpuArray objects into and out of the function.
To avoid this, obtain the source code and compile the function yourself using MEXCUDA.
That said, there's nothing to guarantee it won't work...but in this case it looks like your MEX function is resetting the device itself and thus destroying the CUDA context (for instance, it could be calling the CUDA C runtime function cudaDeviceReset). You won't be able to use it safely with gpuArray variables in your MATLAB workspace. Again, if you had access to the source code, you could modify this behaviour and even have MATLAB successfully respond to the reset event.
  댓글 수: 3
Joss Knight
Joss Knight 2021년 1월 16일
Well, I was being loose with my words. It's possible to have a call to reset in MATLAB successfully clear up persistent state in a MEX function, equivalently it is possible to cleanly reset the device from a MEX function (typically by actually executing the MATLAB function reset(gpuDevice) rather than calling the CUDA API). One of the things you can theoretically do by managing device reset is save GPU variables or return them to the host so the data is not lost.
However, it's pretty rare to need reset the device in a MEX function so perhaps your best bet is to work out why the CUDA context is being destroyed and fixing that problem.
Matt J
Matt J 2021년 1월 22일
Getting rid of the cudaDeviceReset's fixed it. Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 GPU Computing에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by