Execute several statements in parallel without parfor
8 次查看(过去 30 天)
显示 更早的评论
Hi All!
I can't seem to find a way to tell Matlab to execute multiple lines of code in parallel that I don't necessarily want to put into a parfor loop. Is there a way to do this? I did look around to try to find this, but maybe I'm just not searching with the right keywords...
I do have the parallel computing toolbox by the way.
Thanks! -Mike
Here's a code example:
% setup variables
% Now want to execute the following lines in parallel:
% Fresnel sine and cosine integrals
ca1 = mfun('FresnelC', alpha1);
sa1 = mfun('FresnelS', alpha1);
ca2 = mfun('FresnelC', alpha2);
sa2 = mfun('FresnelS', alpha2);
cb1 = mfun('FresnelC', beta1);
sb1 = mfun('FresnelS', beta1);
cb2 = mfun('FresnelC', beta2);
sb2 = mfun('FresnelS', beta2);
% Now end parallel execution, and use computed values in other calculations
0 个评论
回答(2 个)
Yoav Livneh
2011-5-19
You can perform several tasks in parallel using spmd (Single Program Multiple Data). For example:
% Gather all inputs into one variable
Input = [alpha1; alpha1; alpha2; alpha2; beta1; beta1; beta2; beta2];
Method = {'FresnelC'; FresnelS'; 'FresnelC'; FresnelS'; 'FresnelC'; FresnelS'; 'FresnelC'; FresnelS'};
% Open matlabpool. Requires 8 workers
matlabpool open 8
% Perform all 8 calculation together
spmd
%%%mfun cannot be a nested function, has to be an m file %%%
Results = mfun(Method{labindex}, Input(labindex));
end
% Export results. Assume mun output is a scalar
Results = [Results{:}];
matlabpool close
spmd has some limitations, you should read the help file on it. The main idea is that you run your code on every wirker once. So if you have less than 8 workers you won't be able to run all 8 lines at the same time. Try experimenting with it, to see how it works. Good luck!
0 个评论
Konrad Malkowski
2011-5-27
Another options of executing code in parallel, without using parfor is to use the distributed jobs API.
sched = findResource('scheduler','type','local');
job = createJob(sched);
input = [alpha1; alpha1; alpha2; alpha2; beta1; beta1; beta2; beta2];
method = {'FresnelC'; 'FresnelS'; 'FresnelC'; 'FresnelS'; 'FresnelC'; 'FresnelS'; 'FresnelC'; 'FresnelS'};
for index = 1:numel(input)
task = createTask(job, @mfun, 1, {input(index), method{index}})
end
submit(job)
wait(job)
answers = job.getAllOutputArguments()
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Chebyshev 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!