Skip to content

Commit

Permalink
define runtime params in .cpp instead of *_declares.H (#2797)
Browse files Browse the repository at this point in the history

Co-authored-by: Eric T. Johnson <yut23@users.noreply.github.com>
  • Loading branch information
zingale and yut23 authored Mar 26, 2024
1 parent d86384a commit 760ecc4
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 77 deletions.
2 changes: 2 additions & 0 deletions Exec/Make.auto_source
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ AUTO_BUILD_SOURCES += $(CASTRO_AUTO_SOURCE_DIR)/castro_params.H

CPP_PARAMETERS := $(TOP)/Source/driver/_cpp_parameters

$(CASTRO_AUTO_SOURCE_DIR)/runtime_params.cpp: $(CASTRO_AUTO_SOURCE_DIR)/castro_params.H

$(CASTRO_AUTO_SOURCE_DIR)/castro_params.H: $(CPP_PARAMETERS)
@if [ ! -d $(CASTRO_AUTO_SOURCE_DIR) ]; then mkdir -p $(CASTRO_AUTO_SOURCE_DIR); fi
PYTHONPATH=$(MICROPHYSICS_HOME)/util/build_scripts $(TOP)/Source/driver/parse_castro_params.py -o $(CASTRO_AUTO_SOURCE_DIR) $(CPP_PARAMETERS)
Expand Down
1 change: 0 additions & 1 deletion Source/diffusion/Make.package
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ CEXE_sources += diffusion_util.cpp

CEXE_sources += Castro_diffusion.cpp
CEXE_sources += Diffusion.cpp
CEXE_sources += diffusion_params.cpp
CEXE_headers += Diffusion.H
7 changes: 0 additions & 7 deletions Source/diffusion/diffusion_params.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion Source/driver/Make.package
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# these are the files that should be needed for any Castro build

CEXE_sources += Castro.cpp
CEXE_sources += runparams_defaults.cpp
CEXE_sources += runtime_params.cpp
CEXE_sources += Castro_advance.cpp
CEXE_sources += Castro_advance_ctu.cpp
ifeq ($(USE_TRUE_SDC), TRUE)
Expand Down
100 changes: 58 additions & 42 deletions Source/driver/parse_castro_params.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/usr/bin/env python3

"""
This script parses the list of C++ runtime parameters and writes the
necessary header files and Fortran routines to make them available
in Castro's C++ routines.
"""This script parses the list of C++ runtime parameters and writes
the necessary header and source files to make them available in
Castro's C++ routines. They are available in 2 ways: as global
parameter and in the form of a single struct.
parameters have the format:
Expand Down Expand Up @@ -41,16 +41,16 @@
-- name_params.H (for castro, included in Castro.H):
sets up the namespace and extern parameters
-- name_declares.H (for castro, included in Castro.cpp):
declares the runtime parameters
-- name_queries.H (for castro, included in Castro.cpp):
does the parmparse query to override the default in C++
-- name_job_info_tests.H
this tests the current value against the default and outputs
into a file
-- runtime_params.cpp
has the actual definition of the variables (without extern)
"""

import argparse
Expand Down Expand Up @@ -129,45 +129,19 @@ def read_param_file(infile):

return params

def write_headers(params, out_directory, struct_name):
def write_headers_and_source(params, out_directory, struct_name):

# output

# find all the namespaces
namespaces = {q.namespace for q in params}
namespaces = sorted({q.namespace for q in params})

for nm in namespaces:

params_nm = [q for q in params if q.namespace == nm]
# sort by repr since None may be present
ifdefs = sorted({q.ifdef for q in params_nm}, key=repr)

# write name_declares.H
try:
cd = open(f"{out_directory}/{nm}_declares.H", "w", encoding="UTF-8")
except OSError:
sys.exit(f"unable to open {nm}_declares.H for writing")

cd.write(CWARNING)
cd.write(f"#ifndef {nm.upper()}_DECLARES_H\n")
cd.write(f"#define {nm.upper()}_DECLARES_H\n")

cd.write("\n")
cd.write(f"namespace {nm} {{\n")

for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
cd.write(p.get_declare_string())
else:
cd.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
cd.write(p.get_declare_string())
cd.write("#endif\n")
cd.write("}\n\n")
cd.write("#endif\n")
cd.close()

# write name_params.H
try:
cp = open(f"{out_directory}/{nm}_params.H", "w", encoding="UTF-8")
Expand Down Expand Up @@ -238,7 +212,44 @@ def write_headers(params, out_directory, struct_name):

jo.close()

# write a single C++ source file that actually defines the parameters
# (one file for all namespaces)
try:
pf = open(f"{out_directory}/runtime_params.cpp", "w", encoding="UTF-8")
except OSError:
sys.exit(f"unable to open runtime_params.cpp")

pf.write("#include <AMReX_REAL.H>\n")
pf.write("#include <AMReX_Gpu.H>\n")
pf.write("#include <castro_limits.H>\n\n")

for nm in namespaces:
pf.write(f"#include <{nm}_params.H>\n")
pf.write("\n")

for nm in namespaces:
params_nm = [q for q in params if q.namespace == nm]
# sort by repr since None may be present
ifdefs = sorted({q.ifdef for q in params_nm}, key=repr)

pf.write(f"namespace {nm} {{\n")

for ifdef in ifdefs:
if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
pf.write(p.get_declare_string())
else:
pf.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
pf.write(p.get_declare_string())
pf.write("#endif\n")
pf.write("}\n\n")

pf.close()

# now write a single file that contains all of the parameter structs
# to minimize padding, we want to sort on type

try:
sf = open(f"{out_directory}/{struct_name}_type.H", "w", encoding="UTF-8")
except OSError:
Expand All @@ -255,20 +266,25 @@ def write_headers(params, out_directory, struct_name):
params_nm = [q for q in params if q.namespace == nm]
# sort by repr since None may be present
ifdefs = sorted({q.ifdef for q in params_nm}, key=repr)

sf.write(f"struct {nm}_t {{\n")
print("namespace = ", nm)
for ifdef in ifdefs:
params_if = [q for q in params_nm if q.ifdef == ifdef]
types = sorted(set(q.dtype for q in params_if))

if ifdef is None:
for p in [q for q in params_nm if q.ifdef is None]:
sf.write(p.get_struct_entry())
for tt in types:
params_type = [q for q in params_if if q.dtype == tt]
for p in params_type:
sf.write(p.get_struct_entry())
else:
sf.write(f"#ifdef {ifdef}\n")
for p in [q for q in params_nm if q.ifdef == ifdef]:
sf.write(p.get_struct_entry())
for tt in types:
params_type = [q for q in params_if if q.dtype == tt]
for p in params_type:
sf.write(p.get_struct_entry())
sf.write("#endif\n")


sf.write("};\n\n")

# now the parent struct
Expand All @@ -295,7 +311,7 @@ def main():
args = parser.parse_args()

p = read_param_file(args.input_file[0])
write_headers(p, args.o, args.s)
write_headers_and_source(p, args.o, args.s)

if __name__ == "__main__":
main()
6 changes: 0 additions & 6 deletions Source/driver/runparams_defaults.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Source/gravity/Make.package
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# this is included if USE_GRAV = TRUE

CEXE_sources += Gravity.cpp
CEXE_sources += gravity_params.cpp
CEXE_headers += Gravity.H
CEXE_headers += Gravity_util.H
CEXE_headers += Castro_gravity.H
Expand Down
9 changes: 0 additions & 9 deletions Source/gravity/gravity_params.cpp

This file was deleted.

1 change: 0 additions & 1 deletion Source/radiation/Make.package
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ CEXE_sources += HypreExtMultiABec.cpp
CEXE_sources += HypreMultiABec.cpp
CEXE_sources += HypreABec.cpp
CEXE_sources += Radiation.cpp
CEXE_sources += radiation_params.cpp
CEXE_sources += RadSolve.cpp
CEXE_sources += RadBndry.cpp
CEXE_sources += RadMultiGroup.cpp
Expand Down
9 changes: 0 additions & 9 deletions Source/radiation/radiation_params.cpp

This file was deleted.

0 comments on commit 760ecc4

Please sign in to comment.