def test_cyclic_simple(self):
        """Can we compute local indices for a cyclic distribution?"""
        distribution = Distribution.from_shape(comm=self.comm,
                                        shape=(10,), dist={0: 'c'})
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (10,))
        self.assertEqual(la.grid_shape, (4,))

        if la.comm_rank == 0:
            gis = (0, 4, 8)
            self.assertEqual(la.local_shape, (3,))
            calc_result = [la.local_from_global((gi,)) for gi in gis]
            result = [(0,), (1,), (2,)]
        elif la.comm_rank == 1:
            gis = (1, 5, 9)
            self.assertEqual(la.local_shape, (3,))
            calc_result = [la.local_from_global((gi,)) for gi in gis]
            result = [(0,), (1,), (2,)]
        elif la.comm_rank == 2:
            gis = (2, 6)
            self.assertEqual(la.local_shape, (2,))
            calc_result = [la.local_from_global((gi,)) for gi in gis]
            result = [(0,), (1,)]
        elif la.comm_rank == 3:
            gis = (3, 7)
            self.assertEqual(la.local_shape, (2,))
            calc_result = [la.local_from_global((gi,)) for gi in gis]
            result = [(0,), (1,)]

        self.assertEqual(result, calc_result)
Example #2
0
def randint(low, high=None, distribution=None):
    """ Return random integers from low (inclusive) to high (exclusive).

    Return random integers from the “discrete uniform” distribution in the “half-open” interval [low, high).
    If high is None (the default), then results are from [0, low).

    Parameters
    ----------
    low : int
        Lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is the highest such integer).
    high : int, optional
        If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.randint(low, high)
    else:
        dtype = np.random.randint(low, high, size=1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.randint(low, high, size=la.local_shape)
        return la
Example #3
0
 def test_copy_cbc(self):
     distribution = Distribution(comm=self.comm,
                                 dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(12)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
Example #4
0
def randint(low, high=None, distribution=None):
    """ Return random integers from low (inclusive) to high (exclusive).

    Return random integers from the “discrete uniform” distribution in the “half-open” interval [low, high).
    If high is None (the default), then results are from [0, low).

    Parameters
    ----------
    low : int
        Lowest (signed) integer to be drawn from the distribution (unless high=None, in which case this parameter is the highest such integer).
    high : int, optional
        If provided, one above the largest (signed) integer to be drawn from the distribution (see above for behavior if high=None).
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.randint(low, high)
    else:
        dtype = np.random.randint(low, high, size=1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.randint(low, high, size=la.local_shape)
        return la
Example #5
0
    def test_add(self):
        """See if binary ufunc works for a LocalArray."""
        d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        a = LocalArray(d, dtype='int32')
        b = LocalArray(d, dtype='int32')
        a.fill(1)
        b.fill(1)
        c = localarray.add(a, b)
        self.assertTrue(np.all(c.ndarray == 2))

        c = localarray.empty_like(a)
        c = localarray.add(a, b, c)
        self.assertTrue(np.all(c.ndarray == 2))

        d0 = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        d1 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d0, dtype='int32')
        b = LocalArray(d1, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.add, a, b)

        d0 = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        d1 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d0, dtype='int32')
        b = LocalArray(d0, dtype='int32')
        c = LocalArray(d1, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.add, a, b, c)
Example #6
0
 def test_ndenumerate(self):
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(16, 16, 2),
                                 dist=('c', 'b', 'n'))
     a = LocalArray(d)
     for global_inds, value in ndenumerate(a):
         a.global_index[global_inds] = 0.0
 def test_copy_bn(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                     shape=(16, 16), dist=('b', 'n'))
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(11)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
class TestArrayConversion(ParallelTestCase):
    """ Test array conversion methods. """

    def setUp(self):
        # On Python3, an 'int' gets converted to 'np.int64' on copy,
        # so we force the numpy type to start with so we get back
        # the same thing.
        self.int_type = np.int64
        self.distribution = Distribution.from_shape(comm=self.comm,
                                                    shape=(4,))
        self.int_larr = LocalArray(self.distribution, dtype=self.int_type)
        self.int_larr.fill(3)

    def test_astype(self):
        """ Test that astype() works as expected. """
        # Convert int array to float.
        float_larr = self.int_larr.astype(float)
        for global_inds, value in ndenumerate(float_larr):
            self.assertEqual(value, 3.0)
            self.assertTrue(isinstance(value, float))
        # No type specification for a copy.
        # Should get same type as we started with.
        int_larr2 = self.int_larr.astype(None)
        for global_inds, value in ndenumerate(int_larr2):
            self.assertEqual(value, 3)
            self.assertTrue(isinstance(value, self.int_type))
