當前位置: 首頁>>代碼示例>>Python>>正文


Python LocalArray.set_localarray方法代碼示例

本文整理匯總了Python中distarray.local.localarray.LocalArray.set_localarray方法的典型用法代碼示例。如果您正苦於以下問題:Python LocalArray.set_localarray方法的具體用法?Python LocalArray.set_localarray怎麽用?Python LocalArray.set_localarray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在distarray.local.localarray.LocalArray的用法示例。


在下文中一共展示了LocalArray.set_localarray方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: TestInit

# 需要導入模塊: from distarray.local.localarray import LocalArray [as 別名]
# 或者: from distarray.local.localarray.LocalArray import set_localarray [as 別名]
class TestInit(MpiTestCase):

    """Is the __init__ method working properly?"""

    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)

    def test_basic_1d(self):
        """Test basic LocalArray creation."""
        self.assertEqual(self.larr_1d.global_shape, (7,))
        self.assertEqual(self.larr_1d.dist, ('b',))
        self.assertEqual(self.larr_1d.grid_shape, (4,))
        self.assertEqual(self.larr_1d.comm_size, 4)
        self.assertTrue(self.larr_1d.comm_rank in range(4))
        self.assertEqual(len(self.larr_1d.distribution), 1)
        self.assertEqual(self.larr_1d.global_shape, (7,))
        if self.larr_1d.comm_rank == 3:
            self.assertEqual(self.larr_1d.local_shape, (1,))
        else:
            self.assertEqual(self.larr_1d.local_shape, (2,))
        self.assertEqual(self.larr_1d.ndarray.shape, self.larr_1d.local_shape)
        self.assertEqual(self.larr_1d.ndarray.size, self.larr_1d.local_size)
        self.assertEqual(self.larr_1d.local_size, self.larr_1d.local_shape[0])
        self.assertEqual(self.larr_1d.ndarray.dtype, self.larr_1d.dtype)

    def test_basic_2d(self):
        """Test basic LocalArray creation."""
        self.assertEqual(self.larr_2d.global_shape, (16, 16))
        self.assertEqual(self.larr_2d.dist, ('b', 'b'))
        self.assertEqual(self.larr_2d.grid_shape, (4, 1))
        self.assertEqual(self.larr_2d.comm_size, 4)
        self.assertTrue(self.larr_2d.comm_rank in range(4))
        self.assertEqual(len(self.larr_2d.distribution), 2)
        self.assertEqual(self.larr_2d.grid_shape, (4, 1))
        self.assertEqual(self.larr_2d.global_shape, (16, 16))
        self.assertEqual(self.larr_2d.local_shape, (4, 16))
        self.assertEqual(self.larr_2d.local_size,
                         np.array(self.larr_2d.local_shape).prod())
        self.assertEqual(self.larr_2d.ndarray.shape, self.larr_2d.local_shape)
        self.assertEqual(self.larr_2d.ndarray.size, self.larr_2d.local_size)
        self.assertEqual(self.larr_2d.ndarray.dtype, self.larr_2d.dtype)

    def test_localarray(self):
        """Can the ndarray be set and get?"""
        self.larr_2d.get_localarray()
        la = np.random.random(self.larr_2d.local_shape)
        la = np.asarray(la, dtype=self.larr_2d.dtype)
        self.larr_2d.set_localarray(la)
        self.larr_2d.get_localarray()

    def test_bad_localarray(self):
        """ Test that setting a bad local array fails as expected. """
        self.larr_1d.get_localarray()
        local_shape = self.larr_1d.local_shape
        # Double dimension sizes to make an invalid shape.
        bad_shape = tuple(2 * size for size in local_shape)
        la = np.random.random(bad_shape)
        la = np.asarray(la, dtype=self.larr_1d.dtype)
        with self.assertRaises(ValueError):
            self.larr_1d.set_localarray(la)

    def test_cart_coords(self):
        """Test getting the cart_coords attribute"""
        actual_1d = self.larr_1d.cart_coords
        expected_1d = tuple(self.larr_1d.distribution.cart_coords)
        self.assertEqual(actual_1d, expected_1d)
        actual_2d = self.larr_2d.cart_coords
        expected_2d = tuple(self.larr_2d.distribution.cart_coords)
        self.assertEqual(actual_2d, expected_2d)
開發者ID:cowlicks,項目名稱:distarray,代碼行數:77,代碼來源:paralleltest_localarray.py


注:本文中的distarray.local.localarray.LocalArray.set_localarray方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。