Out of memory when calculating very large Matrix (~100000 , 200000)
6 views (last 30 days)
Show older comments
I am trying calculate a very large matrix using a "parfor" loop, but am running out of memory while calculating it. The matrix is sparse, and I write to it column by column. I have looked at the suggestions in the matlab help to reduce memory usage and have implemented as many as I can. Are there any good tricks to reduce memory usage when using the parfor loop? (I use it after running matlabpool in the local configuration, it connects to all 4 cores on my processor)
Update: I tried Tim Davis' suggestion, but now am having issues with the parfor loop not liking my indexing, so I had to use 3 sliced variables instead of just 1...made it a tiny bit faster but I think it is using even more memory now.
Before Tim's suggestion:
%I initialize all of my variables
After Tim's suggestion
row(1,length(projAngles)*3- ((length(projAngles)-(k-1))*3)) = uint16(k*bins-(bins-l));
col(1,length(projAngles)*3-((length(projAngles)-(k- 1))*3)) = uint16(index);
I don't think I did this right...Any suggestions on how to get rid of these three variables and still be able to run this in a parfor loop?
Tim Davis on 2 Nov 2011
Don't attempt to update a sparse matrix that way. The best solution is to create a triplet form, which is an unordered list of row indices, column indices, and values (3 vectors I,J,X where the kth entry in the list is a(I(k),J(k))==X(k)). Then use 'sparse' to convert the triplet form to a sparse matrix all at once, via A = sparse (I,J,X).
More Answers (1)
Naz on 2 Nov 2011
Every time you add a new raw or column to your matrix, the program uses new memory because your old data + new data does not fit in the previous memory slot. You need to preallocate the memory for you matrix. Let's say that your final matrix zise will be 10000x200000, then you need to do this:
In the loop you can access the needed column this way:
Jan on 2 Nov 2011
Can you add the code to your original message? Without seeing the code, we can just guess. And if a guessed solution does not match your problem, as Naz' idea of pre-allocation, it is a waste of time.
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!