7.11 Parallel optimization

In this section we demonstrate the method Model.solveBatch which is a parallel optimization mechanism built-in in MOSEK. It has the following features:

  • It allows to fine-tune the balance between the total number of threads in use by the parallel solver and the number of threads used for each individual model.

  • It is very efficient for optimizing a large number of models of similar size, for example models obtained by cloning an initial model and changing some coefficients.

In the example below we demonstrate a very standard application of Model.solveBatch. We create an initial model, clone it a few times, set different parameter values in each clone and then optimize all the cloned models in parallel. When all models complete we access the status for each of them and, if successfully solved, we gather solutions and other information in the standard way, as if each model was optimized separately.

Listing 7.26 Calling the parallel optimizer. Click here to download.
  /** Example of how to use Model.solveBatch()
   */
  public static void main(String[] argv) throws SolutionError
  {
    // Choose some sample parameters
    int n = 10;                 // Number of models to optimize
    int threadpoolsize = 4;     // Total number of threads available
    int threadspermodel = 1;    // Number of threads per each model

    // Create a toy model for this example
    Model M = makeToyParameterizedModel();

    // Set up n copies of the model with different data
    Model[] models = new Model[n];
    for(int i = 0; i < n ; i++)
    {
      models[i] = M.clone();
      models[i].getParameter("p").setValue(i+1);
      // We can set the number of threads individually per model
      models[i].setSolverParam("numThreads", threadspermodel);
    }

    // Solve all models in parallel
    SolverStatus[] status = Model.solveBatch(false,         // No race
                                             -1.0,          // No time limit
                                             threadpoolsize,
                                             models);       // Array of Models to solve

    // Access the soutions
    for(int i = 0; i < n; i++) 
      if (status[i] == SolverStatus.OK)
        System.out.printf("Model  %d:  Status %s   Solution Status %s   objective %.3f  time %.3f\n", 
          i, 
          status[i],
          models[i].getPrimalSolutionStatus(),
          models[i].primalObjValue(),
          models[i].getSolverDoubleInfo("optimizerTime"));
      else
        System.out.printf("Model  %d:  not solved\n", i);
  }