Multithreaded sparse matrix multiplication?

5 ビュー (過去 30 日間)
Thomas
Thomas 2014 年 8 月 15 日
コメント済み: Tri Nguyen 2021 年 4 月 29 日
Dear community,
I am performing several (thousands) matrix multiplications of an NxN sparse (~1-2%) matrix, let's call it B, with an NxM dense matrix, let's call it A (where M<N). N is large, as is M; on the order of several thousands.
Now, usually, matrix multiplications and most other matrix operations are implicitly parallelized in Matlab, i.e. they make use of multiple threads automatically. This appears NOT to be the case if either of the matrices are sparse (see e.g. this StackOverflow discussion and this largely unanswered MathWorks thread). This is a rather unhappy surprise to me. We can verify this by the following code:
clc; clear all;
N = 5000; % set matrix sizes
M = 3000;
A = randn(N,M); % create dense random matrices
B = sprand(N,N,0.015); % create sparse random matrix
Bf = full(B); %create a dense form of the otherwise sparse matrix B
for i=1:3 % test for 1, 2, and 4 threads
m(i) = 2^(i-1);
maxNumCompThreads(m(i)); % set the thread count available to Matlab
tic % starts timer
y = B*A;
walltime(i) = toc; % wall clock time
speedup(i) = walltime(1)/walltime(i);
end
% display number of threads vs. speed up relative to just a single thread
[m',speedup']
This produces the following output, which illustrates that there is no difference between using 1, 2, and 4 threads:
threads speedup
1.0000 1.0000
2.0000 0.9950
4.0000 1.0155
If, on the other hand, I replace B by its dense form, refered to as Bf above, I get significant speedup:
threads speedup
1.0000 1.0000
2.0000 1.8894
4.0000 3.4841
So, my question: is there any way at all to access a parallelized/threaded version of matrix operations for sparse matrices without converting them to dense form? Alternatively, is this something one might expect would be implemented in future versions of Matlab? I found some suggestion involving .mex files here, but it seems the links are dead and not very well documented/no feedback?
It seems to be a rather severe restriction of implicit parallelism functionality, since sparse matrices are abound in computationally heavy problems, and hyperthreaded functionality highly desirable in these cases.
I appreciate anyone's consideration of the this issue at any level - thanks a bunch in advance! -Thomas
  2 件のコメント
Matt J
Matt J 2014 年 8 月 15 日
Alternatively, is this something one might expect would be implemented in future versions of Matlab?
Future relative to what? What version are you using for these tests?
Thomas
Thomas 2014 年 8 月 15 日
Hi Matt,
Sorry I forgot to include this in my original question; I am using Matlab 2013a.
-Thomas

サインインしてコメントする。

回答 (2 件)

Eric Sampson
Eric Sampson 2014 年 8 月 15 日
I believe the sparse matrix code is implemented by a few specialized TMW engineers rather than an external library like BLAS/LAPACK/LINPACK/etc. So it will be quite a bit of work for them, but you never know... Send them flowers, candy, etc!! :)
  1 件のコメント
Thomas
Thomas 2014 年 8 月 20 日
That is rather sad news. Quite a gap in a 'matrix laboratory' :(.
I don't suppose there are any other external libraries that could be matched up with Matlab to take care of this?
Thanks for providing your insight into this matter.

サインインしてコメントする。


Carlo Monjaraz
Carlo Monjaraz 2018 年 8 月 23 日
In version 2018a, Sparse matrix operations still seem single threaded...
  1 件のコメント
Tri Nguyen
Tri Nguyen 2021 年 4 月 29 日
Still single thread in 2021 :(

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by