What happens when we call a function containing a parfor loop within another parfor loop?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Luqman Saleem
am 6 Feb. 2024
Bearbeitet: Torsten
am 6 Feb. 2024
Consider
answer = zeros(1,5);
parfor x1 = 1:5
answer(x1) = fun();
end
function output = fun()
R = zeros(1,10)
parfor x2 = 1:10
R(x2) = x2;
end
output = sum(R(:));
end
Is the parallelization applied only to 'x1', or is it applied to both 'x1' and 'x2'?
0 Kommentare
Akzeptierte Antwort
Torsten
am 6 Feb. 2024
Bearbeitet: Torsten
am 6 Feb. 2024
This page has the answer to your question:
From the page:
You can also use a function that uses parfor and embed it in a parfor-loop. Parallelization occurs only at the outer level. In the following example, call a function MyFun.m inside the outer parfor-loop. The inner parfor-loop embedded in MyFun.m runs sequentially, not in parallel.
parfor i = 1:10
MyFun(i)
end
function MyFun(i)
parfor j = 1:5
...
end
end
Tip
Nested parfor-loops generally give you no computational benefit.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!