跟踪Fortran 90中的内存使用情况 [英] Track memory usage in Fortran 90
问题描述
我试图在Fortran 90程序中跟踪子程序的内存使用情况和CPU时间。要跟踪cpu时间,我使用以下内容:
$ b
调用cpu_time(tic)
调用子程序(args)
call cpu_time(toc)
time = toc-tic
有没有办法做类似的记录内存使用?做这个的最好方式是什么?感谢您的帮助。
liskawc有一个非常好的解决方案,我一直在寻找像这样的同时。
他要求反馈,并且有几个方面可以改进。
- 有几个系统调用可以通过直接从Fortran程序中读取系统文件来消除
- 解决方案取决于用户目录中的临时文件 li>
- 我的fortran编译器不喜欢以波形开头打开文件
我修改了他的原始程序,以克服这些问题:
子程序system_mem_usage(valueRSS)
隐含无
使用ifport!如果在intel编译器
整数,intent(out):: valueRSS
字符(len = 200):: filename =''
字符(len = 80):: line
character(len = 8):: pid_char =''
integer :: pid
logical :: ifxst
valueRSS = -1!返回负数如果没有找到
!---获取进程ID
pid = getpid()
write(pid_char,'(I8)')pid
filename ='/ proc /'// trim(adjustl(pid_char))//'/ status'
!---读取系统文件
inquire( (*,*)'系统文件不存在'
返回
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继续
关闭(100)
返回
结束子程序system_mem_usage
如果您可以进一步改进此程序,请随时更新!
I am trying to track the memory usage and cpu time of a subroutine in a Fortran 90 program. To track the track the cpu time, I use the following:
call cpu_time(tic)
call subroutine(args)
call cpu_time(toc)
time = toc-tic
Is there a way to do something similar to record memory usage? What is the best way to do this? Thanks in advance for the help.
liskawc has a very nice solution and I've been looking for something like this for a while.
He asked for feedback, and there were a couple of areas that could be improved.
- there are several system calls that can be eliminated by just reading the system file directly from your Fortran program
- the solution depends on a temporary file in the users directory
- my fortran compiler didn't like opening a file starting with the tilde
I've modified his original program to overcome these issues:
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
Please feel free to update if you can improve this program any further!
这篇关于跟踪Fortran 90中的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!