What happens if I use parfor and the fmincon/fminunc option "UseParallel" together?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I want to minimize the same objective function using n different starting points. I have k cores available. I have
.

Consider the following (toy) code:
parfor ii = 1:n
x_opt(ii) = fminunc( obj_fun, starting_point(ii), 'UseParallel', true )
end
Is MatLab smart enough to allocate
cores to each optimization?

7 Kommentare
Mario Malic
am 22 Nov. 2020
I think it's worthy to try a serial optimisation for outer loop and UseParallel option, especially if your function evaluation takes some time to evaluate.
Antworten (1)
Matt J
am 18 Nov. 2020
I don't know the answer to that directly, but it is easy to show by example that turning off 'UseParallel' can be beneficial:
objfun=@(x)sum(x.^2);
x0=rand(2000,1);
opts=optimoptions('fminunc','Display','none','UseParallel',true);
tic;
parfor i=1:4
fminunc(objfun,x0,opts);
end
toc%Elapsed time is 0.369885 seconds.
opts.UseParallel=false;
tic;
parfor i=1:4
fminunc(objfun,x0,opts);
end
toc%Elapsed time is 0.274740 seconds.
9 Kommentare
Raymond Norris
am 19 Nov. 2020
I'm puzzled how Matt's example worked when UseParallel is set to true. If you're running a parfor loop, I would think the optimization would not use the parallel pool and hence run serially. This would explain slightly why the performance.
Secondly, 2000x1 most likely just isn't big enough. Take a look at the following with 20000. For starters, when I benchmark, I'm setting maxNumCompThreads to 1 for a baseline and the setting to 4 later. This tells me what implicit multi-threading MATLAB gives me.
objfun = @(x)sum(x.^2);
x0 = rand(20000,1);
opts = optimoptions('fminunc','Display','none','UseParallel',false);
%% Baseline
maxNumCompThreads(1);
tic
for i=1:4
fminunc(objfun,x0,opts);
end
toc
% Elapsed time is 197.351202 seconds.
%% 4 threads, no additional parallelism
maxNumCompThreads(4);
tic
for i=1:4
fminunc(objfun,x0,opts);
end
toc
% Elapsed time is 75.309341 seconds.
%% 4 workers, but parallelism is minimal
opts = optimoptions('fminunc','Display','none','UseParallel',true);
tic
for i=1:4
fminunc(objfun,x0,opts);
end
toc
% Elapsed time is 64.789117 seconds.
Matt J
am 19 Nov. 2020
Bearbeitet: Matt J
am 19 Nov. 2020
I'm not sure what the manipulation of maxNumCompThreads is supposed to tell us about the interaction between parfor and UseParallel. I would think that reducing maxNumCompThreads will add even more bottlenecks to the computation that wouldn't otherwise be there, because now fminunc cannot maximally exploit the multi-core resources even for basic linear algebra steps. With more bottlenecks, the parallelization or non-parallelization of the gradient calculation step (controlled by UseParallel) will have a diminished impact.
Siehe auch
Kategorien
Mehr zu Get Started with Problem-Based Optimization and Equations finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!