From a6d62898450bbaf74468bef9edcbc14d08609f1f Mon Sep 17 00:00:00 2001 From: Haipeng Lin Date: Mon, 16 Dec 2024 13:21:58 -0500 Subject: [PATCH] Improve robustness of cam open/close file register code to fix issues with history output (#333) Tag name (required for release branches): Originator(s): @jimmielin Description (include the issue title, and the keyword ['closes', 'fixes', 'resolves'] followed by the issue number): - Fixes #332 (`max_mdims` used before defined) - Fixes #331 (unassociated `of%file_desc` in `cam_register_open_file` leading to crash with >2 history files) Describe any changes made to build system: N/A Describe any changes made to the namelist: N/A List any changes to the defaults for the input datasets (e.g. boundary datasets): N/A List all files eliminated and why: N/A List all files added and what they do: N/A List all existing files that have been modified, and describe the changes: (Helpful git command: `git diff --name-status development...`) ``` Fixes #331 M src/history/cam_hist_file.F90 Fixes #332 M src/utils/cam_abortutils.F90 ``` If there are new failures (compared to the `test/existing-test-failures.txt` file), have them OK'd by the gatekeeper, note them here, and add them to the file. If there are baseline differences, include the test and the reason for the diff. What is the nature of the change? Roundoff? derecho/intel/aux_sima: derecho/gnu/aux_sima: If this changes climate describe any run(s) done to evaluate the new climate in enough detail that it(they) could be reproduced: CAM-SIMA date used for the baseline comparison tests if different than latest: --- src/history/cam_hist_file.F90 | 1 + src/utils/cam_abortutils.F90 | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/history/cam_hist_file.F90 b/src/history/cam_hist_file.F90 index 525440a1..7e8b75e8 100644 --- a/src/history/cam_hist_file.F90 +++ b/src/history/cam_hist_file.F90 @@ -1015,6 +1015,7 @@ subroutine config_define_file(this, restart, logname, host, model_doi_url) end do end do ! Determine the maximum number of dimensions + max_mdims = 0 do field_index = 1, size(this%field_list) max_mdims = max(max_mdims, size(this%field_list(field_index)%dimensions())) end do diff --git a/src/utils/cam_abortutils.F90 b/src/utils/cam_abortutils.F90 index ca6cd2ce..241c00ea 100644 --- a/src/utils/cam_abortutils.F90 +++ b/src/utils/cam_abortutils.F90 @@ -80,25 +80,31 @@ subroutine cam_register_open_file(file, file_name) end do ! If we get here, go ahead and register the file if (associated(open_files_pool)) then + ! Reuse pooled structure and point to the next pool entry of_new => open_files_pool + open_files_pool => open_files_pool%next allocate(of_new%file_desc, stat=ierr) call check_allocate(ierr, subname, 'of_file%file_desc', file=__FILE__, & line=__LINE__) of_new%file_desc = file of_new%file_name = file_name - allocate(open_files_pool%next) - open_files_pool%next => open_files_pool + nullify(of_new%next) else allocate(of_new) allocate(of_new%file_desc) of_new%file_desc = file of_new%file_name = file_name - open_files_pool => of_new - end if - open_files_tail => of_new - if (.not. associated(open_files_head)) then - open_files_head => of_new + nullify(of_new%next) end if + + ! Add the registered file to the tail of the open files list + if(associated(open_files_tail)) then + open_files_tail%next => of_new + open_files_tail => of_new + else + open_files_head => of_new + open_files_tail => of_new + endif end subroutine cam_register_open_file subroutine cam_register_close_file(file, log_shutdown_in)