Repository navigation
Open
Labels
Description
In almost every MPAS-Ocean module, config flags are transferred to private module variables. For example, in the ocean core, for del2, we have:
! Private module variables
logical :: del2On
real (kind=RKIND) :: eddyDiff2
and on init:
subroutine ocn_tracer_hmix_del2_init(err)!{{{
...
logical, pointer :: config_use_tracer_del2
real (kind=RKIND), pointer :: config_tracer_del2
...
call mpas_pool_get_config(ocnConfigs, 'config_use_tracer_del2', config_use_tracer_del2)
call mpas_pool_get_config(ocnConfigs, 'config_tracer_del2', config_tracer_del2)
del2on = .false.
if ( config_use_tracer_del2 ) then
if ( config_tracer_del2 > 0.0_RKIND ) then
del2On = .true.
eddyDiff2 = config_tracer_del2
endif
endif
Is there any performance reason to do this? It seems like a lot of extra bookkeeping, and extra variables obscure code. Options are:
- Do it this way (use extra variables with different names)
- Call
mpas_pool_get_configfor config flags every time the main routine is called (i.e. every time step) - Define those
config_*variables as private module variables and fill them in on init - Obtain ALL config_* variables ONCE on init in some new module (say ocn_configs) and then have every module
use ocn_configsat the top and use config variables by name. Then we would never declare config flags or callmpas_pool_get_configwithin these normal modules.
I'm asking now because I'm adding new flags for Redi mixing, but it is relevant to all modules. Any of 1-3 we could alter piecemeal. Number 4 (which I prefer) would need a larger coordinated change.
I remember from @philipwjones talk that evaluating strings in if statements is expensive, so there may be two cases here, one for config flags that are strings, and one for flags that are already logicals, integers, or real.
Activity
amametjanov commentedon Nov 12, 2019
+1 on doing it once with option 4. Not sure if there's a reason for the current method: module-private vars still depend on run-time input namelists: i.e. they're not compile-time constant
parametervars.philipwjones commentedon Nov 12, 2019
We do not want options 1,2 as they are likely causing some unnecessary performance overhead doing the character matching every time step. We definitely want to set them on init and not keep retrieving them from config.
Using local module variables (option 3) keeps most of them encapsulated within the module that's using them. And because they're encapsulated, you can give them shorter variable names with less of a risk of namespace conflict. And for multiple choices, that will keep most changes local to a module if you need to add another option and doesn't clutter up a big comprehensive config module. For variables that need to be accessed by multiple modules, you can either make them shared and include the relevant module where it's used or if that creates complicated dependencies, you could keep them in an independent module (hopefully that's rare).
Either way, please convert strings to other forms. Simple binary options should be implemented as logical flags. For conditions where we have multiple options, the preferred way would be to use the Fortran equivalent of enums - basically named integer parameters. So something like this contrived setup:
integer, parameter :: &
advectChoiceStd = 1,
advectChoiceFCT = 2,
advectChoiceSLT = 3
integer myAdvectChoice
! set myChoice based on input nml on startup
select case (myAdvectChoice)
case(advectChoiceStd)
do stuff
case(advectChoiceFCT)
do stuff
case(advectChoiceSLT)
do stuff
case default
flag error
end select
philipwjones commentedon Nov 12, 2019
To clarify, when I say convert strings - we can (and should) still have string options in the namelists - just want to convert them during init to something that's more efficient for the integration.