Exemple #1
0
def test_constructor():
    m, n = 20, 10
    m1 = Matrix(m, n)
    m2 = Matrix(m, n)
    for i in range(m):
        for j in range(n):
            m1[i, j] = 8.0
            m2[i, j] = 8.0
    assert (compareMat(m1, m2, m, n))
    assert (m1 == m2)
Exemple #2
0
    def test_result(self):
        mat1 = Matrix(np.random.random((1151, 1151)))
        mat2 = Matrix(np.random.random((1151, 1151)))

        naive_ret = multiply_naive(mat1, mat2)
        tile_ret = multiply_tile(mat1, mat2, 64)
        mkl_ret = multiply_mkl(mat1, mat2)
        for i in range(naive_ret.nrow):
            for j in range(naive_ret.ncol):
                assert naive_ret[i, j] == pytest.approx(tile_ret[i, j],
                                                        abs=1e-05)
Exemple #3
0
def test_multiply_mkl():
    for i in range(2):
        m = np.random.randint(200, 400)
        n = np.random.randint(200, 400)
        k = np.random.randint(200, 400)
        np_mat1 = np.random.random((m, k))
        np_mat2 = np.random.random((k, n))
        mat1 = Matrix(np_mat1)
        mat2 = Matrix(np_mat2)
        mkl_ans = multiply_mkl(mat1, mat2)

        assert mkl_ans.nrow == m
        assert mkl_ans.ncol == n
        assert np.array(mkl_ans) == pytest.approx(np.matmul(np_mat1, np_mat2))
Exemple #4
0
def test_multiply():
    m1 = Matrix(10, 20)
    m2 = Matrix(20, 10)

    for i in range(10):
        for j in range(20):
            m1[i, j] = 8.0

    for i in range(20):
        for j in range(10):
            m2[i, j] = 6.0

    m3 = multiply_naive(m1, m2)
    m4 = multiply_mkl(m1, m2)
    assert (compareMat(m3, m4, 10, 10))
Exemple #5
0
def execute(func, i, j, tsize):
    k = np.random.randint(100, 200)
    np_mat1 = np.random.random((i, k))
    np_mat2 = np.random.random((k, j))
    mat1 = Matrix(np_mat1)

    mat2 = Matrix(np_mat2)
    if func == multiply_tile:
        result = func(mat1, mat2, tsize)
    else:
        result = func(mat1, mat2)

    assert result.nrow == i
    assert result.ncol == j
    assert np.array(result) == pytest.approx(np.matmul(np_mat1, np_mat2))
Exemple #6
0
def test_multiply_tile():
    tsize_l = [16, 17, 19]
    for tsize in tsize_l:
        for i in range(2):
            m = np.random.randint(200, 400)
            n = np.random.randint(200, 400)
            k = np.random.randint(200, 400)
            np_mat1 = np.random.random((m, k))
            np_mat2 = np.random.random((k, n))
            mat1 = Matrix(np_mat1)
            mat2 = Matrix(np_mat2)
            tile_ans = multiply_tile(mat1, mat2, tsize)

            assert tile_ans.nrow == m
            assert tile_ans.ncol == n
            assert np.array(tile_ans) == pytest.approx(
                np.matmul(np_mat1, np_mat2))
Exemple #7
0
    def test_multiply_mkl(self):
        for i in range(2):
            row_n_col = 1000
            np_mat1 = np.random.random(row_n_col * row_n_col)
            np_mat2 = np.random.random(row_n_col * row_n_col)
            mat1 = Matrix(row_n_col, row_n_col, np_mat1.tolist())
            mat2 = Matrix(row_n_col, row_n_col, np_mat2.tolist())

            np_mat1 = np_mat1.reshape((row_n_col, row_n_col))
            np_mat2 = np_mat2.reshape((row_n_col, row_n_col))

            answer = multiply_mkl(mat1, mat2)

            assert answer.nrow == row_n_col
            assert answer.ncol == row_n_col

            assert np.array(answer.buffer_vector()).reshape((row_n_col, row_n_col)) ==\
                   pytest.approx(np.matmul(np_mat1, np_mat2))
