Skip to content

Implement variable size caching in iso_c_bmi.f90 - #4

Open
JoshCu wants to merge 28 commits into
robertbartel:i/optimize_set_bmi_inputs/mainfrom
JoshCu:patch-1
Open

Implement variable size caching in iso_c_bmi.f90#4
JoshCu wants to merge 28 commits into
robertbartel:i/optimize_set_bmi_inputs/mainfrom
JoshCu:patch-1

Conversation

@JoshCu

@JoshCu JoshCu commented Aug 7, 2026

Copy link
Copy Markdown

Apologies for the intrusion into your fork,
I was testing out some variable caching similar to NOAA-OWP#942 and saw that libiso_c_bmi.so was also spending a lot of time getting nbytes and itemsize so I added a similar cache to the fortran middleware.

I know very little Fortran so this was heavily AI assisted, but it seems to speed things up ~1.2x on calibration type data of a few catchments run for many years.
For forecast type data the speedup is barely outside margin of error, 24h for the mississippi basin ~309k catchments it was only ~1.02x :(
image

tests using noaa-owp/noah-owp-modular@main with the recent date fixes, and this older cfe pin)

I tested 7 years on 52 catchments here and saw 2.3s drop to 1.9s and get_value_float went from 51% of total execution to 38%

image

robertbartel and others added 28 commits July 14, 2026 13:54
Creating new Bmi_Var_Details struct to hold BMI module variable details
that the framework needs for performing certain operations, so that
these things can be stored in memory for faster execution; also,
creating a new member variable for Bmi_Module_Formulation class that is
a vector of these new Bmi_Var_Details instances containing details on
the given module's BMI input variables.
Modify Bmi_Module_Formulation.set_model_inputs_prior_to_update to store
BMI input variable details (e.g., data provider) on first execution and
then reuse these saved values, rather than executing module BMI function
calls to re-retrieve these values on every time step.
Adjust function signature to change param name for model time to avoid
confusion regarding what might be meant by "init" or "initial" in BMI
contexts, and fix docstring for param, which was wrong even before the
name change.
Adjusting things further in set_model_inputs_prior_to_update
improvements, including

- support both executing with these new optimizations or without them
  (i.e., still fetching and re-calculating input var metadata on each
  call)
- add member variable (default: `false`) and setter function to store
  whether to execute using new set_model_inputs_prior_to_update
  optimizations or original "always re-fetch" logic
- modularize parts of functionality into several different functions for
  better organization and reusability
- add variable units to metadata that is stored
Adding config option for available set_model_inputs_prior_to_update
optimizations that store metadata, along with documentation on use.
Making several adjustments to the optimizations for storing BMI input
variable metadata in a formulation for reuse:

- make Bmi_Var_Details (effectively immutable) class rather than struct
- pull provider pointer out of Bmi_Var_Details and setup a second vector
  in the formulation object for providers that corresponds to the one
  for Bmi_Var_Details (providers can be formulation-specific, so
  relationship belongs there)
- created static std::set of Bmi_Var_Details objects that gets added to
  during logic to populate instance's vector of input Bmi_Var_Details,
  with the latter vector only having values inserted that exist in this
  static set
- move logic for initializing the metadata-containing collections to a
  dedicated function
Updating logic in Bmi_Module_Formulation::get_provider_for_input_var to
avoid searching a map twice back-to-back for the same key.
Updating with more concise params list that rolls several things into a
Bmi_Var_Details object, and then updating usage to match.
Rename store_input_variable_metadata to cache_input_variable_metadata to
clarify things are saved in mem not stored on disk; updating macro name
and documentation to also reflect and better indicate this.
Renaming related params, functions, and similarly (but not identically)
named variables in the same way.
Simplify copy constructor and improve formatting of < overload.
Fixed new tests associated with the optimization changes, and moved the
boilerplate present (now, after some rebasing) in prior tests but
missing from new ones (related to those tests now breaking) into a
reusable function.
Use by-val and moves rather than const ref to reduce copies, per
suggestion from @hellite500.

Co-authored-by: Nels <nels.frazier@noaa.gov>
Add test that caching input variable metadata yields responses
consistent with default execution, where metadata is re-fetched each
time.
More tests to make sure Formulation_Manager behaves properly in various
situations and gets formulations with appropriate cache flag setting for
config.
Added caching for variable sizes to optimize lookups and reduce model queries. Introduced var_size_t type and modified functions to utilize the cache.
@robertbartel
robertbartel force-pushed the i/optimize_set_bmi_inputs/main branch from 2165182 to fea7d14 Compare August 19, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants