6.12 Parallel optimization¶
In this section we demonstrate the method MSK_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 MSK_optimizebatch().
Optimizes tasks whose names were read from command line.
*/
int main(int argc, char **argv)
{
MSKenv_t env;
int n = argc - 1;
MSKtask_t *tasks = NULL;
MSKrescodee *res = NULL;
MSKrescodee *trm = NULL;
MSKrescodee r = MSK_RES_OK;
int i;
/* Size of thread pool available for all tasks */
int threadpoolsize = 6;
tasks = calloc(n, sizeof(MSKtask_t));
res = calloc(n, sizeof(MSKrescodee));
trm = calloc(n, sizeof(MSKrescodee));
MSK_makeenv(&env, NULL);
/* Create an example list of tasks to optimize */
for (i = 0; i < n; i++) {
MSK_makeemptytask(env, &(tasks[i]));
MSK_readdata(tasks[i], argv[i+1]);
/* We can set the number of threads for each task */
MSK_putintparam(tasks[i], MSK_IPAR_NUM_THREADS, 2);
}
/* Optimize all the given tasks in parallel */
r = MSK_optimizebatch(env,
0, // No race
-1.0, // No time limit
threadpoolsize,
n,
tasks, // Array of tasks to optimize
trm,
res);
for(i = 0; i < n; i++) {
double obj, tm;
MSK_getdouinf(tasks[i], MSK_DINF_INTPNT_PRIMAL_OBJ, &obj);
MSK_getdouinf(tasks[i], MSK_DINF_OPTIMIZER_TIME, &tm);
printf("Task %d res %d trm %d obj_val %.5f time %.5f\n",
i,
res[i],
trm[i],
obj,
tm);
}
for(i = 0; i < n; i++)
MSK_deletetask(&(tasks[i]));
free(tasks);
free(trm);
free(res);
MSK_deleteenv(&env);
return 0;
}
Another, slightly more advanced application of the parallel optimizer is presented in Sec. 11.3 (Concurrent optimizer).