GRBaddconstr (or other) permissible inside of #pragma omp parallel for in C?
AnsweredDear Gurobi-Team,
is it permissible to add constraints (or anything else) to a model from the inside of a parallel for loop, like in the following (artificial) example in C?
int ind[num_variants][3];
double val[num_variants][3];
#pragma omp parallel for
for (i = 0; i < num_variants; i++) {
function_to_calculate_ind_and_val(ind[i], val[i], i);
error = GRBaddconstr(model, 3, ind[i], val[i], GRB_EQUAL, 1.0, "New");
}
Or does that lead to "confusion" within the model if constraints (or anything else) are added in parallel ("at the same time")?
Thanks
Thomas
-
Hi Thomas,
You cannot parallelize the calls to addConstrs(). But you can parallelize the construction of the set of constraints that you add. For example, you could try building 8 different sets of constraints (expression objects, like LinExpr) using parallelism, and after they have all been constructed call addConstrs() 8 times in a sequential fashion. This might bring some speedup in case expensive look-ups or function evaluations are involved when creating the expressions.
So you can construct your constraints in parallel - you just have to add them sequentially.
Best regards,
Jaromił0 -
Hi Jaromił,
thanks for your reply.
I already assumed that GRBaddconstr() isn't thread-safe.
But as you said, that's not really a problem because I can create the arguments for the function call in parallel.With GRBaddconstrs() [or GRBXaddconstrs()] the adding of constraints isn't even sequential but in one step, or do these routines internally simply add the constraints sequentially?
Best regards
Thomas
0 -
Hi Thomas,
With GRBaddconstrs() [or GRBXaddconstrs()] the adding of constraints isn't even sequential but in one step, or do these routines internally simply add the constraints sequentially?
Internally these methods are "just" copying memory entries from user arrays into internal data structures.Technically, it is sequential, but still way faster than adding every constraint one by one, because big chunks of data are moved at once instead of in tiny pieces via addconstr().
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
3 comments