Search code examples
matlaboptimizationparallel-processingparformosek

Optimizing in Parallel in Matlab with Mosek


I'm trying to solve a cone program in Matlab by calling MOSEK while varying the bound on one of the constraints.

I would like to do so in parallel to take advantage of all the cores that I have. Here is a modified example to illustrate my point.

testBounds=[0.1, 0.15, 0.2, 0.25, 0.3];
clear prob;

[r, res] = mosekopt('symbcon');
prob.c   = [0 0 0 1 1 1];

% Specify the non-conic part of the problem.
prob.a   = sparse([1 1 2 0 0 0]);
prob.buc = 1;
prob.blx = [0 0 0 -inf -inf -inf];
prob.bux = inf*ones(6,1);

% Specify the cones.
prob.cones.type   = [res.symbcon.MSK_CT_QUAD, res.symbcon.MSK_CT_RQUAD];
prob.cones.sub    = [4, 1, 2, 5, 6, 3];
prob.cones.subptr = [1, 4];

for i=1:5
    % Specify the changing bound   
    prob.blc = testBounds(i);

    % Optimize the problem. 
    [r,res]=mosekopt('minimize',prob);

    % Display the primal solution.
    res.sol.itr.xx'
end

I tried to do this with parfor, but it isn't permitted. Unfortunately, MOSEK documentation doesn't go into detail about parallizing. How can I carry out the above in parallel?


Solution

  • The problem with your code is your use of the variable prob. While on an algorithmic level it is independent because each iteration of the loop uses it's own setting for blc and not uses any previous data, parfor does not support this use. Easiest solution is not to modify the variable prob but instead copy it in each iteration, making prob a broadcast and prob2 a local variable:

    parfor ii=1:5
        % Specify the changing bound
        %copy broadcast variable prob to a temporary variable prob2
        %this way the iteration has writing capabilities
        prob2=prob
        prob2.blc = testBounds(ii);
    
        % Optimize the problem. 
        [r,res]=mosekopt('minimize',prob2);
    
        % Display the primal solution.
        res.sol.itr.xx'
    end
    

    Another issue with your code is the way you are returning data. parfor has no order when processing the data, thus just displaying it to the console will not give you any useful results. It's also slow. I don't know what exactly you need and what the datatypes are, thus I have not touched that part of the code. The code in my answer does the calculations but is not returning any results because res and r are both temporary variables.