def test_different_contexts(self): ctx1 = Context(targets=range(4)) ctx2 = Context(targets=range(3)) da1 = ctx1.ones((10,)) da2 = ctx2.ones((10,)) db1 = self.local_sin(da1) db2 = self.local_sin(da2) ndarr1 = db1.toarray() ndarr2 = db2.toarray() assert_array_equal(ndarr1, ndarr2)
def test_key_and_push_args(self): context = Context() da = context.ones((2, 2)) db = da*2 def dummy_func(*args, **kwargs): fn = lambda x: x db = DecoratorBase(fn) return db.key_and_push_args(args, kwargs) # Push some distarrays arg_keys1, kw_keys1 = dummy_func(da, db, foo=da, bar=db) # with some other data too arg_keys2, kw_keys2 = dummy_func(da, 'question', answer=42, foo=db) self.assertEqual(arg_keys1, "(%s, %s,)" % (da.key, db.key)) # assert we pushed the right key, keystr pair self.assertTrue("'foo': %s" % (da.key) in kw_keys1) self.assertTrue("'bar': %s" % (db.key) in kw_keys1) # lots of string manipulation to parse out the relevant pieces # of the python commands. self.assertEqual(arg_keys2[1: -2].split(', ')[0], da.key) _key = arg_keys2[1: -2].split(', ')[1] self.assertEqual(context._pull0(_key), 'question') self.assertTrue("'answer'" in kw_keys2) self.assertTrue("'foo'" in kw_keys2) self.assertTrue(db.key in kw_keys2)
class TestDistArrayCreation(IpclusterTestCase): """Test distarray creation methods""" def setUp(self): self.context = Context(self.client) def test_zeros(self): shape = (16, 16) zero_distarray = self.context.zeros(shape) zero_ndarray = numpy.zeros(shape) assert_array_equal(zero_distarray.tondarray(), zero_ndarray) def test_ones(self): shape = (16, 16) one_distarray = self.context.ones(shape) one_ndarray = numpy.ones(shape) assert_array_equal(one_distarray.tondarray(), one_ndarray) def test_empty(self): shape = (16, 16) empty_distarray = self.context.empty(shape) self.assertEqual(empty_distarray.shape, shape) def test_fromndarray(self): ndarr = numpy.arange(16).reshape(4, 4) distarr = self.context.fromndarray(ndarr) for (i, j), val in numpy.ndenumerate(ndarr): self.assertEqual(distarr[i, j], ndarr[i, j])
class TestDistArrayCreation(unittest.TestCase): """Test distarray creation methods""" def setUp(self): self.context = Context() def tearDown(self): self.context.close() def test___init__(self): shape = (100, 100) distribution = Distribution.from_shape(self.context, shape, ('b', 'c')) da = DistArray(distribution, dtype=int) da.fill(42) nda = numpy.empty(shape, dtype=int) nda.fill(42) assert_array_equal(da.tondarray(), nda) def test_zeros(self): shape = (16, 16) zero_distarray = self.context.zeros(shape) zero_ndarray = numpy.zeros(shape) assert_array_equal(zero_distarray.tondarray(), zero_ndarray) def test_ones(self): shape = (16, 16) one_distarray = self.context.ones(shape) one_ndarray = numpy.ones(shape) assert_array_equal(one_distarray.tondarray(), one_ndarray) def test_empty(self): shape = (16, 16) empty_distarray = self.context.empty(shape) self.assertEqual(empty_distarray.shape, shape) def test_fromndarray(self): ndarr = numpy.arange(16).reshape(4, 4) distarr = self.context.fromndarray(ndarr) for (i, j), val in numpy.ndenumerate(ndarr): self.assertEqual(distarr[i, j], ndarr[i, j]) def test_grid_rank(self): # regression test for issue #235 a = self.context.empty((4, 4, 4), dist=('b', 'n', 'b'), grid_shape=(1, 1, 4)) self.assertEqual(a.grid_shape, (1, 1, 4)) def test_fromfunction(self): fn = lambda i, j: i + j shape = (7, 9) expected = numpy.fromfunction(fn, shape, dtype=int) result = self.context.fromfunction(fn, shape, dtype=int) assert_array_equal(expected, result.tondarray())
def test_determine_context(self): context = Context() context2 = Context() # for cross Context checking da = context.ones((2, 2)) def dummy_func(*args, **kwargs): fn = lambda x: x db = DecoratorBase(fn) return db.determine_context(args, kwargs) self.assertEqual(dummy_func(6, 7, context), context) self.assertEqual(dummy_func('ab', da), context) self.assertEqual(dummy_func(a=da), context) self.assertEqual(dummy_func(context, a=da), context) self.assertRaises(TypeError, dummy_func, 'foo') self.assertRaises(ContextError, dummy_func, context, context2)