Fortran分配/释放 [英] Fortran allocate/deallocate

查看:1321
本文介绍了Fortran分配/释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下问题:
Fortran90 +中的分配/释放语句的实际开销是多少?
也就是说,几个中等大小的数组被分配在一个循环中,比如

  do i = 1,1000 
allocate(tmp(20))
tmp(1:20)= 1d0
call foo(tmp)
deallocate(tmp)
end do

在这种情况下,根据最大大小值得分配一个工作数组吗?

malloc 空闲。所以如果 foo 是一个非常快速的函数,那么值得静态分配这个数组。通过使用 valgrind 的callgrind功能进行分析很容易看出这种开销(它可能是值得减少你的问题的大小,因为异形执行可能至少慢10倍)。



在2008年的Fortran中,这种类型的问题有更好的解决方案。您可以在运行时确定大小的结构中声明变量。这应该使编译器在堆栈上分配变量要容易得多。不过,我没有亲自使用过,我不确定哪些编译器支持它。


I have the following question: What is the real overhead of allocate/deallocate statements in Fortran90+? I.e., several medium-sized arrays are allocated inside a loop, like

do i = 1, 1000
    allocate(tmp(20))
    tmp(1:20) = 1d0
    call foo(tmp)
    deallocate(tmp)
end do 

Is it worth allocating a single work array based on the maximal size in this case?

解决方案

I have found that dynamic array allocation within tight loops can really slow down the execution of my code, with valgrind showing that a large percentage of cycles is taken up by malloc and free. So if foo is a very quick function, then it would be worth statically allocating this array. It is easy to see this overhead by profiling using valgrind's callgrind functionality (it may be worth reducing the size of your problem as the profiled execution can be at least 10 times slower).

In fortran 2008 there is a nicer solution to this type of problem. You can declare your variables inside a block construct with a size determined at run time. This should make it much easier for the compiler to allocate the variable on the stack. However I haven't used this personally and I'm not sure which compilers support it.

这篇关于Fortran分配/释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