From 244a4aafc600aa3b48864cacf4698ac1f1d74b21 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20Gajdu=C5=A1ek?= <gajdusek.pavel@gmail.com>
Date: Fri, 8 Sep 2017 13:51:35 +0200
Subject: [PATCH] added mpi python example

---
 .../software/mpi/mpi4py-mpi-for-python.md     | 60 +++++++++++++++++++
 1 file changed, 60 insertions(+)

diff --git a/docs.it4i/anselm/software/mpi/mpi4py-mpi-for-python.md b/docs.it4i/anselm/software/mpi/mpi4py-mpi-for-python.md
index 1805ffe60..3e992799f 100644
--- a/docs.it4i/anselm/software/mpi/mpi4py-mpi-for-python.md
+++ b/docs.it4i/anselm/software/mpi/mpi4py-mpi-for-python.md
@@ -107,3 +107,63 @@ $ mpiexec -bycore -bind-to-core python hello_world.py
 ```
 
 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](https://pypi.python.org/pypi/mpi4py).
+
+###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
+#!/usr/bin/python
+
+import numpy
+from mpi4py import MPI
+import time
+
+comm = MPI.COMM_WORLD
+rank = comm.Get_rank()
+size = comm.Get_size()
+
+a = 1
+b = 1000000
+
+perrank = b//size
+summ = numpy.zeros(1)
+
+comm.Barrier()
+start_time = time.time()
+
+temp = 0
+for i in range(a + rank*perrank, a + (rank+1)*perrank):
+    temp = temp + i
+
+summ[0] = temp
+
+if rank == 0:
+    total = numpy.zeros(1)
+else:
+    total = None
+
+comm.Barrier()
+comm.Reduce(summ, total, op=MPI.SUM, root=0)
+
+stop_time = time.time()
+
+if rank == 0:
+    #add the rest numbers to 1 000 000
+    for i in range(a + (size)*perrank, b+1):
+        total[0] = total[0] + i
+    print ("The sum of numbers from 1 to 1 000 000: ", int(total[0]))
+    print ("time spent with ", size, " threads in milliseconds")
+    print ("-----", int((time.time()-start_time)*1000), "-----")
+```
+
+Execute the code above like:
+```console
+$ qsub -I -q qexp -l select=4:ncpus=16,walltime=00:30:00 -A DD-13-5
+
+$ ml Python/3.5.2-intel-2017.00
+
+$ mpirun -n 2 python myprogram.py
+```
+You can increase n and watch time lowering.
-- 
GitLab