コード例 #1
0
    def test_order_2(self):
        # given
        A = M2(1, 2, 3, 4)
        B = M2(3, 5, 1, 0)

        # when
        C = matrix_add(A, B)

        # then
        assert_that(C, is_(M2(4, 7, 4, 4)))
コード例 #2
0
    def test_1_order_2(self):
        # given
        A = M2(1, 2, 3, 4)
        B = M2(3, 5, 1, 0)

        # when
        C = matrix_multiply(A, B)

        # then
        assert_that(C, is_(M2(5, 5, 13, 15)))
コード例 #3
0
    def test_2_order_2(self):
        # given
        A = M2(1, 2, 3, 4)
        B = M2(5, 6, 7, 8)

        # when
        C = matrix_multiply(A, B)

        # then
        assert_that(C, is_(M2(19, 22, 43, 50)))
コード例 #4
0
    def test_split_6x6_in_9_blocks(self):
        # given
        original = M6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                      17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                      31, 32, 33, 34, 35, 36)

        # when
        blocks = matrix_split(original, block_order=2)

        # then
        E00 = M2(1, 2, 7, 8)

        E01 = M2(3, 4, 9, 10)

        E02 = M2(5, 6, 11, 12)

        E10 = M2(13, 14, 19, 20)

        E11 = M2(15, 16, 21, 22)

        E12 = M2(17, 18, 23, 24)

        E20 = M2(25, 26, 31, 32)

        E21 = M2(27, 28, 33, 34)

        E22 = M2(29, 30, 35, 36)

        assert_that(blocks, is_([E00, E01, E02, E10, E11, E12, E20, E21, E22]))
コード例 #5
0
    def test_join_9_2x2_blocks_in_6x6_matrix(self):
        # given
        A0 = M2(1, 2, 7, 8)

        A1 = M2(3, 4, 9, 10)

        A2 = M2(5, 6, 11, 12)

        A3 = M2(13, 14, 19, 20)

        A4 = M2(15, 16, 21, 22)

        A5 = M2(17, 18, 23, 24)

        A6 = M2(25, 26, 31, 32)

        A7 = M2(27, 28, 33, 34)

        A8 = M2(29, 30, 35, 36)

        # when
        actual = matrix_join(A0, A1, A2, A3, A4, A5, A6, A7, A8)
        # then
        expected = M6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                      17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                      31, 32, 33, 34, 35, 36)

        assert_that(actual, is_(expected))
コード例 #6
0
    def test_vertical_shift_2x2_by_blocks_1x1(self):
        # given
        original = M2(1, 2, 3, 4)

        # when
        actual = matrix_vertical_shift(original, block_order=1)

        # then
        expected = M2(1, 4, 3, 2)

        assert_that(actual, is_(expected))
コード例 #7
0
	def test_processors_negative_block(self):
        # given
        P0 = ProcessorI()
        collector = Spy()

        A = M2(-1, 2,
               3, 4)
        B = M2(5, -6,
               -999, 3)
        C = M2(-2003, 12,
               -3981, -6)

        # when
        P0.init(0, 1, None, None, collector)
        P0.injectA(A, 0)
        P0.injectB(B, 0)

        # then
        assert_that(collector.inject, called().with_args(0, C, ANY_ARG))

	def test_processors_6x6_block(self):
        # given
        P0 = ProcessorI()
        collector = Spy()

        A = M6( 1 ,2 ,3 ,4 ,5 ,6  	
		,7 ,8 ,9 ,10 ,11 ,12  
		,13 ,14 ,15 ,16 ,17 ,18  
		,19 ,20 ,21 ,22 ,23 ,24  
		,25 ,26 ,27 ,28 ,29 ,30  
		,31 ,32 ,33 ,34 ,35,36 )
        
        B = M6(36 ,35 ,34 ,33 ,32 ,31  	
		,30 ,29 ,28 ,27 ,26 ,25  
		,24 ,23 ,22 ,21 ,20 ,19  
		,18 ,17 ,16 ,15 ,14 ,13  
		,12 ,11 ,10 ,9 ,8 ,7  
		,6 ,5 ,4 ,3 ,2 ,1  )

        C = M6(336 ,315 ,294 ,273 ,252 ,231 	
		,1092 ,1035 ,978 ,921 ,864 ,807 
		,1848 ,1755 ,1662 ,1569 ,1476 ,1383 
		,2604 ,2475 ,2346 ,2217 ,2088 ,1959 
		,3360 ,3195 ,3030 ,2865 ,2700 ,2535 
		,4116 ,3915 ,3714 ,3513 ,3312 ,3111)

        # when
        P0.init(0, 1, None, None, collector)
        P0.injectA(A, 0)
        P0.injectB(B, 0)

        # then
        assert_that(collector.inject, called().with_args(0, C, ANY_ARG))
