本文整理汇总了Python中distarray.dist.maps.Distribution类的典型用法代码示例。如果您正苦于以下问题:Python Distribution类的具体用法?Python Distribution怎么用?Python Distribution使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Distribution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_2D
def test_2D(self):
da = Distribution.from_shape(self.context, (3, 3))
a = self.context.empty(da)
db = Distribution.from_shape(self.context, (3, 3), dist=('n', 'b'))
b = self.context.empty(db)
self.assertEqual(a.grid_shape, (3, 1))
self.assertEqual(b.grid_shape, (1, 3))
示例2: from_localarrays
def from_localarrays(cls, key, context=None, targets=None, distribution=None,
dtype=None):
"""The caller has already created the LocalArray objects. `key` is
their name on the engines. This classmethod creates a DistArray that
refers to these LocalArrays.
Either a `context` or a `distribution` must also be provided. If
`context` is provided, a ``dim_data_per_rank`` will be pulled from
the existing ``LocalArray``\s and a ``Distribution`` will be created
from it. If `distribution` is provided, it should accurately
reflect the distribution of the existing ``LocalArray``\s.
If `dtype` is not provided, it will be fetched from the engines.
"""
def get_dim_datas_and_dtype(arr):
return (arr.dim_data, arr.dtype)
da = cls.__new__(cls)
da.key = key
if (context is None) == (distribution is None):
errmsg = "Must provide `context` or `distribution` but not both."
raise RuntimeError(errmsg)
# has context, get dist and dtype
elif (distribution is None) and (dtype is None):
res = context.apply(get_dim_datas_and_dtype, args=(key,))
dim_datas = [i[0] for i in res]
dtypes = [i[1] for i in res]
da._dtype = dtypes[0]
da.distribution = Distribution.from_dim_data_per_rank(context,
dim_datas,
targets)
# has context and dtype, get dist
elif (distribution is None) and (dtype is not None):
da._dtype = dtype
dim_datas = context.apply(getattr, args=(key, 'dim_data'))
da.distribution = Distribution.from_dim_data_per_rank(context,
dim_datas,
targets)
# has distribution, get dtype
elif (distribution is not None) and (dtype is None):
da.distribution = distribution
da._dtype = distribution.context.apply(getattr,
args=(key, 'dtype'),
targets=[0])[0]
# has distribution and dtype
elif (distribution is not None) and (dtype is not None):
da.distribution = distribution
da._dtype = dtype
# sanity check that I didn't miss any cases above, because this is a
# confusing function
else:
assert False
return da
示例3: test_different_contexts
def test_different_contexts(self):
ctx1 = Context(targets=range(4))
ctx2 = Context(targets=range(3))
distribution1 = Distribution.from_shape(ctx1, (10,))
distribution2 = Distribution.from_shape(ctx2, (10,))
da1 = ctx1.ones(distribution1)
da2 = ctx2.ones(distribution2)
db1 = self.local_sin(da1)
db2 = self.local_sin(da2)
ndarr1 = db1.toarray()
ndarr2 = db2.toarray()
assert_array_equal(ndarr1, ndarr2)
示例4: test_set_index_error
def test_set_index_error(self):
distribution = Distribution.from_shape(self.context, (10,), dist={0: 'c'})
dap = self.context.empty(distribution)
with self.assertRaises(IndexError):
dap[11] = 55
with self.assertRaises(IndexError):
dap[-11] = 55
示例5: fromfunction
def fromfunction(self, function, shape, **kwargs):
"""Create a DistArray from a function over global indices.
Unlike numpy's `fromfunction`, the result of distarray's
`fromfunction` is restricted to the same Distribution as the
index array generated from `shape`.
See numpy.fromfunction for more details.
"""
dtype = kwargs.get('dtype', None)
dist = kwargs.get('dist', None)
grid_shape = kwargs.get('grid_shape', None)
distribution = Distribution.from_shape(context=self,
shape=shape, dist=dist,
grid_shape=grid_shape)
ddpr = distribution.get_dim_data_per_rank()
function_name, ddpr_name, kwargs_name = \
self._key_and_push(function, ddpr, kwargs)
da_name = self._generate_key()
comm_name = self._comm_key
cmd = ('{da_name} = distarray.local.fromfunction({function_name}, '
'distarray.local.maps.Distribution('
'{ddpr_name}[{comm_name}.Get_rank()], comm={comm_name}),'
'**{kwargs_name})')
self._execute(cmd.format(**locals()))
return DistArray.from_localarrays(da_name, distribution=distribution)
示例6: test_global_tolocal_bug
def test_global_tolocal_bug(self):
# gh-issue #154
distribution = Distribution.from_shape(self.context, (3, 3),
dist=('n', 'b'))
dap = self.context.zeros(distribution)
ndarr = numpy.zeros((3, 3))
numpy.testing.assert_array_equal(dap.tondarray(), ndarr)
示例7: test_tondarray
def test_tondarray(self):
distribution = Distribution.from_shape(self.context, (3, 3))
dap = self.context.empty(distribution)
ndarr = numpy.arange(9).reshape(3, 3)
for (i, j), val in numpy.ndenumerate(ndarr):
dap[i, j] = ndarr[i, j]
numpy.testing.assert_array_equal(dap.tondarray(), ndarr)
示例8: test_randint
def test_randint(self):
low = 1
shape = (3, 4) # aka shape
distribution = Distribution.from_shape(context=self.context,
shape=shape)
a = self.random.randint(distribution, low=low)
self.assertEqual(a.shape, shape)
示例9: test_grid_rank
def test_grid_rank(self):
# regression test for issue #235
d = Distribution.from_shape(self.context, (4, 4, 4),
dist=('b', 'n', 'b'),
grid_shape=(1, 1, 4))
a = self.context.empty(d)
self.assertEqual(a.grid_shape, (1, 1, 4))
示例10: test_get_index_error
def test_get_index_error(self):
distribution = Distribution.from_shape(self.dac, (10,), dist={0: 'c'})
dap = self.dac.empty(distribution)
with self.assertRaises(IndexError):
dap[11]
with self.assertRaises(IndexError):
dap[-11]
示例11: test_key_and_push_args
def test_key_and_push_args(self):
context = Context()
distribution = Distribution.from_shape(context, (2, 2))
da = context.ones(distribution)
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)
示例12: test_load_nu
def test_load_nu(self):
distribution = Distribution.from_dim_data_per_rank(self.dac,
nu_test_data)
da = self.dac.load_npy(self.output_path, distribution)
for i in range(da.shape[0]):
for j in range(da.shape[1]):
self.assertEqual(da[i, j], self.expected[i, j])
示例13: test___init__
def test___init__(self):
shape = (5, 5)
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)
示例14: test_iteration
def test_iteration(self):
size = 10
distribution = Distribution.from_shape(self.context, (size,),
dist={0: 'c'})
dap = self.context.empty(distribution)
dap.fill(10)
for val in dap:
self.assertEqual(val, 10)
示例15: test_local_add_supermix
def test_local_add_supermix(self):
distribution = Distribution.from_shape(self.context, (5, 5))
dm = self.context.empty(distribution)
dm.fill(22)
dn = self.context.empty(distribution)
dn.fill(44)
do = self.local_add_supermix(self.da, 11, dm, 33, dc=dn, num3=55)
expected = 2 * numpy.pi + 11 + 22 + 33 + 44 + 55 + 66
self.assert_allclose(do, expected)