-
Notifications
You must be signed in to change notification settings - Fork 68
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
make shared_intermediates thread-safe #53
Conversation
opt_einsum/sharing.py
Outdated
def currently_sharing(): | ||
"""Check if we are currently sharing a cache -- thread specific. | ||
""" | ||
return threading.get_ident() in _SHARING_STACK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm in Python 2, threading.get_ident
does not exist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, just realized that! I'll have a quick look for an alternative.
opt_einsum/sharing.py
Outdated
|
||
from .parser import alpha_canonicalize, parse_einsum_input | ||
|
||
_SHARING_STACK = [] | ||
|
||
_SHARING_STACK = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making this a defaultdict(list)
to avoid the setdefault
complexity below?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes will do.
opt_einsum/sharing.py
Outdated
|
||
def _remove_sharing_cache(cache): | ||
tid = threading.get_ident() | ||
_SHARING_STACK[tid].remove(cache) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the advantage of .remove()
over .pop()
? If these are really stacks, then we should use .pop()
. If these are not really stacks, then we should use sets or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes should be exactly the same for the current behaviour, remove(cache)
felt more explicit but is probably unnecessary, I'll update it.
Thanks for those comments @fritzo! I've made those changes and added a test compatible with both py2/py3. Ready to go from my end. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for addressing this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think all of this looks good. My one note is that I have a feeling we are going to get an issue where someone wants to share intermediates across threads. But we can wait for that use case.
It doesn't happen automatically, but threads sharing a cache is already possible if you explicitly supply the cache to use (they'll each add it their own 'stack', but will all still be referencing the same object). |
Good point, currently doing a docs pass and I will add that. |
Description
Makes
shared_intermediates
thread safe, by storing the caches in a dict referenced by thread id. All threads can (still) share the same cache among themselves, if it is supplied to them all. Resolves #51. cc @fritzo.Todos
Status