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
5655 code 01-Apr-2023 01:04:03 PM
5654 Animation09 31-Mar-2023 02:40:17 PM
5653 fty 29-Mar-2023 02:12:37 AM
5652 CLIENT 21-Mar-2023 10:14:25 AM
5651 SERVER 21-Mar-2023 10:13:46 AM
5650 repo 18-Mar-2023 05:50:40 PM
5649 MET 17 MAR 17-Mar-2023 12:56:12 PM
5648 MET OL 17 17-Mar-2023 12:55:17 PM
5647 Test 11 Mar1 15-Mar-2023 03:14:56 PM
5646 Test 1 15-Mar-2023 01:01:41 PM
5645 Test 1 14-Mar-2023 12:26:27 PM
5644 MET 14 March 14-Mar-2023 09:42:12 AM
5643 Test 1 13-Mar-2023 12:49:03 PM
5642 Test 2 Mar13 13-Mar-2023 10:02:33 AM
5641 09-Mar-2023 04:23:35 PM
5640 CHILD SEXY 09-Mar-2023 08:09:59 AM
5639 . 08-Mar-2023 05:03:21 PM
5638 07-Mar-2023 02:03:15 PM
5637 MS 07-Mar-2023 11:21:53 AM
5636 SEXY GIRLS 06-Mar-2023 08:35:18 AM
5635 test 1 03-Mar-2023 02:35:15 PM
5634 PHD Mar 3 02-Mar-2023 09:58:42 PM
5633 MET Mar 3 02-Mar-2023 09:58:01 PM
5632 Test12 Mar 3 02-Mar-2023 09:57:18 PM
5631 Test 11 Mar3 02-Mar-2023 09:50:54 PM
5630 Test 8 Mar 3 02-Mar-2023 09:45:38 PM
5629 Test 3 Mar 3 02-Mar-2023 09:39:33 PM
5628 Test 2 Mar 3 02-Mar-2023 09:34:35 PM
5627 Test 1 Mar 3 02-Mar-2023 09:31:07 PM
5626 Test 8 Mar 2 01-Mar-2023 07:58:21 PM

[First] [Prev] 11 | 12 [Next] [Last]
 
web counter
web counter


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