Skip to content

Commit

Permalink
Force setup_run_directory function to raise AttributeError if it does…
Browse files Browse the repository at this point in the history
…n't have access to a cpu layout rather than throw generic error
  • Loading branch information
ashjbarnes committed Jul 1, 2024
1 parent e9d5e3d commit 187bee5
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions regional_mom6/regional_mom6.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ def __init__(
self.grid_type = grid_type
self.repeat_year_forcing = repeat_year_forcing
self.ocean_mask = None
self.layout = None # This should be a tuple. Leaving in a dummy 'None' makes it easy to remind the user to provide a value later on.
if read_existing_grids:
try:
self.hgrid = xr.open_dataset(self.mom_input_dir / "hgrid.nc")
Expand Down Expand Up @@ -1502,18 +1503,26 @@ def setup_run_directory(
mask_table = p.name
x, y = (int(v) for v in layout.split("x"))
ncpus = (x * y) - int(masked)
layout = (x, y) # This is a local variable keeping track of the layout as read from the mask table. Not to be confused with self.layout which is unchanged and may differ.

print(f"Mask table {p.name} read. Using this to infer the cpu layout {layout}, total masked out cells {masked}, and total number of CPUs {ncpus}.")


if mask_table == None:
if self.layout == None:
raise AttributeError("No mask table found, and the cpu layout has not been set. At least one of these is requiret to set up the experiment.")
print(
"No mask table found! This suggests the domain is mostly water, so there are "
f"No mask table found, but the cpu layout has been set to {self.layout} This suggests the domain is mostly water, so there are "
+ "no `non compute` cells that are entirely land. If this doesn't seem right, "
+ "ensure you've already run `FRE_tools`."
+ "ensure you've already run the `FRE_tools` method which sets up the cpu mask table. Keep an eye on any errors that might print while"
+ "the FRE tools (which run C++ in the background) are running."
)
if not hasattr(self, "layout"):
raise AttributeError(
"No layout information found. This suggests that `FRE_tools()` hasn't been called yet. "
+ "Please do so, in order for the number of processors required is computed."
)
ncpus = self.layout[0] * self.layout[1]
# Here we define a local copy of the layout just for use within this function.
# This prevents the layout from being overwritten in the main class in case
# in case the user accidentally loads in the wrong mask table.
layout = self.layout
ncpus = layout[0] * layout[1]

print("Number of CPUs required: ", ncpus)

## Modify the input namelists to give the correct layouts
Expand All @@ -1527,7 +1536,7 @@ def setup_run_directory(
else:
lines[jj] = "# MASKTABLE = no mask table"
if "LAYOUT =" in lines[jj] and "IO" not in lines[jj]:
lines[jj] = f"LAYOUT = {self.layout[1]},{self.layout[0]}\n"
lines[jj] = f"LAYOUT = {layout[1]},{layout[0]}\n"

if "NIGLOBAL" in lines[jj]:
lines[jj] = f"NIGLOBAL = {self.hgrid.nx.shape[0]//2}\n"
Expand Down

0 comments on commit 187bee5

Please sign in to comment.