(par)for k=1:N end; select for or parfor loop without code replication
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I like to use for-loop or parfor-loop with function input flag. One way to do that is using if-else with code replication. I wonder is there a cleverer way?
% solution based on code replication
function foo(parflag)
if parflg==true
parfor k=1:3
% processing codes
end
else
for k=1:3
% copy of processing codes
end
end
end
%% wondering how to make this idea work?
function hoo(parflag)
if parflag==true
forString = str2func('parfor')
else
forString = str2func('for')
end
forString k=1:3
% processing codes
end
end
採用された回答
You can avoid replicating the loop code as follows.
numWorkers={}; % or whatever
if ~parflag
numWorkers={0};
end
parfor (k=1:3, numWorkers{:})
% processing codes
end
Be mindful, though, that parfor loops have stronger requirements on the code structure, and you would be limiting yourself to that, even when running serially.
13 件のコメント
Bruno Luong
2023 年 8 月 23 日
編集済み: Bruno Luong
2023 年 8 月 23 日
does it start the parallel pool, etc... even for simple for-loop?
No, it doesn't.
That would work. Parfor with zero worket would be a bit slower than for loop, but it won't affect much. I think this is an acceptable answer.
Parfor with zero worket would be a bit slower than for loop
Same question for to parfeval? Can I set something for mypool so that it runs without parallel pool?
mypool = backgroundPool;
Future(1) = parfeval(mypool,@(x)sin(x), 1, pi);
% ...
Same question for to parfeval
That I don't know, but it's less of a problem, because parfeval has a functional form, unlike parfor. You could therefore make a wrapper for parfeval that will handle the pool-free case.
To add on to Matt's great comments, you can pass an empty pool flag to parfeval and it will run in serial in the background. Here is an example of what I mean:
delete(gcp); % Ensure no pool is open
p = gcp('nocreate');
% No pool exists, so p is empty
for i = 1:3
f(i) = parfeval(p,@pause, 0);
end
p = parpool("Threads");
% p exists and will be used to run parfeval
for i = 1:3
f(i) = parfeval(p,@pause, 0);
end
In short, you can pass the pool object over to parfeval and if it is empty, it won't be used.
Bruno Luong
2023 年 8 月 23 日
編集済み: Bruno Luong
2023 年 8 月 23 日
@Sam Marshalik great thank you !
I just tested it in my real project and it seems working well. Now I have one base code for both parallel and non-parallel calculation.
This is also a convenient way to debug the parallel code, just switch to empty pool and voilà.
% why using cell?
numWorkers = {};
if ~parflag
numWorkers = {0};
end
% this is simpler, isn't it?
if ~parflag
numWorkers = 0;
end
% Both of them work.
Bruno Luong
2023 年 8 月 24 日
編集済み: Bruno Luong
2023 年 8 月 24 日
Good question, further more passing cell is not documented as from parfor doc
"parfor (loopvar = initval:endval, M); statements; end executes statements in a loop using a maximum of M workers or threads, where M is a nonnegative integer."
Actually, if we adpot numWorkers as a scalar number, we don't need if-else to make the selection.
function foo(x, numWorkers)
parfor (1:length(x), numWorkers)
% processing codes
end
end
foo(x, 0) % for loop
foo(x, 3) % parfor loop using 3 workers
But then you have to manually set numWorkers
Yes, that flexibility works for me. For example, when I am not doing anything but programming Matlab, I can set it to the maximum number of workers my compouter has. But when I heavily multitask, I can set numWorkers, say, 2 or 3, and let other cores to work on non-Matlab tasks. (I guess that's how my Mac would work.)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Parallel for-Loops (parfor) についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
