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
5747 . 30-Jul-2023 12:29:44 PM
5746 26-Jul-2023 10:34:23 PM
5745 VIDEO HOT 25-Jul-2023 12:26:38 PM
5744 Cute Sexy 22-Jul-2023 08:18:25 AM
5743 KIDs Sexy 17-Jul-2023 10:18:41 AM
5742 dfa 11-Jul-2023 09:08:36 AM
5741 HOT CHILD 11-Jul-2023 08:53:45 AM
5740 jj assigment 11-Jul-2023 08:43:20 AM
5739 07-Jul-2023 08:20:42 PM
5738 Kid Sexy 26-Jun-2023 09:33:43 AM
5737 mmmmm 24-Jun-2023 07:31:13 PM
5736 ....... 24-Jun-2023 07:17:01 PM
5735 Child Sexy 22-Jun-2023 10:15:56 AM
5734 Mm 20-Jun-2023 09:15:38 PM
5733 Kkk 20-Jun-2023 09:13:08 PM
5732 Ko 20-Jun-2023 08:44:44 PM
5731 bhatti 420 20-Jun-2023 10:53:59 AM
5730 Abc 20-Jun-2023 08:57:46 AM
5729 Lab 20-Jun-2023 08:53:25 AM
5728 ..... 20-Jun-2023 08:38:08 AM
5727 Best S.E.X.Y 17-Jun-2023 07:31:25 AM
5726 Aab 15-Jun-2023 09:29:28 PM
5725 a 13-Jun-2023 10:33:05 PM
5724 Child Vids 12-Jun-2023 11:21:50 AM
5723 Emi Pet 12-Jun-2023 06:50:51 AM
5722 cmd 06-Jun-2023 12:24:35 PM
5721 CHILD HOT 05-Jun-2023 09:08:42 AM
5720 Lakestone En 04-Jun-2023 05:08:53 PM
5719 . 03-Jun-2023 01:20:03 PM
5718 code 29-May-2023 05:50:57 PM

[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