6.12 Parallel optimization¶
In this section we demonstrate the method Env.optimize_batch
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.
fn parallel(files : Vec<FileOrText>) -> Result<(),String> {
// Create an example list of tasks to optimize
let mut tasks : Vec<(String,Task)> = files.iter().filter_map(|fname| {
let mut t = Task::new().unwrap();
match fname {
FileOrText::File(fname) => {
if let Err(_) = t.read_data(fname.as_str()) { None }
else {
t.put_int_param(mosek::Iparam::NUM_THREADS, 2).unwrap();
Some((fname.as_str().to_string(),t))
}
},
FileOrText::Text(data) => {
if let Err(_) = t.read_ptf_string(data.as_str()) { None }
else {
t.put_int_param(mosek::Iparam::NUM_THREADS, 2).unwrap();
Some(("-".to_string(),t))
}
}
}
}).collect();
let mut res = vec![0i32; tasks.len()];
let mut trm = vec![0i32; tasks.len()];
{
let taskrs : Vec<& mut Task> = tasks.iter_mut().map(|(_a,b)| b).collect();
// Size of thread pool available for all tasks
let threadpoolsize : i32 = 6;
// Optimize all the given tasks in parallel
mosek::optimize_batch(false, // No race
-1.0, // No time limit
threadpoolsize,
taskrs.as_slice(), // Array of tasks to optimize
trm.as_mut_slice(),
res.as_mut_slice())?;
}
for (resi,trmi,(fname,ti)) in izip!(res,trm,tasks.iter()) {
println!("Task {} res {} trm {} obj_val {} time {}",
fname,
resi,
trmi,
ti.get_dou_inf(mosek::Dinfitem::INTPNT_PRIMAL_OBJ)?,
ti.get_dou_inf(mosek::Dinfitem::OPTIMIZER_TIME)?);
}
Ok(())
}
Another, slightly more advanced application of the parallel optimizer is presented in Sec. 11.3 (Concurrent optimizer).