Fortran vs C++,Fortran 现在在数值分析中还有什么优势吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13078736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fortran vs C++, does Fortran still hold any advantage in numerical analysis these days?
提问by user0002128
With the rapid development of C++ compilers,especially the intel ones, and the abilities of directly applying SIMD functions in your C/C++ codes, does Fortran still hold any real advantage in the world of numerical computations?
随着 C++ 编译器,尤其是 intel 编译器的快速发展,以及在 C/C++ 代码中直接应用 SIMD 函数的能力,Fortran 在数值计算领域是否仍然具有真正的优势?
I am from an applied maths background, my job involves a lot of numerical analysis, computations, optimisations and such, with a strictly defined performance-requirement.
我来自应用数学背景,我的工作涉及大量数值分析、计算、优化等,并具有严格定义的性能要求。
I hardly know anything about Fortran, I have some experience in C/CUDA/matlab(if you consider the latter as a computer language to begin with), and my daily task involves analysis of very large data (e.g. 10GB-large matrix), and it seems the program at least spend 2/3 of its time on memory-accessing (thats why I send some of its job to GPU), do you people think it may worth the effects for me to trying the fortran routine on at least some performance-critical part of my codes to improve the performance of my program?
我对 Fortran 几乎一无所知,我在 C/CUDA/matlab 方面有一些经验(如果您将后者视为一种计算机语言),我的日常任务涉及分析非常大的数据(例如 10GB 大矩阵),似乎该程序至少将 2/3 的时间花在内存访问上(这就是为什么我将它的一些工作发送给 GPU),你们认为至少对我来说尝试 fortran 例程的效果值得吗?我的代码的某些性能关键部分来提高我的程序的性能?
Because the complexity and things need to be done involved there, I will only go that routine if only there is significant performance benefit there, thanks in advance.
因为那里涉及的复杂性和事情需要完成,所以只有在那里有显着的性能优势时,我才会执行该例程,提前致谢。
回答by Dietrich Epp
Fortran has strict aliasing semantics compared to C++ and has been aggressively tuned for numerical performance for decades. Algorithms that uses the CPU to work with arrays of data often have the potential to benefit from a Fortran implementation.
与 C++ 相比,Fortran 具有严格的别名语义,并且几十年来一直在积极调整数值性能。使用 CPU 处理数据数组的算法通常有可能从 Fortran 实现中受益。
The programming languages shootout should not be taken too seriously, but of the 15 benchmarks, Fortran ranks #1 for speed on fourof them (for Intel Q6600 one core), more than any other single language. You can see that the benchmarks where Fortran shines are the heavily numerical ones:
不应太认真对待编程语言的对决,但在 15 个基准测试中,Fortran 在其中四个(英特尔 Q6600 一个内核)的速度上排名第一,超过任何其他单一语言。您可以看到 Fortran 闪耀的基准是大量数字的基准:
- spectral norm27% faster
- fasta67% faster
- mandelbrot56% faster
- pidigits18% faster
- 频谱范数快 27%
- fasta快 67%
- mandelbrot快 56%
- pidigits快 18%
Counterexample:
反例:
- k-nucleotide500% slower (this benchmark focuses heavily on more sophisticated data structures and string processing, which is not Fortran's strength)
- k-核苷酸慢 500%(这个基准主要关注更复杂的数据结构和字符串处理,这不是 Fortran 的强项)
You can also see a summary page "how many times slower" that shows that out of all implementations, the Fortran code is on average closest to the fastest implementation for each benchmark -- although the quantile bars are much larger than for C++, indicating Fortran is unsuited for some tasks that C++ is good at, but you should know that already.
您还可以看到一个“慢多少倍”的摘要页面,它表明在所有实现中,Fortran 代码平均最接近每个基准测试的最快实现——尽管分位数条比 C++ 大得多,表明 Fortran不适合 C++ 擅长的某些任务,但您应该已经知道了。
So the questions you will need to ask yourself are:
所以你需要问自己的问题是:
Is the speed of this function so critical that reimplementing it in Fortran is worth my time?
Is performance so important that my investment in learning Fortran will pay off?
Is it possible to use a library like ATLAS instead of writing the code myself?
这个函数的速度是否如此重要以至于在 Fortran 中重新实现它是否值得我花时间?
性能是否如此重要以至于我在学习 Fortran 上的投资会得到回报?
是否可以使用像 ATLAS 这样的库而不是自己编写代码?
Answering these questions would require detailed knowledge of your code base and business model, so I can't answer those. But yes, Fortran implementations are often faster than C++ implementations.
回答这些问题需要详细了解您的代码库和业务模型,因此我无法回答这些问题。但是是的,Fortran 实现通常比 C++ 实现快。
Another factor in your decision is the amount of sample code and the quantity of reference implementations available. Fortran's strong history means that there is a wealth of numerical code available for download and even with a trip to the library. As always you will need to sift through it to find the good stuff.
您决定的另一个因素是示例代码的数量和可用的参考实现的数量。Fortran 悠久的历史意味着有大量的数字代码可供下载,甚至可以前往图书馆。和往常一样,您需要筛选它才能找到好东西。
回答by Jonathan Dursi
The complete and correct answer to your question is, "yes, Fortran does hold some advantages".
您的问题的完整正确答案是,“是的,Fortran 确实具有一些优势”。
C++ also holds some, different, advantages. So do Python, R, etc etc. They're different languages. It's easier and faster to do some things in one language, and some in others. All are widely used in their communities, and for very good reasons.
C++ 还拥有一些不同的优点。Python、R 等也是如此。它们是不同的语言。用一种语言做一些事情,用另一种语言做一些事情更容易、更快。所有这些都在他们的社区中被广泛使用,并且有很好的理由。
Anything else, in the absence of more specific questions, is just noise and language-war-bait, which is why I've voted to close the question and hope others will too.
在没有更具体问题的情况下,其他任何事情都只是噪音和语言War诱饵,这就是为什么我投票结束这个问题并希望其他人也能这样做。
回答by saolof
Also worth mentioning is that Fortran is a lot easier to master than C++. In fact, Fortran has a shorter language spec than plain C and it's syntax is arguably simpler. You can pick it up very quickly.
另外值得一提的是,Fortran 比 C++ 更容易掌握。事实上,Fortran 的语言规范比普通的 C 语言规范更短,而且它的语法可以说更简单。你可以很快地把它捡起来。
Meaning that if you are only interested in learning C++ or Fortran to solve a single specific problem you have at the moment (say, to speed up the bottlenecks in something you wrote in a prototyping language), Fortran might give you a better return on investment.
这意味着如果您只对学习 C++ 或 Fortran 感兴趣,以解决您目前遇到的单个特定问题(例如,加速解决您用原型语言编写的某些内容的瓶颈),Fortran 可能会给您带来更好的投资回报.
回答by haraldkl
Fortran is just naturally suited for numerical programming. You tend to have a large amount of numbers in such programs, typically arranged arrays. Arrays are first class citizens in Fortran and it is often pretty straight forward to translate numerical kernels from Matlab into Fortran. Regarding potential performance advantages see the other answers, that cover this quite nicely. The baseline is probably you can create highly efficient numerical applications with most compiled languages today, but you might jump through some loops to get there. Fortran was carefully designed to allow the compiler to recognize most spots for optimizations, due to the language features. Of course you can also write arbitrary slow code with any compiled language, including Fortran. In any case you should pick the tools as suited. Fortran suits numerical applications, C suits system related development. On a final remark, learning Fortran basics is not hard, and it is always worthwhile to have a look into other languages. This opens a different view on problems you want to solve.
Fortran 很自然地适用于数值编程。在此类程序中,您往往会有大量数字,通常是排列的数组。数组是 Fortran 中的一等公民,将数值内核从 Matlab 转换为 Fortran 通常非常简单。关于潜在的性能优势,请参阅其他答案,它们很好地涵盖了这一点。基准可能是您可以使用当今大多数编译语言创建高效的数值应用程序,但您可能会跳过一些循环以达到目标。由于语言特性,Fortran 被精心设计以允许编译器识别大多数优化点。当然,您也可以使用任何编译语言(包括 Fortran)编写任意慢速代码。在任何情况下,您都应该选择合适的工具。Fortran 适合数值应用,C 适合系统相关的开发。最后说一句,学习 Fortran 基础并不难,学习其他语言总是值得的。这为您要解决的问题打开了一个不同的视角。
回答by Pari Rajaram
Fortran code is better for matrix and vector type operation in general. But you also can produce similar performance with c/c++ code by passing hints/suggestions to the compiler to produce similar quality vector instructions. One option that gave me good boost was not to assume memory aliasing among input variables that are array objects. This way, the compiler can aggressively do inner loop unrolling and pipelining for ILP where it can overlap loads and store operation across loop iteration with right prefetches.
Fortran 代码通常更适合矩阵和向量类型的操作。但是您也可以通过将提示/建议传递给编译器以生成类似质量的向量指令,从而使用 c/c++ 代码生成类似的性能。给我很大提升的一种选择是不要假设数组对象的输入变量之间存在内存别名。通过这种方式,编译器可以积极地为 ILP 执行内部循环展开和流水线操作,其中它可以通过正确的预取在循环迭代中重叠加载和存储操作。