<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135</link>
    <title>MATLAB Central Newsreader - copy-on-write with MEX files</title>
    <description>Feed for thread: copy-on-write with MEX files</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Wed, 23 Jul 2008 22:25:05 -0400</pubDate>
      <title>copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#445008</link>
      <author>Matt </author>
      <description>&lt;br&gt;
I'm trying to understand if there is a difference in the &lt;br&gt;
way copy-on-write is implemented for MEX functions as &lt;br&gt;
compared to Mfiles.&lt;br&gt;
&lt;br&gt;
My understanding is as follows. As part of the copy-on-&lt;br&gt;
write system, MATLAB will pass input data by reference to &lt;br&gt;
an Mfile function if the function performs read-only &lt;br&gt;
operations on that data. Otherwise, it will pass the data &lt;br&gt;
by value. &lt;br&gt;
&lt;br&gt;
However, when it comes to MEX functions, the MATLAB &lt;br&gt;
interpreter obviously cannot know the contents of the &lt;br&gt;
function and cannot determine whether the operations it &lt;br&gt;
will perform are read-only or otherwise. If MATLAB wanted &lt;br&gt;
to have all the same safety nets as for Mfiles, it would &lt;br&gt;
have to always pre-duplicate the input data and send that &lt;br&gt;
to the MEX function instead (effectively passing the data &lt;br&gt;
by value).&lt;br&gt;
&lt;br&gt;
One reason for doing it this way would be to ensure that if &lt;br&gt;
the MEX file crashes, the input data would remain unaltered &lt;br&gt;
in the caller workspace. This will be true for Mfiles, and &lt;br&gt;
the MATLAB software designers may have wanted it to be true &lt;br&gt;
for MEX files as well.&lt;br&gt;
&lt;br&gt;
On the other hand, if data is always passed by value, it &lt;br&gt;
would obviously reduce the possibilities for computational &lt;br&gt;
efficiency that people are usually interested in using MEX &lt;br&gt;
files to obtain.&lt;br&gt;
&lt;br&gt;
In any case, I have several colleagues who believe MEX &lt;br&gt;
files always receive input by value. Can anyone resolve &lt;br&gt;
this?</description>
    </item>
    <item>
      <pubDate>Thu, 24 Jul 2008 03:19:02 -0400</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#445047</link>
      <author>James Tursa</author>
      <description>&quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in&lt;br&gt;
