Should parallelization be switched on manually
1 次查看(过去 30 天)
显示 更早的评论
Hi there! I would like to know whether parallelization in Matlab switches on automatically or should I do that manually. I have a code to solve an optimization problem (determining constants in kinetic equations, ODE), and I start with many initial guesses(doing Latin hypercube sampling). Last time I run my program, it took approx. 6 hours. Here is the code I am running:
load matlab S data input
dimS = size(S);
rankS = rank(S);
td=data(:,1);
xd=data(:,2);
p = 1000; % Number of points (samples)
N = 8; % Number of dimensions
lb = [0.00001 0.000002 0.00045 0.01 0.0001 0.00001 0.0002 0.000001]; % lower bounds
ub = [0.5 0.5 0.5 1 0.5 0.5 0.5 0.5]; % upper bounds
X = lhsdesign(p,N,'criterion','correlation');
D = bsxfun(@plus,lb,bsxfun(@times,X,(ub-lb)));
for i=1:size(D,1)
k0(i,:)=D(i,:);
options =optimset('Display','off','LargeScale','off','MaxFunEvals',2000);
ke=lsqnonlin(@myobjective,k0(i,:),[1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5],[],options, td, xd, input, S);
k_all(i,:)=ke(1,:);
end
0 个评论
回答(3 个)
Matt J
2015-7-1
Its usual internal multithreading should be enabled by default, but you can check with
>>maxNumCompThreads
0 个评论
Titus Edelhofer
2015-7-1
Hi,
on first sight it looks as if the loop on D
for i=1:size(D,1)
k0(i,:)=D(i,:);
options =optimset('Display','off','LargeScale','off','MaxFunEvals',2000);
ke=lsqnonlin(@myobjective,k0(i,:),[1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5,1e-5],[],options, td, xd, input, S);
k_all(i,:)=ke(1,:);
end
could be parallelized using parfor and the Parallel Computing Toolbox ...
Titus
1 个评论
Sean de Wolski
2015-7-1
This is probably a better option than the 'UseParallel' flag, parallelize out as far as possible.
Sean de Wolski
2015-7-1
If you have the Parallel Computing Toolbox, you can turn on Parallel Computing using the 'UseParallel' flag in optimset (or optimoptions).
The speedup will vary but if there is a high number of dimensions it can help significantly for gradient calculations.
2 个评论
Steven Lord
2015-7-1
Not all of the options are available for all of the solvers, and even when an option is available for one of the solvers it may not be available for all the algorithms that solver can use. So for this particular situation Sean's answer doesn't apply, but in general it's something to consider when using Optimization Toolbox (or PARTICLESWARM in Global Optimization Toolbox, according to the OPTIMOPTIONS page.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!