On 9 February 2018 at 15:36, <
chengd...@gmail.com> wrote:
> HI everyone,
>
> I am a little confused about the master - slave/worker design pattern's
> implementation with MPI. I have a function f(x) which will cost ~10s to
> initialize (initializer init() ) and ~10s to calculate a value. So I want to
> make a MPI worker pool to serve the master process to do optimization.
>
> However, the worker pool needs `Queue` and `while True` loops. I have
> difficult in writing the dispatch code. Do you have some examples on this?
>
Do you know that last mpi4py release comes with mpi4py.futures, an
almost drop-in replacement for concurrent.futures? I would really
recommend you to use that rather than implementing your own
master/worker tool.
Here you have a working example for your use case:
# Run with: mpiexec -n 1 python script.py
from mpi4py.futures import MPIPoolExecutor
def square(i):
global initialized
try:
initialized
except NameError:
initialized = False
if not initialized:
print("expensive initialization")
import time
time.sleep(2)
initialized = True
return i**2
if __name__ == '__main__':
with MPIPoolExecutor(2) as ex:
for result in ex.map(square, range(7)):
print result
--
Lisandro Dalcin
============
Research Scientist
Computer, Electrical and Mathematical Sciences & Engineering (CEMSE)
Extreme Computing Research Center (ECRC)
King Abdullah University of Science and Technology (KAUST)
http://ecrc.kaust.edu.sa/
4700 King Abdullah University of Science and Technology
al-Khawarizmi Bldg (Bldg 1), Office # 0109
Thuwal 23955-6900, Kingdom of Saudi Arabia
http://www.kaust.edu.sa
Office Phone:
+966 12 808-0459