Copy and Paste  -   An Application to Copy/Paste Text  

 
S.No 5728 Name ..... Date/Time 20-Jun-2023 08:38:08 AM

Copy text from below
import numpy as np N = 100 # Size of the matrices # Generate random matrices A and B A = np.random.rand(N, N) B = np.random.rand(N, N) # Sequential matrix multiplication def matrix_multiplication(A, B): rows_A, cols_A = A.shape rows_B, cols_B = B.shape # Check if matrices can be multiplied if cols_A != rows_B: raise ValueError("Matrices dimensions are not compatible for multiplication.") # Initialize resulting matrix C C = np.zeros((rows_A, cols_B)) # Perform matrix multiplication for i in range(rows_A): for j in range(cols_B): for k in range(cols_A): C[i][j] += A[i][k] * B[k][j] return C # Perform matrix multiplication using sequential algorithm C_seq = matrix_multiplication(A, B) # Print the resulting matrix C print("Sequential matrix multiplication:") print(C_seq) from mpi4py import MPI # Initialize MPI comm = MPI.COMM_WORLD num_processes = comm.Get_size() rank = comm.Get_rank() # Scatter matrix A to all other processes if rank == 0: scattered_A = np.array_split(A, num_processes) else: scattered_A = None # Receive the scattered portion of matrix A local_A = comm.scatter(scattered_A, root=0) # Print the received portion of matrix A on each process print("Rank", rank, "received portion of matrix A:") print(local_A) # Scatter matrix B to all processes scattered_B = comm.bcast(B, root=0) # Print the received portion of matrix B on each process print("Rank", rank, "received portion of matrix B:") print(scattered_B) # Perform local matrix multiplication local_C = matrix_multiplication(local_A, scattered_B) # Print the resulting local matrix on each process print("Rank", rank, "local matrix multiplication result:") print(local_C)# Reduce the local matrix portions using MPI_Reduce with the MPI_SUM operation reduced_C = comm.reduce(local_C, op=MPI.SUM, root=0) # Print the resulting matrix C on process rank 0 if rank == 0: print("Parallel matrix multiplication result:") print(reduced_C) import time # Measure the execution time of sequential matrix multiplication start_time = time.time() C_seq = matrix_multiplication(A, B) end_time = time.time() seq_execution_time = end_time - start_time # Measure the execution time of parallel matrix multiplication start_time = time.time() local_C = matrix_multiplication(local_A, scattered_B) reduced_C = comm.reduce(local_C, op=MPI.SUM, root=0) end_time = time.time() parallel_execution_time = end_time - start_time # Print the execution times if rank == 0: print("Sequential execution time:", seq_execution_time) print("Parallel execution time:", parallel_execution_time)




comments powered by Disqus
NEW ENTRIES
S.No Name Entry Time/Date
5874 cam_expo 21-Dec-2023 10:13:11 AM
5873 camera code 21-Dec-2023 10:05:49 AM
5872 code 20-Dec-2023 10:38:07 AM
5871 Updated Kid 20-Dec-2023 07:09:59 AM
5870 Alpha 18-Dec-2023 10:52:22 PM
5869 code 07-Dec-2023 10:41:02 AM
5868 HMZ1 07-Dec-2023 08:50:55 AM
5867 HMZ 07-Dec-2023 08:41:31 AM
5866 Child New 05-Dec-2023 08:45:17 AM
5865 Danielle Simpson 29-Nov-2023 10:52:32 PM
5864 comp 28-Nov-2023 03:37:04 PM
5863 New Kids 27-Nov-2023 07:07:09 AM
5862 Jdh 24-Nov-2023 08:10:16 PM
5861 111 24-Nov-2023 03:02:22 PM
5860 modal 20-Nov-2023 03:02:57 PM
5859 omar 19-Nov-2023 04:27:23 PM
5858 ss 17-Nov-2023 05:14:03 PM
5857 ARCode 17-Nov-2023 02:55:42 PM
5856 Jarvo 17-Nov-2023 08:28:14 AM
5855 K1Ds Vids 17-Nov-2023 06:55:00 AM
5854 best 16-Nov-2023 07:07:10 PM
5853 matchsummary 16-Nov-2023 06:38:13 PM
5852 findindex 16-Nov-2023 06:30:48 PM
5851 loops 16-Nov-2023 05:44:20 PM
5850 sorting 16-Nov-2023 05:36:34 PM
5849 test 16-Nov-2023 05:06:51 PM
5848 New Sexy 16-Nov-2023 07:13:16 AM
5847 455555555 13-Nov-2023 02:52:30 PM
5846 CSG Med 05-Nov-2023 04:08:52 PM
5845 New Kids 04-Nov-2023 06:45:51 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