Matlab coder prevent memcpy/ name alias without copy

조회 수: 3 (최근 30일)
FB
FB 2019년 3월 18일
답변: David Fink 2019년 3월 20일
I use Matlab 2018A (soon B).
I have a data structue like:
Structure1.structure2.list
My function gets Structure1 as argument.
For easier use i declare
myList = Structure1.structure2.list;
Then i use myList for my calculations and save it back to the stucture.
Structure1.structure2.list = myList;
When i generate the C-Code witgh Matlab Coder the myList is created using memcpy and saving back leads to a memcpy as well, which is quite painful for the performance. Is there anything like coder.ref to force a pointer for myList?
Our workaround is to pass myList (and all other lists/arrays we need from the structure) directly to the function, which leads to a lot of input and output arguments, but has a lot better performance.

답변 (1개)

David Fink
David Fink 2019년 3월 20일
If Structure1.structure2.list is a Handle Class, you can assign to another name (myList) and modify the original object via myList. This also makes the final reassignment unnecessary.
Another option is the workaround you mentioned.
As another option, you could try pulling the logic dealing with myList into a new function, where myList is both an input and output.
This (with some caveats) should pass Structure1.structure2.list by pointer, or directly inline that function without a data copy.
The caveats are that a copy is generated when it is unsafe to pass by pointer. For example:
[x,x.f] = myfun(x,x.f);
Within myfun, the input values are distinct, so if one is modified, the other should not be. Thus one of x and x.f must be copied before calling myfun.

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by