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.
# Example of how to use env.optimizebatch()
# Optimizes tasks whose names were read from command line.
def main(argv):
n = len(argv) - 1
tasks = []
threadpoolsize = 6 # Size of thread pool available for all tasks
with mosek.Env() as env:
# Set up some example list of tasks to optimize
for i in range(n):
t = mosek.Task(env, 0, 0)
t.readdata(argv[i+1])
# We can set the number of threads for each task
t.putintparam(mosek.iparam.num_threads, 2)
tasks.append(t)
# Optimize all the given tasks in parallel
trm, res = env.optimizebatch(False, # No race
-1.0, # No time limit
threadpoolsize,
tasks) # List of tasks to optimize
for i in range(n):
print("Task {0} res {1} trm {2} obj_val {3} time {4}".format(
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).