Skip to content

Refactoring: nonce() and DISTARRAY_BASE_NAME #580

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

Merged
merged 4 commits into from
Aug 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions distarray/globalapi/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import numpy

from distarray.externals import six
from distarray import DISTARRAY_BASE_NAME
from distarray.globalapi import ipython_cleanup
from distarray.globalapi.distarray import DistArray
from distarray.globalapi.maps import Distribution, asdistribution

from distarray.globalapi.ipython_utils import IPythonClient
from distarray.utils import uid, DISTARRAY_BASE_NAME, has_exactly_one
from distarray.utils import uid, nonce, has_exactly_one
from distarray.localapi.proxyize import Proxy

# mpi context
Expand Down Expand Up @@ -815,7 +816,7 @@ def func_wrapper(func, apply_nonce, context_key, args, kwargs, autoproxyize):
# default arguments
args = () if args is None else args
kwargs = {} if kwargs is None else kwargs
apply_nonce = uid()[13:]
apply_nonce = nonce()
wrapped_args = (func, apply_nonce, self.context_key, args, kwargs, autoproxyize)

targets = self.targets if targets is None else targets
Expand Down Expand Up @@ -973,7 +974,7 @@ def apply(self, func, args=None, kwargs=None, targets=None, autoproxyize=False):
kwargs = {} if kwargs is None else kwargs
targets = self.targets if targets is None else targets

apply_nonce = uid()[13:]
apply_nonce = nonce()
apply_metadata = (apply_nonce, self.context_key)

if not isinstance(func, types.BuiltinFunctionType):
Expand Down
7 changes: 7 additions & 0 deletions distarray/localapi/proxyize.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ def cleanup(self):

class Proxyize(object):

"""Callable that, given an object, returns a Proxy object.

You must call `set_state` on the instance before you can "call" it.
"""

def __init__(self):
self.count = None
self.state = None
Expand All @@ -39,6 +44,7 @@ def set_state(self, state):
self.count = 0

def str_counter(self):
"""Return the str value of `self.count`, then increment its value."""
res = str(self.count)
self.count += 1
return res
Expand All @@ -50,5 +56,6 @@ def next_name(self):
return DISTARRAY_BASE_NAME + self.state + self.str_counter()

def __call__(self, obj):
"""Return a `Proxy` object given an object `obj`."""
new_name = self.next_name()
return Proxy(new_name, obj, '__main__')
4 changes: 2 additions & 2 deletions distarray/mpi_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ def __init__(self):

# make engines intracomm (Context._base_comm):
Engine.INTERCOMM = initial_comm_setup()
assert self.world.rank != 0
assert self.world.rank != self.client_rank
while True:
msg = Engine.INTERCOMM.recv(source=0)
msg = Engine.INTERCOMM.recv(source=self.client_rank)
val = self.parse_msg(msg)
if val == 'kill':
break
Expand Down
12 changes: 8 additions & 4 deletions distarray/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
import random
import uuid

from distarray import DISTARRAY_BASE_NAME
from distarray.externals.six import next

DISTARRAY_BASE_NAME = '__distarray__'

DISTARRAY_RANDOM = random.Random()


Expand All @@ -31,10 +32,13 @@ def distarray_random_getstate():
return DISTARRAY_RANDOM.getstate()


def nonce():
return uuid.UUID(int=DISTARRAY_RANDOM.getrandbits(8 * 16)).hex[:16]


def uid():
""" Get a unique name for a distarray object. """
suffix = uuid.UUID(int=DISTARRAY_RANDOM.getrandbits(8 * 16)).hex[:16]
return DISTARRAY_BASE_NAME + suffix
"""Get a unique name for a distarray object. """
return DISTARRAY_BASE_NAME + nonce()


def multi_for(iterables):
Expand Down