6.12 Parallel optimization

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

  • One license token checked out by the environment will be shared by the tasks.

  • 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 task.

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

In the example below we simply load a few different tasks and optimize them together. When all tasks complete we access the response codes, solutions and other information in the standard way, as if each task was optimized separately.

Listing 6.24 Calling the parallel optimizer. Click here to download.
  /** Example of how to use env.optimizebatch(). 
      Optimizes tasks whose names were read from command line.
   */
  public static void main(String[] argv)
  {
    int n = argv.length;
    mosek.Task[]  tasks  = new mosek.Task[n];
    mosek.rescode[] res  = new mosek.rescode[n];
    mosek.rescode[] trm  = new mosek.rescode[n];

    mosek.Env env = new mosek.Env();

    // Size of thread pool available for all tasks
    int threadpoolsize = 6; 

    // Create an example list of tasks to optimize
    for(int i = 0; i < n; i++) 
    {
      tasks[i] = new mosek.Task(env);
      tasks[i].readdata(argv[i]);
      // We can set the number of threads for each task
      tasks[i].putintparam(mosek.iparam.num_threads, 2);
    }

    // Optimize all the given tasks in parallel
    env.optimizebatch(0,              // No race
                      -1.0,           // No time limit
                      threadpoolsize,
                      tasks,          // Array of tasks to optimize
                      trm,            
                      res);

    for(int i = 0; i < n; i++) 
      System.out.printf("Task  %d  res %s   trm %s   obj_val  %f  time %f\n", 
        i, 
        res[i], 
        trm[i],  
        tasks[i].getdouinf(mosek.dinfitem.intpnt_primal_obj),
        tasks[i].getdouinf(mosek.dinfitem.optimizer_time));
  }

Another, slightly more advanced application of the parallel optimizer is presented in Sec. 11.3 (Concurrent optimizer).