FMS  2024.03
Flexible Modeling System
memutils.F90
1 !***********************************************************************
2 !! GNU Lesser General Public License
3 !!
4 !! This file is part of the GFDL Flexible Modeling System (FMS).
5 !!
6 !! FMS is free software: you can redistribute it and/or modify it under
7 !! the terms of the GNU Lesser General Public License as published by
8 !! the Free Software Foundation, either version 3 of the License, or (at
9 !! your option) any later version.
10 !!
11 !! FMS is distributed in the hope that it will be useful, but WITHOUT
12 !! ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 !! FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 !! for more details.
15 !!
16 !! You should have received a copy of the GNU Lesser General Public
17 !! License along with FMS. If not, see <http://www.gnu.org/licenses/>.
18 !***********************************************************************
19 !> @defgroup memutils_mod memutils_mod
20 !> @ingroup memutils
21 !! @brief Module to expose the memory printing API
22 !! @author V. Balaji
23 !!
24 !! Module to expose the memory printing API
25 !!
26 !! Model components at times desire to print the memory statics to stderr,
27 !! or another file (e.g. the log file). This can be useful in debugging.
28 !! This module exposes the print_memuse_stat and memutils_init calls for
29 !! use in external user code.
30 
31 !> @addtogroup memutils_mod
32 !> @{
33 module memutils_mod
34 !Author: Balaji (V.Balaji@noaa.gov)
35  use mpp_mod, only: mpp_pe, mpp_root_pe, mpp_npes, mpp_min, mpp_max, mpp_sum, stderr
36  use mpp_memutils_mod, only: mpp_print_memuse_stats
37 
38  implicit none
39 
40  logical :: memutils_initialized=.false. !< Indicate if module has been initialized.
41  logical, private :: print_memory_usage=.false. !< Default behavior of print_memuse_stats()
42 
43  public :: memutils_init
44  public :: print_memuse_stats
45 
46 contains
47 
48  !> @brief Initialize the memutils module
49  !!
50  !! memutils_init initializes the print_memory_usage and memutils_initialized
51  !! module variables when called. This configures if print_memuse_stats() should
52  !! print the memory statistics when called. memutils_init, at present, can
53  !! be called multiple times, and potentially change the behavior of print_memuse_stats().
54  subroutine memutils_init(print_flag)
55  logical, optional :: print_flag !< Indicate if memory statistics should be printed by default
56 
57  if ( PRESENT(print_flag) ) print_memory_usage = print_flag
58  memutils_initialized = .true.
59  return
60  end subroutine memutils_init
61 
62 !> @brief Print memory usage stats to stdout, or a particular file
63 !!
64 !! API to allow external user code to print memory statistics to stderr, or an
65 !! optional file (Fortran file unit). The module variable `print_memory_usage`
66 !! will control the default behavior (set when memutils_init is called). The
67 !! default behavior can be modified passing in the optional `always` (logical)
68 !! parameter. If `always.eqv..TRUE.`, print_memuse_stats() will print the memory
69 !! statistics.
70  subroutine print_memuse_stats( text, unit, always )
71  character(len=*), intent(in) :: text !< Text to be printed before the memory statistics
72  integer, intent(in), optional :: unit !< Fortran file unit to where memory statistics should be recorded
73  logical, intent(in), optional :: always !< If `.TRUE.`, force memory statistics to be printed
74 
75  if( PRESENT(always) )then
76  if( .NOT.always )return
77  else
78  if( .NOT.print_memory_usage )return
79  end if
80  call mpp_print_memuse_stats(text, unit)
81  return
82  end subroutine print_memuse_stats
83 end module memutils_mod
84 !> @}
85 ! close documentation grouping
subroutine, public memutils_init(print_flag)
Initialize the memutils module.
Definition: memutils.F90:55
logical, private print_memory_usage
Default behavior of print_memuse_stats()
Definition: memutils.F90:41
subroutine, public print_memuse_stats(text, unit, always)
Print memory usage stats to stdout, or a particular file.
Definition: memutils.F90:71
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:51
integer function mpp_npes()
Returns processor count for current pelist.
Definition: mpp_util.inc:421
integer function mpp_pe()
Returns processor ID.
Definition: mpp_util.inc:407
Reduction operations. Find the max of scalar a from the PEs in pelist result is also automatically br...
Definition: mpp.F90:538
Reduction operations. Find the min of scalar a from the PEs in pelist result is also automatically br...
Definition: mpp.F90:560
Reduction operation.
Definition: mpp.F90:597