message &amp;lt;g68b41$cu8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm trying to understand if there is a difference in the &lt;br&gt;
&amp;gt; way copy-on-write is implemented for MEX functions as &lt;br&gt;
&amp;gt; compared to Mfiles.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; My understanding is as follows. As part of the copy-on-&lt;br&gt;
&amp;gt; write system, MATLAB will pass input data by reference to &lt;br&gt;
&amp;gt; an Mfile function if the function performs read-only &lt;br&gt;
&amp;gt; operations on that data. Otherwise, it will pass the data &lt;br&gt;
&amp;gt; by value. &lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
No. MATLAB does not pre-parse the m-file to determine if an&lt;br&gt;
input variable is changed. MATLAB *always* passes the&lt;br&gt;
variables by reference. Once inside the m-file, if the input&lt;br&gt;
variable is changed, *then* MATLAB makes a copy. That is the&lt;br&gt;
copy-on-write behavior.&lt;br&gt;
&lt;br&gt;
&amp;gt; However, when it comes to MEX functions, the MATLAB &lt;br&gt;
&amp;gt; interpreter obviously cannot know the contents of the &lt;br&gt;
&amp;gt; function and cannot determine whether the operations it &lt;br&gt;
&amp;gt; will perform are read-only or otherwise. If MATLAB wanted &lt;br&gt;
&amp;gt; to have all the same safety nets as for Mfiles, it would &lt;br&gt;
&amp;gt; have to always pre-duplicate the input data and send that &lt;br&gt;
&amp;gt; to the MEX function instead (effectively passing the data &lt;br&gt;
&amp;gt; by value).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; One reason for doing it this way would be to ensure that if &lt;br&gt;
&amp;gt; the MEX file crashes, the input data would remain unaltered &lt;br&gt;
&amp;gt; in the caller workspace. This will be true for Mfiles, and &lt;br&gt;
&amp;gt; the MATLAB software designers may have wanted it to be true &lt;br&gt;
&amp;gt; for MEX files as well.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
There is no safety net for MEX files. There is no&lt;br&gt;
copy-on-write behavior for MEX files. The variables are&lt;br&gt;
passed by reference to the MEX file, same as an m-file. If&lt;br&gt;
you change an input variable in a MEX file, then the&lt;br&gt;
original is changed (as well as all other variables that&lt;br&gt;
share the same data memory). This can mess up the workspace&lt;br&gt;
of course, which is why you see warnings to never do this in&lt;br&gt;
the doc. However, it *is* safe to do if you *know* that the&lt;br&gt;
data memory is not shared with another variable. This can be&lt;br&gt;
tricky and is not recommended unless you really need to&lt;br&gt;
(e.g., very large variables that you don't want duplicated).&lt;br&gt;
&lt;br&gt;
&amp;gt; On the other hand, if data is always passed by value, it &lt;br&gt;
&amp;gt; would obviously reduce the possibilities for computational &lt;br&gt;
&amp;gt; efficiency that people are usually interested in using MEX &lt;br&gt;
&amp;gt; files to obtain.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; In any case, I have several colleagues who believe MEX &lt;br&gt;
&amp;gt; files always receive input by value. Can anyone resolve &lt;br&gt;
&amp;gt; this?&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Variables are never passed by value to m-files or MEX files.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Thu, 24 Jul 2008 12:24:44 -0400</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#445145</link>
      <author>Peter Boettcher</author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguywithaknotac@hotmail.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; &quot;Matt &quot; &amp;lt;mjacobson.removethis@xorantech.com&amp;gt; wrote in&lt;br&gt;
&amp;gt; message &amp;lt;g68b41$cu8$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; I'm trying to understand if there is a difference in the &lt;br&gt;
&amp;gt;&amp;gt; way copy-on-write is implemented for MEX functions as &lt;br&gt;
&amp;gt;&amp;gt; compared to Mfiles.&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; My understanding is as follows. As part of the copy-on-&lt;br&gt;
&amp;gt;&amp;gt; write system, MATLAB will pass input data by reference to &lt;br&gt;
&amp;gt;&amp;gt; an Mfile function if the function performs read-only &lt;br&gt;
&amp;gt;&amp;gt; operations on that data. Otherwise, it will pass the data &lt;br&gt;
&amp;gt;&amp;gt; by value. &lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; No. MATLAB does not pre-parse the m-file to determine if an&lt;br&gt;
&amp;gt; input variable is changed. MATLAB *always* passes the&lt;br&gt;
&amp;gt; variables by reference. Once inside the m-file, if the input&lt;br&gt;
&amp;gt; variable is changed, *then* MATLAB makes a copy. That is the&lt;br&gt;
&amp;gt; copy-on-write behavior.&lt;br&gt;
&lt;br&gt;
To expand on this a little, there is a C-level function in MATLAB that&lt;br&gt;
&quot;unshares&quot; a variable.  Internal functions in MATLAB that write to&lt;br&gt;
variables (subsasgn, etc) call this function before writing to the&lt;br&gt;
memory.  Function parameters are exactly like assignments in the same&lt;br&gt;
workspace.  Nothing happens at function call time except a shared-data&lt;br&gt;
assignment.&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; However, when it comes to MEX functions, the MATLAB interpreter&lt;br&gt;
&amp;gt;&amp;gt; obviously cannot know the contents of the function and cannot&lt;br&gt;
&amp;gt;&amp;gt; determine whether the operations it will perform are read-only or&lt;br&gt;
&amp;gt;&amp;gt; otherwise. If MATLAB wanted to have all the same safety nets as for&lt;br&gt;
&amp;gt;&amp;gt; Mfiles, it would have to always pre-duplicate the input data and send&lt;br&gt;
&amp;gt;&amp;gt; that to the MEX function instead (effectively passing the data by&lt;br&gt;
&amp;gt;&amp;gt; value).&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; One reason for doing it this way would be to ensure that if &lt;br&gt;
&amp;gt;&amp;gt; the MEX file crashes, the input data would remain unaltered &lt;br&gt;
&amp;gt;&amp;gt; in the caller workspace. This will be true for Mfiles, and &lt;br&gt;
&amp;gt;&amp;gt; the MATLAB software designers may have wanted it to be true &lt;br&gt;
&amp;gt;&amp;gt; for MEX files as well.&lt;br&gt;
&amp;gt;&amp;gt; &lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; There is no safety net for MEX files. There is no&lt;br&gt;
&amp;gt; copy-on-write behavior for MEX files. The variables are&lt;br&gt;
&amp;gt; passed by reference to the MEX file, same as an m-file. If&lt;br&gt;
&amp;gt; you change an input variable in a MEX file, then the&lt;br&gt;
&amp;gt; original is changed (as well as all other variables that&lt;br&gt;
&amp;gt; share the same data memory). This can mess up the workspace&lt;br&gt;
&amp;gt; of course, which is why you see warnings to never do this in&lt;br&gt;
&amp;gt; the doc. However, it *is* safe to do if you *know* that the&lt;br&gt;
&amp;gt; data memory is not shared with another variable. This can be&lt;br&gt;
&amp;gt; tricky and is not recommended unless you really need to&lt;br&gt;
&amp;gt; (e.g., very large variables that you don't want duplicated).&lt;br&gt;
&lt;br&gt;
The MEX API clearly defines the input variables as &quot;const&quot;.  You can&lt;br&gt;
defeat this, of course, but you shouldn't unless you're willing to&lt;br&gt;
really dig into what happens at this level, and need that extra 5%&lt;br&gt;
efficiency for some in-place computation.&lt;br&gt;
&lt;br&gt;
-Peter</description>
    </item>
    <item>
      <pubDate>Thu, 24 Jul 2008 15:59:02 -0400</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#445217</link>
      <author>Matt </author>
      <description>&lt;br&gt;
&lt;br&gt;
OK. That does clear it all up!&lt;br&gt;
&lt;br&gt;
Much obliged to you both.</description>
    </item>
    <item>
      <pubDate>Fri, 28 Nov 2008 08:34:04 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#613679</link>
      <author>Ryan Ollos</author>
      <description>Peter Boettcher &amp;lt;boettcher@ll.mit.edu&amp;gt; wrote in message &amp;lt;muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; To expand on this a little, there is a C-level function in MATLAB that&lt;br&gt;
&amp;gt; &quot;unshares&quot; a variable.  Internal functions in MATLAB that write to&lt;br&gt;
&amp;gt; variables (subsasgn, etc) call this function before writing to the&lt;br&gt;
&amp;gt; memory.&lt;br&gt;
&lt;br&gt;
Maybe you can help me clarify how this works.  Suppose the following:&lt;br&gt;
&lt;br&gt;
X = ones(10, 1);&lt;br&gt;
Y = X;&lt;br&gt;
Z= X;&lt;br&gt;
X(1) = 2;&lt;br&gt;
&lt;br&gt;
Based on my understanding and the tests I have done, it seems like the following happens:&lt;br&gt;
&lt;br&gt;
Memory is allocated and X is set to point to address a1.&lt;br&gt;
Y is set to point to a1.&lt;br&gt;
Z is set to point to a1.&lt;br&gt;
A deep copy of the memory pointed by X, Y, and Z is performed (address a2), Y and Z are changed to point to a2, and a new value is assigned to the location pointed to by X(1).&lt;br&gt;
&lt;br&gt;
However, I suppose its possible that the situation is reversed and X points to a2 while Y and Z continue to point to a1.  However, this seems like it would introduce a number of problems with sharing data between the MATLAB and MEX workspaces.&lt;br&gt;
&lt;br&gt;
Can you clarify if the behaviour I have described is correct?&lt;br&gt;
&lt;br&gt;
Thanks!</description>
    </item>
    <item>
      <pubDate>Fri, 28 Nov 2008 09:04:02 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#613682</link>
      <author>Ryan Ollos</author>
      <description>&quot;Ryan Ollos&quot; &amp;lt;ryano@physiosonics.com&amp;gt; wrote in message &amp;lt;ggoads$sqr$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Peter Boettcher &amp;lt;boettcher@ll.mit.edu&amp;gt; wrote in message &amp;lt;muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; To expand on this a little, there is a C-level function in MATLAB that&lt;br&gt;
&amp;gt; &amp;gt; &quot;unshares&quot; a variable.  Internal functions in MATLAB that write to&lt;br&gt;
&amp;gt; &amp;gt; variables (subsasgn, etc) call this function before writing to the&lt;br&gt;
&amp;gt; &amp;gt; memory.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Maybe you can help me clarify how this works.  Suppose the following:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X = ones(10, 1);&lt;br&gt;
&amp;gt; Y = X;&lt;br&gt;
&amp;gt; Z= X;&lt;br&gt;
&amp;gt; X(1) = 2;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Based on my understanding and the tests I have done, it seems like the following happens:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Memory is allocated and X is set to point to address a1.&lt;br&gt;
&amp;gt; Y is set to point to a1.&lt;br&gt;
&amp;gt; Z is set to point to a1.&lt;br&gt;
&amp;gt; A deep copy of the memory pointed by X, Y, and Z is performed (address a2), Y and Z are changed to point to a2, and a new value is assigned to the location pointed to by X(1).&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, I suppose its possible that the situation is reversed and X points to a2 while Y and Z continue to point to a1.  However, this seems like it would introduce a number of problems with sharing data between the MATLAB and MEX workspaces.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can you clarify if the behaviour I have described is correct?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Thanks!&lt;br&gt;
&lt;br&gt;
I found some info in another newsgroup post, where someone noted that you can type 'format debug' and get the memory address location printed to the terminal.&lt;br&gt;
&lt;br&gt;
It seems to be the case for my example that Y and Z continue to point to a1, while X points to a2.  I suppose this is a computational advantage because it changes a fewer number of pointers.</description>
    </item>
    <item>
      <pubDate>Sat, 29 Nov 2008 01:20:19 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#613799</link>
      <author>Jan Simon</author>
      <description>Dear Matt!&lt;br&gt;
&lt;br&gt;
&amp;gt; I'm trying to understand if there is a difference in the &lt;br&gt;
&amp;gt; way copy-on-write is implemented for MEX functions as &lt;br&gt;
&amp;gt; compared to Mfiles.&lt;br&gt;
&lt;br&gt;
Look into the file matrix.h (in &amp;lt;matlabroot&amp;gt;\extern\include\). There you find the code for array_access_inlining, which shows the implementation of a Matlab array.&lt;br&gt;
Besides the obvious fields as pointer to real and imaginary part of the data, number of dimensions and size of an element, you find a bunch of flags and several pointers to void called &quot;reserved&quot;, &quot;reserved1&quot; etc.&lt;br&gt;
It might be easy to find out, which field is the vector keeping the dimensions, the class and the perhaps the name of the array. But a reverse-engineering of the bunch of flags wil be impossible and as far as I know: this might be conflicting with the terms of use.&lt;br&gt;
&lt;br&gt;
Sometimes one really needs passing variables by reference to a MEX file and changing the original memory - i.e. if your data occupy 50% of your available RAM: Then just create a new class, which works with memory persistently stored in a MEX file: see mexMakeMemoryPersistent. Then the object does not contain the data itself, but just a pointer to it and passing by reference is performed implicitly. A drawback is, that the complete processing must be done in MEX functions.&lt;br&gt;
&lt;br&gt;
Kind regards, Jan</description>
    </item>
    <item>
      <pubDate>Sun, 27 Dec 2009 16:25:05 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#704604</link>
      <author>Matt J </author>
      <description>&quot;Ryan Ollos&quot; &amp;lt;ryano@physiosonics.com&amp;gt; wrote in message &amp;lt;ggoc62$8p1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; I found some info in another newsgroup post, where someone noted that you can type 'format debug' and get the memory address location printed to the terminal.&lt;br&gt;
=========&lt;br&gt;
&lt;br&gt;
For some reason, this does not seem to be reflecting copy-on-write behavior. When I do the following, the &quot;format debug&quot; output seems to say that X and XX are pointing to different data locations, even though we know from copy-on-write rules that they should be pointing to the same one:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; format debug&lt;br&gt;
&amp;gt;&amp;gt; X=27&lt;br&gt;
&lt;br&gt;
X =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 25cdc08 &lt;br&gt;
m = 1&lt;br&gt;
n = 1&lt;br&gt;
pr = 193319b0 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; XX=X&lt;br&gt;
&lt;br&gt;
XX =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 2579b98 &lt;br&gt;
m = 1&lt;br&gt;
n = 1&lt;br&gt;
pr = 193319f0 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27</description>
    </item>
    <item>
      <pubDate>Sun, 27 Dec 2009 16:46:02 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#704606</link>
      <author>James Tursa</author>
      <description>&quot;Matt J &quot; &amp;lt;mattjacREMOVE@THISieee.spam&amp;gt; wrote in message &amp;lt;hh81p0$qdk$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &quot;Ryan Ollos&quot; &amp;lt;ryano@physiosonics.com&amp;gt; wrote in message &amp;lt;ggoc62$8p1$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I found some info in another newsgroup post, where someone noted that you can type 'format debug' and get the memory address location printed to the terminal.&lt;br&gt;
&amp;gt; =========&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; For some reason, this does not seem to be reflecting copy-on-write behavior. When I do the following, the &quot;format debug&quot; output seems to say that X and XX are pointing to different data locations, even though we know from copy-on-write rules that they should be pointing to the same one:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; format debug&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; X=27&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; X =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Structure address = 25cdc08 &lt;br&gt;
&amp;gt; m = 1&lt;br&gt;
&amp;gt; n = 1&lt;br&gt;
&amp;gt; pr = 193319b0 &lt;br&gt;
&amp;gt; pi = 0&lt;br&gt;
&amp;gt;     27&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; XX=X&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; XX =&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Structure address = 2579b98 &lt;br&gt;
&amp;gt; m = 1&lt;br&gt;
&amp;gt; n = 1&lt;br&gt;
&amp;gt; pr = 193319f0 &lt;br&gt;
&amp;gt; pi = 0&lt;br&gt;
&amp;gt;     27&lt;br&gt;
&lt;br&gt;
Here is what I get:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; format debug&lt;br&gt;
&amp;gt;&amp;gt; X = 27&lt;br&gt;
X =&lt;br&gt;
Structure address = 2d8d6a0 &lt;br&gt;
m = 1 &lt;br&gt;
n = 1&lt;br&gt;
pr = 1cda1a0 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;br&gt;
&amp;gt;&amp;gt; XX = X&lt;br&gt;
XX =&lt;br&gt;
Structure address = 31b62e0 &lt;br&gt;
m = 1 &lt;br&gt;
n = 1&lt;br&gt;
pr = 1cda1a0 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;br&gt;
&lt;br&gt;
Shared memory. What version of MATLAB are you running? On what machine? Maybe your version does scalars differently (just guessing here). Try this to see what you get:&lt;br&gt;
&lt;br&gt;
format debug&lt;br&gt;
X = rand(3)&lt;br&gt;
XX = X&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
    <item>
      <pubDate>Sun, 27 Dec 2009 16:50:20 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#704608</link>
      <author>Matt J </author>
      <description>Peter Boettcher &amp;lt;boettcher@ll.mit.edu&amp;gt; wrote in message &amp;lt;muyd4l35v1v.fsf@G99-Boettcher.llan.ll.mit.edu&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &amp;gt; No. MATLAB does not pre-parse the m-file to determine if an&lt;br&gt;
&amp;gt; &amp;gt; input variable is changed. MATLAB *always* passes the&lt;br&gt;
&amp;gt; &amp;gt; variables by reference. Once inside the m-file, if the input&lt;br&gt;
&amp;gt; &amp;gt; variable is changed, *then* MATLAB makes a copy. That is the&lt;br&gt;
&amp;gt; &amp;gt; copy-on-write behavior.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; To expand on this a little, there is a C-level function in MATLAB that&lt;br&gt;
&amp;gt; &quot;unshares&quot; a variable.  Internal functions in MATLAB that write to&lt;br&gt;
&amp;gt; variables (subsasgn, etc) call this function before writing to the&lt;br&gt;
&amp;gt; memory.  Function parameters are exactly like assignments in the same&lt;br&gt;
&amp;gt; workspace.  Nothing happens at function call time except a shared-data&lt;br&gt;
&amp;gt; assignment.&lt;br&gt;
&amp;gt; &lt;br&gt;
&lt;br&gt;
Somewhat related to my last post, I'm seeing things that contradict this. As I test, I have the following two very similar functions&lt;br&gt;
&lt;br&gt;
function A=tst(A) %READ-ONLY&lt;br&gt;
&amp;nbsp;&amp;nbsp;A,&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
function A=tst2(A)  %NOT READ-ONLY&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A=A+1;&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
The following output seems to show that the non-read-only version tst2() assigns a different pointer value pr to the input data as soon as it is passed to the workspace of tst2(). This does not occur with the read-only version, suggesting that some sort of pre-parsing for non-read operations does indeed occur...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt;format debug&lt;br&gt;
&amp;gt;&amp;gt; X=27&lt;br&gt;
&lt;br&gt;
X =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 2579578 &lt;br&gt;
m = 1&lt;br&gt;
n = 1&lt;br&gt;
pr = 19332230 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tst(X);&lt;br&gt;
&lt;br&gt;
A =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 25795b0 &lt;br&gt;
m = 1&lt;br&gt;
n = 1&lt;br&gt;
pr = 19332230 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tst2(X);&lt;br&gt;
&lt;br&gt;
A =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 2579658 &lt;br&gt;
m = 1&lt;br&gt;
n = 1&lt;br&gt;
pr = 193338f0 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;27</description>
    </item>
    <item>
      <pubDate>Sun, 27 Dec 2009 17:16:04 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#704610</link>
      <author>Matt J </author>
      <description>&quot;James Tursa&quot; &amp;lt;aclassyguy_with_a_k_not_a_c@hotmail.com&amp;gt; wrote in message &amp;lt;hh830a$e7q$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; Shared memory. What version of MATLAB are you running? On what machine? &lt;br&gt;
========================&lt;br&gt;
&lt;br&gt;
R2009b under Windows XP Pro SP3.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Maybe your version does scalars differently (just guessing here). Try this to see what you get:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; format debug&lt;br&gt;
&amp;gt; X = rand(3)&lt;br&gt;
&amp;gt; XX = X&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt;&amp;gt; X=rand(3), XX=X&lt;br&gt;
================================&lt;br&gt;
&lt;br&gt;
Yep, I am indeed seeing the expected memory sharing for non-scalars. I've also repeated my function pre-parsing experiment below, and it appears that no pre-parsing is done for write operations on a non-scalar. &lt;br&gt;
&lt;br&gt;
It all still seems a little strange though. I can see why maybe memory sharing for scalar objects is counter-productive, but clearly pre-parsing of a function is done for scalar write-operations. Isn't that counter-productive as well? And since MATLAB doesn't know in advance whether a function argument is going to be a scalar or not, it would have to repeat this parsing every time the function is called, no?&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
X =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 2579578 &lt;br&gt;
m = 3&lt;br&gt;
n = 3&lt;br&gt;
pr = 1e9fe150 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3629    0.6046    0.3735&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2629    0.7566    0.1733&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.7640    0.9488    0.4833&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
XX =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 25795e8 &lt;br&gt;
m = 3&lt;br&gt;
n = 3&lt;br&gt;
pr = 1e9fe150 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3629    0.6046    0.3735&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2629    0.7566    0.1733&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.7640    0.9488    0.4833&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tst(X);&lt;br&gt;
&lt;br&gt;
A =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 25796c8 &lt;br&gt;
m = 3&lt;br&gt;
n = 3&lt;br&gt;
pr = 1e9fe150 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3629    0.6046    0.3735&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2629    0.7566    0.1733&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.7640    0.9488    0.4833&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; tst2(X);&lt;br&gt;
&lt;br&gt;
A =&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Structure address = 25795b0 &lt;br&gt;
m = 3&lt;br&gt;
n = 3&lt;br&gt;
pr = 1e9fe150 &lt;br&gt;
pi = 0&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.3629    0.6046    0.3735&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.2629    0.7566    0.1733&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;0.7640    0.9488    0.4833</description>
    </item>
    <item>
      <pubDate>Sun, 27 Dec 2009 19:17:02 -0500</pubDate>
      <title>Re: copy-on-write with MEX files</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/173135#704632</link>
      <author>James Tursa</author>
      <description>&quot;Matt J &quot; &amp;lt;mattjacREMOVE@THISieee.spam&amp;gt; wrote in message &amp;lt;hh84ok$51h$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; It all still seems a little strange though. I can see why maybe memory sharing for scalar objects is counter-productive, but clearly pre-parsing of a function is done for scalar write-operations. Isn't that counter-productive as well? And since MATLAB doesn't know in advance whether a function argument is going to be a scalar or not, it would have to repeat this parsing every time the function is called, no?&lt;br&gt;
&lt;br&gt;
Seems that way. I have replicated your results with R2008b. All m-files are, of course, pre-parsed and various optimizations are performed before it even gets run once. It appears that a deep copy of a scalar is passed in cases where there might be a data change, but deep copies of non-scalars are not made at call time. e.g., try this file:&lt;br&gt;
&lt;br&gt;
function A=tst3(A) %NOT READ-ONLY&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A,&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if( any(A == 29) )&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;A=A+1;&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
In this case a deep copy of a scalar is passed, even though it might not be necessary. But a shared data copy of a non-scalar is passed. This is one of those optimizations that may or may not be consistent from version to version of MATLAB, since TMW is always changing the JIT Accelerator.&lt;br&gt;
&lt;br&gt;
Since the original title of this thread concerned mex files, I thought I would throw in the following. For m-files, a shared data copy is passed to the function (new structure header but same data area) except for the scalar stuff mentioned above. But for mex routines, the actual variable is passed (same structure header). i.e., the prhs pointers point directly at the inputs in a mex routine, not at a shared data copy of the inputs.&lt;br&gt;
&lt;br&gt;
James Tursa</description>
    </item>
  </channel>
</rss>

