Is there an example on how to use parallel programming with Parallel Computing Toolbox to do a simple matrix computation?
3 views (last 30 days)
Show older comments
I would like to know how to compute A*B using the Parallel Computing Toolbox 4.0 (R2008b).
Accepted Answer
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}
0 Comments
More Answers (0)
See Also
Categories
Find more on Server Management in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!