- What OS are you using?
- Which release of MATLAB?
- How many physical cores do you have - what's the result of maxNumCompThreads.
Improving performance of parallel code sections
1 回表示 (過去 30 日間)
古いコメントを表示
Consider the following test code which benchmarks different methods of parallelization within MATLAB:
function partest()
delete(gcp("nocreate"));
pool = parpool("threads", 3);
njob = pool.NumWorkers;
ndim = 10000000;
state = rand(ndim, njob);
dummy = 0;
% parfor
tic
func = zeros(njob, 1);
parfor ijob = 1 : njob
func(ijob) = getFunc(state(:, ijob));
end
disp(" parfor = " + toc + " seconds.")
dummy = dummy + sum(func);
% spmd
tic
func = zeros(njob, 1);
spmd
indx = spmdIndex;
func(indx) = getFunc(state(:, indx));
end
disp(" spmd = " + toc + " seconds.")
%dummy = dummy + sum([func{:}]);
% parfeval
tic
clear func
fout(1 : njob) = parallel.FevalFuture;
for ijob = 1 : njob
fout(ijob) = parfeval(pool, @getFunc, 1, state(:, ijob));
end
func = fetchOutputs(fout);
disp("parfeval = " + toc + " seconds.")
dummy = dummy + sum(func);
% serial
tic
func = zeros(njob, 1);
for ijob = 1 : njob
func(ijob) = getFunc(state(:, ijob));
end
disp(" serial = " + toc + " seconds.")
dummy = dummy + sum(func);
disp(dummy)
delete(gcp("nocreate"));
end
function func = getFunc(state)
%func = -sum(state.^2);
func = 0;
for idim = 1 : length(state)
func = func - state(idim)^2;
end
end
Here is the benchmark output:
parfor = 0.092802 seconds.
spmd = 0.044583 seconds.
parfeval = 0.032457 seconds.
serial = 0.050159 seconds.
Firstly, why is parfeval outperforming all others?
Secondly, are there anything that could be done to any of these parallel constructs to improve their performance against the last (serial) case?
2 件のコメント
Edric Ellis
2024 年 2 月 1 日
Can I ask
回答 (1 件)
Matt J
2024 年 1 月 31 日
編集済み: Matt J
2024 年 1 月 31 日
The task is too small for parallelization to have any meaningful effect. Similarly, the relative performance numbers for the different methods is not meaningful. You need a task that is at least 30 seconds long serially for any of the comparisons to make any sense.
9 件のコメント
Matt J
2024 年 2 月 1 日
編集済み: Matt J
2024 年 2 月 1 日
What about my earlier comment about having more than 1 loop iteration per worker? It makes intuitive sense to me that parFeval would be optimal when you have only 1 function call per worker. Otherwise, if parfor were optimal for everything, why would they provide parFeval?
参考
カテゴリ
Help Center および File Exchange で Parallel Computing Fundamentals についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!