forked from sjvrijn/ConfiguringCMAES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMPI_slave.py
More file actions
51 lines (36 loc) · 1.5 KB
/
MPI_slave.py
File metadata and controls
51 lines (36 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function, unicode_literals
__author__ = 'Sander van Rijn <[email protected]>'
### NOTE: Do not remove these 'unused' imports! ###
# The following are imports that are required by the functions that are passed to this MPI slave in order to run
import numpy as np
from EvolvingES import MPI
comm = MPI.COMM_SELF.Get_parent()
size = comm.Get_size()
rank = comm.Get_rank()
def runSlaveRun():
"""
This function has the sole purpose of performing the distributed computations in an MPI-setting, by running the
broadcast function on the scattered arguments.
N.B.: The broadcast function must be imported in this file for this method to work!
To use this function, call as follows:
>>> comm = MPI.COMM_SELF.Spawn(sys.executable, args=['MPI_slave.py'], maxprocs=num_procs)
>>> comm.bcast(runFunction, root=MPI.ROOT)
>>> comm.scatter(arguments, root=MPI.ROOT)
>>> comm.Barrier()
>>> results = comm.gather(results, root=MPI.ROOT)
>>> comm.Disconnect()
"""
np.set_printoptions(linewidth=1000)
function = None
options = None
# print("Process {}/{} reporting for duty!".format(rank, size))
function = comm.bcast(function, root=0)
arguments = comm.scatter(options, root=0)
results = function(*arguments)
comm.Barrier()
comm.gather(results, root=0)
comm.Disconnect()
if __name__ == '__main__':
runSlaveRun()