Copy and Paste  -   An Application to Copy/Paste Text  

 
S.No 5740 Name jj assigment Date/Time 11-Jul-2023 08:43:20 AM

Copy text from below

from mpi4py import MPI
import numpy as np
import time

# Function to perform matrix multiplication
def matrix_multiply(a, b):
    rows_a, cols_a = a.shape
    rows_b, cols_b = b.shape

    if cols_a != rows_b:
        raise ValueError("Cannot perform matrix multiplication. Invalid dimensions.")

    result = np.zeros((rows_a, cols_b), dtype=np.float32)

    for i in range(rows_a):
        for j in range(cols_b):
            for k in range(cols_a):
                result[i, j] += a[i, k] * b[k, j]

    return result


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

# Matrix size
matrix_sizes = [100, 200, 400, 800]

for size in matrix_sizes:
    # Create matrices on process 0
    if rank == 0:
        a = np.random.rand(size, size).astype(np.float32)
        b = np.random.rand(size, size).astype(np.float32)
    else:
        a = None
        b = None

    # Scatter the matrices to all processes
    a = comm.scatter(a, root=0)
    b = comm.scatter(b, root=0)

    # Perform local matrix multiplication
    local_result = matrix_multiply(a, b)

    # Gather all local results on process 0
    result = comm.gather(local_result, root=0)

    # Combine the results on process 0
    if rank == 0:
        final_result = np.sum(result, axis=0)

        # Print the result if desired
        # print(final_result)

# Measure and compare execution times
if rank == 0:
    sequential_times = []
    parallel_times = []

    for size in matrix_sizes:
        # Sequential matrix multiplication
        a = np.random.rand(size, size).astype(np.float32)
        b = np.random.rand(size, size).astype(np.float32)

        start_time = time.time()
        sequential_result = matrix_multiply(a, b)
        end_time = time.time()
        sequential_times.append(end_time - start_time)

        # Parallel matrix multiplication
        start_time = time.time()
        # Create matrices on process 0
        a = np.random.rand(size, size).astype(np.float32)
        b = np.random.rand(size, size).astype(np.float32)

        # Scatter the matrices to all processes
        a = comm.scatter(a, root=0)
        b = comm.scatter(b, root=0)

        # Perform local matrix multiplication
        local_result = matrix_multiply(a, b)

        # Gather all local results on process 0
        result = comm.gather(local_result, root=0)

        # Combine the results on process 0
        final_result = np.sum(result, axis=0)
        end_time = time.time()
        parallel_times.append(end_time - start_time)

    # Print the execution times
    print("Sequential Execution Times:", sequential_times)
    print("Parallel Execution Times:", parallel_times)





comments powered by Disqus
NEW ENTRIES
S.No Name Entry Time/Date
5687 More 4 You 27-Apr-2023 12:55:44 PM
5686 Elif Yener 26-Apr-2023 03:51:34 PM
5685 cycling 26-Apr-2023 01:26:18 PM
5684 Muslim matri 23-Apr-2023 11:13:13 AM
5683 xxxxx 18-Apr-2023 08:57:31 AM
5682 Aa 16-Apr-2023 10:23:04 PM
5681 Apr 12 MET 12-Apr-2023 09:56:35 AM
5680 MET 12 Apr 12-Apr-2023 09:43:37 AM
5679 online 11-Apr-2023 01:16:47 PM
5678 MS Test 11-Apr-2023 12:59:16 PM
5677 asdf 11-Apr-2023 12:58:53 PM
5676 asdf 11-Apr-2023 12:58:38 PM
5675 asdf 11-Apr-2023 12:58:33 PM
5674 asdf 11-Apr-2023 12:58:27 PM
5673 asdf 11-Apr-2023 12:58:20 PM
5672 asdf 11-Apr-2023 12:58:14 PM
5671 asdf 11-Apr-2023 12:58:07 PM
5670 asdf 11-Apr-2023 12:57:59 PM
5669 asdf 11-Apr-2023 12:57:53 PM
5668 asdf 11-Apr-2023 12:57:45 PM
5667 asdf 11-Apr-2023 12:57:38 PM
5666 asdf 11-Apr-2023 12:57:30 PM
5665 asdf 11-Apr-2023 12:57:22 PM
5664 asdf 11-Apr-2023 12:57:14 PM
5663 asdf 11-Apr-2023 12:57:07 PM
5662 xxxx 11-Apr-2023 09:30:22 AM
5661 xxxx 11-Apr-2023 09:16:52 AM
5660 xxxx 11-Apr-2023 09:10:30 AM
5659 Geopharmarx 07-Apr-2023 10:10:22 PM
5658 ttt2 04-Apr-2023 08:53:46 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