Skip to content

Commit

Permalink
Removed make_distinct argument, adjusted example.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkness committed Mar 31, 2014
1 parent 3250923 commit 0dfe677
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
17 changes: 7 additions & 10 deletions distarray/random.py
Expand Up @@ -15,7 +15,7 @@ def __init__(self, context):
self.context = context
self.context._execute('import distarray.local.random')

def seed(self, seed=None, make_distinct=True):
def seed(self, seed=None):
"""
Seed the random number generators on each engine.
Expand All @@ -26,19 +26,16 @@ def seed(self, seed=None, make_distinct=True):
If None, then a non-deterministic seed is obtained from the
operating system. Otherwise, the seed is used as passed,
and the sequence of random numbers will be deterministic.
make_distinct : bool
If True, then each engine has its state adjusted so that
it is different from each other engine. Then, each engine
Each individual engine has its state adjusted so that
it is different from each other engine. Thus, each engine
will compute a different sequence of random numbers.
If False, then each engine is given the same state,
and so each engine will generate the same sequence.
"""
cmd = 'numpy.random.seed(seed=%r)' % (seed)
self.context._execute(cmd)
if make_distinct:
cmd = 'distarray.local.random.label_state(%s)' % (
self.context._comm_key)
self.context._execute(cmd)
cmd = 'distarray.local.random.label_state(%s)' % (
self.context._comm_key)
self.context._execute(cmd)

def rand(self, size=None, dist={0: 'b'}, grid_shape=None):
"""
Expand Down
35 changes: 11 additions & 24 deletions distarray/tests/test_random.py
Expand Up @@ -56,13 +56,12 @@ def test_seed_same(self):
""" Test that the same seed generates the same sequence. """
shape = (8, 6)
seed = 0xfeedbeef
make_distinct = True
# Seed and get some random numbers.
self.random.seed(seed, make_distinct=make_distinct)
self.random.seed(seed)
a = self.random.rand(shape)
aa = a.toarray()
# Seed again and get more random numbers.
self.random.seed(seed, make_distinct=make_distinct)
self.random.seed(seed)
b = self.random.rand(shape)
bb = b.toarray()
# For an explicit seed, these should match exactly.
Expand All @@ -72,54 +71,42 @@ def test_seed_none(self):
""" Test that if seed=None, the sequences are not deterministic. """
shape = (8, 6)
# Seed and get some random numbers.
self.random.seed(None, make_distinct=True)
self.random.seed(None)
a = self.random.rand(shape)
aa = a.toarray()
# Seed again and get more random numbers.
self.random.seed(None, make_distinct=True)
self.random.seed(None)
b = self.random.rand(shape)
bb = b.toarray()
# For seed=None, these should *not* match.
self.assertFalse((aa == bb).all())

def test_rand_per_engine_distinct(self):
""" Test that, if make_distinct=True when seeding,
that each engine produces a different sequence of random numbers,
while if make_distinct=False, then each engine produces
the same sequence.
"""
def test_engines_distinct(self):
""" Test that each engine makes a different sequence of numbers. """

def get_rand_array_per_engine(context, num_cols):
""" Get a distarray of random numbers,
with each row coming from a separate engine.
"""
num_engines = len(context.targets)
shape = (num_engines, num_cols)
darr = self.random.rand(size=shape, dist={0: 'c', 1: 'n'})
darr = self.random.rand(size=shape, dist={0: 'b', 1: 'n'})
return darr

# Seed generators so that each engine is different.
# Seed generators.
seed = [0x12345678, 0xdeadbeef, 42]
self.random.seed(seed, make_distinct=True)
self.random.seed(seed)
# Get array of random values, with one row per engine.
a = get_rand_array_per_engine(self.context, 6)
aa = a.toarray()
# Each row should be different. We just test consecutive rows.
# If the rows are not different, then the generators on
# distinct engines are in the same state.
num_rows = aa.shape[0]
for r in range(num_rows - 1):
r0 = aa[r, :]
r1 = aa[r + 1, :]
self.assertFalse((r0 == r1).all())
# Now seed so that each engine is the same, and get another array.
self.random.seed(seed, make_distinct=False)
a = get_rand_array_per_engine(self.context, 6)
aa = a.toarray()
# Now each row should be the same. We just test consecutive rows.
num_rows = aa.shape[0]
for r in range(num_rows - 1):
r0 = aa[r, :]
r1 = aa[r + 1, :]
self.assertTrue((r0 == r1).all())


if __name__ == '__main__':
Expand Down

0 comments on commit 0dfe677

Please sign in to comment.