フィルターのクリア

Which loop to run in parallel if I have three of them?

3 ビュー (過去 30 日間)
Alex
Alex 2014 年 3 月 17 日
回答済み: Kevin Claytor 2014 年 3 月 17 日
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.

採用された回答

Kevin Claytor
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 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by