Ejemplo n.º 1
0
    def test_imul(self):

        row = np.array([0, 3, 1, 2, 3, 0])
        col = np.array([0, 0, 1, 2, 3, 3])
        data = np.array([2., 1, 3, 4, 5, 1])
        m = coo_matrix((data, (row, col)), shape=(4, 4))
        rank = comm.Get_rank()

        # create mpi matrix
        rank_ownership = [[0, -1], [-1, 1]]
        bm = MPIBlockMatrix(2, 2, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m)
        if rank == 1:
            bm.set_block(1, 1, m)

        serial_bm = BlockMatrix(2, 2)
        serial_bm.set_block(0, 0, m)
        serial_bm.set_block(1, 1, m)

        bm *= 2.0
        serial_bm *= 2.0

        rows, columns = np.nonzero(bm.ownership_mask)
        for i, j in zip(rows, columns):
            if bm.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        bm.get_block(i, j).toarray(),
                        serial_bm.get_block(i, j).toarray()))
Ejemplo n.º 2
0
    def test_isub(self):

        row = np.array([0, 3, 1, 2, 3, 0])
        col = np.array([0, 0, 1, 2, 3, 3])
        data = np.array([2., 1, 3, 4, 5, 1])
        m = coo_matrix((data, (row, col)), shape=(4, 4))
        rank = comm.Get_rank()

        # create mpi matrix
        rank_ownership = [[0, -1], [-1, 1]]
        bm = MPIBlockMatrix(2, 2, rank_ownership, comm)
        if rank == 0:
            bm.set_block(0, 0, m.copy())
        if rank == 1:
            bm.set_block(1, 1, m.copy())
        bm.broadcast_block_sizes()

        serial_bm = BlockMatrix(2, 2)
        serial_bm.set_block(0, 0, m.copy())
        serial_bm.set_block(1, 1, m.copy())

        bm -= bm
        serial_bm -= serial_bm

        rows, columns = np.nonzero(bm.ownership_mask)
        for i, j in zip(rows, columns):
            if bm.get_block(i, j) is not None:
                self.assertTrue(np.allclose(bm.get_block(i, j).toarray(),
                                            serial_bm.get_block(i, j).toarray()))

        with self.assertRaises(Exception) as context:
            bm -= serial_bm
Ejemplo n.º 3
0
    def test_add(self):

        mat1 = self.square_mpi_mat
        mat2 = self.square_mpi_mat2

        serial_mat1 = self.square_serial_mat
        serial_mat2 = self.square_serial_mat2

        res = mat1 + mat1
        serial_res = serial_mat1 + serial_mat1
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership))
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray(),
                        serial_res.get_block(i, j).toarray()))
            else:
                self.assertIsNone(serial_res.get_block(i, j))

        res = mat1 + mat2
        serial_res = serial_mat1 + serial_mat2
        self.assertIsInstance(res, MPIBlockMatrix)
        rows, columns = np.nonzero(res.ownership_mask)
        self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership))
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray(),
                        serial_res.get_block(i, j).toarray()))
            else:
                self.assertIsNone(serial_res.get_block(i, j))

        with self.assertRaises(Exception) as context:
            res = mat1 + serial_mat2

        with self.assertRaises(Exception) as context:
            res = serial_mat2 + mat1

        with self.assertRaises(Exception) as context:
            res = mat1 + serial_mat2.tocoo()

        with self.assertRaises(Exception) as context:
            res = serial_mat2.tocoo() + mat1
