http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013
MATLAB Central Newsreader  Temporary output when assigning into slices?
Feed for thread: Temporary output when assigning into slices?
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Wed, 23 Jan 2013 18:49:08 +0000
Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896128
Matt J
I've been curious, after various recent observations, about if/when matrix multiplication allocates fresh memory for its output. For example, suppose I do something like<br>
<br>
A(:,1)=B*x;<br>
<br>
Is this equivalent to<br>
<br>
z=B*x; %memory allocated here<br>
A(:,1)=z;<br>
<br>
Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)? Obviously, the latter would be more efficient, but I wasn't sure how it worked. I know for example that this<br>
<br>
A(1,:)=B(1,:)*x;<br>
<br>
is equivalent to<br>
<br>
z=B(1,:); %memory allocated here<br>
A(1,:)=z*x;<br>
<br>
so obviously not everything is as well optimized as it could be.

Wed, 23 Jan 2013 20:10:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896137
Bruno Luong
"Matt J" wrote in message <kdpbb4$gtp$1@newscl01ah.mathworks.com>...<br>
> I've been curious, after various recent observations,<br>
<br>
I must miss something, what are discussions?<br>
<br>
about if/when matrix multiplication allocates fresh memory for its output. For example, suppose I do something like<br>
> <br>
> A(:,1)=B*x;<br>
> <br>
> Is this equivalent to<br>
> <br>
> z=B*x; %memory allocated here<br>
> A(:,1)=z;<br>
> <br>
> Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)?<br>
<br>
On recent MATLAB versions (2010a?), MATLAB is smart and puts directly the output inplace.<br>
<br>
>Obviously, the latter would be more efficient, but I wasn't sure how it worked. I know for example that this<br>
> <br>
> A(1,:)=B(1,:)*x;<br>
> <br>
> is equivalent to<br>
> <br>
> z=B(1,:); %memory allocated here<br>
> A(1,:)=z*x;<br>
> <br>
> so obviously not everything is as well optimized as it could be.<br>
<br>
If the resulting is not in a contiguous memory, similar optimization on memory space will slow down the speed. I suspect that is the main reason why the optimization cannot be carried out in the later example.<br>
<br>
Bruno

Wed, 23 Jan 2013 21:05:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896148
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpg30$6gl$1@newscl01ah.mathworks.com>...<br>
><br>
> > A(:,1)=B*x;<br>
> > <br>
> > Is this equivalent to<br>
> > <br>
> > z=B*x; %memory allocated here<br>
> > A(:,1)=z;<br>
> > <br>
> > Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)?<br>
> <br>
> On recent MATLAB versions (2010a?), MATLAB is smart and puts directly the output inplace.<br>
================<br>
<br>
OK. That's strangely inconsistent with things I'm seeing, but so be it.<br>
<br>
<br>
<br>
<br>
<br>
> If the resulting is not in a contiguous memory, similar optimization on memory space will slow down the speed. I suspect that is the main reason why the optimization cannot be carried out in the later example.<br>
===============<br>
<br>
Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why <br>
<br>
A=B(1,idx)*C <br>
<br>
might have to extract B(1,idx) to a continguous memory block when C is a matrix. In that case the elements in B(1,idx) will be accessed many times.

Wed, 23 Jan 2013 21:17:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896149
Bruno Luong
"Matt J" wrote in message <kdpja4$jnk$1@newscl01ah.mathworks.com>...<br>
<br>
> <br>
> Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why <br>
> <br>
> A=B(1,idx)*C <br>
> <br>
> might have to extract B(1,idx) to a continguous memory block when C is a matrix. In that case the elements in B(1,idx) will be accessed many times.<br>
<br>
I'm talking about accessing memory of the result, meaning lhs.<br>
<br>
Bruno

Wed, 23 Jan 2013 21:26:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896152
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpk0k$mmi$1@newscl01ah.mathworks.com>...<br>
> "Matt J" wrote in message <kdpja4$jnk$1@newscl01ah.mathworks.com>...<br>
> <br>
> > <br>
> > Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why <br>
> > <br>
> > A=B(1,idx)*C <br>
> > <br>
> > might have to extract B(1,idx) to a continguous memory block when C is a matrix. In that case the elements in B(1,idx) will be accessed many times.<br>
> <br>
> I'm talking about accessing memory of the result, meaning lhs.<br>
=============<br>
<br>
If so, why doesn't this apply to both examples? The LHS is the same in these examples.

