<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>recipes | Computo ergo sum</title>
    <link>https://hiliev.eu/tags/recipes/</link>
      <atom:link href="https://hiliev.eu/tags/recipes/index.xml" rel="self" type="application/rss+xml" />
    <description>recipes</description>
    <generator>Source Themes Academic (https://sourcethemes.com/academic/)</generator><language>en-us</language><copyright>(cc) 2008&amp;ndash;2020 Hristo Iliev</copyright><lastBuildDate>Thu, 22 May 2014 12:34:30 +0200</lastBuildDate>
    <image>
      <url>https://hiliev.eu/images/icon_hu0b7a4cb9992c9ac0e91bd28ffd38dd00_9727_512x512_fill_lanczos_center_2.png</url>
      <title>recipes</title>
      <link>https://hiliev.eu/tags/recipes/</link>
    </image>
    
    <item>
      <title>Recipe: Obtaining Peak VM Size in Pure Fortran</title>
      <link>https://hiliev.eu/post/recipe-obtaining-peak-vm-memory-size-in-pure-fortran/</link>
      <pubDate>Thu, 22 May 2014 12:34:30 +0200</pubDate>
      <guid>https://hiliev.eu/post/recipe-obtaining-peak-vm-memory-size-in-pure-fortran/</guid>
      <description>&lt;p&gt;Often in High Performance Computing one needs to know about the various memory metrics of a given program with the peak memory usage probably being the most important one.
While the &lt;code&gt;getrusage(2)&lt;/code&gt; syscall provides some of that information, it&amp;rsquo;s use in Fortran programs is far from optimal and there are lots of metrics that are not exposed by it.&lt;/p&gt;
&lt;p&gt;On Linux one could simply parse the &lt;code&gt;/proc/PID/status&lt;/code&gt; file.
Being a simple text file it could easily be processed entirely with the built-in Fortran machinery as shown in the following recipe:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-fortran&#34;&gt;program test
  integer :: vmpeak

  call get_vmpeak(vmpeak)
  print *, &#39;Peak VM size: &#39;, vmpeak, &#39; kB&#39;
end program test

!---------------------------------------------------------------!
! Returns current process&#39; peak virtual memory size             !
! Requires Linux procfs mounted at /proc                        !
!---------------------------------------------------------------!
! Output: peak - peak VM size in kB                             !
!---------------------------------------------------------------!
subroutine get_vmpeak(peak)
  implicit none
  integer, intent(out) :: peak
  character(len=80) :: stat_key, stat_value
  !
  peak = 0
  open(unit=1000, name=&#39;/proc/self/status&#39;, status=&#39;old&#39;, err=99)
  do while (.true.)
    read(unit=1000, fmt=*, err=88) stat_key, stat_value
    if (stat_key == &#39;VmPeak:&#39;) then
      read(unit=stat_value, fmt=&#39;(I)&#39;) peak
      exit
    end if
  end do
88 close(unit=1000)
  if (peak == 0) goto 99
  return
  !
99 print *, &#39;ERROR: procfs not mounted or not compatible&#39;
  peak = -1
end subroutine get_vmpeak
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The code accesses the status file of the calling process &lt;code&gt;/proc/self/status&lt;/code&gt;.
The unit number is hard-coded which could present problems in some cases.
Modern Fortran 2008 compilers support the &lt;code&gt;NEWUNIT&lt;/code&gt; specifier and the following code could be used instead:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&#34;language-fortran&#34;&gt;integer :: unitno 

open(newunit=unitno, name=&#39;/proc/self/status&#39;, status=&#39;old&#39;, err=99)
! ...
close(unit=unitno)
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With older compilers the same functionality could be simulated using the 
&lt;a href=&#34;http://fortranwiki.org/fortran/show/newunit&#34; target=&#34;_blank&#34; rel=&#34;noopener&#34;&gt;following code&lt;/a&gt;.&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>
