Decision Optimization

Decision Optimization

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


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  Multithreading with Python API

    Posted 05/14/14 04:39 AM

    Originally posted by: TalRaviv


    Hi,

    I am implementing a branch and cut algorithm in Python 2,7,6 using the UserCutCallback class of the API with Cplex 12.6.

    Everything works fine, except that my algorithm can only run in a single thread.  I read that by default Cplex turn off multithreading when you define callbacks but you can overwrite this by setting the number of threads manually which I did with

    model.parameters.threads.set(4)

    Interestingly Cplex reports "Parallel mode: deterministic, using up to 4 threads" when it starts but the performance of my algorithm is not affected and the CPU utilization remains low.

    What am I doing wrong?

    Do I need to define my callback class differently in order to make it compatible with multithreading?

    Also, I  see that Cplex set the MIP search method to "traditional branch-and-cut."  What exactly does it mean?


    #CPLEXOptimizers
    #DecisionOptimization


  • 2.  Re: Multithreading with Python API

    Posted 05/14/14 01:15 PM

    If your callback code is thread-safe, you might try running in opportunistic mode (see the parameters.parallel parameter).

    It is expected that CPLEX switches from dynamic search to traditional branch-and-cut in the presence of control callbacks. That is what the "control callbacks may disable some MIP features" message is about.


    #CPLEXOptimizers
    #DecisionOptimization


  • 3.  Re: Multithreading with Python API

    Posted 05/15/14 06:49 AM

    Originally posted by: TalRaviv


    Thank you rkersh for your post but this does not exactly answer my question.

    My problem is not with the deterministic search strategy but with the fact that cplex seem to launch only one thread, since the utilization of my cpu is not affected (and remains around 25%) even when I set the number of threads to 4 or more.

    Another question, what does it take for a Python callback class to be  thread-safe and what should I expect if it is not one?

    Is there any python code sample that demonstrates using user cut callbacks in multithreading environment?

     

     

     

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 4.  Re: Multithreading with Python API

    Posted 05/15/14 07:06 AM

    Deterministic multi-threading introduces more communication/synchronization overhead between threads, in particular in the presence of control callbacks. This means that your application may not benefit much from multiple threads, especially if your callback runs for a long time compared to what else is done at a node. That is why Ryan suggested to try opportunistic mode, this reduces the thread communication/synchronization to a minimum.

    Also note that while processing the root node it may well be that only a single thread is active. Only when there is a sufficient number of open nodes you can expect all threads to get busy.

    For your callback to be thread-safe it must, well, be safe to be called from multiple threads simultaneously. What that means in detail highly depends on the algorithm you have implemented in the callback. Usually you need to at least protect accesses to shared data with locks.


    #CPLEXOptimizers
    #DecisionOptimization


  • 5.  Re: Multithreading with Python API

    Posted 05/18/14 09:29 AM

    Originally posted by: TalRaviv


    Thanks. I am setting my model as follow:

    model.parameters.parallel.set(-1) #  opportunistic parallel search mode

    model.parameters.threads.set(4)  

     

    Indeed when cplex starts is says
    MIP search method: traditional branch-and-cut.
    Parallel mode: opportunistic, using up to 4 threads.
     

    My callback functions seem to be thread safe as they only read shared data and add cuts and lazy constraints but otherwise never write anything to global variables.

    The model creates hundreds of nodes and has most of them open at some point and still my cpu usage is around 25% on a four cores machine. 

    Can I find a somewhere a python sample code that implements branch and cut with cplex API and supposed to be multithreads?

    Any additional ideas?

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 6.  Re: Multithreading with Python API

    Posted 05/21/14 01:52 PM

    I just replied to this recent thread.  I believe you're running into the same issue.

     


    #CPLEXOptimizers
    #DecisionOptimization


  • 7.  Re: Multithreading with Python API

    Posted 05/22/14 03:08 AM

    Originally posted by: TalRaviv


    Thanks rkersh. Surly not I wanted to hear but at least I know that I can stop searching.


    #CPLEXOptimizers
    #DecisionOptimization