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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Call C from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!