Why adding a subfunction breaks an unrelated in-place function call?
15 ビュー (過去 30 日間)
古いコメントを表示
Debugging some memory issues in my code, I reduced the problem into this test. This works as I expect:
function inplaceTest
x = randn(2^28, 1);
tic, y = f(x); toc % Argument copied - Elapsed time is 0.488432 seconds.
tic, x = f(x); toc % In-place call - Elapsed time is 0.000304 seconds.
end
function x = f(x)
x(50) = 0;
end
But adding a nested subfunction, even a dummy one, somehow breaks the in-place operation:
function inplaceTest2
x = randn(2^28, 1);
tic, y = f(x); toc % Argument copied - Elapsed time is 0.497300 seconds.
tic, x = f(x); toc % Takes even longer? - Elapsed time is 0.686611 seconds.
function dummy()
end
end
function x = f(x)
x(50) = 0;
end
The ordering of the function calls does not seem to matter.
What is going on here? Does this mean that in-place operations are disabled in functions that have subfunctions?
0 件のコメント
回答 (1 件)
Jan
2021 年 3 月 12 日
You can check with
format debug
if the variable is copied or re-used. Does the pointer to the data change?
I guess, that the JIT cannot work reliably if a nested function might interfere. Therefore I avoid nested functions in general.
2 件のコメント
Jan
2021 年 3 月 12 日
The decision to use nested function cannot be undone by setting a magic flag. As long, as Matlab's JIT accelerator cannot handle nested structs efficiently, there is no chance to solve this. Ask the MathWorks team for an enhancement of the JIT. This will take at least a year until it is implemented.
参考
カテゴリ
Help Center および File Exchange で Parallel Computing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!