Example #9
0
class TestArrayConversion(ParallelTestCase):
    """ Test array conversion methods. """
    def setUp(self):
        # On Python3, an 'int' gets converted to 'np.int64' on copy,
        # so we force the numpy type to start with so we get back
        # the same thing.
        self.int_type = np.int64
        self.distribution = Distribution.from_shape(comm=self.comm,
                                                    shape=(4, ))
        self.int_larr = LocalArray(self.distribution, dtype=self.int_type)
        self.int_larr.fill(3)

    def test_astype(self):
        """ Test that astype() works as expected. """
        # Convert int array to float.
        float_larr = self.int_larr.astype(float)
        for global_inds, value in ndenumerate(float_larr):
            self.assertEqual(value, 3.0)
            self.assertTrue(isinstance(value, float))
        # No type specification for a copy.
        # Should get same type as we started with.
        int_larr2 = self.int_larr.astype(None)
        for global_inds, value in ndenumerate(int_larr2):
            self.assertEqual(value, 3)
            self.assertTrue(isinstance(value, self.int_type))
 def test_pack_unpack_index(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                     shape=(16, 16, 2), dist=('c', 'b', 'n'))
     a = LocalArray(distribution)
     for global_inds, value in ndenumerate(a):
         packed_ind = a.pack_index(global_inds)
         self.assertEqual(global_inds, a.unpack_index(packed_ind))
Example #11
0
 def test_copy_bn(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                            shape=(16, 16),
                                            dist=('b', 'n'))
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(11)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
Example #12
0
 def test_pack_unpack_index(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                            shape=(16, 16, 2),
                                            dist=('c', 'b', 'n'))
     a = LocalArray(distribution)
     for global_inds, value in ndenumerate(a):
         packed_ind = a.pack_index(global_inds)
         self.assertEqual(global_inds, a.unpack_index(packed_ind))
Example #13
0
 def test_abs(self):
     """See if unary ufunc works for a LocalArray."""
     d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
     a = LocalArray(d, dtype='int32')
     a.fill(-5)
     a[2, 3] = 11
     b = abs(a)
     self.assertTrue(np.all(abs(a.ndarray) == b.ndarray))
Example #14
0
 def test_abs(self):
     """See if unary ufunc works for a LocalArray."""
     d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
     a = LocalArray(d, dtype='int32')
     a.fill(-5)
     a[2, 3] = 11
     b = abs(a)
     self.assertTrue(np.all(abs(a.ndarray) == b.ndarray))
