print_memory_usage Subroutine

public subroutine print_memory_usage(show_peak, show_current, unit)

Writes current estimate of memory use and peak memory use to provided unit.

Arguments

Type IntentOptional Attributes Name
logical, intent(in), optional :: show_peak
logical, intent(in), optional :: show_current
integer, intent(in), optional :: unit

Contents

Source Code


Source Code

  subroutine print_memory_usage(show_peak, show_current, unit)
    use iso_fortran_env, only: output_unit
    use optionals, only: get_option_with_default
    implicit none
    logical, intent(in), optional :: show_peak, show_current
    integer, intent(in), optional :: unit
    logical :: peak, current
    integer :: output
    integer(int64) :: bits, peak_bits
    ! Handle optionals
    peak = get_option_with_default(show_peak, .true.)
    current = get_option_with_default(show_current, .true.)
    output = get_option_with_default(unit, output_unit)

    ! Leave early if possible
    if (.not. (peak.or.current)) return

    write(output, '(35("-"))')

    if (current) then
       bits = number_of_bits_in_use()
       if ( bits >= 0 ) then
          write(output, '(" Current memory usage  : ", A)') &
               trim(adjustl(bits_to_human_readable(bits)))
       else
          write(output, '(" Cannot estimate memory usage.")')
       end if
    end if

    if (peak) then
       peak_bits = peak_number_of_bits_in_use()
       if ( peak_bits >= 0 ) then
          write(output, '(" Peak memory usage     : ", A)') &
               trim(adjustl(bits_to_human_readable(peak_bits)))
       else
          write(output, '(" Cannot estimate peak memory usage.")')
       end if
    end if

    write(output, '(35("-"))')
  end subroutine print_memory_usage