Which loop to run in parallel if I have three of them?
1 次查看(过去 30 天)
显示 更早的评论
Assuming I have a code like this:
for ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
I have parallel programming toolbox and want to run the code on all processors available. Which loop should I change to parfor? Or maybe all three?
Please, give a detailed explanation.
0 个评论
采纳的回答
Kevin Claytor
2014-3-17
It's going to depend.
Specifically, where is the heavy lifting done? If you have code that runs fast on a single CPU, it may be anti-productive to wrap that in a parfor loop as you'll have to distribute and re-gather the data every time.
I would suggest coming up with a test case and using tic and toc to time how long it takes for parfor in each of the cases.
tic;
parfor ii=1:51
do something
for jj=1:100
do something
for kk=1:200
do something
end
end
end
a = toc;
tic;
for ii=1:51
do something
parfor jj=1:100
do something
for kk=1:200
do something
end
end
end
b = toc;
tic;
for ii=1:51
do something
for jj=1:100
do something
parfor kk=1:200
do something
end
end
end
c = toc;
fprintf('parfor in 1st loop: %ds\nparfor in 2nd loop: %ds\nparfor in 3rd loop: %ds\n',a,b,c);
Also have a look at the matlab profiler to see which functions take longer to run, and where you should be spending your time optimizing your code.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!