Exemple #8
0
def test_performance():
    np_mat1 = np.random.random((1000, 1000))
    np_mat2 = np.random.random((1000, 1000))
    mat1 = Matrix(np_mat1)
    mat2 = Matrix(np_mat2)

    naive_timing = []
    for i in range(5):
        start = time.time()
        multiply_naive(mat1, mat2)
        end = time.time()
        naive_timing.append(end - start)

    tile_timing = []
    for i in range(5):
        start = time.time()
        multiply_tile(mat1, mat2, 8)
        end = time.time()
        tile_timing.append(end - start)

    mkl_timing = []
    for i in range(5):
        start = time.time()
        multiply_mkl(mat1, mat2)
        end = time.time()
        mkl_timing.append(end - start)

    with open('performance.txt', 'w') as f:
        naivesec = np.min(naive_timing)
        tilesec = np.min(tile_timing)
        mklsec = np.min(mkl_timing)
        print('multiply_naive runtime = {0:2.4f} seconds'.format(naivesec),
              file=f)
        print('multiply_tile runtime = {0:2.4f} seconds'.format(tilesec),
              file=f)
        print('multiply_mkl runtime = {0:2.4f} seconds'.format(mklsec), file=f)
        print('Tile speed-up over naive: {0:2.2f} x'.format(naivesec /
                                                            tilesec),
              file=f)
        print('MKL speed-up over naive: {0:2.2f} x'.format(naivesec / mklsec),
              file=f)
Exemple #9
0
        def check_tile(row, col1, col2, tile_size):
            np_mat1 = np.random.random(row * col1)
            np_mat2 = np.random.random(col1 * col2)
            mat1 = Matrix(row, col1, np_mat1.tolist())
            mat2 = Matrix(col1, col2, np_mat2.tolist())

            np_mat1 = np_mat1.reshape((row, col1))
            np_mat2 = np_mat2.reshape((col1, col2))

            tile_answer = multiply_tile(mat1, mat2, tile_size)

            assert tile_answer.nrow == row
            assert tile_answer.ncol == col2

            mkl_answer = multiply_mkl(mat1, mat2)

            tile_ver_in_np = np.array(tile_answer.buffer_vector()).reshape(
                (row, col2))
            mkl_ver_in_np = np.array(mkl_answer.buffer_vector()).reshape(
                (row, col2))
            assert tile_ver_in_np == pytest.approx(mkl_ver_in_np, abs=1e-05)

            # benchmark
            ns = dict(multiply_naive=multiply_naive, multiply_tile=multiply_tile,\
                      mat1=mat1, mat2=mat2, tile_size=tile_size)
            t_naive = timeit.Timer('multiply_naive(mat1, mat2)', globals=ns)
            t_tile = timeit.Timer('multiply_tile(mat1, mat2, tile_size)',
                                  globals=ns)
            # t_mkl = timeit.Timer('_matrix.multiply_mkl(mat1, mat2)', globals=ns)

            time_naive = min(t_naive.repeat(5, 1))
            time_tile = min(t_tile.repeat(5, 1))
            # time_mkl = min(t_mkl.repeat(10, 1))
            ratio = time_tile / time_naive

            with open("performance.txt", "w") as file:
                file.write(
                    "tile time (tile_size: {}) / naive time: {}/{}={}".format(
                        tile_size, time_tile, time_naive, ratio))
            return ratio
Exemple #10
0
    def test_multiply_naive(self):
        for i in range(2):
            row_n_col = 1000
            np_mat1 = np.random.random(row_n_col * row_n_col)
            np_mat2 = np.random.random(row_n_col * row_n_col)
            mat1 = Matrix(row_n_col, row_n_col, np_mat1.tolist())
            mat2 = Matrix(row_n_col, row_n_col, np_mat2.tolist())

            np_mat1 = np_mat1.reshape((row_n_col, row_n_col))
            np_mat2 = np_mat2.reshape((row_n_col, row_n_col))

            naive_answer = multiply_naive(mat1, mat2)

            assert naive_answer.nrow == row_n_col
            assert naive_answer.ncol == row_n_col

            mkl_answer = multiply_mkl(mat1, mat2)

            naive_ver_in_np = np.array(naive_answer.buffer_vector()).reshape(
                (row_n_col, row_n_col))
            mkl_ver_in_np = np.array(mkl_answer.buffer_vector()).reshape(
                (row_n_col, row_n_col))
            assert naive_ver_in_np == pytest.approx(mkl_ver_in_np, abs=1e-05)
