-
Notifications
You must be signed in to change notification settings - Fork 240
/
case_recorder.py
357 lines (304 loc) · 12.4 KB
/
case_recorder.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
"""
Class definition for CaseRecorder, the base class for all recorders.
"""
from openmdao.core.system import System
from openmdao.core.driver import Driver
from openmdao.solvers.solver import Solver
from openmdao.core.problem import Problem
from openmdao.utils.mpi import MPI
from openmdao.utils.options_dictionary import OptionsDictionary
from openmdao.utils.record_util import check_path
# default pickle protocol version for serialization
PICKLE_VER = 4
class CaseRecorder(object):
"""
Base class for all case recorders and is not a functioning case recorder on its own.
Parameters
----------
record_viewer_data : bool, optional
If True, record data needed for visualization.
Attributes
----------
_record_viewer_data : bool
Flag indicating whether to record data needed to generate N2 diagram.
_counter : int
A global counter for execution order, used in iteration coordinate.
_inputs : dict
System inputs values, post-filtering, to be used by a derived recorder.
_outputs : dict
System or Solver output values, post-filtering, to be used by a derived recorder.
_resids : dict
System or Solver residual values, post-filtering, to be used by a derived recorder.
_abs_error : float
Solver abs_error value, to be used by a derived recorder.
_rel_error : float
Solver abs_error value, to be used by a derived recorder.
_iteration_coordinate : str
The unique iteration coordinate of where an iteration originates.
_parallel : bool
Flag indicating if this recorder will record on multiple processes.
_record_on_proc : bool or None
Flag indicating if this recorder will record on the current process (None if unspecified).
_recording_ranks : list
List of ranks on which this recorder will record if running under MPI.
"""
def __init__(self, record_viewer_data=True):
"""
Initialize.
Parameters
----------
record_viewer_data : bool, optional
If True, record data needed for visualization.
"""
self._record_viewer_data = record_viewer_data
# global counter that is used in iteration coordinate
self._counter = 0
# For Systems
self._inputs = None
self._outputs = None
# For Solvers
self._abs_error = 0.0
self._rel_error = 0.0
# For Drivers, Systems, and Solvers
self._iteration_coordinate = None
# By default, this is False, but it will be set to True if the recorder
# will record data on multiple processes
self._parallel = False
# Flag indicating if recording will be performed on the current process.
# If the value is not set to True on any process (the default), then
# recording will be performed only on rank 0.
# If the value is set to True on any process, then the _parallel flag
# will be set and recording will occur on all processes for which the
# value is True.
self._record_on_proc = None
# List of ranks on which this recorder will record if running under MPI.
# Only used when running under MPI with communicator size greater than one.
self._recording_ranks = None
@property
def record_on_process(self):
"""
Determine if recording should be performed on this process.
"""
return self._record_on_proc
@record_on_process.setter
def record_on_process(self, record):
"""
Specify that recording should be performed on this process.
Parameters
----------
record : bool
If True, then recording should be performed on this process.
"""
self._record_on_proc = record
@property
def parallel(self):
"""
Return True if this recorder is recording on multiple processes.
"""
return self._parallel
def startup(self, recording_requester, comm=None):
"""
Prepare for a new run.
Parameters
----------
recording_requester : object
Object to which this recorder is attached.
comm : MPI.Comm or <FakeComm> or None
The MPI communicator for the recorder (should be the comm for the Problem).
"""
self._counter = 0
if MPI and comm and comm.size > 1:
record_on_ranks = comm.allgather(self._record_on_proc)
recording_ranks = [rnk for rnk, rec in enumerate(record_on_ranks) if rec is True]
if recording_ranks:
# recording ranks have been specified
self._recording_ranks = recording_ranks
self._parallel = len(recording_ranks) > 1
else:
# default to just record on rank 0
self._record_on_proc = comm.rank == 0
self._recording_ranks = [0]
def _get_metadata_system(self, system):
"""
Get system metadata.
Parameters
----------
system : System
The System for which to record metadata.
Returns
-------
dict
dictionary of scaling vectors
OptionsDictionary
dictionary with recordable options for system
"""
# Cannot handle PETScVector yet
from openmdao.api import PETScVector
if PETScVector and isinstance(system._outputs, PETScVector):
return None, None # Cannot handle PETScVector yet
# collect scaling arrays
scaling_vecs = {}
for kind, odict in system._vectors.items():
scaling_vecs[kind] = scaling = {}
for vecname, vec in odict.items():
scaling[vecname] = vec._scaling
# create a copy of the system's metadata excluding what is in 'options_excludes'
excludes = system.recording_options['options_excludes']
if excludes:
user_options = OptionsDictionary()
user_options._all_recordable = system.options._all_recordable
for key in system.options._dict:
if check_path(key, [], excludes, True):
user_options._dict[key] = system.options._dict[key]
user_options._read_only = system.options._read_only
return scaling_vecs, user_options
else:
return scaling_vecs, system.options
def record_metadata_system(self, system, run_number=None):
"""
Record system metadata.
Parameters
----------
system : System
The System for which to record metadata.
run_number : int or None
Number indicating which run the metadata is associated with.
None for the first run, 1 for the second, etc.
"""
raise NotImplementedError()
def record_metadata_solver(self, solver, run_number=None):
"""
Record solver metadata.
Parameters
----------
solver : Solver
The Solver for which to record metadata.
run_number : int or None
Number indicating which run the metadata is associated with.
None for the first run, 1 for the second, etc.
"""
raise NotImplementedError()
def record_iteration(self, recording_requester, data, metadata, **kwargs):
"""
Route the record_iteration call to the proper method.
Parameters
----------
recording_requester : object
System, Solver, Driver in need of recording.
data : dict
Dictionary containing desvars, objectives, constraints, responses, and System vars.
metadata : dict, optional
Dictionary containing execution metadata.
**kwargs : keyword args
Some implementations of record_iteration need additional args.
"""
if not self._parallel or self._record_on_proc is True:
self._counter += 1
self._iteration_coordinate = \
recording_requester._recording_iter.get_formatted_iteration_coordinate()
if isinstance(recording_requester, Driver):
self.record_iteration_driver(recording_requester, data, metadata)
elif isinstance(recording_requester, System):
self.record_iteration_system(recording_requester, data, metadata)
elif isinstance(recording_requester, Solver):
self.record_iteration_solver(recording_requester, data, metadata)
elif isinstance(recording_requester, Problem):
self.record_iteration_problem(recording_requester, data, metadata)
else:
raise ValueError("Recorders must be attached to Drivers, Systems, or Solvers.")
def record_iteration_driver(self, recording_requester, data, metadata):
"""
Record data and metadata from a Driver.
Parameters
----------
recording_requester : Driver
Driver in need of recording.
data : dict
Dictionary containing desvars, objectives, constraints, responses, and System vars.
metadata : dict
Dictionary containing execution metadata.
"""
raise NotImplementedError("record_iteration_driver has not been overridden")
def record_iteration_system(self, recording_requester, data, metadata):
"""
Record data and metadata from a System.
Parameters
----------
recording_requester : System
System in need of recording.
data : dict
Dictionary containing inputs, outputs, and residuals.
metadata : dict
Dictionary containing execution metadata.
"""
raise NotImplementedError("record_iteration_system has not been overridden")
def record_iteration_solver(self, recording_requester, data, metadata):
"""
Record data and metadata from a Solver.
Parameters
----------
recording_requester : Solver
Solver in need of recording.
data : dict
Dictionary containing outputs, residuals, and errors.
metadata : dict
Dictionary containing execution metadata.
"""
raise NotImplementedError("record_iteration_solver has not been overridden")
def record_iteration_problem(self, recording_requester, data, metadata):
"""
Record data and metadata from a Problem.
Parameters
----------
recording_requester : Problem
Problem in need of recording.
data : dict
Dictionary containing desvars, objectives, constraints.
metadata : dict
Dictionary containing execution metadata.
"""
raise NotImplementedError("record_iteration_problem has not been overridden")
def record_derivatives(self, recording_requester, data, metadata, **kwargs):
"""
Route the record_derivatives call to the proper method.
Parameters
----------
recording_requester : object
System, Solver, Driver in need of recording.
data : dict
Dictionary containing derivatives keyed by 'of,wrt' to be recorded.
metadata : dict
Dictionary containing execution metadata.
**kwargs : keyword args
Some implementations of record_derivatives need additional args.
"""
self._iteration_coordinate = \
recording_requester._recording_iter.get_formatted_iteration_coordinate()
self.record_derivatives_driver(recording_requester, data, metadata)
def record_derivatives_driver(self, recording_requester, data, metadata):
"""
Record derivatives data from a Driver.
Parameters
----------
recording_requester : Driver
Driver in need of recording.
data : dict
Dictionary containing derivatives keyed by 'of,wrt' to be recorded.
metadata : dict
Dictionary containing execution metadata.
"""
raise NotImplementedError("record_derivatives_driver has not been overridden")
def record_viewer_data(self, model_viewer_data):
"""
Record model viewer data.
Parameters
----------
model_viewer_data : dict
Data required to visualize the model.
"""
raise NotImplementedError("record_viewer_data has not been overridden")
def shutdown(self):
"""
Shut down the recorder.
"""
pass