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()
def main():
  # Choose some sample parameters
  n = 10                 # Number of models to optimize
  threadpoolsize = 4     # Total number of threads available
  threadspermodel = 1    # Number of threads per each model

  # Create a toy model for this example
  M = makeToyParameterizedModel()

  # Set up n copies of the model with different data
  models = [M.clone() for _ in range(n)]
  for i in range(n):
    # We can set the number of threads individually per model
    models[i].setSolverParam("numThreads", threadspermodel)

  # Solve all models in parallel
  status = Model.solveBatch(False,         # No race
                            -1.0,          # No time limit
                            models)        # Array of Models to solve

  # Access information about each model
  for i in range(n):
    if status[i] == SolverStatus.OK:
      print("Model {}: Status {}, Solution Status {}, Objective {:.3f}, Time {:.3f}".format(
      print("Model {}: not solved".format(i))