Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve NLv2 incompatibility with multithreading #3332

Merged
merged 11 commits into from
Aug 19, 2024

Conversation

jsiirola
Copy link
Member

Fixes #3329.

Summary/Motivation:

The NLv2 writer makes use of a visitor to both walk / compile the model expressions and to generate the string representation of the nonlinear portion of expressions. To accommodate this, the AMPLRepn result needed a handle on the visitor to be able to know what the current template is (terse or annotated). We implemented this using a module-level singleton, which works for general serial Python processes, but is not thread-safe. This PR redesigns the AMPLRepn so that the template belongs to the repn class and not the visitor, thereby removing the need for the singleton.

As a bonus, removing the singleton allows us to more easily break the visitor out of the writer module and into a separate pyono.repn.ampl module (following the pattern of the LP writer)

Changes proposed in this PR:

  • remove the reliance on a module-level singleton for communicating the active template from the writer/visitor to the AMPLRepn class
  • split the AMPLRepn and AMPLRepnVisitor into a separate module

Legal Acknowledgement

By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

Copy link

codecov bot commented Jul 26, 2024

Codecov Report

Attention: Patch coverage is 94.53237% with 38 lines in your changes missing coverage. Please review.

Project coverage is 88.70%. Comparing base (7f779ab) to head (6661d82).
Report is 394 commits behind head on main.

Files Patch % Lines
pyomo/repn/ampl.py 94.33% 38 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3332      +/-   ##
==========================================
+ Coverage   88.52%   88.70%   +0.17%     
==========================================
  Files         868      878      +10     
  Lines       98436   101165    +2729     
==========================================
+ Hits        87144    89739    +2595     
- Misses      11292    11426     +134     
Flag Coverage Δ
linux 86.09% <93.80%> (+0.05%) ⬆️
osx 75.76% <91.93%> (+0.09%) ⬆️
other 86.59% <93.80%> (+0.05%) ⬆️
win 83.92% <93.80%> (+0.07%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@blnicho blnicho requested review from emma58 and blnicho July 30, 2024 18:37
}


class text_nl_debug_template(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - don't need (object).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also this class name violates our own standards. I get that this is a legacy thing, but we may want to find places where we violate our own rules at some point.

_create_strict_inequality_map(vars())


class NLFragment(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - don't need (object).

return 'nl(' + self._node.name + ')'


class AMPLRepn(object):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - don't need (object).

pyomo/repn/ampl.py Outdated Show resolved Hide resolved
Copy link
Contributor

@mrmundt mrmundt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still want you to remove the objects but I won't let it hold this up...

Copy link
Contributor

@emma58 emma58 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One docstring request, but this looks good.

pyomo/repn/ampl.py Outdated Show resolved Hide resolved
Co-authored-by: Bethany Nicholson <[email protected]>
Copy link
Member

@blnicho blnicho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found one other typo but otherwise this looks fine

pyomo/repn/ampl.py Outdated Show resolved Hide resolved
Co-authored-by: Bethany Nicholson <[email protected]>
@jsiirola jsiirola merged commit ccc96bf into Pyomo:main Aug 19, 2024
32 checks passed
@jsiirola jsiirola deleted the nlv2_threading_fix branch August 19, 2024 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Error running Pyomo with threading
4 participants