コード例 #8
0
    def test_order_2_floats(self):
        # given
        A = M2(1.1, 2.3, 3.5, 4.6)
        B = M2(3.7, 5.1, 1.5, 1.0 / 3)

        # when
        C = matrix_multiply(A, B)

        # then
        assert_that(C.ncols, is_(2))
        assert_array_almost_equal(C.data, [7.52, 6.37, 19.85, 19.38],
                                  decimal=2)
コード例 #9
0
    def test_order_4_blocks_2x2(self):
        # given
        collector = CollectorI(order=2)
        collector.inject(0, M2(1, 2, 5, 6))
        collector.inject(1, M2(3, 4, 7, 8))
        collector.inject(2, M2(9, 10, 13, 14))

        # when
        collector.inject(3, M2(11, 12, 15, 16))

        # then
        expected = M4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

        assert_that(collector.get_result(), is_(expected))
コード例 #10
0
    def test_load_4x4_operands_in_2x2_processors(self):
        nprocs = 4

        # given
        A = M4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

        B = M4(17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)

        procs = [Spy(Cannon.Processor) for i in range(nprocs)]
        frontend = FrontendI(procs)

        # when
        frontend.load_processors(A, B)

        # then
        A_blocks = [
            M2(1, 2, 5, 6),
            M2(3, 4, 7, 8),
            M2(11, 12, 15, 16),
            M2(9, 10, 13, 14)
        ]

        B_blocks = [
            M2(17, 18, 21, 22),
            M2(27, 28, 31, 32),
            M2(25, 26, 29, 30),
            M2(19, 20, 23, 24)
        ]

        for i in range(nprocs):
            assert_that(procs[i].injectA, called().with_args(A_blocks[i], 0))
            assert_that(procs[i].injectB, called().with_args(B_blocks[i], 0))
コード例 #11
0
    def test_split_4x4_in_4_blocks(self):
        # given
        original = M4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

        # when
        actual = matrix_split(original, block_order=2)

        # then
        E00 = M2(1, 2, 5, 6)

        E01 = M2(3, 4, 7, 8)

        E10 = M2(9, 10, 13, 14)

        E11 = M2(11, 12, 15, 16)

        assert_that(actual, is_([E00, E01, E10, E11]))
コード例 #12
0
    def test_join_4_2x2_blocks_in_4x4_matrix(self):
        # given
        A0 = M2(1, 2, 5, 6)

        A1 = M2(3, 4, 7, 8)

        A2 = M2(9, 10, 13, 14)

        A3 = M2(11, 12, 15, 16)

        # when
        actual = matrix_join(A0, A1, A2, A3)

        # then
        expected = M4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)

        assert_that(actual, is_(expected))
コード例 #13
0
    def test_collector_called(self):
        # given
        processor = self.broker.add_servant(ProcessorI(), Cannon.ProcessorPrx)

        collector_servant = Mimic(Spy, Cannon.Collector)
        collector = self.broker.add_servant(collector_servant, Cannon.CollectorPrx)

        A = M2(1, 2, 3, 4)
        B = M2(5, 6, 7, 8)

        # when
        processor.init(1, 1, None, None, 1, collector)
        processor.injectFirst(A, 0)
        processor.injectSecond(B, 0)

        # then
        C = M2(19, 22, 43, 50)
        assert_that(collector_servant.injectSubmatrix,
                    called().asyncio(1).with_args(C, 1, 1, anything()))
