def test_get_rank_bounds(self): def _run_(arr, nproc): desired = arr.sum() actual = 0 length = len(arr) for pet in range(nproc + 3): bounds = get_rank_bounds(length, nproc, pet) if bounds is None: try: self.assertTrue(pet >= (nproc - length) or (nproc > length and pet >= length)) self.assertTrue(length < nproc or pet >= nproc) except AssertionError: self.log.debug(' args: {}, {}, {}'.format( length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) raise else: actual += arr[bounds[0]:bounds[1]].sum() try: assert np.isclose(actual, desired) except AssertionError: self.log.debug(' args: {}, {}, {}'.format( length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) self.log.debug(' actual: {}'.format(actual)) self.log.debug('desired: {}'.format(desired)) raise lengths = [1, 2, 3, 4, 5, 6, 8, 100, 333, 1333, 10001] nproc = [1, 2, 3, 4, 5, 6, 8, 1000, 1333] for l, n in itertools.product(lengths, nproc): arr = np.random.rand(l) * 100.0 _run_(arr, n) # Test with Nones. res = get_rank_bounds(10, MPI_SIZE, MPI_RANK) self.assertEqual(res, (0, 10)) # Test outside the number of elements. res = get_rank_bounds(4, size=1000, rank=900) self.assertIsNone(res) # Test on the edge. ret = get_rank_bounds(5, size=8, rank=5) self.assertIsNone(ret) # Test with more elements than procs. _run_(np.arange(6), 5) # Test with rank higher than size. res = get_rank_bounds(6, size=5, rank=6) self.assertIsNone(res)
def test_get_rank_bounds(self): def _run_(arr, nproc): desired = arr.sum() actual = 0 length = len(arr) for pet in range(nproc + 3): bounds = get_rank_bounds(length, nproc, pet) if bounds is None: try: self.assertTrue(pet >= (nproc - length) or (nproc > length and pet >= length)) self.assertTrue(length < nproc or pet >= nproc) except AssertionError: self.log.debug(' args: {}, {}, {}'.format(length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) raise else: actual += arr[bounds[0]:bounds[1]].sum() try: assert np.isclose(actual, desired) except AssertionError: self.log.debug(' args: {}, {}, {}'.format(length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) self.log.debug(' actual: {}'.format(actual)) self.log.debug('desired: {}'.format(desired)) raise lengths = [1, 2, 3, 4, 5, 6, 8, 100, 333, 1333, 10001] nproc = [1, 2, 3, 4, 5, 6, 8, 1000, 1333] for l, n in itertools.product(lengths, nproc): arr = np.random.rand(l) * 100.0 _run_(arr, n) # Test with Nones. res = get_rank_bounds(10, MPI_SIZE, MPI_RANK) self.assertEqual(res, (0, 10)) # Test outside the number of elements. res = get_rank_bounds(4, size=1000, rank=900) self.assertIsNone(res) # Test on the edge. ret = get_rank_bounds(5, size=8, rank=5) self.assertIsNone(ret) # Test with more elements than procs. _run_(np.arange(6), 5) # Test with rank higher than size. res = get_rank_bounds(6, size=5, rank=6) self.assertIsNone(res)
def _run_(arr, nproc): desired = arr.sum() actual = 0 length = len(arr) for pet in range(nproc + 3): bounds = get_rank_bounds(length, nproc, pet) if bounds is None: try: self.assertTrue(pet >= (nproc - length) or (nproc > length and pet >= length)) self.assertTrue(length < nproc or pet >= nproc) except AssertionError: self.log.debug(' args: {}, {}, {}'.format(length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) raise else: actual += arr[bounds[0]:bounds[1]].sum() try: assert np.isclose(actual, desired) except AssertionError: self.log.debug(' args: {}, {}, {}'.format(length, nproc, pet)) self.log.debug(' bounds: {}'.format(bounds)) self.log.debug(' actual: {}'.format(actual)) self.log.debug('desired: {}'.format(desired)) raise