Mex Files and Variables in the Matlab Workspace
Show older comments
Hello Everyone. I have a quick question. I have been using mex files, and I have recently noticed something that has puzzled me. Suppose that I start with a vector in Matlab, call it V, and I call a function in a mex file, MexFun, which accepts V as an input, and produces some output, Y, but in the process of running, suppose that MexFun directly operates on the elements of V. For example (this is not what I'm doing, but it is a simple example to illustrate what is happening), suppose that MexFun returns mean(V)+5, and it does this by adding 5 to each element of V, and then taking the mean of the new transformed V. When I run Y=MexFun(V), this will produce the correct output Y, but also change the input vector V in the Matlab workplace. This is all expected.
The question I have is the following. I have noticed that if I create a new variable that I set equal to V before I call MexFun, and then call MexFun, it will not only change V, but also T as well:
T=V
Y=MexFun(V)
Then after this is run, T will also be transformed. I don't understand what is going on, and why MexFun should be operating on T as well ... isn't it a separate variable at this point? If you changed elements in V through standard matlab commands, this wouldn't change the elements of T, but yet this appears to happen when mex files are involved. Could someone explain why this is happening?
Thank you,
Eddie
Accepted Answer
More Answers (2)
James Tursa
on 23 Jun 2012
This is called "shared data copy", or "copy-on-write", or "lazy copy" in various contexts. I will be producing a guide to all of this and uploading to the FEX "soon", but the short of it is:
A MATLAB variable is:
- A structure that contains information such as class, dimensions, and data pointers
- Data memory
When you do things that do not change the class or the data arrangement in memory, MATLAB will often produce what is called a "shared data copy" result. Meaning that a new information structure is created for the new variable, but the data pointers are exactly the same as the original variable (i.e., both variables point to the same data memory). An internal linked list keeps track of all variables that are sharing the same data memory (there can be any number of variables sharing the same data memory). If you alter the data or the arrangement of the data for one of the shared data copies, MATLAB will then copy the data to a new location in memory, remove it from the linked list, and then alter that. E.g., the following will produce a shared data copy result of X (for this discussion assume X is full):
format debug
X = [1 2 3 4 5 6];
Y = X;
Y = X(:) ; % Different dimensions potentially, but same arrangement in memory
Y = reshape(X,2,3);
Y = X.' ; % If X is a vector
etc.
When you look at the data pointer pr you can see that they are all the same.
Unfortunately, MATLAB provides no official mex API functions to detect when a passed in variable is shared, and in addition there are multiple ways a variable can be shared (a shared data copy, a shared reference copy, a shared parent copy, a handle classdef property, etc) which I will not go into here. You will have to wait for me to upload my mex guide to the FEX in the next few weeks to learn all of these grisly details.
MATLAB passes shared data copies of all arguments to m-code and p-code functions, but it passes the original variable address to mex functions (except in the case of cell or struct references which are regarded as expressions and resolved as shared data copies of the original even when used as a mex function argument).
Edward W
on 19 Jul 2012
0 votes
Categories
Find more on Variables in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!