コード例 #14
0
    def test_2x2_processors_2x2_operands(self):
        nprocs = 4

        # given
        P = [
            self.broker.add_servant(ProcessorI(), Cannon.ProcessorPrx)
            for i in range(nprocs)
        ]
        frontend = self.broker.add_servant(FrontendI(P), Cannon.FrontendPrx)

        A = M2(1, 2, 3, 4)
        B = M2(3, 5, 1, 0)

        # when
        C = frontend.multiply(A, B)

        # then
        expected = M2(5, 5, 13, 15)

        assert_that(C, is_(expected))
コード例 #15
0
    def test_2x2_processors_2x2_operands(self):
        nprocs = 4

        # given
        P = [
            self.broker.add_servant(ProcessorI(), Cannon.ProcessorPrx)
            for i in range(nprocs)
        ]
        loader = self.broker.add_servant(OperationsI(P), Cannon.OperationsPrx)

        A = M2(1, 2, 3, 4)
        B = M2(3, 5, 1, 0)

        # when
        C = loader.matrixMultiply(A, B)

        # then
        expected = M2(5, 5, 13, 15)

        assert_that(C, is_(expected))
コード例 #16
0
    def test_order_4_blocks_1x1(self):
        # given
        collector = CollectorI(order=2)
        collector.inject(0, M1(1))
        collector.inject(1, M1(2))
        collector.inject(2, M1(3))

        # when
        collector.inject(3, M1(4))

        # then
        assert_that(collector.get_result(), is_(M2(1, 2, 3, 4)))
コード例 #17
0
    def test_order_4_blocks_2x2(self):
        # given
        collector = CollectorI(order=2)
        collector.injectSubmatrix(M2(1, 2,
                                     5, 6), 0, 0)
        collector.injectSubmatrix(M2(3, 4,
                                     7, 8), 0, 1)
        collector.injectSubmatrix(M2(9, 10,
                                    13, 14), 1, 0)

        # when
        collector.injectSubmatrix(M2(11, 12,
                                     15, 16), 1, 1)

        # then
        expected = M4(1,  2,  3,  4,
                      5,  6,  7,  8,
                      9, 10, 11, 12,
                     13, 14, 15, 16)

        assert_that(collector.get_result(), is_(expected))
コード例 #18
0
    def test_load_2x2_operands_in_2x2_processors(self):
        nprocs = 4

        # given
        A = M2(1, 2, 3, 4)

        B = M2(5, 6, 7, 8)

        procs = [Spy(Cannon.Processor) for i in range(nprocs)]

        frontend = FrontendI(procs)

        # when
        frontend.load_processors(A, B)

        # then
        A_blocks = [M1(1), M1(2), M1(4), M1(3)]
        B_blocks = [M1(5), M1(8), M1(7), M1(6)]

        for i in range(nprocs):
            assert_that(procs[i].injectA, called().with_args(A_blocks[i], 0))
            assert_that(procs[i].injectB, called().with_args(B_blocks[i], 0))
コード例 #19
0
    def test_order_4_blocks_1x1(self):
        # given
        collector = CollectorI(order=2)
        collector.injectSubmatrix(M1(1), 0, 0)
        collector.injectSubmatrix(M1(2), 0, 1)
        collector.injectSubmatrix(M1(3), 1, 0)

        # when
        collector.injectSubmatrix(M1(4), 1, 1)

        # then
        assert_that(collector.get_result(), is_(M2(1, 2,
                                                   3, 4)))
コード例 #20
0
    def test_load_2x2_operands_in_2x2_processors(self):
        nprocs = 4

        # given
        A = M2(1, 2, 3, 4)

        B = M2(5, 6, 7, 8)

        procs = [Spy(Cannon.Processor) for i in range(nprocs)]

        loader = OperationsI(procs)

        # when
        loader.load_processors(A, B)

        # then
        A_blocks = [M1(1), M1(2), M1(4), M1(3)]
        B_blocks = [M1(5), M1(8), M1(7), M1(6)]

        for i in range(nprocs):
            assert_that(procs[i].injectFirst,
                        called().with_args(A_blocks[i], 0))
            assert_that(procs[i].injectSecond,
                        called().with_args(B_blocks[i], 0))
