FMS  2025.04
Flexible Modeling System
memutils.F90
1 !***********************************************************************
2 !* Apache License 2.0
3 !*
4 !* This file is part of the GFDL Flexible Modeling System (FMS).
5 !*
6 !* Licensed under the Apache License, Version 2.0 (the "License");
7 !* you may not use this file except in compliance with the License.
8 !* You may obtain a copy of the License at
9 !*
10 !* http://www.apache.org/licenses/LICENSE-2.0
11 !*
12 !* FMS is distributed in the hope that it will be useful, but WITHOUT
13 !* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied;
14 !* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
15 !* PARTICULAR PURPOSE. See the License for the specific language
16 !* governing permissions and limitations under the License.
17 !***********************************************************************
18 !> @defgroup memutils_mod memutils_mod
19 !> @ingroup memutils
20 !! @brief Module to expose the memory printing API
21 !! @author V. Balaji
22 !!
23 !! Module to expose the memory printing API
24 !!
25 !! Model components at times desire to print the memory statics to stderr,
26 !! or another file (e.g. the log file). This can be useful in debugging.
27 !! This module exposes the print_memuse_stat and memutils_init calls for
28 !! use in external user code.
29 
30 !> @addtogroup memutils_mod
31 !> @{
32 module memutils_mod
33 !Author: Balaji (V.Balaji@noaa.gov)
34  use mpp_mod, only: mpp_pe, mpp_root_pe, mpp_npes, mpp_min, mpp_max, mpp_sum, stderr
35  use mpp_memutils_mod, only: mpp_print_memuse_stats
36 
37  implicit none
38 
39  logical :: memutils_initialized=.false. !< Indicate if module has been initialized.
40  logical, private :: print_memory_usage=.false. !< Default behavior of print_memuse_stats()
41 
42  public :: memutils_init
43  public :: print_memuse_stats
44 
45 contains
46 
47  !> @brief Initialize the memutils module
48  !!
49  !! memutils_init initializes the print_memory_usage and memutils_initialized
50  !! module variables when called. This configures if print_memuse_stats() should
51  !! print the memory statistics when called. memutils_init, at present, can
52  !! be called multiple times, and potentially change the behavior of print_memuse_stats().
53  subroutine memutils_init(print_flag)
54  logical, optional :: print_flag !< Indicate if memory statistics should be printed by default
55 
56  if ( PRESENT(print_flag) ) print_memory_usage = print_flag
57  memutils_initialized = .true.
58  return
59  end subroutine memutils_init
60 
61 !> @brief Print memory usage stats to stdout, or a particular file
62 !!
63 !! API to allow external user code to print memory statistics to stderr, or an
64 !! optional file (Fortran file unit). The module variable `print_memory_usage`
65 !! will control the default behavior (set when memutils_init is called). The
66 !! default behavior can be modified passing in the optional `always` (logical)
67 !! parameter. If `always.eqv..TRUE.`, print_memuse_stats() will print the memory
68 !! statistics.
69  subroutine print_memuse_stats( text, unit, always )
70  character(len=*), intent(in) :: text !< Text to be printed before the memory statistics
71  integer, intent(in), optional :: unit !< Fortran file unit to where memory statistics should be recorded
72  logical, intent(in), optional :: always !< If `.TRUE.`, force memory statistics to be printed
73 
74  if( PRESENT(always) )then
75  if( .NOT.always )return
76  else
77  if( .NOT.print_memory_usage )return
78  end if
79  call mpp_print_memuse_stats(text, unit)
80  return
81  end subroutine print_memuse_stats
82 end module memutils_mod
83 !> @}
84 ! close documentation grouping
subroutine, public memutils_init(print_flag)
Initialize the memutils module.
Definition: memutils.F90:54
logical, private print_memory_usage
Default behavior of print_memuse_stats()
Definition: memutils.F90:40
subroutine, public print_memuse_stats(text, unit, always)
Print memory usage stats to stdout, or a particular file.
Definition: memutils.F90:70
subroutine, public mpp_print_memuse_stats(text, unit)
Print the current memory high water mark to stderr, or the unit specified.
integer function stderr()
This function returns the current standard fortran unit numbers for error messages.
Definition: mpp_util.inc:50
integer function mpp_npes()
Returns processor count for current pelist.
Definition: mpp_util.inc:420
integer function mpp_pe()
Returns processor ID.
Definition: mpp_util.inc:406
Reduction operations. Find the max of scalar a from the PEs in pelist result is also automatically br...
Definition: mpp.F90:537
Reduction operations. Find the min of scalar a from the PEs in pelist result is also automatically br...
Definition: mpp.F90:559
Reduction operation.
Definition: mpp.F90:596