gpt4 book ai didi

c - fortran 程序的 mtrace

转载 作者:太空狗 更新时间:2023-10-29 15:05:07 27 4
gpt4 key购买 nike

我正在尝试使用 mtrace 检测 Fortran 程序中的内存泄漏。我正在使用 gfortran 编译器。有关 mtrace 的(工作)C 示例,请参见维基百科条目:http://en.wikipedia.org/wiki/Mtrace

我尝试了两种方法,即包装 mtrace() 和 muntrace() 并从 fortran 程序调用它们,以及创建一个直接调用 mtrace() 和 muntrace() 的 C 程序,除了泄漏的 fortran 代码之间。这两种方法都无法检测到内存泄漏,但这里我只介绍后者。

例子.c

#include <stdlib.h>
#include <mcheck.h>

extern void leaky_(); // this might be different on your system
// if it doesn't work, try to run:
// 1) gfortran leaky.f90 -c
// 2) nm leaky.o
// and then change this declaration and its use below

void main() {
mtrace();
leaky_();
muntrace();
}

泄漏.f90

subroutine leaky()
real, allocatable, dimension(:) :: tmp
integer :: error
allocate (tmp(10), stat=error)
if (error /= 0) then
print*, "subroutine leaky could not allocate space for array tmp"
endif
tmp = 1
!of course the actual code makes more...
print*, ' subroutine leaky run '
return
end subroutine leaky

我编译:

gfortran -g example.c leaky.f90

然后我运行:

export MALLOC_TRACE=`pwd`/raw.txt; ./a.out

然后我解析 raw.txt mtrace 输出:

mtrace a.out raw.txt

并得到:

没有内存泄漏。

有什么我做错了,或者我可以做些什么让 mtrace 找到泄漏的 Fortran 内存分配吗?我猜 gfortran 正在使用不同的 malloc 调用,mtrace 没有跟踪...事实上,正如我上面所写,如果我编写一个调用(包装的)mtrace()muntrace() 的 fortran main,我会得到相同的结果。

已编辑:我考虑了其他选项(包括此处尚未提及的一些选项),但正在调试的实际代码在 P6/AIX 上运行,因此 Valgrind 将“只是”不方便(它需要在不同的机器上运行),而Forcheck 会很不方便(它需要在不同的机器上运行)并且很昂贵(~3k$)。如果有效,mtrace 将是目前最好的解决方案。

再次编辑:我的猜测

I guess gfortran is using a different malloc call, which mtrace does not trace...

是正确的。查看可执行文件(使用 nmreadelf)没有任何 malloc() 调用,但是 _gfortran_allocate_array 那些——可能会调用 malloc)。还有其他想法吗?

再次编辑:我发布了答案,但我不能接受它(转到 http://stackoverflow.uservoice.com/pages/general/suggestions/39426 并请求该功能,它确实需要 - 不需要获得声誉)

最佳答案

我不是 mtrace 方面的专家,所以我无能为力。我建议你试试 valgrind如果您使用的是受支持的系统,则可以使用该工具查找内存泄漏。使用 valgrind 查找内存泄漏就像调用 valgrind --leak-check=full ./a.out 一样简单。

关于c - fortran 程序的 mtrace,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/260192/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com