Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

oop - Memory-Allocation for function calls of form x = f(x) in MATLAB

In my code, I have lots of places where I invoke functions of the form

X = f(X)

and X can be a rather large matrix. In my special case, I have mostly calls like

X = feval(somefunc, X)

or

X = obj.myfunc(X)

It would be bad if, every time the function is called, there is new space allocated for X. Is MATLAB smart enough to deal with such function calls? Is there a way to tell?

The answer to this question helps would help very much with a design descision. I like to program in object oriented style and if MATLAB is not smart enough for this, it might pay off for me to add another member for X in the class, although I would rather not do this otherwise.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Whether a copy is made of the input arguments or not when calling a function in MATLAB depends upon what happens inside of the function.

MATLAB uses a system referred to as copy-on-write. This means that if you pass a large variable to a function as an input, as long as you do not modify the variable within that function, the variable will not be copied into the workspace of the function and the function will instead read the data from it's current location in memory.

function Y = func(X)
    Y = X + 1;
end

If you are modifying the variable within the function, then a copy of the input variable is made and placed into the local workspace of the function.

function X = func(X)
    X = X + 1;
end

There is more information on Loren's Mathworks blog.

An easy way to determine if a copy of the data is made or not is to use the undocumented format debug mode which will show you where the data for a given variable is stored in memory.

format debug

%// Create a variable a and show where debug info
a = [1,2]

%// Structure address = 141f567f0
%// m = 1
%// n = 2
%// pr = 7f9540b85e20
%// pi = 0
%//      1     2

%// Assign b = a but don't modify
b = a

%// Structure address = 141f567f0
%// m = 1
%// n = 2
%// pr = 7f9540b85e20  <= POINTER TO DATA REMAINED UNCHANGED
%// pi = 0
%//      1     2

%// Modify (Will create a new copy)
b = b + 1

%// Structure address = 141f55b40
%// m = 1
%// n = 2
%// pr = 7f953bcf1a20   <= POINTER TO DATA CHANGED (COPY)
%// pi = 0
%//      2     3

If you prefer you can use this little anonymous function I've created to inspect the memory location of any particular variable.

memoryLocation = @(x)regexp(evalc('disp(x)'), '(?<=prs*=s*)[a-z0-9]*', 'match')

a = [1,2];
memoryLocation(a)
%// 7f9540b85e20

b = a;
memoryLocation(b)
%// 7f9540b85e20

b = b + 1;
memoryLocation(b)
%// 7f953bcf1a20

As a bit of a side-note, I would recommend against using feval throughout your code and instead just use the function names directly.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...