From: us on
"us " <us(a)neurol.unizh.ch> wrote in message <i2i3n3$ebb$1(a)fred.mathworks.com>...
> Qianqian Fang <fangqq(a)gmail.com> wrote in message <4b984d05-ba1f-4ef7-97ca-fbce6e7c6ea4(a)q35g2000yqn.googlegroups.com>...
> > hi
> >
> > I want to know if matlab support in-place operations for simple
> > calculations. This particular question raised when I run a vector
> > update with a long index:
> >
> > vol(longidx)=vol(longidx)+weight*val(goodidx);
> >
> > Very often, matlab complains that memory is running out. I need
> > something like "vol+=newadd" as in C/C++, where the memory is updated
> > in-place without creating a temporary variable.
> >
> > does any one know if matlab is capable of doing this?
> >
> > Qianqian
>
> this
> 1) has been discussed over and over in this NG (and TMW blogs)...
> -and-
> 2) is currently not a ML feature...

clarification: i referred to the
v+=x;
syntax, only...
other CSSMers have shown you the smart approach ML takes re memory issues...

sorry for confusion...
us
From: James Tursa on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i2idg7$jgf$1(a)fred.mathworks.com>...
> Qianqian Fang <fangqq(a)gmail.com> wrote in message <4b984d05-ba1f-4ef7-97ca-fbce6e7c6ea4(a)q35g2000yqn.googlegroups.com>...
> > hi
> >
> > I want to know if matlab support in-place operations for simple
> > calculations. This particular question raised when I run a vector
> > update with a long index:
> >
> > vol(longidx)=vol(longidx)+weight*val(goodidx);
> >
> > Very often, matlab complains that memory is running out. I need
> > something like "vol+=newadd" as in C/C++, where the memory is updated
> > in-place without creating a temporary variable.
> ==============
>
> In-place processing happens for operations that make element-wise changes to an existing array, like the one you've shown. You can verify this with some simple experiments like below:
>
> clear V; V=rand(500,500,330); %a big array
>
> >> A=V+1;
> ??? Out of memory. Type HELP MEMORY for your options.
>
> >> V=V+1; %No error

I would point out that his behavior is a somewhat recent change that came in either R2007b or R2008a. Prior to that, MATLAB would not do the operation in place and would potentially generate an out of memory error for this operation. Apparently the newer versions check the size of the rhs and lhs (and presumably the complexity) to see if an in-place operation will work.

James Tursa
From: James Tursa on
"Jan Simon" <matlab.THIS_YEAR(a)nMINUSsimon.de> wrote in message <i2ieph$9ci$1(a)fred.mathworks.com>...
> Dear Qianqian,
>
> > I want to know if matlab support in-place operations for simple
> > calculations.
> > vol(longidx)=vol(longidx)+weight*val(goodidx);
>
> Matlab 2009a, WinXP, 32 bit:
> format debug
> x = rand(1000, 1);
> x(1)
> >> structure address = a820940
> >> pr = bb51b80 (pointer to the real double array)
>
> v = rand(1000, 1) < 0.2;
> x(v) = x(v) + rand(sum(v), 1);
> x(1)
> >> structure address = a820940
> >> pr = bb51b80
>
> So it looks, like Matlab *does* inplace operations even *without* the += operator.

The above is not a fair test of the in-place behavior. When you use the x(v) notation on the lhs you are explicitly telling MATLAB to store the results back into the original x data memory locations. So one would expect the pr address of x to remain the same because you told MATLAB to keep it the same. However, that does not address the question of whether the rhs calculation takes place in separate memory and is then copied back into the x data area, or whether the rhs calculation actually takes place in the x data area itself. To get that answer I think you have to create huge arrays and play memory games to see if one way runs out of memory and another way doesn't.

Also, you appear to be using x(1) to *look* at the address of the x data area. This does not work. The expression x(1) creates a completely new temporary variable with the data of the first original x element copied into a brand new data area. Your two x(1) expressions above match simply because they *happen* to grab the same memory for the temporary variable x(1) expression. To get the address of the data areas of a huge array, using format debug is of course out of the question because you flood the output display with numbers. I typically get these printed out with a simple C-mex function that prints the result of the mxGetPr and mxGetPi calls.

James Tursa
From: Matt J on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2sspg$kck$1(a)fred.mathworks.com>...

> I would point out that his behavior is a somewhat recent change that came in either R2007b or R2008a. Prior to that, MATLAB would not do the operation in place and would potentially generate an out of memory error for this operation. Apparently the newer versions check the size of the rhs and lhs (and presumably the complexity) to see if an in-place operation will work.
=======

I think it just checks whether the operation is element-wise. I remember Loren writing something about that.
From: James Tursa on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <i2sva3$m9$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i2sspg$kck$1(a)fred.mathworks.com>...
>
> > I would point out that his behavior is a somewhat recent change that came in either R2007b or R2008a. Prior to that, MATLAB would not do the operation in place and would potentially generate an out of memory error for this operation. Apparently the newer versions check the size of the rhs and lhs (and presumably the complexity) to see if an in-place operation will work.
> =======
>
> I think it just checks whether the operation is element-wise. I remember Loren writing something about that.

I did check a C = C + A*B operation with A, B, and C all being matrices and it still put the result in-place in C. I also did various permutations of this, like C = 1.2*C + 3.4*A*B etc and it still put the result in-place. The + operation is of course element-wise and maybe that is the key. What I don't know is if the A*B is done off to the side in new memory and then added into C or if a custom call to a BLAS routine does the whole C=C+A*B operation in-place. I will try to find Loren's article. Thanks for the tip.

James Tursa