Loadlibrary C library (dll) changing Matrix in place?

3 次查看(过去 30 天)
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

采纳的回答

Joss Knight
Joss Knight 2021-11-15
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Call C from MATLAB 的更多信息

产品


版本

R2015b

Community Treasure Hunt

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

Start Hunting!

Translated by