Solving different models in parallel (C++/OpenMP)
AnsweredDisclaimer: I am rather new to Gurobi so don't fully understand all the details.
I have a lot (thousands) of relatively small linear programs to solve. My idea is to write an OpenMP loop where each thread solves its part of liinear programs. Later, all the solutions are merged and output. Something like that:
vector<MyParams> lp_problem_descriptions;
// load lp_problem_descritptions
int num_of_problems = lp_problem_descriptions.size();
vector<double> global_solutions;
GRBenv env = GRBenv(true); // or should it be one for each thread?
env.start();
#pragma omp parallel
{
vector<double> local_solutions;
#pragma omp for nowait
for (int i = 0; i < num_of_problems; i++) {
GRBmodel model = GRBmodel(env);
// fill in model with values from lp_problem_descritptions[i]
model.optimize();
if (model.get(GRB_IntAttr_Status) == GRB_OPTIMAL) {
local_solutions.push_back(model.get(GRB_DoubleAttr_ObjVal));
}
} // for
#pragma omp critical
{
for (double val: local_solutions) global_solutions.push_back(val);
}
}
// output global_solutions
My question is whether it's possible with Gurobi (running independent models in parallel threads). And if so, should I use the same GRBenv for all the threads or each thread should have its own?
-
Yes, with Gurobi it is possible to solve multiple models in parallel. (It actually depends on the license type, but with an academic license, this is allowed.)
Please use a different environment for each thread. (Within one thread, you can reuse the environment.)
2 -
Great, thanks!
0 -
Silke Horn Under the C API, I get a segfault whenever I call GRBnewmodel inside an OMP parallel for loop, e.g.
#pragma omp parallel for // works when this line alone is commented
for (long batch = 0; batch < n_batch; ++batch)
{
printf("hello from batch %ld\n", batch);
GRBenv* env;
GRBemptyenv(&env);
GRBsetintparam(env, GRB_INT_PAR_OUTPUTFLAG, 0);
GRBstartenv(env);
GRBmodel* model;
GRBnewmodel(env, &model, NULL, 1, NULL, NULL, NULL, NULL, NULL); // works when this line alone is commented
GRBfreemodel(model);
GRBfreeenv(env);
}What might be causing this?
0
Please sign in to leave a comment.
Comments
3 comments