Loadlibrary C library (dll) changing Matrix in place?

1 vue (au cours des 30 derniers jours)
Jonatan Peleg
Jonatan Peleg le 15 Nov 2021
Hi,
I'm using 'loadlibrary' and 'calllib' in order to run a function from a library that I've created outside of Matlab - I need a specific function to be executed on GPU (CUDA) due to performance considerations. I was following the example in 'shrlibsample' and indeed my code works fine.
I've noticed however, that when calling the 'lib' with a matrix, Matlab does not pass the original Matrix but rather generates a copy of the matrix and passes it. For example, when calling 'multDoubleArray' from the 'shrlibsample', we indeed get back the original vector multiply by 3 as a new vector. However, the original vector remains untouched. I'm working on very big matrices, and I was wondering whether it's possible to use the DLL to change the matrix 'inplace'. In the 'multDoubleArray' context, the vector will not be copied but rather the C code will replace each of it's element by multiplying it by 3. Is it possible to achieve something like that? Or is it a known limitation of the 'shrlibsample' that matrices are always duplicated? If it's possible, can you share a sample code that achieves it?
Thanks,
Yoni

Réponse acceptée

Joss Knight
Joss Knight le 15 Nov 2021
MATLAB is being exception safe and preserving your workspace by not silently modifying variables as a side effect. The only certain way to work around this that I know of is to write a MEX function and call your library from that. Even then, you will have to const_cast the input variable to retrieve the data to modify in-place.
However, what happens if you create your input data inside a function? Data in a function workspace doesn't affect the global workspace, and then when you use a syntax like x = myfunc(x) it can safely operate in-place because there is no workspace that needs to be preserved if there's an error. calllib might take advantage of this.

Plus de réponses (0)

Catégories

En savoir plus sur Call C from MATLAB dans Help Center et File Exchange

Produits


Version

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by