Copy and Paste  -   An Application to Copy/Paste Text  

 
S.No 5729 Name Lab Date/Time 20-Jun-2023 08:53:25 AM

Copy text from below
import numpy as np
from mpi4py import MPI
import time

def sequential_matrix_multiplication(A, B):
    return np.dot(A, B)

def print_matrix(matrix):
    for row in matrix:
        for element in row:
            print("{:.2f}".format(element), end="\t")
        print()

def generate_random_matrix(size):
    return np.random.rand(size, size)

if __name__ == "__main__":
    # Step 1: Matrix Generation
    matrix_sizes = [100, 200, 400, 800]
    A_matrices = []
    B_matrices = []

    for size in matrix_sizes:
        A = generate_random_matrix(size)
        B = generate_random_matrix(size)
        A_matrices.append(A)
        B_matrices.append(B)

    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()

    for i in range(len(matrix_sizes)):
        N = matrix_sizes[i]
        A = A_matrices[i]
        B = B_matrices[i]

        if rank == 0:
            print(f"Matrix size: {N} x {N}")

        comm.Barrier()

        if rank == 0:
            # Step 2: Sequential Matrix Multiplication
            start_time = time.time()
            C_seq = sequential_matrix_multiplication(A, B)
            end_time = time.time()

            if rank == 0:
                print("Sequential Matrix Multiplication:")
                print_matrix(C_seq)
                print(f"Execution Time: {end_time - start_time} seconds")
                print()

        comm.Barrier()

        if rank == 0:
            print(f"Parallel Matrix Multiplication using {size} processes:")

        comm.Barrier()

        # Step 3: MPI Scatter
        local_A = np.zeros((N // size, N), dtype=np.float64)
        comm.Scatter(A, local_A, root=0)

        if rank == 0:
            print(f"Process {rank} - Local A:")
            print_matrix(local_A)
            print()

        local_B = np.zeros((N, N // size), dtype=np.float64)
        comm.Scatter(B, local_B, root=0)

        if rank == 0:
            print(f"Process {rank} - Local B:")
            print_matrix(local_B)
            print()

        comm.Barrier()

        # Step 4: Matrix Multiplication
        local_C = np.dot(local_A, local_B)

        if rank == 0:
            print(f"Process {rank} - Local C:")
            print_matrix(local_C)
            print()

        comm.Barrier()

        # Step 5: MPI Reduce
        C = np.zeros((N, N), dtype=np.float64)
        comm.Reduce(local_C, C, op=MPI.SUM, root=0)

        if rank == 0:
            print("Parallel Matrix Multiplication:")
            print_matrix(C)
            print()

        comm.Barrier()

    if rank == 0:
        # Step 6: Performance Analysis
        num_processes = [2, 4, 6, 8]

        for i in range(len(matrix_sizes)):
            N = matrix_sizes[i]
            A = A_matrices[i]
            B = B_matrices[i]

            print(f"Matrix size: {N} x {N}")

            for num_procs in num_processes:
                if num_procs > size:
                    continue

                comm.Barrier()

                print(f"Parallel Matrix Multiplication using {num_procs} processes:")

                comm.Barrier()

                start_time = time.time()
                C_parallel = parallel_matrix_multiplication(A, B, num_procs, comm)
                end_time = time.time()

                if rank == 0:
                    print("Parallel Matrix Multiplication:")
                    print_matrix(C_parallel)
                    print(f"Execution Time: {end_time - start_time} seconds")
                    print()

                comm.Barrier()




comments powered by Disqus
NEW ENTRIES
S.No Name Entry Time/Date
5846 CSG Med 05-Nov-2023 04:08:52 PM
5845 New Kids 04-Nov-2023 06:45:51 AM
5844 BMP 31-Oct-2023 06:41:06 PM
5843 ab 29-Oct-2023 08:51:02 PM
5842 a 29-Oct-2023 08:44:33 PM
5841 c++ program 29-Oct-2023 08:42:28 PM
5840 Child Vids 26-Oct-2023 07:07:04 AM
5839 23-Oct-2023 08:11:25 PM
5838 22-Oct-2023 06:29:37 PM
5837 Organic Loom 17-Oct-2023 07:02:03 PM
5836 Test 1 17-Oct-2023 11:16:30 AM
5835 PAF-IAST MET 17-Oct-2023 11:15:49 AM
5834 for jazz 16-Oct-2023 09:26:26 PM
5833 New Kid 16-Oct-2023 07:08:13 AM
5832 test 12-Oct-2023 10:38:09 AM
5831 a 12-Oct-2023 10:28:26 AM
5830 75 Videos 12-Oct-2023 09:32:23 AM
5829 11-Oct-2023 05:45:55 PM
5828 q44 10-Oct-2023 05:47:12 PM
5827 q4 10-Oct-2023 05:40:56 PM
5826 jflex q3 10-Oct-2023 05:37:43 PM
5825 q3 10-Oct-2023 05:33:13 PM
5824 q2 10-Oct-2023 05:32:15 PM
5823 jflex 10-Oct-2023 05:30:32 PM
5822 dddd 10-Oct-2023 04:41:21 PM
5821 nn 09-Oct-2023 11:55:27 AM
5820 Jarvo 09-Oct-2023 11:44:19 AM
5819 NEW CHILD 07-Oct-2023 11:16:57 AM
5818 05-Oct-2023 06:32:33 PM
5817 LandscapeLay 05-Oct-2023 11:42:00 AM

[First] [Prev] 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 [Next] [Last]
 
web counter
web counter


To report any error messages or bugs, or other issues, please send email at: info@pakproject.com