Skip to main content

Free an environment without deleting the model

Answered

Comments

10 comments

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Jake,

    Freeing an environment without deleting the model is not possible, because a model is attached to a given environment.

    Best regards, 
    Jaromił

    1
  • Jacob Wren
    • Gurobi-versary
    • Curious
    • Collaborator

    Hi Jaromił,

    To solve the problem I outlined above, would you recommend having a single environment (i.e., one environment for all model solves)? If so, how do I implement this (there doesn't seem to be any documentation when working with Pyomo)?

    Further, is there a cost to having a single environment for all model solves (as against a 1-1 relation)?

    Many thanks,

    Jake

     

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Jake,

    To solve the problem I outlined above, would you recommend having a single environment (i.e., one environment for all model solves)?

    No, I would recommend to have a single environment for every model you solve. This is because the gurobipy module is not thread safe, see How do I use multiprocessing in Python with Gurobi?

    f so, how do I implement this (there doesn't seem to be any documentation when working with Pyomo)?

    I am not a Pyomo user so unfortunately, I cannot help here. Maybe someone more knowledgeable in Pyomo can help.

    Further, is there a cost to having a single environment for all model solves (as against a 1-1 relation)?

    No, there is no cost regarding performance, but you lose thread safety which can be dramatic for parallel applications.

    Best regards, 
    Jaromił

    0
  • Jacob Wren
    • Gurobi-versary
    • Curious
    • Collaborator

    Hi Jaromił,

    Thank you.

    No, there is no cost regarding performance, but you lose thread safety which can be dramatic for parallel applications.

    What do you mean by dramatic?

    Thanks,

    Jake

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    What do you mean by dramatic?

    By that I mean that without thread safety you can experience all kind of unexpected behavior like wrong solutions, wrong array accesses, and non-determinism.

    0
  • Jacob Wren
    • Gurobi-versary
    • Curious
    • Collaborator

    Hello Jaromił,

    Will Gurobi issue a warning message if you violate thread safety (this would be desirable)?

    If not, is there a (quick) way to check? 

    Thanks,

    Jake

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Jake,

    Will Gurobi issue a warning message if you violate thread safety (this would be desirable)?

    Unfortunately not, it is very hard from a programmatic point of view to check for thread safety violation. This is the reason why we always tell our users to use one environment for one model when solving models in parallel, because we can ensure that running models in different environments is thread safe.

    If not, is there a (quick) way to check? 

    I am not aware of a reliable way to check this but maybe you can find something online.

    Best regards, 
    Jaromił

    0
  • Jacob Wren
    • Gurobi-versary
    • Curious
    • Collaborator

    Hi Jaromił,

    This is the reason why we always tell our users to use one environment for one model when solving models in parallel...

    Just to clarify, one environment per model creation, per model solve, or per process (recall, I am using Pyomo)?

    Also, does setting Threads equal to 1 ensure thread safety?

    Thanks,

    Jake

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Jake,

    Just to clarify, one environment per model creation, per model solve, or per process (recall, I am using Pyomo)?

    Yes, one environment per exactly one model. You can solve the same model multiple times with as many threads you want. There is no need to set the Threads parameter then. For example, if you have 2 models which you want to solve in parallel on 4 threads, you should define two environments, one for each model. You can then run the models in parallel. It would be best to set the Threads parameter to 2 for each model such that they don't get in each others way. Setting the Threads parameter is not for thread-safety as it is already ensured by the separate environments, it is for best performance.

    As already mentioned I am not a Pyomo user so unfortunately, I cannot help here. Maybe someone more knowledgeable in Pyomo can help. I would think that Pyomo already creates one environment per model but I cannot guarantee that this is true.

    Also, does setting Threads equal to 1 ensure thread safety?

    No, if you would run 2 models in parallel within the same environment, it is not guaranteed that Threads=1 ensures thread safety.

    Best regards, 
    Jaromił

    0
  • Jacob Wren
    • Gurobi-versary
    • Curious
    • Collaborator

    Jaromił, Thank you!

    0

Please sign in to leave a comment.