3

Is there a relatively easy to implement or transparent way to multiply two large matrices in Matlab in parallel?

Ideally, I would like to perform this parallel multiplication with at most a few lines of code, perhaps something like:

    C_1 = A*B        % normal
    C_2 = pmult(A,B) % parallel
    % C_1 and C_2 have the same entries

If there is a way to easily do this paralell multiplication, can someone please point me to the code? If not, does anyone have any ideas on what they feel is the best way to implement a parallel matrix multiplication algorithm in Matlab?

Thanks in advance, awesome Stackoverflow community.

EDIT -- I believe that part of the issue I was running into is that matrix multiplication for sparse matrices is not automatically parallelized; it is automatically parallelized for dense matrices. New question: can Matlab do sparse matrix multiplication in parallel? (CPU parallelization as I don't have CUDA enabled graphics cards)

Malcolm
  • 2,394
  • 1
  • 23
  • 26

2 Answers2

7

Matlab probably already does this via its implicit multithreading support. See http://www.mathworks.com/support/solutions/en/data/1-4PG4AN/?solution=1-4PG4AN; the "*" operator. The trivially parallelizable operations are already done for you by Matlab; just run it on a multicore machine.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
  • Interesting. I could have sworn earlier that my multiplications were not parallel. Initially, I saw that using matlabpool open local parallelized the multiplication. However, after revisiting simple *, I have seen that this also results in a parallel matrix multiply. Thank you for the help. – Malcolm Nov 30 '11 at 17:32
  • 1
    I believe that Matlab does not automatically parallelize multiplication between sparse matrices. Do you know how to to make Matlab perform sparse matrix multiplication in parallel? Or am I barking up the wrong tree? – Malcolm Nov 30 '11 at 20:54
4

What do you mean by parallel? These two approaches use explicit parallelism, and both require Parallel Computing Toolbox (the second also requires a capable GPU).

Option 1: MPI parallelism

matlabpool open local
D = distributed.rand(2000); % distributed across workers of matlabpool
R = D * D; % mtimes overloaded to compute in parallel

Option 2: GPU parallelism

G = gpuArray(rand(2000)); % place data on the GPU
G2 = G * G; % operate on it in parallel
Edric
  • 23,676
  • 2
  • 38
  • 40
  • Thank you. I ended up getting what I needed using matlabpool open local. Interestingly enough, distributed.rand doesn't work in my Matlab (R2008a). – Malcolm Nov 30 '11 at 17:35
  • I believe that Matlab does not automatically parallelize multiplication between sparse matrices. Do you know how to to make Matlab perform sparse matrix multiplication in parallel? Or am I barking up the wrong tree? – Malcolm Nov 30 '11 at 20:54