Skip to content

Commit

Permalink
Profiling: subparameter parser support
Browse files Browse the repository at this point in the history
The very crude MOM_input parser in the automatic profiler did not
support subparameters (e.g. MLE% ... %MLE), which caused an error when
trying to read the FMS clock output.

This patch adds the support, or at least enough support to avoid errors.
  • Loading branch information
marshallward authored and Hallberg-NOAA committed May 30, 2023
1 parent 501fcff commit b32b2ed
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .testing/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ prof.p0: $(WORKSPACE)/work/p0/opt/clocks.json $(WORKSPACE)/work/p0/opt_target/cl
python tools/compare_clocks.py $^

$(WORKSPACE)/work/p0/%/clocks.json: $(WORKSPACE)/work/p0/%/std.out
python tools/parse_fms_clocks.py -d $(@D) $^ > $@
python tools/parse_fms_clocks.py -d $(@D) $^ > $@ \
|| !( rm $@ )

$(WORKSPACE)/work/p0/opt/std.out: build/opt/MOM6
$(WORKSPACE)/work/p0/opt_target/std.out: build/opt_target/MOM6
Expand Down
54 changes: 46 additions & 8 deletions .testing/tools/parse_fms_clocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,61 @@ def main():
print(json.dumps(config))


def parse_mom6_param(param_file):
def parse_mom6_param(param_file, header=None):
"""Parse a MOM6 input file and return its contents.
param_file: Path to MOM input file.
header: Optional argument indicating current subparameter block.
"""
params = {}
for line in param_file:
# Remove any trailing comments from the line.
# NOTE: Exotic values containing `!` will behave unexpectedly.
param_stmt = line.split('!')[0].strip()
if param_stmt:
key, val = [s.strip() for s in param_stmt.split('=')]

# TODO: Convert to equivalent Python types
if val in ('True', 'False'):
params[key] = bool(val)
else:
params[key] = val
# Skip blank lines
if not param_stmt:
continue

if param_stmt[-1] == '%':
# Set up a subparameter block which returns its own dict.

# Extract the (potentially nested) subparameter: [...%]param%
key = param_stmt.split('%')[-2]

# Construct subparameter endline: %param[%...]
subheader = key
if header:
subheader = header + '%' + subheader

# Parse the subparameter contents and return as a dict.
value = parse_mom6_param(param_file, header=subheader)

elif header and param_stmt == '%' + header:
# Finalize the current subparameter block.
break

else:
# Extract record from `key = value` entry
# NOTE: Exotic values containing `=` will behave unexpectedly.
key, value = [s.strip() for s in param_stmt.split('=')]

if value in ('True', 'False'):
# Boolean values are converted into Python logicals.
params[key] = bool(value)
else:
# All other values are currently stored as strings.
params[key] = value

return params


def parse_clocks(log):
"""Parse the FMS time stats from MOM6 output log and return as a dict.
log: Path to file containing MOM6 stdout.
"""

clock_start_msg = 'Tabulating mpp_clock statistics across'
clock_end_msg = 'MPP_STACK high water mark='

Expand Down

0 comments on commit b32b2ed

Please sign in to comment.