开发者

Matlab - Assignment into array causes error: "Maximum variable size allowed by the program is exceeded"

I开发者_StackOverflow am running a script which creates a lot of big arrays. Everything runs fine until the following lines:

%dist is a sparse matrix
inds=dist~=0;
inserts=exp(-dist(inds).^2/2*sig_dist);
dist(inds)=inserts;

The last line causes the error: ??? Maximum variable size allowed by the program is exceeded.

I don't understand how could the last line increase the variable size - notice I am inserting into the matrix dist only in places which were non-zero to begin with. So what's happening here?


I'm not sure why you are seeing that error. However, I suggest you use the Matlab function spfun to apply a function to the nonzero elements in a sparse matrix. For example:

>>dist = sprand(10000,20000,0.001);
>>f = @(x) exp(-x.^2/2*sig_dist);
>>dist = spfun(f,dist)


MATLAB implements a "lazy copy-on-write" model. Let me explain with an example.

First, create a really large vector

x = ones(5*1e7,1);

Now, say we wanted to create another vector of the same size:

y = ones(5*1e7,1);

On my machine, this will fail with the following error

??? Out of memory. Type HELP MEMORY for your options.

We know that y will require 5*1e7*8 = 400000000 bytes ~ 381.47 MB (which is also confirmed by which x), but if we check the amount of free contiguous-memory left:

>> memory
Maximum possible array:             242 MB (2.540e+008 bytes) *
Memory available for all arrays:    965 MB (1.012e+009 bytes) **
Memory used by MATLAB:              820 MB (8.596e+008 bytes)
Physical Memory (RAM):             3070 MB (3.219e+009 bytes)

*  Limited by contiguous virtual address space available.
** Limited by virtual address space available.

we can see that it exceeds the 242 MB available.

On the other hand, if you assign:

y = x;

it will succeed almost instantly. This is because MATLAB is not actually allocating another memory chunk of the same size as x, instead it creates a variable y that shares the same underlying data as x (in fact, if you call memory again, you will see almost no difference).

MATLAB will only try to make another copy of the data once one of the variables changes, thus if you try this rather innocent assignment statement:

y(1) = 99;

it will throw an error, complaining that it ran out of memory, which I suspect is what is happening in your case...


EDIT:

I was able to reproduce the problem with the following example:

%# a large enough sparse matrix (you may need to adjust the size)
dist = sparse(1:3000000,1:3000000,1);

First, lets check the memory status:

» whos
  Name            Size                    Bytes  Class     Attributes

  dist      3000000x3000000            48000004  double    sparse    

» memory
Maximum possible array:             394 MB (4.132e+008 bytes) *
Memory available for all arrays:   1468 MB (1.539e+009 bytes) **
Memory used by MATLAB:              328 MB (3.440e+008 bytes)
Physical Memory (RAM):             3070 MB (3.219e+009 bytes)

*  Limited by contiguous virtual address space available.
** Limited by virtual address space available.

say we want to apply a function to all non-zeros elements:

f = @(X) exp(-X.^2 ./ 2);

strange enough, if you try to slice/assign then it will fail:

» dist(dist~=0) = f( dist(dist~=0) );
??? Maximum variable size allowed by the program is exceeded.

However the following assignment does not throw an error:

[r,c,val] = find(dist);
dist = sparse(r, c, f(val));

I still don't have an explanation why the error is thrown in the first case, but maybe using the FIND function this way will solve your problem...


In general, reassigning non-sparse elements does not change the memory footprint of the matrix. Call whos before and after assignment to check.

dist = sparse(10, 10);
dist(1,1) = 99;
dist(6,7) = exp(1);
inds = dist ~= 0;
whos
dist(inds) = 1;
whos

Without a reproducible example it is hard to determine the cause of the problem. It may be that some intermediate assignment is taking place that isn't sparse. Or you have something specific to your problem that we can't see here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