-
Notifications
You must be signed in to change notification settings - Fork 240
/
mpi.py
284 lines (237 loc) · 7.95 KB
/
mpi.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
"""A bunch of MPI utilities."""
from contextlib import contextmanager
import io
import os
import sys
import traceback
import unittest
import functools
from openmdao.core.analysis_error import AnalysisError
def _redirect_streams(to_fd):
"""
Redirect stdout/stderr to the given file descriptor.
Based on: http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/.
Parameters
----------
to_fd : int
File descriptor to redirect to.
"""
original_stdout_fd = sys.stdout.fileno()
original_stderr_fd = sys.stderr.fileno()
# Flush and close sys.stdout/err - also closes the file descriptors (fd)
sys.stdout.close()
sys.stderr.close()
# Make original_stdout_fd point to the same file as to_fd
os.dup2(to_fd, original_stdout_fd)
os.dup2(to_fd, original_stderr_fd)
# Create a new sys.stdout that points to the redirected fd
sys.stdout = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb'))
sys.stderr = io.TextIOWrapper(os.fdopen(original_stdout_fd, 'wb'))
def use_proc_files():
"""
Cause stdout/err from each MPI process to be written to [rank].out.
"""
if MPI is not None:
working_dir = os.environ.get('PROC_FILES_DIR')
if not working_dir:
ofile = open("%d.out" % MPI.COMM_WORLD.rank, 'wb')
else:
if not os.path.isdir(working_dir):
raise RuntimeError("directory '%s' does not exist." % working_dir)
ofile = open(os.path.join(working_dir, "%d.out" % MPI.COMM_WORLD.rank), 'wb')
_redirect_streams(ofile.fileno())
# Attempt to import mpi4py.
# If OPENMDAO_REQUIRE_MPI is set to a recognized positive value, attempt import
# and raise exception on failure. If set to anything else, no import is attempted.
if 'OPENMDAO_REQUIRE_MPI' in os.environ:
if os.environ['OPENMDAO_REQUIRE_MPI'].lower() in ['always', '1', 'true', 'yes']:
from mpi4py import MPI
else:
MPI = None
# If OPENMDAO_REQUIRE_MPI is unset, attempt to import mpi4py, but continue on failure
# with a notification.
else:
try:
from mpi4py import MPI
except ImportError:
MPI = None
sys.stdout.write("Unable to import mpi4py. Parallel processing unavailable.\n")
sys.stdout.flush()
else:
# If the import succeeded, but it doesn't look like a parallel
# run was intended, don't use MPI
if MPI.COMM_WORLD.size == 1:
MPI = None
if MPI:
def debug(*msg): # pragma: no cover
"""
Print debug message to stdout.
Parameters
----------
*msg : tuple of str
Strings to be printed.
"""
newmsg = ["%d: " % MPI.COMM_WORLD.rank] + list(msg)
for m in newmsg:
sys.stdout.write("%s " % m)
sys.stdout.write('\n')
sys.stdout.flush()
else:
def debug(*msg): # pragma: no cover
"""
Print debug message to stdout.
Parameters
----------
*msg : tuple of str
Strings to be printed.
"""
for m in msg:
sys.stdout.write("%s " % str(m))
sys.stdout.write('\n')
class FakeComm(object):
"""
Fake MPI communicator class used if mpi4py is not installed.
Attributes
----------
rank : int
index of current proc; value is 0 because there is only 1 proc.
size : int
number of procs in the comm; value is 1 since MPI is not available.
"""
def __init__(self):
"""
Initialize attributes.
"""
self.rank = 0
self.size = 1
@contextmanager
def multi_proc_fail_check(comm):
"""
Raise an AnalysisError on all procs if it is raised on one.
Wrap this around code that you want to globally fail if it fails
on any MPI process in comm. If not running under MPI, don't
handle any exceptions.
Parameters
----------
comm : MPI communicator or None
Communicator from the ParallelGroup that owns the calling solver.
"""
if MPI is None:
yield
else:
try:
yield
except AnalysisError:
msg = traceback.format_exc()
else:
msg = ''
fails = comm.allgather(msg)
for i, f in enumerate(fails):
if f:
raise AnalysisError("AnalysisError raised in rank %d: traceback follows\n%s"
% (i, f))
@contextmanager
def multi_proc_exception_check(comm):
"""
Raise an exception on all procs if it is raised on one.
Wrap this around code that you want to globally fail if it fails
on any MPI process in comm. If not running under MPI, don't
handle any exceptions.
Parameters
----------
comm : MPI communicator or None
Communicator from the ParallelGroup that owns the calling solver.
"""
if MPI is None or comm is None or comm.size == 1:
yield
else:
try:
yield
except Exception:
exc = sys.exc_info()
fail = 1
else:
fail = 0
failed = comm.allreduce(fail)
if failed:
if fail:
msg = f"{exc[1]}"
else:
msg = None
allmsgs = comm.allgather(msg)
if fail:
msg = f"Exception raised on rank {comm.rank}: {exc[1]}"
raise exc[0](msg).with_traceback(exc[2])
else:
for m in allmsgs:
if m is not None:
raise RuntimeError(f"Exception raised on other rank: {m}.")
if MPI:
def check_mpi_exceptions(fn):
"""
Wrap a function in multi_proc_exception_check.
This should be used only as a method decorator on an instance that
has a 'comm' attribute that refers to an MPI communicator.
Parameters
----------
fn : function
The function being checked for possible memory leaks.
Returns
-------
function
A wrapper for fn that reports possible memory leaks.
"""
@functools.wraps(fn)
def wrapper(*args, **kwargs):
with multi_proc_exception_check(args[0].comm):
return fn(*args, **kwargs)
return wrapper
else:
# do nothing decorator
def check_mpi_exceptions(fn):
"""
Wrap a function in multi_proc_exception_check.
This does nothing if not running under MPI.
Parameters
----------
fn : function
The function being checked for possible memory leaks.
Returns
-------
function
A wrapper for fn that reports possible memory leaks.
"""
return fn
if MPI:
def mpirun_tests():
"""
Run individual tests under MPI.
This is used in the "if __name__ == '__main__'" block to run an
individual test in the file under MPI. Note that if the test
file has not been run under mpirun, this reverts to running
unittest.main().
"""
mod = __import__('__main__')
tests = [arg for arg in sys.argv[1:] if not arg.startswith('-')]
if tests:
for test in tests:
parts = test.split('.', 1)
if len(parts) == 2:
tcase_name, method_name = parts
testcase = getattr(mod, tcase_name)(methodName=method_name)
setup = getattr(testcase, 'setUp', None)
if setup is not None:
setup()
getattr(testcase, method_name)()
teardown = getattr(testcase, 'tearDown', None)
if teardown:
teardown()
else:
funcname = parts[0]
getattr(mod, funcname)()
else:
unittest.main()
else:
mpirun_tests = unittest.main
if os.environ.get('USE_PROC_FILES') or os.environ.get('PROC_FILES_DIR'):
use_proc_files()