Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Temporary output when assigning into slices?

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 23 Jan, 2013 18:49:08

Message: 1 of 11

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

    A(:,1)=B*x;

Is this equivalent to

    z=B*x; %memory allocated here
    A(:,1)=z;

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

 A(1,:)=B(1,:)*x;

is equivalent to

 z=B(1,:); %memory allocated here
 A(1,:)=z*x;

so obviously not everything is as well optimized as it could be.

Subject: Temporary output when assigning into slices?

From: Bruno Luong

Date: 23 Jan, 2013 20:10:08

Message: 2 of 11

"Matt J" wrote in message <kdpbb4$gtp$1@newscl01ah.mathworks.com>...
> I've been curious, after various recent observations,

I must miss something, what are discussions?

 about if/when matrix multiplication allocates fresh memory for its output. For example, suppose I do something like
>
> A(:,1)=B*x;
>
> Is this equivalent to
>
> z=B*x; %memory allocated here
> A(:,1)=z;
>
> Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)?

On recent MATLAB versions (2010a?), MATLAB is smart and puts directly the output in-place.

>Obviously, the latter would be more efficient, but I wasn't sure how it worked. I know for example that this
>
> A(1,:)=B(1,:)*x;
>
> is equivalent to
>
> z=B(1,:); %memory allocated here
> A(1,:)=z*x;
>
> so obviously not everything is as well optimized as it could be.

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.

Bruno

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 23 Jan, 2013 21:05:08

Message: 3 of 11

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpg30$6gl$1@newscl01ah.mathworks.com>...
>
> > A(:,1)=B*x;
> >
> > Is this equivalent to
> >
> > z=B*x; %memory allocated here
> > A(:,1)=z;
> >
> > Or, does the output of B*x get directly generated in the memory locations occupied by A(:,1)?
>
> On recent MATLAB versions (2010a?), MATLAB is smart and puts directly the output in-place.
================

OK. That's strangely inconsistent with things I'm seeing, but so be it.





> 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.
===============

Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why

 A=B(1,idx)*C

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.

Subject: Temporary output when assigning into slices?

From: Bruno Luong

Date: 23 Jan, 2013 21:17:08

Message: 4 of 11

"Matt J" wrote in message <kdpja4$jnk$1@newscl01ah.mathworks.com>...

>
> Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why
>
> A=B(1,idx)*C
>
> 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.

I'm talking about accessing memory of the result, meaning lhs.

Bruno

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 23 Jan, 2013 21:26:08

Message: 5 of 11

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpk0k$mmi$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <kdpja4$jnk$1@newscl01ah.mathworks.com>...
>
> >
> > Don't you mean, that optimization cannot be carried out in the *latter* example? I can see for example why
> >
> > A=B(1,idx)*C
> >
> > 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.
>
> I'm talking about accessing memory of the result, meaning lhs.
=============

If so, why doesn't this apply to both examples? The LHS is the same in these examples.

Subject: Temporary output when assigning into slices?

From: Bruno Luong

Date: 23 Jan, 2013 23:22:08

Message: 6 of 11

"Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...

>
> If so, why doesn't this apply to both examples? The LHS is the same in these examples.

Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 24 Jan, 2013 00:02:08

Message: 7 of 11

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdprb0$ka6$1@newscl01ah.mathworks.com>...
> "Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...
>
> >
> > If so, why doesn't this apply to both examples? The LHS is the same in these examples.
>
> Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....

OK. Well then a 3rd example

 A(:,1)=B*C(:,1);

If I'm not mistaken, this will be equivalent to

 z=C(:,1); %memory allocation

 A(:,1)=B*z;

All subsref operations, AFAIK, result in freshly allocated memory, even though it's not necessary here.

Subject: Temporary output when assigning into slices?

From: Bruno Luong

Date: 24 Jan, 2013 00:15:11

Message: 8 of 11

"Matt J" wrote in message <kdptm0$rtt$1@newscl01ah.mathworks.com>...
> "Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdprb0$ka6$1@newscl01ah.mathworks.com>...
> > "Matt J" wrote in message <kdpkhg$p22$1@newscl01ah.mathworks.com>...
> >
> > >
> > > If so, why doesn't this apply to both examples? The LHS is the same in these examples.
> >
> > Not sure I understand, A(1,:) = ... is not the same as A(:,1) = ....
>
> OK. Well then a 3rd example
>
> A(:,1)=B*C(:,1);
>
> If I'm not mistaken, this will be equivalent to
>
> z=C(:,1); %memory allocation
>
> A(:,1)=B*z;
>

There is no temporal memory allocation according to my test (2012B).

Bruno

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 24 Jan, 2013 16:05:08

Message: 9 of 11

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <kdpuef$118$1@newscl01ah.mathworks.com>...
>
>
> There is no temporal memory allocation according to my test (2012B).
=============

Then that's pretty fancy and new. I definitely see a difference in R2012a:


N=1e7;
M=1;
B=rand(M,N);
C=rand(N,10);


tic;
 B*C(:,1);
toc;
%Elapsed time is 0.043724 seconds.

z=C(:,1);
tic
 B*z;
toc
%Elapsed time is 0.011419 seconds.

Subject: Temporary output when assigning into slices?

From: Matt J

Date: 24 Jan, 2013 17:09:08

Message: 10 of 11

"Matt J" wrote in message <kdrm3k$ecb$1@newscl01ah.mathworks.com>...
>
> Then that's pretty fancy and new. I definitely see a difference in R2012a:
>
>
> N=1e7;
> M=1;
> B=rand(M,N);
> C=rand(N,10);
>
>
> tic;
> B*C(:,1);
> toc;
> %Elapsed time is 0.043724 seconds.
>
> z=C(:,1);
> tic
> B*z;
> toc
> %Elapsed time is 0.011419 seconds.
=====================

In fact, I just tested it with R2012b and I see the same relative performance. So, I still think SUBSREF ops always allocate memory.

Subject: Temporary output when assigning into slices?

From: Bruno Luong

Date: 27 Jan, 2013 08:53:07

Message: 11 of 11

"Matt J" wrote in message <kdrprk$dq$1@newscl01ah.mathworks.com>...
>
> In fact, I just tested it with R2012b and I see the same relative performance. So, I still think SUBSREF ops always allocate memory.

I did some other tests and it looks like you are right.

Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us