Hello, World! I am process 91 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 15 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 105 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 112 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 11 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 83 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 58 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 103 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 4 of 512 on cn041.karolina.it4i.cz.
Hello, World! I am process 28 of 512 on cn041.karolina.it4i.cz.
```
```
In this example, we run MPI4Py-enabled code on 4 nodes, 16 cores per node (total of 64 processes), each Python process is bound to a different core. More examples and documentation can be found on [MPI for Python webpage][a].
### Mandelbrot
### Adding Numbers
Task: count sum of numbers from 1 to 1 000 000. (There is an easy formula to count the sum of arithmetic sequence, but we are showing the MPI solution with adding numbers one by one).
```python
```python
#!/usr/bin/python
importnumpy
frommpi4pyimportMPI
frommpi4pyimportMPI
importtime
importnumpyasnp
comm=MPI.COMM_WORLD
tic=MPI.Wtime()
rank=comm.Get_rank()
size=comm.Get_size()
a=1
x1=-2.0
b=1000000
x2=1.0
y1=-1.0
y2=1.0
perrank=b//size
w=150
summ=numpy.zeros(1)
h=100
maxit=127
comm.Barrier()
defmandelbrot(x,y,maxit):
start_time=time.time()
c=x+y*1j
z=0+0j
it=0
whileabs(z)<2andit<maxit:
z=z**2+c
it+=1
returnit
temp=0
comm=MPI.COMM_WORLD
foriinrange(a+rank*perrank,a+(rank+1)*perrank):
size=comm.Get_size()
temp=temp+i
rank=comm.Get_rank()
summ[0]=temp
rmsg=np.empty(4,dtype='f')
imsg=np.empty(3,dtype='i')
ifrank==0:
ifrank==0:
total=numpy.zeros(1)
rmsg[:]=[x1,x2,y1,y2]
else:
imsg[:]=[w,h,maxit]
total=None
comm.Bcast([rmsg,MPI.FLOAT],root=0)
comm.Barrier()
comm.Bcast([imsg,MPI.INT],root=0)
#collect the partial results and add to the total sum
In this example, we run MPI4Py-enabled code on 4 nodes, 128 cores per node (total of 512 processes), each Python process is bound to a different core. More examples and documentation can be found on [MPI for Python webpage][a].