コード例 #21
0
    def test_join_16_2x2_blocks_in_8x8_matrix(self):
        # given
        A0 = M2(1, 2, 9, 10)

        A1 = M2(3, 4, 11, 12)

        A2 = M2(5, 6, 13, 14)

        A3 = M2(7, 8, 15, 16)

        A4 = M2(17, 18, 25, 26)

        A5 = M2(19, 20, 27, 28)

        A6 = M2(21, 22, 29, 30)

        A7 = M2(23, 24, 31, 32)

        A8 = M2(33, 34, 41, 42)

        A9 = M2(35, 36, 43, 44)

        A10 = M2(37, 38, 45, 46)

        A11 = M2(39, 40, 47, 48)

        A12 = M2(49, 50, 57, 58)

        A13 = M2(51, 52, 59, 60)

        A14 = M2(53, 54, 61, 62)

        A15 = M2(55, 56, 63, 64)

        # when
        actual = matrix_join(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11,
                             A12, A13, A14, A15)

        # then
        expected = M8(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                      17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                      31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
                      45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
                      59, 60, 61, 62, 63, 64)

        assert_that(actual, is_(expected))
コード例 #22
0
    def test_2x2_processors_2x2_blocks_with_collector_injection(self):
        '''
        A = (1,  2,  3,  4,
             5,  6,  7,  8,
             9, 10, 11, 12,
            13, 14, 15, 16)

        B = (17, 18, 19, 20,
             21, 22, 23, 24,
             25, 26, 27, 28,
             29, 30, 31, 32)
        '''
        nprocs = 4

        # given
        P = [ProcessorI() for i in range(nprocs)]
        collector = Spy(Cannon.Collector)

        # by-hand shift and split
        A0 = M2(1, 2, 5, 6)

        A1 = M2(3, 4, 7, 8)

        A2 = M2(11, 12, 15, 16)

        A3 = M2(9, 10, 13, 14)

        B0 = M2(17, 18, 21, 22)

        B1 = M2(27, 28, 31, 32)

        B2 = M2(25, 26, 29, 30)

        B3 = M2(19, 20, 23, 24)

        # when
        P[0].init(0, 0, P[2], P[1], 2, collector)
        P[1].init(0, 1, P[3], P[0], 2, collector)
        P[2].init(1, 0, P[0], P[3], 2, collector)
        P[3].init(1, 1, P[1], P[2], 2, collector)

        P[0].injectFirst(A0, 0)
        P[0].injectSecond(B0, 0)
        P[1].injectFirst(A1, 0)
        P[1].injectSecond(B1, 0)
        P[2].injectFirst(A2, 0)
        P[2].injectSecond(B2, 0)
        P[3].injectFirst(A3, 0)
        P[3].injectSecond(B3, 0)

        # then
        C0 = M2(250, 260, 618, 644)

        C1 = M2(270, 280, 670, 696)

        C2 = M2(986, 1028, 1354, 1412)

        C3 = M2(1070, 1112, 1470, 1528)

        self.assert_collector_called(collector, C0, 0, 0, anything())
        self.assert_collector_called(collector, C1, 0, 1, anything())
        self.assert_collector_called(collector, C2, 1, 0, anything())
        self.assert_collector_called(collector, C3, 1, 1, anything())
コード例 #23
0
    def test_split_8x8_in_16_blocks(self):
        # given
        original = M8(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                      17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
                      31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
                      45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
                      59, 60, 61, 62, 63, 64)

        # when
        blocks = matrix_split(original, block_order=2)

        # then
        E00 = M2(1, 2, 9, 10)

        E01 = M2(3, 4, 11, 12)

        E02 = M2(5, 6, 13, 14)

        E03 = M2(7, 8, 15, 16)

        E10 = M2(17, 18, 25, 26)

        E11 = M2(19, 20, 27, 28)

        E12 = M2(21, 22, 29, 30)

        E13 = M2(23, 24, 31, 32)

        E20 = M2(33, 34, 41, 42)

        E21 = M2(35, 36, 43, 44)

        E22 = M2(37, 38, 45, 46)

        E23 = M2(39, 40, 47, 48)

        E30 = M2(49, 50, 57, 58)

        E31 = M2(51, 52, 59, 60)

        E32 = M2(53, 54, 61, 62)

        E33 = M2(55, 56, 63, 64)

        assert_that(
            blocks,
            is_([
                E00, E01, E02, E03, E10, E11, E12, E13, E20, E21, E22, E23,
                E30, E31, E32, E33
            ]))