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];
  
      /* Size of thread pool available for all tasks */
      int threadpoolsize = 6; 

      using (var env = new mosek.Env())
      {
        /* 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++) 
          Console.WriteLine("Task  {0}  res {1}   trm {2}   obj_val  {3}  time {4}", 
            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).