sparse function is intrisically slow?
Show older comments
I am building the sparse matrix using the function sparse(I,J,V,N,N)
I, J and V are already a column vector. I checked previous questions, it seems already being the best way. But, it takes extreamely long time if the size goes to 1E5.
Any way out? Thanks!
14 Comments
Bjorn Gustavsson
on 7 May 2020
Is N 1e5 or the number of non-zero elements 1e5?
Jiangtao Lu
on 7 May 2020
Matt J
on 7 May 2020
I suggest you attach I,J,V in a .mat file so we can test it.
Bjorn Gustavsson
on 7 May 2020
I get nothing obviously "strange" when running this snippet:
Nall = 10.^[1:8];
for i1 = 1:numel(Nall),
iRows = randi(Nall(i1),[Nall(i1),1]);
iCols = randi(Nall(i1),[Nall(i1),1]);
Vals = randn([Nall(i1),1]);
tic,M = sparse(iRows,iCols,Vals,Nall(i1),Nall(i1));toc
end
Elapsed time is 0.014283 seconds.
Elapsed time is 0.000035 seconds.
Elapsed time is 0.000135 seconds.
Elapsed time is 0.001263 seconds.
Elapsed time is 0.014471 seconds.
Elapsed time is 0.167366 seconds.
Elapsed time is 1.779908 seconds.
Error using randn
Out of memory. Type HELP MEMORY for your options.
The time increases reasonably linearly with N until I run out of memory. Transposing the inputs doesn't change timings much either. (I know that the timing is only order-of-magnitude, I know that the indices generated are not necessarily unique).
1e5 is nothing in terms of nnz elements.
Jiangtao Lu
on 11 May 2020
Walter Roberson
on 11 May 2020
Is there any faster way for sparse creation?
NO
Question: are you assigning into the matrix after you build it with sparse? If you are, then be sure to use the sixth parameter to sparse() to tell it the maximum number of non-zero entries there will be.
Jiangtao Lu
on 11 May 2020
Edited: Jiangtao Lu
on 11 May 2020
Walter Roberson
on 11 May 2020
I am not sure at the moment for large vectors whether it would be faster to sparse() a new matrix into existence or to update all of the entries using linear indexing.
James Tursa
on 11 May 2020
Edited: James Tursa
on 11 May 2020
For the newly created sparse matrix at each iteration, how are you generating the values? Are they in a full double vector that you use with the same indexing to do the rebuild? What version of MATLAB are you using? There may be a very fast unnoficial mex solution involving modifying the values in place if you are desperate for speed, but it is a bit tricky to do without clobbering another shared variable. How do you use this sparse matrix downstream in your code? Any chance there are shared data copies (e.g., from A = your_sparse_matrix) or reference copies (e.g., from C{1} = your_sparse_matrix) of it created? How many iterations are we talking about? Do all of the values of the sparse matrix change each iteration, or only some of them?
Jiangtao Lu
on 12 May 2020
James Tursa
on 12 May 2020
Edited: James Tursa
on 12 May 2020
"... not generated using the same index for sparse creation ..."
"... but positions are not. ..."
These two statements seem to say the opposite. Is the sparse pattern the same for each iteration or not? First sentence seems to say they are not the same, but second sentence seems to say they are the same. Which is it?
Jiangtao Lu
on 12 May 2020
Edited: Jiangtao Lu
on 12 May 2020
James Tursa
on 12 May 2020
OK. Are the values in the double vector stored in the same memory order as the sparse matrix columns? Can you give a small example of your indexing and values vector? I'm asking all of these questions to see if a mex routine could work for you.
Jiangtao Lu
on 12 May 2020
Edited: Jiangtao Lu
on 12 May 2020
Answers (1)
Robert
on 24 Dec 2020
0 votes
I was able to speed my sparse matrix build for very large problems by using sparse2.m (mexFunction) from SuiteSparse.
1 Comment
Yingzhi Liu
on 19 Mar 2022
I downloaded SuiteSparse, but I don't konw how sould I do to use sparse2.m in matlab. Can you help me list the steps after downloaded SuiteSparse? Thanks.
Categories
Find more on Matrix Indexing 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!