Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out of memory in Matlab - how to do in-place operation on matrix elements?

I am loading a quite large matrix into Matlab. Loading this matrix already pushes Matlab to its limits - but it fits.

Then I do the following and I get an out-of-memory error.

data( :, 2:2:end, :, : ) = - data( :, 2:2:end, :, : );

Is Matlab allocating a new matrix for this operation? I would assume this operation would not need extra memory. How do I force Matlab to be more efficient for this?

Bonus question:

'data = permute(data,[1 2 3 4 5 12 8 7 6 9 10 11]);'

Can matlab do this in-place?

like image 274
Stiefel Avatar asked Dec 06 '11 13:12

Stiefel


1 Answers

There are a few constraints (further to those from Loren's block cited by John):

  • Your code must be running inside a function
  • You must have no other aliases to 'data'

The 'aliases' thing is both important and potentially hard to get right. MATLAB uses copy-on-write, which means that when you call a function, the argument you pass isn't duplicated immediately, but might be copied if you modify it within the function. For example, consider

x = rand(100);
y = myfcn(x);
% with myfcn.m containing:
function out = myfcn(in)
  in(1) = 3;
  out = in * 2;
end

In that case, the variable x is passed to myfcn. MATLAB has value semantics, so any modifications to the input argument in must not be seen in the calling workspace. So, the first line of myfcn causes the argument in to become a copy of x, rather than simply an alias to it. Consider what happens with try/catch - this can be an in-place killer, because MATLAB has to be able to preserve values if you error out. In the following:

% consider this function
function myouterfcn()
  x = rand(100);
  x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
  arg = -arg;
end

then, that should get in-place optimisation for x in myouterfcn. But the following can't:

% consider this function
function myouterfcn()
  x = rand(100);
  x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
  try
    arg = -arg;
  catch E
    disp( 'Oops.' );
  end
end

Hope some of this info helps...

like image 62
Edric Avatar answered Nov 15 '22 03:11

Edric