Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add esmf_logmsg to shr_abort #62

Merged
merged 5 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 18 additions & 44 deletions src/nuopc_shr_methods.F90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module nuopc_shr_methods
use ESMF , only : operator(<), operator(/=), operator(+)
use ESMF , only : operator(-), operator(*) , operator(>=)
use ESMF , only : operator(<=), operator(>), operator(==), MOD
use ESMF , only : ESMF_LOGERR_PASSTHRU, ESMF_LogFoundError, ESMF_LOGMSG_ERROR, ESMF_MAXSTR
use ESMF , only : ESMF_LOGERR_PASSTHRU, ESMF_LogFoundError, ESMF_MAXSTR
use ESMF , only : ESMF_SUCCESS, ESMF_LogWrite, ESMF_LOGMSG_INFO, ESMF_FAILURE
use ESMF , only : ESMF_State, ESMF_StateGet
use ESMF , only : ESMF_Field, ESMF_FieldGet
Expand Down Expand Up @@ -373,9 +373,7 @@ subroutine state_diagnose(State, string, rc)
call ESMF_LogWrite(trim(msgString), ESMF_LOGMSG_INFO)
endif
else
call ESMF_LogWrite(trim(subname)//": ERROR rank not supported ", ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//": ERROR rank not supported ")
endif
enddo

Expand Down Expand Up @@ -411,10 +409,8 @@ subroutine field_getfldptr(field, fldptr1, fldptr2, rank, abort, rc)
! ----------------------------------------------

if (.not.present(rc)) then
call ESMF_LogWrite(trim(subname)//": ERROR rc not present ", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//": ERROR rc not present ", &
line=__LINE__, file=u_FILE_u)
endif

rc = ESMF_SUCCESS
Expand Down Expand Up @@ -465,27 +461,21 @@ subroutine field_getfldptr(field, fldptr1, fldptr2, rank, abort, rc)
ESMF_LOGMSG_INFO)
elseif (lrank == 1) then
if (.not.present(fldptr1)) then
call ESMF_LogWrite(trim(subname)//": ERROR missing rank=1 array ", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//": ERROR missing rank=1 array ", &
line=__LINE__, file=u_FILE_u)
endif
call ESMF_FieldGet(field, farrayPtr=fldptr1, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
elseif (lrank == 2) then
if (.not.present(fldptr2)) then
call ESMF_LogWrite(trim(subname)//": ERROR missing rank=2 array ", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//": ERROR missing rank=2 array ", &
line=__LINE__, file=u_FILE_u)
endif
call ESMF_FieldGet(field, farrayPtr=fldptr2, rc=rc)
if (chkerr(rc,__LINE__,u_FILE_u)) return
else
call ESMF_LogWrite(trim(subname)//": ERROR in rank ", &
ESMF_LOGMSG_ERROR, line=__LINE__, file=u_FILE_u)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//": ERROR in rank ", &
line=__LINE__, file=u_FILE_u)
endif

