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

I would like to know how to compute A*B using the Parallel Computing Toolbox 4.0 (R2008b).

 채택된 답변

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}

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Server Management에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by