Skip to content

Reduce the memory usage that is important for ne1024 simulation#4102

Open
sjsprecious wants to merge 117 commits into
ESCOMP:masterfrom
sjsprecious:reduce_init_memory
Open

Reduce the memory usage that is important for ne1024 simulation#4102
sjsprecious wants to merge 117 commits into
ESCOMP:masterfrom
sjsprecious:reduce_init_memory

Conversation

@sjsprecious

@sjsprecious sjsprecious commented Jun 25, 2026

Copy link
Copy Markdown

Description of changes

This PR aims at reducing memory usage of a simulation at ne1024 resolution. All the changes are done by Claude under my supervisory.


The goal is to cut CTSM initialization memory (and some init time) at high resolution (like ne1024), where per-rank data replication and duplicate ESMF mesh construction dominate startup cost.

The detailed edits:

  1. New per-node shared-memory helper: clm_shmem_mod.F90

A MPI-3 shared-memory module and specialized for CTSM's decomposition setup. The idea is that arrays that are otherwise allocated identically on every MPI rank instead get one physical copy per shared-memory node, mapped into every rank on that node — freeing ranks_per_node − 1 copies per node.

  • clm_shmem_alloc_i4_1d(ptr, win, n) — allocate a node-shared default-integer rank-1 array (only the node leader requests storage via MPI_Win_allocate_shared; peers map the leader's segment via MPI_Win_shared_query).

  • clm_shmem_leader_allreduce_sum_i4(ptr, win, n) — fence → node leaders sum partials across nodes over a leader-only communicator → fence to publish. Builds a globally-summed array in the shared buffer without every rank holding a global-sized copy.

  • clm_shmem_free / clm_shmem_fence / clm_shmem_is_leader / clm_shmem_leader_comm / clm_shmem_npes_per_node — lifecycle and query helpers; lazily build node-local and node-leader communicators via mpi_comm_split_type(MPI_COMM_TYPE_SHARED).

  1. lnd_set_decomp_and_domain.F90 — apply the shmem helper to the global land mask

The global land mask lndmask_glob(gsize) was previously allocated on every rank and built with an all-rank ESMF_VMAllReduce into a second global-sized temporary (itemp_glob). Now, in both code paths (lnd_set_lndmask_from_maskmesh and lnd_set_lndmask_from_lndmesh):

  • lndmask_glob is allocated once per node via clm_shmem_alloc_i4_1d, with a new lndmask_win window handle threaded through both subroutine signatures.

  • Leader zeroes it, fence, each rank fills its disjoint local indices, then clm_shmem_leader_allreduce_sum_i4 replaces the ESMF_VMAllReduce + itemp_glob temporary (the temporary is deleted entirely).

  • Cleanup is now branch-aware: the cmeps driver paths free via clm_shmem_free(lndmask_glob, lndmask_win); the lilac path still uses plain deallocate (it uses a plain allocate).

This removes two global-sized integer arrays per rank (the mask copy + the all-reduce temp), replaced by one node-shared copy.

  1. NetCDF file-handle close fixes

Closing pio file handles that were opened but closed late or never — frees buffers earlier in init:

  • clm_instMod.F90: moves ncd_pio_closefile(params_ncid) earlier — to right after its last use (bgc_vegetation_inst%Init) instead of at the end of init_accflds.
  • initVerticalMod.F90: moves ncd_pio_closefile(ncid) to right after the last read (STD_ELEV) instead of the end of initVertical.
  • UrbanParamsType.F90: adds a missing ncd_pio_closefile(ncid) on the early-return path (nlevurb == 0) that previously leaked the handle.
  • organicFileMod.F90: adds ncd_pio_closefile(ncid) after reading ORGANIC.
    surfrdMod.F90: adds two ncd_pio_closefile(ncid) calls after dimension reads complete (after the pft/cft dims, and after nlevurb).
  1. reuse already-built model mesh for redist streams
  • PrigentRoughnessStreamType.F90 / UrbanTimeVarType.F90: the changes are now done in CDEPS whenever these streams use stream_mapalgo='redist'

Specific notes

Contributors other than yourself, if any:

  • (Replace this text and add more list items as needed)

CTSM issues resolved or otherwise addressed, if any:

Resolves #4103

Any user interface changes (namelist or namelist defaults changes)?

No.

Testing planned or performed, if any:

aux_clm

Requirements before merge:

  • The code in this PR branch builds with no errors.
  • The code in this PR branch runs with no errors. Briefly describe tested configuration(s): aux_clm
  • This either (a) does not change answers, (b) it only changes answers at roundoff level, or (c) I have performed a scientific evaluation of the answer changes. (a) does not change answers
  • I have reviewed relevant parts of the CLM documentation Tech Note or User's Guide to determine if anything needs to be changed or added. If it does, describe:
  • This PR either (a) does not create a need to update the documentation or (b) includes required documentation updates (see guidelines for contributing documentation). Which?:

 Conflicts:
	src/cpl/share_esmf/lnd_set_decomp_and_domain.F90
…e destroyed so remove the destroy for the distgrid, and the two meshes, this runs but doesn't seem to lower memory
…e about leaving the distgrid around, and also delete the meshes as it seems to work with this in place
 Conflicts:
	src/main/decompInitMod.F90
 Conflicts:
	src/cpl/share_esmf/lnd_set_decomp_and_domain.F90
…ted error messaging

 Conflicts:
	src/main/decompInitMod.F90
…he subname, create new internal subroutines in decompInit_lnd for allocate, clean, and check errors, move the check errors part to the first thing done

 Conflicts:
	src/main/decompInitMod.F90
…array sizes are set before allocates, initialize some decompMod values to invalid for error checking, add error checking to get_proc_bounds/get_proc_clumps, seperate out allocate for gindex to own allocate method, as it has be be done later after decomp is done, these are all improvements in ESCOMP#3448

 Conflicts:
	src/main/decompInitMod.F90
… add error handling of nsegspc, don't check endCohort in get_proc_bounds and get_clump_bounds as doesn't seem to be set

 Conflicts:
	src/main/decompInitMod.F90
…etup/clean for each DecompInit test, move the decomp_mod_clean to decompMod and use it for the decompInit tests

 Conflicts:
	src/main/decompInitMod.F90
	src/self_tests/TestDecompInit.F90 --- removed
…re to the regular operation

 Conflicts:
	src/main/decompInitMod.F90
…mpi-serial

 Conflicts:
	src/main/decompInitMod.F90
 Conflicts:
	src/main/decompInitMod.F90
…sor_type structure, and start adding a couple methods to help get them set
…for mpiscan and verify it, allocate the new procinfo gi and gj indices, make sure they are set, compiles but fails at run

 Conflicts:
	src/main/decompInitMod.F90
…the call expects all subgrid levels to be set
…ocate for the local task, this works for the serial case

 Conflicts:
	src/main/decompInitMod.F90
… clump_pproc for the setting of ggidx

 Conflicts:
	src/main/decompInitMod.F90
…moved for the final version

 Conflicts:
	src/main/decompInitMod.F90
…s for serial mode

 Conflicts:
	src/main/decompInitMod.F90
…global clumps

 Conflicts:
	src/self_tests/TestDecompInit.F90
… the log that aren't needed anymore

 Conflicts:
	src/main/decompInitMod.F90
@samsrabin samsrabin removed the next this should get some attention in the next week or two. Normally each Thursday SE meeting. label Jul 2, 2026
@samsrabin samsrabin requested a review from ekluzek July 2, 2026 16:41
@sjsprecious

Copy link
Copy Markdown
Author

SE meeting:

  • This is blocked by the required CDEPS update.
  • Since this has submodule updates, we will merge this to master just to be safe—even though it should be bit-for-bit.
  • @ekluzek will handle this.

Thanks @samsrabin . My CDEPS PR was merged and I just updated the CDEPS tag here. Let me know if you or @ekluzek have any questions or comments before merging it.

@samsrabin

Copy link
Copy Markdown
Member

@briandobbins I might have misremembered. Is there also a CMEPS PR this depends on?

@sjsprecious

Copy link
Copy Markdown
Author

My CMEPS PR is independent of my CTSM PR here.

@briandobbins

Copy link
Copy Markdown
Contributor

How critical is your CMEPS PR to the memory reduction you need to run, Jian? (On a conversation about it right now - there's some fundamental complexities in CMEPS that this is highlighting, so I would just like your assessment.)

@sjsprecious

Copy link
Copy Markdown
Author

How critical is your CMEPS PR to the memory reduction you need to run, Jian? (On a conversation about it right now - there's some fundamental complexities in CMEPS that this is highlighting, so I would just like your assessment.)

Thanks @briandobbins . We have a very tight memory headroom on Derecho so I need all the changes in my CDEPS, CMEPS, CTSM, and future CAM PRs to get the ne1024 simulation to work with more ranks per node. Bill mentioned some concerns about CMEPS but I think that is unrelated to my changes directly.

My CMEPS PR seems to be BFB so if Bill or you can merge it, I can update the tag here as well.

@briandobbins

Copy link
Copy Markdown
Contributor

Unfortunately it's not that simple -- Bill, Bob and Mariana are speaking this afternoon about the CMEPS issues, and I'll know more after that, but I can't commit to the CMEPS PR yet.

@sjsprecious

Copy link
Copy Markdown
Author

Unfortunately it's not that simple -- Bill, Bob and Mariana are speaking this afternoon about the CMEPS issues, and I'll know more after that, but I can't commit to the CMEPS PR yet.

I see. Just want to clarify again: my CTSM PR is independent of my CMEPS PR, so it should not affect the review or merge process here.

@briandobbins

briandobbins commented Jul 2, 2026 via email

Copy link
Copy Markdown
Contributor

ekluzek and others added 4 commits July 6, 2026 11:09
…ed longer times and to make it easier to get in the queues
…st the basic configuration for mpasa3p75 more often
b4b: Move the changes to initialize the task decomposition from mpi_scan to main development
@sjsprecious

Copy link
Copy Markdown
Author

Hi @samsrabin and @ekluzek , just wants to give you a heads up: Both my CDEPS and CMEPS PRs are now merged. The CDEPS PR is BFB but the CMEPS PR is answer-changing at roundoff level.

Since my CTSM PR does not depend on the CMEPS PR, I prefer not to include the CMEPS changes in my PR here so that my CTSM changes remain BFB as shown in the previous test. Let me know if you would like to proceed with a different option.

@ekluzek

ekluzek commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Hi @samsrabin and @ekluzek , just wants to give you a heads up: Both my CDEPS and CMEPS PRs are now merged. The CDEPS PR is BFB but the CMEPS PR is answer-changing at roundoff level.

Since my CTSM PR does not depend on the CMEPS PR, I prefer not to include the CMEPS changes in my PR here so that my CTSM changes remain BFB as shown in the previous test. Let me know if you would like to proceed with a different option.

Thanks so much @sjsprecious this is great. Our preference is to seperate out any answer changes from things that are expected to be bit-for-bit. And actually it might be good to remove the CDEPS submodule update here to a seperate thing.

Before we bring this in we need to see how it merges with the mpi_scan branch that was merged onto b4b-dev. So it might be best to remove the CDEPS update here, and rebase this to the b4b-dev branch. See the instructions under "1" here: https://github.com/ESCOMP/CTSM/wiki/b4b%E2%80%90dev-branch-procedures#merging-to-b4b-dev

Since you've already done work here, and I'm asking for more -- I'll create a PR on master to bring the bit-for-bit CDEPS update in, as well as a different one to bring in the answer changing CMEPS one. How does that sound?

@sjsprecious

Copy link
Copy Markdown
Author

Thanks so much @sjsprecious this is great. Our preference is to seperate out any answer changes from things that are expected to be bit-for-bit. And actually it might be good to remove the CDEPS submodule update here to a seperate thing.

Thanks @ekluzek . It sounds good to me.

Before we bring this in we need to see how it merges with the mpi_scan branch that was merged onto b4b-dev. So it might be best to remove the CDEPS update here, and rebase this to the b4b-dev branch. See the instructions under "1" here: https://github.com/ESCOMP/CTSM/wiki/b4b%E2%80%90dev-branch-procedures#merging-to-b4b-dev

I will do it as suggested and let you know if I run into any problem later.

Since you've already done work here, and I'm asking for more -- I'll create a PR on master to bring the bit-for-bit CDEPS update in, as well as a different one to bring in the answer changing CMEPS one. How does that sound?

That sounds good to me. Thanks for your help.

@sjsprecious sjsprecious force-pushed the reduce_init_memory branch from d7d199c to acc0b2b Compare July 9, 2026 02:45
@sjsprecious

Copy link
Copy Markdown
Author

Hi @ekluzek , I have removed the CDEPS update in my PR and rebased my branch to the latest b4b-dev branch. Let me know if you have any questions about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocked: dependency Wait to work on this until dependency is resolved performance idea or PR to improve performance (e.g. throughput, memory)

Development

Successfully merging this pull request may close these issues.

Out of memory issue in CTSM at ne1024 resolution

4 participants