Example #15
0
 def setUp(self):
     # On Python3, an 'int' gets converted to 'np.int64' on copy,
     # so we force the numpy type to start with so we get back
     # the same thing.
     self.int_type = np.int64
     self.distribution = Distribution.from_shape(comm=self.comm,
                                                 shape=(4, ))
     self.int_larr = LocalArray(self.distribution, dtype=self.int_type)
     self.int_larr.fill(3)
 def test_astype_cbc(self):
     new_dtype = np.int8
     d = Distribution(comm=self.comm, dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(d, dtype=np.int32)
     a.fill(12)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
Example #17
0
    def setUp(self):
        self.dist_1d = Distribution.from_shape(comm=self.comm,
                                               shape=(7, ),
                                               grid_shape=(4, ))
        self.larr_1d = LocalArray(self.dist_1d, buf=None)

        self.dist_2d = Distribution.from_shape(comm=self.comm,
                                               shape=(16, 16),
                                               grid_shape=(4, 1))
        self.larr_2d = LocalArray(self.dist_2d, buf=None)
Example #18
0
 def test_astype_cbc(self):
     new_dtype = np.int8
     d = Distribution(comm=self.comm,
                      dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(d, dtype=np.int32)
     a.fill(12)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
 def test_astype_bn(self):
     new_dtype = np.float32
     d = Distribution.from_shape(comm=self.comm,
                          shape=(16, 16), dist=('b', 'n'))
     a = LocalArray(d, dtype=np.int_)
     a.fill(11)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
Example #20
0
 def test_astype_bn(self):
     new_dtype = np.float32
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(16, 16),
                                 dist=('b', 'n'))
     a = LocalArray(d, dtype=np.int_)
     a.fill(11)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
 def test_global_limits_cyclic(self):
     """Find the boundaries of a cyclic distribution"""
     d = Distribution.from_shape(comm=self.comm,
                         shape=(16, 16), dist=('c', 'n'))
     a = LocalArray(d)
     answers = [(0, 12), (1, 13), (2, 14), (3, 15)]
     limits = a.global_limits(0)
     self.assertEqual(limits, answers[a.comm_rank])
     answers = 4 * [(0, 15)]
     limits = a.global_limits(1)
     self.assertEqual(limits, answers[a.comm_rank])
Example #22
0
 def test_global_limits_cyclic(self):
     """Find the boundaries of a cyclic distribution"""
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(16, 16),
                                 dist=('c', 'n'))
     a = LocalArray(d)
     answers = [(0, 12), (1, 13), (2, 14), (3, 15)]
     limits = a.global_limits(0)
     self.assertEqual(limits, answers[a.comm_rank])
     answers = 4 * [(0, 15)]
     limits = a.global_limits(1)
     self.assertEqual(limits, answers[a.comm_rank])
    def test_arecompatible(self):
        """Test if two DistArrays are compatible."""
        d0 = Distribution.from_shape(comm=self.comm, shape=(16,16))
        a = LocalArray(d0, dtype='int64')
        b = LocalArray(d0, dtype='float32')
        self.assertTrue(arecompatible(a,b))

        da = Distribution.from_shape(comm=self.comm, shape=(16, 16), dist='c')
        a = LocalArray(da, dtype='int64')
        db = Distribution.from_shape(comm=self.comm, shape=(16, 16), dist='b')
        b = LocalArray(db, dtype='float32')
        self.assertFalse(arecompatible(a,b))
    def test_global_limits_block(self):
        """Find the boundaries of a block distribution"""
        d = Distribution.from_shape(comm=self.comm,
                            shape=(16, 16), dist=('b', 'n'))
        a = LocalArray(d)

        answers = [(0, 3), (4, 7), (8, 11), (12, 15)]
        limits = a.global_limits(0)
        self.assertEqual(limits, answers[a.comm_rank])

        answers = 4 * [(0, 15)]
        limits = a.global_limits(1)
        self.assertEqual(limits, answers[a.comm_rank])
    def test_block_simple(self):
        """Can we compute local indices for a block distribution?"""
        distribution = Distribution.from_shape(comm=self.comm, shape=(4, 4))
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (4, 4))
        self.assertEqual(la.grid_shape, (4, 1))
        self.assertEqual(la.local_shape, (1, 4))
        row_result = [(0, 0), (0, 1), (0, 2), (0, 3)]

        row = la.comm_rank
        calc_row_result = [la.local_from_global((row, col)) for col in
                           range(la.global_shape[1])]
        self.assertEqual(row_result, calc_row_result)
 def test_indexing_1(self):
     """Can we get and set local elements for a complex dist?"""
     distribution = Distribution.from_shape(comm=self.comm,
                                     shape=(16, 16, 2), dist=('c', 'b', 'n'))
     a = LocalArray(distribution)
     b = LocalArray(distribution)
     for i, value in ndenumerate(a):
         a.global_index[i] = 0.0
     for i, value in ndenumerate(a):
         b.global_index[i] = a.global_index[i]
     for i, value in ndenumerate(a):
         self.assertEqual(b.global_index[i], a.global_index[i])
         self.assertEqual(a.global_index[i], 0.0)
Example #27
0
    def test_global_limits_block(self):
        """Find the boundaries of a block distribution"""
        d = Distribution.from_shape(comm=self.comm,
                                    shape=(16, 16),
                                    dist=('b', 'n'))
        a = LocalArray(d)

        answers = [(0, 3), (4, 7), (8, 11), (12, 15)]
        limits = a.global_limits(0)
        self.assertEqual(limits, answers[a.comm_rank])

        answers = 4 * [(0, 15)]
        limits = a.global_limits(1)
        self.assertEqual(limits, answers[a.comm_rank])
    def test_asdist_like(self):
        """Test asdist_like for success and failure."""
        d = Distribution.from_shape(comm=self.comm,
                             shape=(16, 16), dist=('b', 'n'))
        a = LocalArray(d)
        b = LocalArray(d)
        new_a = a.asdist_like(b)
        self.assertEqual(id(a), id(new_a))

        d2 = Distribution.from_shape(comm=self.comm,
                              shape=(16, 16), dist=('n', 'b'))
        a = LocalArray(d)
        b = LocalArray(d2)
        self.assertRaises(IncompatibleArrayError, a.asdist_like, b)