Wed, 23 Jan 2013 23:22:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896159
Bruno Luong
"Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...<br>
<br>
> <br>
> If so, why doesn't this apply to both examples? The LHS is the same in these examples.<br>
<br>
Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....

Thu, 24 Jan 2013 00:02:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896165
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdprb0$ka6$1@newscl01ah.mathworks.com>...<br>
> "Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...<br>
> <br>
> > <br>
> > If so, why doesn't this apply to both examples? The LHS is the same in these examples.<br>
> <br>
> Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....<br>
<br>
OK. Well then a 3rd example<br>
<br>
A(:,1)=B*C(:,1);<br>
<br>
If I'm not mistaken, this will be equivalent to <br>
<br>
z=C(:,1); %memory allocation<br>
<br>
A(:,1)=B*z;<br>
<br>
All subsref operations, AFAIK, result in freshly allocated memory, even though it's not necessary here.

Thu, 24 Jan 2013 00:15:11 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896167
Bruno Luong
"Matt J" wrote in message <kdptm0$rtt$1@newscl01ah.mathworks.com>...<br>
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdprb0$ka6$1@newscl01ah.mathworks.com>...<br>
> > "Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...<br>
> > <br>
> > > <br>
> > > If so, why doesn't this apply to both examples? The LHS is the same in these examples.<br>
> > <br>
> > Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....<br>
> <br>
> OK. Well then a 3rd example<br>
> <br>
> A(:,1)=B*C(:,1);<br>
> <br>
> If I'm not mistaken, this will be equivalent to <br>
> <br>
> z=C(:,1); %memory allocation<br>
> <br>
> A(:,1)=B*z;<br>
> <br>
<br>
There is no temporal memory allocation according to my test (2012B).<br>
<br>
Bruno

Thu, 24 Jan 2013 16:05:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896208
Matt J
"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpuef$118$1@newscl01ah.mathworks.com>...<br>
><br>
> <br>
> There is no temporal memory allocation according to my test (2012B).<br>
=============<br>
<br>
Then that's pretty fancy and new. I definitely see a difference in R2012a:<br>
<br>
<br>
N=1e7;<br>
M=1;<br>
B=rand(M,N);<br>
C=rand(N,10);<br>
<br>
<br>
tic;<br>
B*C(:,1);<br>
toc;<br>
%Elapsed time is 0.043724 seconds.<br>
<br>
z=C(:,1);<br>
tic<br>
B*z;<br>
toc<br>
%Elapsed time is 0.011419 seconds.

Thu, 24 Jan 2013 17:09:08 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896214
Matt J
"Matt J" wrote in message <kdrm3k$ecb$1@newscl01ah.mathworks.com>...<br>
><br>
> Then that's pretty fancy and new. I definitely see a difference in R2012a:<br>
> <br>
> <br>
> N=1e7;<br>
> M=1;<br>
> B=rand(M,N);<br>
> C=rand(N,10);<br>
> <br>
> <br>
> tic;<br>
> B*C(:,1);<br>
> toc;<br>
> %Elapsed time is 0.043724 seconds.<br>
> <br>
> z=C(:,1);<br>
> tic<br>
> B*z;<br>
> toc<br>
> %Elapsed time is 0.011419 seconds.<br>
=====================<br>
<br>
In fact, I just tested it with R2012b and I see the same relative performance. So, I still think SUBSREF ops always allocate memory.

Sun, 27 Jan 2013 08:53:07 +0000
Re: Temporary output when assigning into slices?
http://www.mathworks.com/matlabcentral/newsreader/view_thread/326013#896381
Bruno Luong
"Matt J" wrote in message <kdrprk$dq$1@newscl01ah.mathworks.com>...<br>
> <br>
> In fact, I just tested it with R2012b and I see the same relative performance. So, I still think SUBSREF ops always allocate memory.<br>
<br>
I did some other tests and it looks like you are right.<br>
<br>
Bruno