endif ! status
Expand Down Expand Up @@ -566,14 +556,10 @@ subroutine alarmInit( clock, alarm, option, &
! Error checks
if (trim(option) == optdate) then
if (.not. present(opt_ymd)) then
call ESMF_LogWrite(trim(subname)//trim(option)//' requires opt_ymd', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(trim(subname)//trim(option)//' requires opt_ymd')
end if
if (lymd < 0 .or. ltod < 0) then
call ESMF_LogWrite(subname//trim(option)//'opt_ymd, opt_tod invalid', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(subname//trim(option)//'opt_ymd, opt_tod invalid')
end if
else if (&
trim(option) == optNSteps .or. trim(option) == trim(optNSteps)//'s' .or. &
Expand All @@ -584,14 +570,10 @@ subroutine alarmInit( clock, alarm, option, &
trim(option) == optNMonths .or. trim(option) == trim(optNMonths)//'s' .or. &
trim(option) == optNYears .or. trim(option) == trim(optNYears)//'s' ) then
if (.not.present(opt_n)) then
call ESMF_LogWrite(subname//trim(option)//' requires opt_n', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(subname//trim(option)//' requires opt_n')
end if
if (opt_n <= 0) then
call ESMF_LogWrite(subname//trim(option)//' invalid opt_n', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(subname//trim(option)//' invalid opt_n')
end if
end if
call ESMF_TimeIntervalSet(AlarmInterval, yy=9999, rc=rc)
Expand Down Expand Up @@ -629,9 +611,7 @@ subroutine alarmInit( clock, alarm, option, &
AlarmInterval = AlarmInterval * opt_n
! timestepinterval*0 is 0 of kind ESMF_TimeStepInterval
if (mod(AlarmInterval, TimestepInterval) /= (TimestepInterval*0)) then
call ESMF_LogWrite(subname//'illegal Alarm setting for '//trim(alarmname), ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(subname//'illegal Alarm setting for '//trim(alarmname))
endif
update_nextalarm = .true.

Expand Down Expand Up @@ -691,9 +671,7 @@ subroutine alarmInit( clock, alarm, option, &
if (ChkErr(rc,__LINE__,u_FILE_u)) return

case default
call ESMF_LogWrite(subname//'unknown option '//trim(option), ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort(subname//'unknown option '//trim(option))

end select

Expand Down Expand Up @@ -804,14 +782,10 @@ integer function get_minimum_timestep(gcomp, rc)
enddo

if(get_minimum_timestep == huge(1)) then
call ESMF_LogWrite('minimum_timestep_error: this option is not supported ', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort('minimum_timestep_error: this option is not supported ')
endif
if(get_minimum_timestep <= 0) then
call ESMF_LogWrite('minimum_timestep_error ERROR ', ESMF_LOGMSG_ERROR)
rc = ESMF_FAILURE
return
call shr_sys_abort('minimum_timestep_error ERROR ')
endif
end function get_minimum_timestep

Expand Down
24 changes: 10 additions & 14 deletions src/shr_abort_mod.F90
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ module shr_abort_mod
use, intrinsic :: iso_fortran_env, only: output_unit, error_unit

use shr_kind_mod, only : shr_kind_in, shr_kind_cx
use shr_mpi_mod , only : shr_mpi_initialized, shr_mpi_abort
use shr_log_mod , only : s_logunit => shr_log_Unit

#ifdef CPRNAG
Expand All @@ -35,15 +34,15 @@ module shr_abort_mod
contains

!===============================================================================
subroutine shr_abort_abort(string,rc)
subroutine shr_abort_abort(string,rc, line, file)
use esmf, only : ESMF_LOGWRITE, ESMF_LOGMSG_ERROR, ESMF_FINALIZE, ESMF_END_ABORT
! Consistent stopping mechanism

!----- arguments -----
character(len=*) , intent(in), optional :: string ! error message string
integer(shr_kind_in), intent(in), optional :: rc ! error code

!----- local -----
logical :: flag
integer(shr_kind_in), intent(in), optional :: line
character(len=*), intent(in), optional :: file

! Local version of the string.
! (Gets a default value if string is not present.)
Expand All @@ -55,20 +54,17 @@ subroutine shr_abort_abort(string,rc)
else
local_string = "Unknown error submitted to shr_abort_abort."
end if
if(present(rc)) then
write(local_string, *) trim(local_string), ' rc=',rc
endif

call print_error_to_logs("ERROR", local_string)

call ESMF_LogWrite(local_string, ESMF_LOGMSG_ERROR, line=line, file=file)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jedwards4b question. What will happen if line and/or file aren't present in the call and they are sent down this way to ESMF_LogWrite? It seems like it might be compiler dependent on how it handles passing down a non-present argument.

But, I'm interested if you did any testing around that. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the standard: If you pass an omitted dummy argument as the actual argument to a procedure, the corresponding dummy argument is considered to be omitted as well.

We do this in many places in cesm, as far as I know all of our compilers behave correctly.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, good to know!


call shr_abort_backtrace()

call shr_mpi_initialized(flag)

if (flag) then
if (present(rc)) then
call shr_mpi_abort(trim(local_string),rc)
else
call shr_mpi_abort(trim(local_string))
endif
endif
call ESMF_Finalize(endflag=ESMF_END_ABORT)

! A compiler's abort method may print a backtrace or do other nice
! things, but in fact we can rarely leverage this, because MPI_Abort
Expand Down
Loading