fenix2222
fenix2222

Reputation: 4730

Multiplying two large matrices in matlab produces out of memory error

I have two large matrices that I need to multiply:

A x D

where A = 2358048 x 1 B = 1 x 492020

I understand that multiplication requires enourmous amoun of RAM and this is why I get Out of Memory in matlab (I have 90GB of RAM available on the server).

Is there a way to do in in few steps. Maybe break it down some how and save pieces in some files and do multiplication step by step. Then in the end combine it all together? Sample matlab code would be most valuable. Thanks

Upvotes: 2

Views: 2507

Answers (1)

Danica
Danica

Reputation: 28846

The full product is going to be 2358048 x 492020, meaning it has 1,160,206,776,960 elements. If you store those in float32, that's over 4 terabytes of data. Are you sure you need the full matrix? You're certainly not going to load it in RAM, anyway.

Since it's just an outer product of two enormous vectors, though, it's pretty easy to find any given subelement on demand: if C = AB, then C(i, j) = A(i, 1) * B(1, j). Most things that you need to do with the matrix can then probably be done like that, maybe computing blocks of the product as needed on the fly, rather than storing the whole huge thing.

What do you need to use the product for?

If for some reason you do need the entire enormous thing written out to disk, it's pretty easy to either loop over A and write a row at a time to some file (just A(i,1) * B), or over B and write a column at a time (B(1,i) * A).

Upvotes: 8

Related Questions