0% found this document useful (0 votes)
7 views2 pages

المستند

The document contains Python code snippets demonstrating the use of the mpi4py library for parallel computing with MPI. It includes examples of sending and receiving data between processes, broadcasting data from one process to all others, and scattering data from one process to multiple processes. Each code block illustrates different MPI communication methods and their respective outputs based on the rank of the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

المستند

The document contains Python code snippets demonstrating the use of the mpi4py library for parallel computing with MPI. It includes examples of sending and receiving data between processes, broadcasting data from one process to all others, and scattering data from one process to multiple processes. Each code block illustrates different MPI communication methods and their respective outputs based on the rank of the process.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

from mpi4py import MPI #

comm = MPI.COMM_WORLD #

)(rank = comm.Get_rank #

'data = 'none #

:if rank == 0 #

data = 45 # #

comm.send(45,dest=1) #

:if rank == 1 #

data = comm.recv(source=0) #

print('process num',rank,'has data',data) #

import numpy as np

from mpi4py import MPI #

comm = MPI.COMM_WORLD #

)(rank = comm.Get_rank #

data = None #

:if rank == 0 #

data = [1,4,6] #

print('before broadcasting process',rank,' has data ',data) #

data = comm.bcast(data,root=0) #

print('after broadcasting process ',rank,' has data',data) #

################################################################

from mpi4py import MPI #

import numpy as np #

comm = MPI.COMM_WORLD #

)(rank = comm.Get_rank #
#

data = np.zeros(5,dtype=int) #

:if rank == 0

data = np.arange(1,6)

print('before broadcasting process',rank,' has data ', data)

data = comm.bcast(data , root=0)

print('after broadcasting process',rank,' has data ', data)

###################################################################
#########

from mpi4py import MPI

comm = MPI.COMM_WORLD

)(rank = comm.Get_rank

)(size = comm.get_size#

data = None

:if rank == 0

data = [1,4,5,9] #[(i+1) ** 2 for i in range(size)]

print('Before scattering process',rank,' has data', data)

data = comm.scatter(data, root=0)

print('After scattering process',rank,' has data', data)

You might also like