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
5656 02-Apr-2023 09:20:42 AM
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

[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