Is there an example on how to use parallel programming with Parallel Computing Toolbox to do a simple matrix computation?
6 次查看(过去 30 天)
显示 更早的评论
I would like to know how to compute A*B using the Parallel Computing Toolbox 4.0 (R2008b).
采纳的回答
MathWorks Support Team
2009-6-27
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Server Management 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!