Example #29
0
    def test_block_simple(self):
        """Can we compute local indices for a block distribution?"""
        distribution = Distribution.from_shape(comm=self.comm, shape=(4, 4))
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (4, 4))
        self.assertEqual(la.grid_shape, (4, 1))
        self.assertEqual(la.local_shape, (1, 4))
        row_result = [(0, 0), (0, 1), (0, 2), (0, 3)]

        row = la.comm_rank
        calc_row_result = [
            la.local_from_global((row, col))
            for col in range(la.global_shape[1])
        ]
        self.assertEqual(row_result, calc_row_result)
Example #30
0
 def test_cyclic(self):
     """Can we go from global to local indices and back for cyclic?"""
     distribution = Distribution.from_shape(comm=self.comm,
                                            shape=(8, 8),
                                            dist=('c', 'n'))
     la = LocalArray(distribution)
     self.round_trip(la)
Example #31
0
 def test_crazy(self):
     """Can we go from global to local indices and back for a complex case?"""
     distribution = Distribution.from_shape(comm=self.comm,
                                            shape=(10, 100, 20),
                                            dist=('b', 'c', 'n'))
     la = LocalArray(distribution)
     self.round_trip(la)
Example #32
0
 def test_rank_coords(self):
     """ Test that we can go from rank to coords and back. """
     d = Distribution.from_shape(comm=self.comm, shape=(4, 4))
     la = LocalArray(d)
     max_size = self.comm.Get_size()
     for rank in range(max_size):
         self.round_trip(la, rank=rank)
Example #33
0
    def test_negative(self):
        """See if unary ufunc works for a LocalArray."""
        d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        a = LocalArray(d, dtype='int32')
        a.fill(1)
        b = localarray.negative(a)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        b = localarray.empty_like(a)
        b = localarray.negative(a, b)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        d2 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d, dtype='int32')
        b = LocalArray(d2, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.negative, b, a)
Example #34
0
    def setUp(self):
        global_size = 50
        if self.comm.Get_rank() == 0:
            local_size = 20
            arr = np.arange(local_size)
            dim_data = ({
                'dist_type': 'b',
                'size': global_size,
                'proc_grid_size': 2,
                'proc_grid_rank': 0,
                'start': 0,
                'stop': local_size,
            }, )
        elif self.comm.Get_rank() == 1:
            local_size = 30
            arr = np.arange(local_size)
            dim_data = ({
                'dist_type': 'b',
                'size': global_size,
                'proc_grid_size': 2,
                'proc_grid_rank': 1,
                'start': 20,
                'stop': global_size,
            }, )

        d = Distribution(comm=self.comm, dim_data=dim_data)
        self.larr = LocalArray(d, buf=arr)
 def test_fromndarray_like(self):
     """Can we build an array using fromndarray_like?"""
     d = Distribution.from_shape(comm=self.comm,
                         shape=(16, 16), dist=('c', 'b'))
     la0 = LocalArray(d, dtype='int8')
     la1 = localarray.fromndarray_like(la0.ndarray, la0)
     assert_localarrays_equal(la0, la1, check_dtype=True)
 def setUp(self):
     # On Python3, an 'int' gets converted to 'np.int64' on copy,
     # so we force the numpy type to start with so we get back
     # the same thing.
     self.int_type = np.int64
     self.distribution = Distribution.from_shape(comm=self.comm,
                                                 shape=(4,))
     self.int_larr = LocalArray(self.distribution, dtype=self.int_type)
     self.int_larr.fill(3)
Example #37
0
 def setUp(self):
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(16, 16),
                                 dist={
                                     0: 'b',
                                     1: 'n'
                                 },
                                 grid_shape=(4, 1))
     self.larr = LocalArray(d)
Example #38
0
 def setUp(self):
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(30, 60),
                                 dist={
                                     0: 'n',
                                     1: 'b'
                                 },
                                 grid_shape=(1, 4))
     self.larr = LocalArray(d)
