diff --git a/src/main.py b/src/main_cg.py similarity index 85% rename from src/main.py rename to src/main_cg.py index 05314ae..23eaac4 100644 --- a/src/main.py +++ b/src/main_cg.py @@ -12,7 +12,7 @@ comm = MPI.COMM_WORLD size = comm.Get_size() rank = comm.Get_rank() -n = 1_00 +n = 1_000 h = 1 / (n - 1) A = Matrix([-1, 2, -1], structure="tridiagonal", n=n) @@ -21,5 +21,5 @@ b = Vector([h**2 * 2] * n) x = cg.cg(A, x0, b) -if rank == 0: - print(x) +# if rank == 0: +# print(f"ranks = {size}: x = {x}") diff --git a/src/main_cg_timeit.py b/src/main_cg_timeit.py new file mode 100644 index 0000000..e8f3000 --- /dev/null +++ b/src/main_cg_timeit.py @@ -0,0 +1,27 @@ +from mpi4py import MPI +import sys +import timeit + +import cg + +from matrix_mpi import MatrixMPI as Matrix +from vector_mpi import VectorMPI as Vector + +# from matrix import Matrix +# from vector import Vector + +comm = MPI.COMM_WORLD +size = comm.Get_size() +rank = comm.Get_rank() + +n = int(sys.argv[1]) +h = 1 / (n - 1) + +A = Matrix([-1, 2, -1], structure="tridiagonal", n=n) +x0 = Vector([1] * n) +b = Vector([h**2 * 2] * n) + +time = timeit.timeit(lambda: cg.cg(A, x0, b), number=1) + +if rank == 0: + print(f"ranks = {size}: time = {time}") diff --git a/src/main_diag_vec.py b/src/main_diag_vec.py new file mode 100644 index 0000000..7cd4db1 --- /dev/null +++ b/src/main_diag_vec.py @@ -0,0 +1,21 @@ +from mpi4py import MPI + +from matrix_mpi import MatrixMPI as Matrix +from vector_mpi import VectorMPI as Vector + +# from matrix import Matrix +# from vector import Vector + +comm = MPI.COMM_WORLD +size = comm.Get_size() +rank = comm.Get_rank() + +n = 10_000 + +A = Matrix([3], structure="diagonal", offset=0, n=n) +v = Vector([7] * n) + +x = A * v + +# if rank == 0: +# print(f"ranks = {size}: x = {x}") diff --git a/src/main_diag_vec_timeit.py b/src/main_diag_vec_timeit.py new file mode 100644 index 0000000..c4be531 --- /dev/null +++ b/src/main_diag_vec_timeit.py @@ -0,0 +1,23 @@ +from mpi4py import MPI +import sys +import timeit + +from matrix_mpi import MatrixMPI as Matrix +from vector_mpi import VectorMPI as Vector + +# from matrix import Matrix +# from vector import Vector + +comm = MPI.COMM_WORLD +size = comm.Get_size() +rank = comm.Get_rank() + +n = int(sys.argv[1]) + +A = Matrix([3], structure="diagonal", offset=0, n=n) +v = Vector([7] * n) + +time = timeit.timeit(lambda: A * v, number=1) + +if rank == 0: + print(f"ranks = {size}: time = {time}s") diff --git a/src/main_matrix_vec.py b/src/main_matrix_vec.py new file mode 100644 index 0000000..4366170 --- /dev/null +++ b/src/main_matrix_vec.py @@ -0,0 +1,22 @@ +from mpi4py import MPI + +from matrix_mpi import MatrixMPI as Matrix +from vector_mpi import VectorMPI as Vector + +# from matrix import Matrix +# from vector import Vector + +comm = MPI.COMM_WORLD +size = comm.Get_size() +rank = comm.Get_rank() + +n = 10_000 + +m_data = [(i / k) for i in range(1, n+1) for k in range(1, n+1)] +A = Matrix(m_data, (n, n)) +v = Vector(list(range(1, n+1))) + +x = A * v + +# if rank == 0: +# print(f"ranks = {size}: x = {x}") diff --git a/src/main_matrix_vec_timeit.py b/src/main_matrix_vec_timeit.py new file mode 100644 index 0000000..2903676 --- /dev/null +++ b/src/main_matrix_vec_timeit.py @@ -0,0 +1,24 @@ +from mpi4py import MPI +import sys +import timeit + +from matrix_mpi import MatrixMPI as Matrix +from vector_mpi import VectorMPI as Vector + +# from matrix import Matrix +# from vector import Vector + +comm = MPI.COMM_WORLD +size = comm.Get_size() +rank = comm.Get_rank() + +n = int(sys.argv[1]) + +m_data = [(i / k) for i in range(1, n+1) for k in range(1, n+1)] +A = Matrix(m_data, (n, n)) +v = Vector(list(range(1, n+1))) + +time = timeit.timeit(lambda: A * v, number=1) + +if rank == 0: + print(f"ranks = {size}: time = {time}s") diff --git a/src/matrix_mpi.py b/src/matrix_mpi.py index c23f53f..5ef8883 100644 --- a/src/matrix_mpi.py +++ b/src/matrix_mpi.py @@ -100,6 +100,9 @@ class MatrixMPI: """ return self.transpose() + def __str__(self): + return str(self.__data__) + def __eq__(self, other): """ Return ``self==value`` diff --git a/src/vector_mpi.py b/src/vector_mpi.py index 35341e0..bcb8f21 100644 --- a/src/vector_mpi.py +++ b/src/vector_mpi.py @@ -1,7 +1,6 @@ import math import numpy -from mpi4py import MPI from matrix_mpi import MatrixMPI from vector import Vector