Exemple #11
0
    def test_performance(self):
        mat1 = Matrix(np.random.random((1151, 1151)))
        mat2 = Matrix(np.random.random((1151, 1151)))

        timer_naive = []
        timer_tile = []
        timer_mkl = []

        for i in range(5):
            timer = time.time()
            multiply_naive(mat1, mat2)
            timer_naive.append(time.time() - timer)

            timer = time.time()
            multiply_tile(mat1, mat2, 64)
            timer_tile.append(time.time() - timer)

            timer = time.time()
            multiply_mkl(mat1, mat2)
            timer_mkl.append(time.time() - timer)

        naive_avg = np.average(timer_naive)
        tile_avg = np.average(timer_tile)
        mkl_avg = np.average(timer_mkl)

        with open("performance.txt", "w") as f:
            f.write("[Average execution time]\n")
            f.write("multiply_naive: {} secs\n".format("%.4f" % naive_avg))
            f.write("multiply_tile : {} secs\n".format("%.4f" % tile_avg))
            f.write("multiply_mkl  : {} secs\n".format("%.4f" % mkl_avg))
            f.write("The tiling version is {:.1%} faster than naive version\n".
                    format(naive_avg / tile_avg))
            f.write(
                "The mkl version is {:.1%} faster than naive version\n".format(
                    naive_avg / mkl_avg))
            f.close()
Exemple #12
0
def test_mat_info():
    r, c = 13, 19
    m1 = Matrix(r, c)
    assert (r == m1.nrow)
    assert (c == m1.ncol)
Exemple #13
0
def test_performance():
    m = np.random.randint(500, 501)
    n = np.random.randint(500, 501)
    k = np.random.randint(500, 501)
    np_mat1 = np.random.random((m, k))
    np_mat2 = np.random.random((k, n))
    mat1 = Matrix(np_mat1)
    mat2 = Matrix(np_mat2)

    naive_timing = []
    for i in range(2):
        start = time.time()
        naive_ans = multiply_naive(mat1, mat2)
        end = time.time()
        naive_timing.append(end - start)

    mkl_timing = []
    for i in range(2):
        start = time.time()
        mkl_ans = multiply_mkl(mat1, mat2)
        end = time.time()
        mkl_timing.append(end - start)

    tile16_timing = []
    for i in range(2):
        start = time.time()
        tile16_ans = multiply_tile(mat1, mat2, 16)
        end = time.time()
        tile16_timing.append(end - start)

    tile17_timing = []
    for i in range(2):
        start = time.time()
        tile17_ans = multiply_tile(mat1, mat2, 17)
        end = time.time()
        tile17_timing.append(end - start)

    tile19_timing = []
    for i in range(2):
        start = time.time()
        tile19_ans = multiply_tile(mat1, mat2, 19)
        end = time.time()
        tile19_timing.append(end - start)

    naive_timing = np.mean(naive_timing)
    mkl_timing = np.mean(mkl_timing)
    tile16_timing = np.mean(tile16_timing)
    tile17_timing = np.mean(tile17_timing)
    tile19_timing = np.mean(tile19_timing)

    with open('performance.txt', 'w') as f:
        print('Performance Measurement', file=f)
        print('Input Matrix size:', file=f)
        print('Matrix 1: {} x {}'.format(m, k), file=f)
        print('Matrix 2: {} x {}'.format(k, n), file=f)
        print('Average Time for multiply_naive: {}'.format(naive_timing),
              file=f)
        print('Average Time for multiply_mkl: {}'.format(mkl_timing), file=f)
        print('Average Time for multiply_tile16: {}'.format(tile16_timing),
              file=f)
        print('Average Time for multiply_tile17: {}'.format(tile17_timing),
              file=f)
        print('Average Time for multiply_tile19: {}'.format(tile19_timing),
              file=f)

    assert tile16_timing / naive_timing <= 0.8
    assert tile17_timing / naive_timing <= 0.8
    assert tile19_timing / naive_timing <= 0.8