I am using AMPL on MATLAB through the AMPL API. In one of my applications I intend to call AMPL in a “parfor” (parallelizing calling AMPL). So I start by setting up AMPL on each worker (core) as follows:
“myCluster = parcluster('local');
poolsize=myCluster.NumWorkers;
parfor i=1:poolsize
setUp;
end
“
And then I run “parfor” (parallel “for” on MATLAB) as follows
“
basef = fileparts(which('example'));
u=-1:0.1:2;
parfor i=1:length(u)
ampl{i}= AMPL;
ampl{i}.read([basef '/' 'example.mod']);
ampl{i}.getParameter('u').setValues(u(i));
ampl{i}.setOption('solver' ,'gurobi');
ampl{i}.solve();
p(i)=ampl{i}.getValue('Objective');
ampl{i}.close;
% i
end
“
This works perfectly and it is much faster than a regular “for”. Notice that in this case there is only one parameter (‘u’) but in other instances where there are other fixed parameters on top of the ones that are changing inside "parfor", I find myself having to use dataframes to setup these fixed parameters inside the parfor. This however slows the performance significantly as the ampl structure for each subproblems is created every time from inside the for loop (at each core). Is there a way to use the AMPL API more efficiently with parfor? Like for instance forming the AMPL structures beforehand as in a regular for loop? As far as i know "parfor" does not allow being passed an array of ampl structures. Is there a way around this issue?
Thanks a lot.