Page 1 of 1

CPLEX optimization in parallel

PostPosted: Tue Feb 14, 2012 5:15 am
by alrengifo
anyone has experience using OpenMP for the libraries of CPLEX? I want to optimize parallel models, but despite all considerations still running in series. Anyone know if is possible to optimize in parallel or whether the reverse is not possible.

Re: CPLEX optimization in parallel

PostPosted: Wed Feb 22, 2012 10:22 pm
by shivaram
Hi,

Can you please provide some example code in CPLEX library, which you are trying to run in parallel?. That will help in understanding the problem.

Regards,
Shivaram

Re: CPLEX optimization in parallel

PostPosted: Thu Feb 23, 2012 3:11 am
by alrengifo
I'm working with a fairly complex model, but this is a linear programming example illustrates well the problem I have.

In the object "define_modelos" is scheduled processes that want to run in parallel, then the call from the main using "# pragma omp parallel for" is like when I run sequentially, and is most evident when the models are more complex.

Thanks for any help you can give me.


#include <ilcplex/ilocplex.h> // CPLEX library
#include <omp.h> // OpenMP library
ILOSTLBEGIN
const int Max_thread=10;
// structures
typedef IloArray<IloNumVarArray> IloNumVarArray2;
typedef struct{
float FO;
float X[2];
} ThreadsParam; // structure for save threads information
//declare parameters
IloNumArray2 A, b, c;
IloInt i,j,h,N,M,id;
ThreadsParam param[Max_thread]; //
////////////////////////////////////////
//////// Introduce data / ////////
void define_data(IloEnv env) {
N=2;
M=4;
c=IloNumArray2(env,Max_thread);
c[0]=IloNumArray (env,N,-2,1);
c[1]=IloNumArray (env,N,-3,1);
c[2]=IloNumArray (env,N,1,1);
c[3]=IloNumArray (env,N,-3,2);
c[4]=IloNumArray (env,N,-5,0);
c[5]=IloNumArray (env,N,2,-1);
c[6]=IloNumArray (env,N,3,-1);
c[7]=IloNumArray (env,N,1,-2);
c[8]=IloNumArray (env,N,3,-2);
c[9]=IloNumArray (env,N,5,0);
A=IloNumArray2(env,M);
A[0]=IloNumArray (env,N,1,2);
A[1]=IloNumArray (env,N,-4,2);
A[2]=IloNumArray (env,N,1,-1);
A[3]=IloNumArray (env,N,0,1);
b=IloNumArray2(env,Max_thread);
b[0]=IloNumArray (env,M,6,4,4,2);
b[1]=IloNumArray (env,M,6,5,4,3);
b[2]=IloNumArray (env,M,6,6,4,4);
b[3]=IloNumArray (env,M,6,7,4,5);
b[4]=IloNumArray (env,M,6,4,4,6);
b[5]=IloNumArray (env,M,6,4,4,3);
b[6]=IloNumArray (env,M,6,4,6,4);
b[7]=IloNumArray (env,M,6,4,7,5);
b[8]=IloNumArray (env,M,6,4,9,6);
b[9]=IloNumArray (env,M,6,5,4,7);
}
////////////////////////////////////////
//////// Introduce Models //////////
void define_modelos(IloEnv env) {
//Subproblems
IloModel SubModelo(env);
//Variables
IloNumVarArray x;
x= IloNumVarArray(env,N, 0, IloInfinity,ILOINT);
SubModelo.add(x);
//Constrictions
IloConstraintArray Res(env,M);
IloExpr ERes(env,0);
for (j=0;j<M;++j){
for (i=0; i<N;++i){
ERes +=A[j][i] * x[i];
}
Res[j]=IloConstraint(ERes <= b[h][j]);
ERes.end();
ERes= IloExpr(env,0);
}
ERes.end();
SubModelo.add (Res);
//Objective Function
IloExpr EObj(env,0);
for (i=0; i<N;++i){
EObj +=c[h][i]*x[i];
}
SubModelo.add (IloMinimize(env,EObj));
EObj= IloExpr(env,0);
EObj.end();
//Solver Configuration (Cplex)
IloCplex cplexModelo(SubModelo);
cplexModelo.setDefaults();
cplexModelo.setOut(env.getNullStream());
cplexModelo.setWarning(env.getNullStream());
cplexModelo.setParam(cplexModelo.EpGap,1E-10);
cplexModelo.setParam(cplexModelo.EpAGap,0);
cplexModelo.setParam(cplexModelo.EpInt,1E-10);
cplexModelo.setParam(cplexModelo.Threads, 2);
cplexModelo.setParam(cplexModelo.ParallelMode, 1);
//RESOLUTION
cplexModelo.solve();
if (cplexModelo.getStatus() == IloAlgorithm::Infeasible)
env.out() << "No Solution" << endl;
//WRITING RESULTS
// results on screen
env.out() << " runtime = " << cplexModelo.getCplexTime() << endl;
env.out() << "Objective function value = " << cplexModelo.getObjValue() << endl;
env.out() << "MIP = " << cplexModelo.isMIP() << endl;
env.out() << "requested number of solutions = " << cplexModelo.getSolnPoolNsolns() << endl;
env.out() << "Info = " << cplexModelo.getCplexStatus() << endl;
//Saving Results
param[h].FO=cplexModelo.getObjValue();
for(i=0;i<N;++i){
param[h].X[i]=cplexModelo.getValue(x[i]);
env.out()<<cplexModelo.getValue(x[i])<<endl;
}
}
////////////////////////////////////////
//// main (calling threads) ////
int main (){
IloEnv env;
try {
define_data(env); //calling data
// Calling threads in parallel.
#pragma omp parallel for shared(A,b,c,N,M,param)
private(env,h,j,i)
for(h=0;h<Max_thread;++h){
define_modelos(env);
}
for(h=0;h<Max_thread;++h){
cout<< endl<< "FO" << h <<" = "<<param[h].FO<<endl;
for(i=0;i<N;++i){
cout<<param[h].X[i]<<" , ";
}
}
}
catch (IloException& ex) {
cerr << "Error: " << ex << endl;
}
catch (...) {
cerr << "Error" << endl;
}
env.end();
return 0;
}

Re: CPLEX optimization in parallel

PostPosted: Fri Feb 24, 2012 12:34 am
by shivaram
Hi,

Can you check if the openmp setup/initialization is happened correctly (If not done already)?

a) Check the gcc version, you need to use gcc 4.2 or above
b) The program need to be compiled with -fopenmp option
c) Can you also set OMP_NUM_THREADS environment variable (e.g: setenv OMP_NUM_THREADS=4)
d) Run top -H to check whether the code actually runs on multiple threads. You can also execute a call to omp_get_num_threads
within a parallel regions to check how many threads are executing a parallel region.

Regards,
Shivaram