FORTRAN 77 程序比 Fortran 90 程序更快吗?
今天,我正在阅读一些用 FORTRAN 77 编写的非常流行的数值库中的代码,例如 QUADPACK(最后更新于 1987 年),我想知道是否有任何理由不在 Fortran 90 中重写这些库,除了会带来大量工作之外,考虑到 Fortran 90 给该语言带来的巨大改进,包括自由格式源代码、更好的控制结构(这样 GO TO 可能会被遗忘)、矢量化、接口等等。
是因为 FORTRAN 77 编译器产生了更优化的代码,也许它更适合并行执行?请注意,我什至没有谈论 Fortran 2003,它只有 8 岁了:我谈论的是 Fortran 90,所以我认为它已经足够广泛并且编译器已经准备好了。反正我没接触过这个行业。
编辑:janneb 是对的:LAPACK 实际上是用 Fortran 90 编写的。
Today I was reading code from some very popular numerical libraries written in FORTRAN 77 such as QUADPACK (last updated in 1987), and I was wondering if there is any reason not to rewrite those libraries in Fortran 90 apart from the big amount of work it would pose, given the great improvements Fortran 90 brought to the language, including free-form source, better control structures so GO TO could be forgotten, vectorization, interfaces and so on.
Is it because FORTRAN 77 compilers produce more optimized code, maybe it is better for parallel execution? Notice that I'm not even talking about Fortran 2003 for example, which is only 8 years old: I'm talking about Fortran 90, so I assume it has enough widespread and the compilers are ready. I don't have contact with the industry, anyway.
Edit: janneb is right: LAPACK is actually written in Fortran 90.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,就像提到的“pst”一样,“它会带来大量的工作”是一个相当重要的原因。
一些进一步的小要点:
LAPACK 如今是 Fortran 90,因为最新版本不再使用 F77 编译器进行编译。话虽这么说,它远非重写,只是做了一些更改。
您提到的大多数 F90 功能使编写健壮的程序变得更加容易和更快,但不一定使生成的程序更快。
不久前还没有免费的 F90 编译器(很多人使用 g77!),因此对于像 LAPACK 这样广泛使用的库,不使用 F90 功能可能是一个有意识的决定。
F77 编译器通常不会比 F90 编译器生成更快的代码。如果不是出于任何其他原因,那就是因为它可能已过时并且无法针对最新的 CPU 进行优化。现代 Fortran 编译器可能会从 F77 创建比从等效 F90 程序更快的代码,后者广泛使用指针等内容,但这高度依赖于所讨论的程序(例如,使用指针和更高级的数据结构可能允许使用更好的算法,允许 F90 程序更快地生成结果,即使它可能以较低的 CPU 算术单元平均利用率执行。
矢量化(如果您指的是 F90+ 数组语法)主要是程序员便利性问题,而不是允许更快的代码。一个有能力的编译器也会对等效的 DO 循环进行矢量化。
Well, like "pst" mentioned, the "big amount of work it would pose" is a pretty major reason.
Some further minor points:
LAPACK IS Fortran 90, these days, in the sense that the latest version no longer compiles with a F77 compiler. That being said, it's far from a rewrite, only a few things that were changed.
Most of the F90 features you mention make it easier and faster to write robust programs, it doesn't necessary make the resulting programs any faster.
It wasn't that long ago that free F90 compilers were not available (plenty of people used g77!), so for a widely used library like LAPACK not using F90 features was likely a conscious decision.
A F77 compiler does not, generally, produce faster code than a F90 compiler. If not for any other reason, then because it's likely obsolete and cannot optimize for the latest CPU's. A modern Fortran compiler might create faster code from F77 than from an equivalent F90 program which makes extensive use of things like pointers etc., but that is highly dependent on the program in question (e.g. using pointers and fancier data structures may allow usage of better algorithms, allowing the F90 program to produce results faster even though it might execute at a lower average utilization of the CPU arithmetic units).
Vectorization, if by this you mean the F90+ array syntax, is mostly a programmer convenience issue rather than allowing faster code. A competent compiler will vectorize the equivalent DO loop just as well.
没有理由投入大量工作来(也许)改进效果良好的东西。需要注意的是,尽管 Fortran 90 引入了许多新功能,但它并没有改变语言。请记住,Fortran 90 标准向后兼容 FORTRAN 77。现代编译器也能够优化 FORTRAN 77 代码。 Fortan 90 中引入的一些功能(例如指针)会降低效率,如果关心执行时间(例如在 HPC 中),则应避免使用这些功能。
所以这不会真正产生影响。对于现代编译器来说,编写良好的 FORTAN 77 也是可优化的——它就像没有糖衣的蛋糕。在这里,对于 Fortran 90 及更高版本来说,锦上添花——这对程序员来说是一种方便,但并不一定会提高程序效率。
There is no reason to put in a ton of work to (maybe) improve something that works well. It should be noted that even though Fortran 90 introduced many new features, it did not change the language. Remember that Fortran 90 Standard is backwards compatible with FORTRAN 77. A modern compiler will be able to optimize a FORTRAN 77 code just as well. There are features introduced in Fortan 90 (e.g. pointers) that would hinder the efficiency and that should be avoided if one cares about execution time, e.g. in HPC.
So it would not really make a difference. For a modern compiler, well written FORTAN 77 is just as optimizable - it is like a cake without icing. Here, in case of Fortran 90 and later, icing looks better than it tastes - it is a convenience for the programmer, but does not necessarily improve the program efficiency.
Fortran 77 程序可能更快的一个主要原因是:
分配的数组 (Fortran 90) 比编译时声明的数组慢得多。
两者没有放在内存中的同一位置。这就是 Fortran 中的堆栈与堆内存管理。
请参见此处话虽
这么说,Fortran 90+ 具有快速'全部在块数组操作中。我是 gcc-fortran (gfortran) 的忠实粉丝,在没有任何编译选项的情况下,对于大小为 N 的数组 a 来说,
速度比这个工作台快 4 倍。
这个工作台适合我,在我的机器上,在 gfortran 上,没有优化选项,也没有任何声称这是专业标杆。
分配“问题”(不是问题而是功能)不是体系结构、编译器或任何相关的东西,而是与 Fortran 标准相关。
There is one major reason why Fortran 77 programs might be faster :
Allocated arrays (Fortran 90) are much slower than declared-at-compile-time arrays.
Both are not put at the same place in memory. That's the stack vs heap memory management in Fortran.
See here for instance
This being said, Fortran 90+ have fast 'all in block' array operations. I'm a big fan of gcc-fortran (gfortran) and without any compilation option, for an array a of size N
is 4 times faster than
This bench is for me, on my machine, on gfortran without optimization option, and without any claming that this is professional benchmarking.
The allocation "problem" (which is not a problem but a feature) is not architecture, compiler or anything related but to the Fortran Standard.
F77 程序是否会比使用更新语法重写的等效 F90 程序更慢或更快是可以讨论的,但我们现在先忽略这一点。
然而应该考虑的是,没有人真正关心仅速度。人们只关心相关以及商业盈利情况下的执行速度(我确信有一个更好的术语来形容这一点,但现在什么也没有想到......成本效益也许)。
由于这两个(相当流行的库)仍然在 F77 中,很明显,人们普遍认为重写它们的成本超过了所获得的好处,执行速度方面的好处以及成本效益方面的好处整个过程。
Whether F77 program will be slower or faster then the equivalent F90 program, rewritten with newer syntax is discussable, but let's disregard that for now.
What should however be taken into account, is that nobody really cares about speed only. People only care about speed of execution in the cases where it is relevant, and where it is bussiness profitable (I'm sure there is a better term for this, but nothing comes to mind right now ... cost effective maybe).
Since those two (rather popular libraries) are still in F77, it is obvious that it is the general opinion that the costs of rewriting them outweights the benefits gained, benefits in term of speed of execution, and benefits in terms of cost effectiveness of that whole process.