Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  memory leak problem

    Posted 09/25/12 07:43 PM

    Originally posted by: SystemAdmin


    Hello,

    I am using a cut callback to generate cuts in my code. I just tested my code via PurifyPlus and it found memory leak in my code. I know that the memory leak comes from my callback because when I remove it I don't have memory leak anymore. I read my code several times and I am confused, because I only define one IloExpr, cut, and one dynamic array, R[], and I end both of them before leaving the callback. The rest of the variables have been defined as the parameters of the callback. I don't know what causes the memory leak!

    I appreciate if you can let me know what you think.

    I copy my callback code here:

    ILOCUTCALLBACK7(makecut, IloNumVarArray, U, BoolVarMatrix, Y, BoolVarMatrix3D, X, BoolVarMatrix, u, double*, probability, int*, orig, int**, A){

    IloEnv env = getEnv();

    bool integrality = true;

    for (int p=0; p<origM+1; p++){
    if (integrality == false)
    break;
    for (int i=0; i<U.getSize(); i++){
    if (getValue(Y[p][i]) > 0.0001 && getValue(Y[p][i]) < 0.9999){
    integrality = false;
    break;
    }
    }
    }
    for (int i=0; i<U.getSize(); i++){
    if (integrality == false)
    break;
    for (int j=0; j<U.getSize(); j++){
    if (integrality == false)
    break;
    if (A[i][j] == 1){
    //origM+3 = K
    for (int k=0; k<origM+3; k++){
    if (getValue(X[i][j][k]) > 0.0001 && getValue(X[i][j][k]) < 0.9999){
    integrality = false;
    break;
    }
    }
    }
    }
    }
    for (int i=0; i<U.getSize(); i++){
    if (integrality == false)
    break;
    //origM+4 = G
    for (int g=0; g<origM+4; g++){
    if (getValue(u[i][g]) > 0.0001 && getValue(u[i][g]) < 0.9999){
    integrality = false;
    break;
    }
    }
    }
    if(integrality){

    double origin, prob;

    double *R;
    R = new doublehttp://U.getSize();

    for (int k = 0; k < origM+3; k++){
    origin = 0;
    for (int i = 0; i < U.getSize(); i++){
    R[i] = 1 - (1.0 * getValue(U[i]) * getValue(U[i])) / (1.0 * orig[M] * orig[M]);
    origin += (R[i] * getValue(Y[origk][i]));
    }
    prob = log(origin);
    for (int i = 0; i < U.getSize(); i++)
    for(int j = 0; j < U.getSize(); j++)
    if(A[i][j] == 1)
    prob += (getValue(X[i][j][k]) * log(R[j]));
    if (prob < log(probability[k])){
    IloExpr cut(env, 0);
    for (int j = 0; j < U.getSize(); j++){
    for (int i = 0; i < U.getSize(); i++){
    if ((A[i][j] > 0.999 && getValue(X[i][j][k]) > 0.999) || getValue(Y[origk][j]) > 0.999){
    for (int l=0; l<origM+3; l++){
    if (getValue(Y[origl][j]) > 0.999){
    cut += (1-Y[origl][j]);
    }
    else{
    for (int ii=0; ii<U.getSize(); ii++){
    if (Aii[j] > 0.999 && getValue(Xii[j][l]) > 0.999){
    cut += (1-Xii[j][l]);
    break;
    }
    }
    }
    }
    break;
    }
    }
    }
    cout << cut << endl;
    add (cut >= 1);
    origM+6++;
    cut.end();
    }
    }

    delete[] R;
    }
    }
    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: memory leak problem

    Posted 09/26/12 12:16 AM

    Originally posted by: SystemAdmin


    Ok! I think I need to attach a text file. This looks messy!
    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: memory leak problem

    Posted 09/26/12 06:34 AM

    Originally posted by: SystemAdmin


    Source code should be wrapped into '{code}' tags to display nicely in this Forum.
    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: memory leak problem

    Posted 09/26/12 06:33 AM

    Originally posted by: SystemAdmin


    Does it help to do
    add(cut >= 1).end();
    

    instead of only
    add(cut >= 1);
    

    ? If you look at the iloadmipex5.cpp example then you see that the callback end()s the return value of add().
    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: memory leak problem

    Posted 09/26/12 09:05 AM

    Originally posted by: SystemAdmin


    Thanks Daniel, I tried the add(cut >= 1).end();

    Memory leak is still there!
    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: memory leak problem

    Posted 09/26/12 09:40 AM

    Originally posted by: SystemAdmin


    I don't see any other obvious problems in your code.
    What happens if you keep the code as is but don't add() the cut? Does the leak persist?
    Another thing you may want to try is to not generate the cut using operator+= but instead do this:
    IloNumVarArray vars(env);
    IloNumVarArray vals(env);
    double sum = 0.0;
    for (...) {
       for (...) {
          // cut += (1-X[ii][j][l])
          sum += 1.0;
          vars.add(X[ii][j][l]);
          vals.add(-1.0);
       }
    }
    IloNumExpr cut(IloScalProd(vars, vals));
    add(cut >= 1.0 - sum).end();
    cut.end();
    vals.end();
    vars.end();
    

    Does this help with the leak?

    Does purify give you more information? For example other tools like valgrind can give a backtrace for the allocation that was not freed so that you could tell which line of code triggers the leak.
    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: memory leak problem

    Posted 09/26/12 10:32 AM

    Originally posted by: SystemAdmin


    Daniel,

    I tried your suggested method and it solved the memory leak problem for most of the instances. However, some instances still have memory leak!
    #CPLEXOptimizers
    #DecisionOptimization


  • 8.  Re: memory leak problem

    Posted 10/01/12 10:39 AM

    Originally posted by: SystemAdmin


    And can you get more information from purify? Such as the line number that triggered the allocation for which there was no free/delete?
    #CPLEXOptimizers
    #DecisionOptimization


  • 9.  Re: memory leak problem

    Posted 10/01/12 02:47 PM

    Originally posted by: SystemAdmin


    Daniel,

    PurifyPlus gave me some information, but in my case it wasn't clear at all. It turned out that I simply forgot to remove a for loop that was specified for a special instance. That's why I had memory leak error in other instances. Briefly speaking, What you suggested solved the memory leak problem. I very much appreciate your help.
    #CPLEXOptimizers
    #DecisionOptimization


  • 10.  Re: memory leak problem

    Posted 10/02/12 09:29 AM

    Originally posted by: SystemAdmin


    Good. As far as I can tell the leak you observed before (and which could be fixed by the changes I suggested) is a known issue that should be fixed with the next release of CPLEX.
    #CPLEXOptimizers
    #DecisionOptimization