Is there an example on how to use parallel programming with Parallel Computing Toolbox to do a simple matrix computation?

2 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
This example computes A*B over four workers using the local scheduler. Note that in this simple example the communication outweighs the actual computation, so it is not a useful speed benchmark.
The code for the task is as follows
function out = multtrans(x,y)
% Codistribute the input matrices
A = codistributed(x,'convert');
B = codistributed(y,'convert');
% Multiply local parts
C = A * B;
% Gather the result on lab 1
out = gather(C,1);
end
The code that is run on the client is
% Declare the two matrices on the client. The matrix must preserve innner product convention: 'm x n' matrix can multiply only with 'n x p'
X = [1 2;3 4;5 6];
Y = [7 8 9;10 11 12];
% Pass sections to workers
jm = findresource('scheduler', 'type', 'local');
pjob = createParallelJob(jm);
tsk = createTask(pjob, @multtrans, 1, {X,Y});
pjob.MaximumNumberOfWorkers=4;
pjob.MinimumNumberOfWorkers=4;
set(pjob, 'FileDependencies', {'multtrans.m'});
submit(pjob);
waitForState(pjob);
result = getAllOutputArguments(pjob);
destroy(pjob);
To access the result, execute the following on the client
result{1}

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!