Matlab memory optimization regarding output arguments

Suppose we have the following code in functie.m file:
"
function functie()
x = [9];
x = functieLocala(x);
disp(x);
end
function y = functieLocala(x)
if length(x) > 10
y = [x 1];
else
y = x;
end
end
"
The question is will Matlab make the output argument of functieLocala y be a copy of the input argument x, or y will share the data of x? On one hand, function called functie tells that x doesn't need to be preserved, but I do not know how functieLocala behaves in terms of copying or sharing the value(s) of the input argument x to the output argument y.

 Accepted Answer

It depends.
If the else block runs (y = x) in functieLocala, y shares x because it is just another name of same matrix. If length(x) > 10, then y is not same as x. So the values of x are copied to y and they does not share anymore.
You can test it using memory and making a sufficient large array.
clear
memory
x = functie();
memory
function x = functie()
x = rand(10000);
x(1,1) = 0; % change here and see what happens
memory
x = functieLocala(x);
memory
end
function y = functieLocala(x)
if x(1,1) ~= 0
y = [x rand(10000, 1)];
else
y = x;
end
memory
end

3 Comments

Sorry I don't think memory can capture what is really going on. When one invokes memory it is too late.
The copy-on(write could carry out or not within the function functieLocala or after the function returns during the asignment in very short time. One cannot observe the memory activty after the assignment has been done.
x = functieLocala(x)
There is a blog article written by TMW satff few year back about using the same output+input name https://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/
and MATLAB is able to avoid deep memory copy.
It is not clear to me what is the situation now.
It says,
"When you assign an array to a second variable (for instance, when you execute B = A), MATLAB does not allocate new memory right away. Instead, it creates a copy of the array reference. As long as you do not modify the contents of the memory block being referenced by A and B, there is no need to store more than one copy of data. However, if you modify any elements of the memory block using either A or B, MATLAB allocates new memory, copies the data into it, and then modifies the created copy."
No worry, the copy on write I know for decades.
But my point is MATLAB can even go further in the optimization and might do NOT do copy-on-write if the function prototype is
x = functieLocala(x)
for both if branch, and the local x is on functie does not share with anything. This is call inplace assigment and the Loren blog (article 2007) recommends to do that. She uses Windows memory to show the behavior NOT MATLAB memory since it is not observable by this MATLAB command.
Recent MATLAB does even further memory optimization from few of the tests, the behavior is not very clear to me, but it looks like it can keep track of variable read and write of user intention, and do something smart on that.

Sign in to comment.

More Answers (1)

You better have your funtion codded like this
function functie()
x = 9; % Remove the braket
x = functieLocala(x);
disp(x);
end
function x = functieLocala(x) % make inplace otput when it can
if size(x,2) > 10 % use size rather than length
x = [x 1];
% remove the else branch
end
end

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!