Ejemplo n.º 4
0
    def test_eq(self):

        mat1 = self.square_mpi_mat
        mat2 = self.square_mpi_mat2

        serial_mat1 = self.square_serial_mat
        serial_mat2 = self.square_serial_mat2

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            res = mat1 == mat2
            serial_res = serial_mat1 == serial_mat2

            self.assertIsInstance(res, MPIBlockMatrix)
            self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership))
            rows, columns = np.nonzero(res.ownership_mask)
            for i, j in zip(rows, columns):
                if res.get_block(i, j) is not None:
                    self.assertTrue(np.allclose(res.get_block(i, j).toarray(),
                                                serial_res.get_block(i, j).toarray()))
                else:
                    self.assertIsNone(serial_res.get_block(i, j))

        with self.assertRaises(Exception) as context:
            res = mat1 == serial_mat2

        mat1 = self.rectangular_mpi_mat
        serial_mat1 = self.rectangular_serial_mat

        with warnings.catch_warnings():
            warnings.simplefilter("ignore")
            res = mat1 == mat1
            serial_res = serial_mat1 == serial_mat1

            self.assertIsInstance(res, MPIBlockMatrix)
            self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership))
            rows, columns = np.nonzero(res.ownership_mask)
            for i, j in zip(rows, columns):
                if res.get_block(i, j) is not None:
                    self.assertTrue(np.allclose(res.get_block(i, j).toarray(),
                                                serial_res.get_block(i, j).toarray()))
                else:
                    self.assertIsNone(serial_res.get_block(i, j))

        with self.assertRaises(Exception) as context:
            res = mat1 == serial_mat1
Ejemplo n.º 5
0
 def _compare_mpi_and_serial_block_matrices(self, mpi_mat, serial_mat):
     self.assertIsInstance(mpi_mat, MPIBlockMatrix)
     rows, columns = np.nonzero(mpi_mat.ownership_mask)
     for i, j in zip(rows, columns):
         if mpi_mat.get_block(i, j) is not None:
             self.assertTrue(np.allclose(mpi_mat.get_block(i, j).toarray(),
                                         serial_mat.get_block(i, j).toarray()))
         else:
             self.assertIsNone(serial_mat.get_block(i, j))
Ejemplo n.º 6
0
    def test_div(self):

        mat1 = self.square_mpi_mat
        serial_mat1 = self.square_serial_mat

        res =  mat1 / 3.0
        serial_res = serial_mat1 / 3.0

        self.assertIsInstance(res, MPIBlockMatrix)
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(np.allclose(res.get_block(i, j).toarray(),
                                            serial_res.get_block(i, j).toarray()))
            else:
                self.assertIsNone(serial_res.get_block(i, j))
