subroutine WriteMemSize(iunit, MemSize)
! Write out a human-readable amount of memory. MemSize is in bytes.
integer, intent(in) :: iunit
integer(int64), intent(in) :: MemSize
character(len=*), parameter :: fmt1 = '(f6.1, a2)'
character(len=*), parameter :: fmt2 = '(i7, a1)'
if (MemUnitsBytes) then
if (MemSize < 1024_int64**2) then
! output in KB.
write (iunit, fmt1) real(MemSize, dp) / 1024._dp, 'KB'
else if (MemSize < 1024_int64**3) then
! output in MB.
write (iunit, fmt1) real(MemSize, dp) / 1024._dp**2, 'MB'
else if (MemSize < 1024_int64**4) then
! output in GB.
write (iunit, fmt1) real(MemSize, dp) / 1024._dp**3, 'GB'
else if (MemSize < 1024_int64**5) then
! output in GB.
write (iunit, fmt1) real(MemSize, dp) / 1024._dp**4, 'TB'
else
write (iunit, '(A)') '> 1 PB'
end if
else
write (iunit, fmt2) MemSize / 8, 'W'
end if
end subroutine WriteMemSize