gpt4 book ai didi

fortran - gfortran 中的 random_seed 导致内存泄漏?

转载 作者:行者123 更新时间:2023-12-05 03:18:09 29 4
gpt4 key购买 nike

Valgrind 报告使用 random_seed 例程时仍可到达的 block 。这是预期的吗?它是 gfortran 中的错误,还是我遗漏了什么?

例如,下面的程序,当用gfortran编译时:

rng.f90

program rng
implicit none

integer :: n

call random_seed(size=n)
end program rng

valgrind 生成以下报告:

==233092== 40 bytes in 1 blocks are still reachable in loss record 1 of 1
==233092== at 0x484A464: calloc (vg_replace_malloc.c:1328)
==233092== by 0x4893EF6: _gfortrani_xcalloc (memory.c:78)
==233092== by 0x4AF0DCE: get_rand_state (random.c:248)
==233092== by 0x4AF0DCE: get_rand_state (random.c:238)
==233092== by 0x4AF0DCE: _gfortran_random_seed_i4 (random.c:889)
==233092== by 0x401168: MAIN__ (in /home/baptiste/dev/misc/f90/rng/rng)
==233092== by 0x40119F: main (in /home/baptiste/dev/misc/f90/rng/rng)
==233092==
==233092== LEAK SUMMARY:
==233092== definitely lost: 0 bytes in 0 blocks
==233092== indirectly lost: 0 bytes in 0 blocks
==233092== possibly lost: 0 bytes in 0 blocks
==233092== still reachable: 40 bytes in 1 blocks
==233092== suppressed: 0 bytes in 0 blocks
==233092==

gfortran 版本:GNU Fortran (GCC) 12.2.1 20220819 (Red Hat 12.2.1-2)

我使用以下代码编译了上面的程序:gfortran -o rng rng.f90

最佳答案

这是某处的(小)错误,可能与 valgrind 如何处理线程析构函数等问题有关。在 GFortran 中,每个线程都有自己的 PRNG 状态,它会在第一次需要时延迟分配,然后由线程析构函数释放。

参见 https://gcc.gnu.org/git?p=gcc.git;a=blob;f=libgfortran/intrinsics/random.c;h=b5732e6c25a14359745c5f698c2dd4cab6f3c81f;hb=HEAD#l238对于各种高级例程用于访问 PRNG 状态的 get_rand_state() 函数,以及 https://gcc.gnu.org/git?p=gcc.git;a=blob;f=libgfortran/intrinsics/random.c;h=b5732e6c25a14359745c5f698c2dd4cab6f3c81f;hb=HEAD#l1034对于构造函数和析构函数。

关于fortran - gfortran 中的 random_seed 导致内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73812813/

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