Ejemplo n.º 7
0
    def test_mul(self):

        mat1 = self.square_mpi_mat
        mat2 = self.square_mpi_mat2

        serial_mat1 = self.square_serial_mat
        serial_mat2 = self.square_serial_mat2

        rank = comm.Get_rank()

        bv1 = MPIBlockVector(2, [0, 1], comm)

        if rank == 0:
            bv1.set_block(0, np.arange(4, dtype=np.float64))
        if rank == 1:
            bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)
        bv1.broadcast_block_sizes()

        serial_bv1 = BlockVector(2)
        serial_bv1.set_block(0, np.arange(4, dtype=np.float64))
        serial_bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)

        res = mat1 * bv1
        serial_res = serial_mat1 * serial_bv1
        self.assertIsInstance(res, BlockVector)
        self.assertEqual(res.nblocks, serial_res.nblocks)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        res = mat2 * bv1
        serial_res = serial_mat2 * serial_bv1
        self.assertIsInstance(res, BlockVector)
        self.assertEqual(res.nblocks, serial_res.nblocks)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        bv1 = MPIBlockVector(2, [0, -1], comm)

        if rank == 0:
            bv1.set_block(0, np.arange(4, dtype=np.float64))
        bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)
        bv1.broadcast_block_sizes()

        res = mat1 * bv1
        serial_res = serial_mat1 * serial_bv1
        self.assertIsInstance(res, BlockVector)
        self.assertEqual(res.nblocks, serial_res.nblocks)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        res = mat2 * bv1
        serial_res = serial_mat2 * serial_bv1
        self.assertIsInstance(res, BlockVector)
        self.assertEqual(res.nblocks, serial_res.nblocks)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        # rectangular matrix
        mat1 = self.rectangular_mpi_mat
        serial_mat1 = self.rectangular_serial_mat

        bv1 = MPIBlockVector(3, [0, 1, 2], comm)

        if rank == 0:
            bv1.set_block(0, np.arange(4, dtype=np.float64))
        if rank == 1:
            bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)
        if rank == 2:
            bv1.set_block(2, np.arange(2, dtype=np.float64) + 8)

        bv1.broadcast_block_sizes()

        serial_bv1 = BlockVector(3)
        serial_bv1.set_block(0, np.arange(4, dtype=np.float64))
        serial_bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)
        serial_bv1.set_block(2, np.arange(2, dtype=np.float64) + 8)

        # with warnings.catch_warnings():
        #     warnings.simplefilter("ignore")
        res = mat1 * bv1
        serial_res = serial_mat1 * serial_bv1

        self.assertIsInstance(res, BlockVector)
        self.assertEqual(serial_res.nblocks, 2)
        self.assertEqual(res.nblocks, 2)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        bv1 = MPIBlockVector(3, [0, 1, 0], comm)

        if rank == 0:
            bv1.set_block(0, np.arange(4, dtype=np.float64))
            bv1.set_block(2, np.arange(2, dtype=np.float64) + 8)
        if rank == 1:
            bv1.set_block(1, np.arange(4, dtype=np.float64) + 4)
        bv1.broadcast_block_sizes()

        res = mat1 * bv1
        serial_res = serial_mat1 * serial_bv1
        self.assertIsInstance(res, BlockVector)
        self.assertEqual(res.nblocks, serial_res.nblocks)
        for bid in range(serial_res.nblocks):
            self.assertTrue(
                np.allclose(res.get_block(bid), serial_res.get_block(bid)))

        res = mat1 * 3.0
        serial_res = serial_mat1 * 3.0
        self.assertIsInstance(res, MPIBlockMatrix)
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray(),
                        serial_res.get_block(i, j).toarray()))
            else:
                self.assertIsNone(serial_res.get_block(i, j))

        res = 3.0 * mat1
        serial_res = serial_mat1 * 3.0

        self.assertIsInstance(res, MPIBlockMatrix)
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray(),
                        serial_res.get_block(i, j).toarray()))
            else:
                self.assertIsNone(serial_res.get_block(i, j))
Ejemplo n.º 8
0
    def test_transpose(self):

        mat1 = self.square_mpi_mat
        mat2 = self.rectangular_mpi_mat

        res = mat1.transpose()
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat1.bshape[1], res.bshape[0])
        self.assertEqual(mat1.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat1.get_block(j, i).toarray()))

        res = mat2.transpose()
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat2.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat2.bshape[1], res.bshape[0])
        self.assertEqual(mat2.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat2.get_block(j, i).toarray()))

        res = mat1.transpose(copy=True)
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat1.bshape[1], res.bshape[0])
        self.assertEqual(mat1.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat1.get_block(j, i).toarray()))

        res = mat2.transpose(copy=True)
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat2.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat2.bshape[1], res.bshape[0])
        self.assertEqual(mat2.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat2.get_block(j, i).toarray()))

        res = mat1.T
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat1.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat1.bshape[1], res.bshape[0])
        self.assertEqual(mat1.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat1.get_block(j, i).toarray()))

        res = mat2.T
        self.assertIsInstance(res, MPIBlockMatrix)
        self.assertTrue(np.allclose(mat2.rank_ownership, res.rank_ownership.T))
        self.assertEqual(mat2.bshape[1], res.bshape[0])
        self.assertEqual(mat2.bshape[0], res.bshape[1])
        rows, columns = np.nonzero(res.ownership_mask)
        for i, j in zip(rows, columns):
            if res.get_block(i, j) is not None:
                self.assertTrue(
                    np.allclose(
                        res.get_block(i, j).toarray().T,
                        mat2.get_block(j, i).toarray()))