Memory 在Fortran 90中跟踪内存使用情况
我试图在Fortran 90程序中跟踪子例程的内存使用和cpu时间。为了跟踪cpu时间,我使用以下方法:Memory 在Fortran 90中跟踪内存使用情况,memory,fortran,Memory,Fortran,我试图在Fortran 90程序中跟踪子例程的内存使用和cpu时间。为了跟踪cpu时间,我使用以下方法: 呼叫cpu\u时间(tic) 调用子例程(args) 呼叫cpu_时间(toc) 时间=toc tic 有没有类似于记录内存使用情况的方法?最好的方法是什么?提前感谢您的帮助。根据内存使用情况,我假设您在linux机器上的程序中需要RSS。如果不想使用探查器,则此例程可能会有所帮助: 我使用这个程序(老实说,它实际上很难看),但到目前为止仍然有效。子程序的简要说明: 当您调用子例程时,它会发
呼叫cpu\u时间(tic)
调用子例程(args)
呼叫cpu_时间(toc)
时间=toc tic
有没有类似于记录内存使用情况的方法?最好的方法是什么?提前感谢您的帮助。根据内存使用情况,我假设您在linux机器上的程序中需要RSS。如果不想使用探查器,则此例程可能会有所帮助: 我使用这个程序(老实说,它实际上很难看),但到目前为止仍然有效。子程序的简要说明: 当您调用子例程时,它会发出一系列系统调用,在/proc/文件系统中搜索有关您的程序的信息。不管你能得到什么,如果你修改了这个sub,我就用这个来获取RSS值。该例程以字符形式返回valueRSS,如果您希望获得程序中ram使用情况的配置文件,则可以将其写入特定文件。您也可以将字符串转换为数字,但如果您需要,我将留给您
subroutine system_mem_usage(valueRSS)
use ifport !if on intel compiler
character(len=30) :: count_char,pid_char, dummy
character(len=200) :: filename
character(len=200) :: command
integer :: count,pid,res
character(len=50), intent(out) :: valueRSS
call system_clock(count)
pid=getpid()
write(count_char,'(I10)') count
write(pid_char,'(I10)') pid
filename='~/tmp/mem_use.'//trim(count_char)
command='cat /proc/'//trim(adjustl(pid_char))//'/status >'//trim(adjustl(filename))
res=system(command)
command='cat '//trim(adjustl(filename))//' | grep RSS > ~/tmp/rss_use.'//trim(count_char)
res=system(command)
open(unit=100, file='~/tmp/rss_use.'//trim(count_char))
read(100,*) dummy, valueRSS
close(100)
return
end subroutine
如果您对RSS以外的其他价值感兴趣,那么只需在“grep RSS”编辑该部分即可。此子例程还假定您在家中有一个tmp目录
哦,是的,您可以随意修改脚本,如果有人对如何更优雅地完成此操作有任何想法,我将不胜感激。您可以使用valgrind和massif。这将转储一个非常详细的输出文件。然后,您可以使用gprof2dot工具动态地可视化它,该工具为您提供了一个非常直观的代码流程图,包括调用图和调用图中每个内联子例程的内存使用情况。如果你想变得更花哨,那么你可以使这个过程自动化,例如你可以编写你的doxygen脚本,这样每当你提交一个更改时,它就会在你的代码文档中更新这些内容。我希望这有帮助 liskawc有一个非常好的解决方案,我一直在寻找类似的解决方案 他要求得到反馈,有几个方面可以改进
- 有几个系统调用可以通过直接从Fortran程序读取系统文件来消除
- 解决方案依赖于用户目录中的临时文件
- 我的fortran编译器不喜欢打开以tilde开头的文件
subroutine system_mem_usage(valueRSS)
implicit none
use ifport !if on intel compiler
integer, intent(out) :: valueRSS
character(len=200):: filename=' '
character(len=80) :: line
character(len=8) :: pid_char=' '
integer :: pid
logical :: ifxst
valueRSS=-1 ! return negative number if not found
!--- get process ID
pid=getpid()
write(pid_char,'(I8)') pid
filename='/proc/'//trim(adjustl(pid_char))//'/status'
!--- read system file
inquire (file=filename,exist=ifxst)
if (.not.ifxst) then
write (*,*) 'system file does not exist'
return
endif
open(unit=100, file=filename, action='read')
do
read (100,'(a)',end=120) line
if (line(1:6).eq.'VmRSS:') then
read (line(7:),*) valueRSS
exit
endif
enddo
120 continue
close(100)
return
end subroutine system_mem_usage
如果您可以进一步改进此程序,请随时更新 我有一个这样做的方法,涉及到以下结构:我正在尝试找到代码现在…我使用了以下:我没有任何代码更不幸。。。但我所做的是用C创建一个静态库,它返回给定进程ID的进程内存计数器结构,然后我使用ISO_C_绑定与该库进行接口。您尝试在哪个平台上进行此操作?您可以使用valgrind(Linux上)进行评测。酷!非常感谢。我必须将“implicit none”和“use ifport”倒置才能使其工作。如果我们有许多子例程,我们是否需要在每次调用system_mem_usage时对值进行减法,以测量每个子例程的内存使用情况?