1

I need to globally optimize the parameter inputs to a fortran program in matlab. The function accepts inputs in the following manner:

    z= fort_fun(X,str)

Where X is a vector of decimal numbers and str is a string. I need to identify the minimal z corresponding to the optimal X for each of 1020 str's. I can perform the process in serial by declaring str as a global variable with the function defined locally at the end of the parent script. However, in order to execute my code in a more timely manner (< 1 month) I would like to run this process in parallel with parfor as follows :

    parfor i=1:n
         %code to setupt global optomization problem....
         z(i)=optimal output of ---  fort_fun(X,str(i))  ---
    end

...storing each final optimized value of z. If I declare str as a global variable, each thread of the parfor command will simultaneously optimize the same str(i).

Does anyone know of a way that I can configure each thread of the parfor command to optimize"" fort_fun(X,str(i)) "" ? I believe the OMP analog would be to use the "private" modifier applied to the str input.

Thanks!

FooAnon
  • 566
  • 1
  • 4
  • 11

1 Answers1

0

After some digging, I came up with the following solution, it appears to be working.

parfor i=1:1019
    str=str1(i,:);
  [x1(i,:),x4(i,1)]=optimal(str,x0);
end 

function [xf, z]=optimal(str,x0)  
    lb=[0,0,0];
    opts = optimoptions(@fmincon,'Algorithm','interior-point','FunctionTolerance',1E-4,...
        'OptimalityTolerance',1E-4,'StepTolerance',1E-4);
    problem = createOptimProblem('fmincon','objective',@resulto,'options',opts...
        ,'x0',x0,'lb',lb);
    gs = GlobalSearch('FunctionTolerance',1E-4,...
        'XTolerance',1E-4)
    [xf, z] = run(gs,problem);
    function [result]=resulto(X)
        result=fort_fun (X(1),X(2),X(3),str);
    end

end

Above is the code, adapted to the general case discussed in this question. MATLAB conveniently assumes a broader scope in the declaration of the two nested functions.

FooAnon
  • 566
  • 1
  • 4
  • 11