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

make shared_intermediates thread-safe #53

Merged
merged 3 commits into from
Aug 25, 2018

Conversation

jcmgray
Copy link
Collaborator

@jcmgray jcmgray commented Aug 24, 2018

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

  • Implement a python2/3 compatible test

Status

  • Ready to go

@jcmgray jcmgray changed the title switch _SHARING_STACK to dict make shared_intermediates thread-safe Aug 24, 2018
@codecov-io
Copy link

codecov-io commented Aug 24, 2018

Codecov Report

Merging #53 into master will decrease coverage by 0.27%.
The diff coverage is 88.88%.

def currently_sharing():
"""Check if we are currently sharing a cache -- thread specific.
"""
return threading.get_ident() in _SHARING_STACK
Copy link
Contributor

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

Copy link
Collaborator Author

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.


from .parser import alpha_canonicalize, parse_einsum_input

_SHARING_STACK = []

_SHARING_STACK = {}
Copy link
Contributor

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes will do.


def _remove_sharing_cache(cache):
tid = threading.get_ident()
_SHARING_STACK[tid].remove(cache)
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

@jcmgray
Copy link
Collaborator Author

jcmgray commented Aug 24, 2018

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.

Copy link
Contributor

@fritzo fritzo left a 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!

Copy link
Owner

@dgasmith dgasmith left a 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.

@dgasmith dgasmith merged commit 89997c3 into dgasmith:master Aug 25, 2018
@jcmgray
Copy link
Collaborator Author

jcmgray commented Aug 25, 2018

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).

@dgasmith dgasmith mentioned this pull request Aug 25, 2018
3 tasks
@dgasmith dgasmith added this to the v2.2 milestone Aug 25, 2018
@dgasmith
Copy link
Owner

Good point, currently doing a docs pass and I will add that.

@jcmgray jcmgray deleted the threadsafe-sharing branch August 28, 2018 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants