Parallel Matrix Multiplication

조회 수: 19 (최근 30일)
Mohammad
Mohammad 2015년 5월 20일
편집: Joel Lynch 2023년 5월 14일
I am looking for a short tutorial/example explaining how we do matrix multiplication in parallel. Note that, the size of matrix is currently 200 * 200 (40000 elements). Thanks.

채택된 답변

James Tursa
James Tursa 2015년 5월 20일
The best way to do matrix multiply in MATLAB is to use the * operator, as you normally would. This will call highly optimized BLAS routines that have parallel algorithms in the background as appropriate. If you have sparse matrices, the * operator will call specialized sparse matrix multiply routines. You will be very hard pressed to do better on your own.
  댓글 수: 4
James Tursa
James Tursa 2015년 5월 22일
What is it exactly that you want demonstrated? That * is multi-threaded? How to do sparse matrix operations? Or what?
E.g., here is a matrix multiply on a quad core system:
>> a = rand(5000);
>> b = rand(5000);
>> tic;a*b;toc
Elapsed time is 2.307906 seconds.
Here is the same matrix multiply on the same quad core system when starting MATLAB with the -singleCompThread option:
>> a = rand(5000);
>> b = rand(5000);
>> tic;a*b;toc
Elapsed time is 9.796487 seconds.
So the matrix multiply operation * for full matrices is obviously multi-threaded in the background.
And here is a demonstration of taking advantage of sparse matrix multiply:
>> a(a>.01) = 0;
>> sa = sparse(a);
>> b(b>.01) = 0;
>> sb = sparse(b);
>> tic;a*b;toc
Elapsed time is 2.252949 seconds.
>> tic;sa*sb;toc
Elapsed time is 0.433186 seconds.
Joel Lynch
Joel Lynch 2023년 5월 14일
편집: Joel Lynch 2023년 5월 14일
"If you have sparse matrices, the * operator will call specialized sparse matrix multiply routines. You will be very hard pressed to do better on your own."
Unfortunately, this statement is highly misleading, if not incorrect. At least as of R2023, sparse matrix multiplication on CPU's are limited to a single thread. James's tests don't show this, but it's easy enough to see:
N = 20000;
density = 0.2;
A = sprand(N,N, density);
b = rand(N, 1);
Nmax_threads = maxNumCompThreads('automatic')
Nmax_threads = 2
timeit(@() A*b)
ans = 0.0510
maxNumCompThreads(1);
timeit(@() A*b)
ans = 0.0506
It's incredibly suprising, as MKL has supported multithreaded sparse matrix math for quite a while. Until this gets updated, the best best for a large sparse matrix problem is to use gpuArray, even a commercial card can do significantly better.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by