MEX function with CUDA code works once. Then I need to restart Matlab or recompile it!?

1 view (last 30 days)
I’ve written a MEX file that uses the CUDA solver, specifically the cusolverDnDgesvd function, to compute the equivalent of Matlabs SVD calculation: [U, S, Vt] = svd(A)
I can compile it using mexcuda like this:
mexcuda CUDA_SVD.cu -lcublas -lcusolver -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\include" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v7.5\lib\x64"
and it compiles without any errors and when I run it, it seems to produce the same singular value as the Matlab version does when running it on the CPU. Which is great. However. The second time I run it, with the exact same input, the output is 0! Example:
A = rand(500,1);
[U, S, Vt] = CUDA_SVD(A);
S =
12.7203
[U, S, Vt] = CUDA_SVD(A);
S =
0
[U, S, Vt] = CUDA_SVD(A);
S =
0
It’s like the function is ruined after one call to it. And it doesn’t stop producing 0 as output until I either recompile the .cu file or restart Matlab. If I do that it works one time again before going back to producing zeros. Do I need to reset/clear something inside the MEX file that I have missed or why in the name of all that is holy does it behave like this? Anyone who has any ideas?
I have attached the file here as a .zip if someone would be kind enough to look at it. The CUDA code is largely based on the SVD example from NVIDIA’s webpage:
  2 Comments
Petter Stefansson
Petter Stefansson on 19 Jan 2017
Update: it also appears to work if I ‘ clear all ’ every time between runs. Manually clearing every single variable in my Matlab workspace however, like: ‘ clear A U S Vt ’, does not work…

Sign in to comment.

Answers (2)

T Mullai
T Mullai on 10 Jul 2018
Hi Petter, I am looking for a MEX file that uses CUDA solver to compute SVD of a matrix. Iam able to download CUDA_SVD.zip file you have shared here. However, an error occurs when I try to unzip. Can you please reload the zip file again?I would like to explore in fixing the problem of zero coming for the second time.
Thanks.

Guillaume GODEFROY
Guillaume GODEFROY on 26 Feb 2020
Hi Peter,
I have a similar problem, the code works the first time but the second time it give me always the same result even if I change the input data, did you find a way to manage it?
Thanks

Community Treasure Hunt

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

Start Hunting!