Example #39
0
 def setUp(self):
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(53, 77),
                                 dist={
                                     0: 'c',
                                     1: 'b'
                                 },
                                 grid_shape=(2, 2))
     self.larr = LocalArray(d)
Example #40
0
 def setUp(self):
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(53, 77, 99),
                                 dist={
                                     0: 'b',
                                     1: 'n',
                                     2: 'c'
                                 },
                                 grid_shape=(2, 1, 2))
     self.larr = LocalArray(d, dtype='float64')
    def test_values(self):
        if self.comm.Get_rank() == 0:
            assert_array_equal(np.arange(20), self.larr.ndarray)
        elif self.comm.Get_rank() == 1:
            assert_array_equal(np.arange(30), self.larr.ndarray)

        larr = LocalArray.from_distarray(comm=self.comm, obj=self.larr)
        if self.comm.Get_rank() == 0:
            assert_array_equal(np.arange(20), larr.ndarray)
        elif self.comm.Get_rank() == 1:
            assert_array_equal(np.arange(30), larr.ndarray)
Example #42
0
def randn(distribution=None):
    """ Return a sample (or samples) from the “standard normal” distribution.

    Parameters
    ----------
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.randn()
    else:
        dtype = np.random.randn(1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.randn(*la.local_shape)
        return la
Example #43
0
def rand(distribution=None):
    """ Return an array with random numbers distributed over the interval [0, 1).

    Parameters
    ----------
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.rand()
    else:
        dtype = np.random.rand(1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.rand(*la.local_shape)
        return la
Example #44
0
    def test_block_complex(self):
        """Can we compute local indices for a block distribution?"""
        distribution = Distribution.from_shape(comm=self.comm, shape=(8, 2))
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (8, 2))
        self.assertEqual(la.grid_shape, (4, 1))
        self.assertEqual(la.local_shape, (2, 2))
        expected_lis = [(0, 0), (0, 1), (1, 0), (1, 1)]

        if la.comm_rank == 0:
            gis = [(0, 0), (0, 1), (1, 0), (1, 1)]
        elif la.comm_rank == 1:
            gis = [(2, 0), (2, 1), (3, 0), (3, 1)]
        elif la.comm_rank == 2:
            gis = [(4, 0), (4, 1), (5, 0), (5, 1)]
        elif la.comm_rank == 3:
            gis = [(6, 0), (6, 1), (7, 0), (7, 1)]

        result = [la.local_from_global(gi) for gi in gis]
        self.assertEqual(result, expected_lis)
Example #45
0
def randn(distribution=None):
    """ Return a sample (or samples) from the “standard normal” distribution.

    Parameters
    ----------
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.randn()
    else:
        dtype = np.random.randn(1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.randn(*la.local_shape)
        return la
Example #46
0
def rand(distribution=None):
    """ Return an array with random numbers distributed over the interval [0, 1).

    Parameters
    ----------
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.rand()
    else:
        dtype = np.random.rand(1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.rand(*la.local_shape)
        return la
Example #47
0
    def test_values(self):
        if self.comm.Get_rank() == 0:
            assert_array_equal(np.arange(20), self.larr.ndarray)
        elif self.comm.Get_rank() == 1:
            assert_array_equal(np.arange(30), self.larr.ndarray)

        larr = LocalArray.from_distarray(comm=self.comm, obj=self.larr)
        if self.comm.Get_rank() == 0:
            assert_array_equal(np.arange(20), larr.ndarray)
        elif self.comm.Get_rank() == 1:
            assert_array_equal(np.arange(30), larr.ndarray)
    def test_block_complex(self):
        """Can we compute local indices for a block distribution?"""
        distribution = Distribution.from_shape(comm=self.comm, shape=(8, 2))
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (8, 2))
        self.assertEqual(la.grid_shape, (4, 1))
        self.assertEqual(la.local_shape, (2, 2))
        expected_lis = [(0, 0), (0, 1), (1, 0), (1, 1)]

        if la.comm_rank == 0:
            gis = [(0, 0), (0, 1), (1, 0), (1, 1)]
        elif la.comm_rank == 1:
            gis = [(2, 0), (2, 1), (3, 0), (3, 1)]
        elif la.comm_rank == 2:
            gis = [(4, 0), (4, 1), (5, 0), (5, 1)]
        elif la.comm_rank == 3:
            gis = [(6, 0), (6, 1), (7, 0), (7, 1)]

        result = [la.local_from_global(gi) for gi in gis]
        self.assertEqual(result, expected_lis)
Example #49
0
    def test_cyclic_simple(self):
        """Can we compute local indices for a cyclic distribution?"""
        distribution = Distribution.from_shape(comm=self.comm,
                                               shape=(10, ),
                                               dist={0: 'c'})
        la = LocalArray(distribution)
        self.assertEqual(la.global_shape, (10, ))
        self.assertEqual(la.grid_shape, (4, ))

        if la.comm_rank == 0:
            gis = (0, 4, 8)
            self.assertEqual(la.local_shape, (3, ))
            calc_result = [la.local_from_global((gi, )) for gi in gis]
            result = [(0, ), (1, ), (2, )]
        elif la.comm_rank == 1:
            gis = (1, 5, 9)
            self.assertEqual(la.local_shape, (3, ))
            calc_result = [la.local_from_global((gi, )) for gi in gis]
            result = [(0, ), (1, ), (2, )]
        elif la.comm_rank == 2:
            gis = (2, 6)
            self.assertEqual(la.local_shape, (2, ))
            calc_result = [la.local_from_global((gi, )) for gi in gis]
            result = [(0, ), (1, )]
        elif la.comm_rank == 3:
            gis = (3, 7)
            self.assertEqual(la.local_shape, (2, ))
            calc_result = [la.local_from_global((gi, )) for gi in gis]
            result = [(0, ), (1, )]

        self.assertEqual(result, calc_result)
Example #50
0
def normal(loc=0.0, scale=1.0, distribution=None):
    """ Return an array with random numbers from a normal (Gaussian) probability distribution.

    Parameters
    ----------
    loc: float
        The mean (or center) of the probability distribution.
    scale: float
        The standard deviation (or width) of the probability distribution.
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.normal(loc, scale)
    else:
        dtype = np.random.normal(loc, scale, size=1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.normal(loc, scale, size=la.local_shape)
        return la
Example #51
0
def normal(loc=0.0, scale=1.0, distribution=None):
    """ Return an array with random numbers from a normal (Gaussian) probability distribution.

    Parameters
    ----------
    loc: float
        The mean (or center) of the probability distribution.
    scale: float
        The standard deviation (or width) of the probability distribution.
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.normal(loc, scale)
    else:
        dtype = np.random.normal(loc, scale, size=1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.normal(loc, scale, size=la.local_shape)
        return la
Example #52
0
def beta(a, b, distribution=None):
    """ Return an array with random numbers from the beta probability distribution.

    Parameters
    ----------
    a: float
        Parameter that describes the beta probability distribution.
    b: float
        Parameter that describes the beta probability distribution.
    distribution: The desired distribution of the array.
        If None, then a normal NumPy array is returned.
        Otherwise, a LocalArray with this distribution is returned.

    Returns
    -------
    An array with random numbers.
    """
    if distribution is None:
        return np.random.beta(a, b)
    else:
        dtype = np.random.beta(a, b, size=1).dtype
        la = LocalArray(distribution, dtype=dtype)
        la.ndarray[:] = np.random.beta(a, b, size=la.local_shape)
        return la
 def test_round_trip_equality_from_dict(self):
     larr = LocalArray.from_distarray(comm=self.comm, obj=self.larr.__distarray__())
     self.assert_round_trip_equality(larr)
 def test_ndenumerate(self):
     d = Distribution.from_shape(comm=self.comm,
                          shape=(16, 16, 2), dist=('c', 'b', 'n'))
     a = LocalArray(d)
     for global_inds, value in ndenumerate(a):
         a.global_index[global_inds] = 0.0
 def test_bad_global_limits(self):
     """ Test that invalid global_limits fails as expected. """
     d = Distribution.from_shape(comm=self.comm, shape=(4, 4))
     a = LocalArray(d)
     with self.assertRaises(InvalidDimensionError):
         a.global_limits(-1)
 def test_copy_cbc(self):
     distribution = Distribution(comm=self.comm, dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(12)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
 def test_round_trip_elements(self):
     larr = LocalArray.from_distarray(comm=self.comm, obj=self.larr)
     if self.comm.Get_rank() == 0:
         idx = (0,) * larr.ndarray.ndim
         larr.ndarray[idx] = 99
     assert_array_equal(larr.ndarray, self.larr.ndarray)