为什么 Fortran 用于科学计算?

发布于 2024-12-28 22:45:04 字数 222 浏览 1 评论 0原文

我了解到 Fortran 仍然大量用于科学计算。对于已经大量投入 Fortran 的代码,这对我来说很有意义。

但是,在新项目中是否有理由使用 Fortran 而不是其他现代语言呢?与更流行的语言(C++、Java、Python、Ruby 等)相比,Fortran 中的语言设计决策是否使其更适合科学计算?例如,与我提到的其他语言相比,Fortran 是否有特定的语言功能可以允许编译器中的数值优化达到更高的程度?

I've read that Fortran is still heavily used for scientific computing. For code already heavily invested in Fortran this makes sense to me.

But is there a reason to use Fortran over other modern languages for a new project? Are there language design decisions in Fortran that makes it much more suitable for scientific computing compared to say the more popular languages (C++, Java, Python, Ruby, etc.)? For example, are there specific language features of Fortran that maybe allow numeric optimization in compilers to a much higher degree compared to other languages I mentioned?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

可爱暴击 2025-01-04 22:45:04

无论好坏,Fortran 都是唯一专门为科学数值计算而设计的主要语言。它的数组处理非常好,可以对整个数组和切片进行简洁的数组操作,与 matlab 或 numpy 相当,但速度超快。该语言经过精心设计,使其很难意外编写缓慢的代码 - 指针受到限制,如果可能存在别名,就像标准示例一样,立即显而易见 - 因此优化器可以根据你的情况进行优化。代码。当前的版本具有 coarray fortran 之类的功能,并且将并发和 forall 内置到语言中,允许分布式内存和共享内存并行性以及矢量化。

Fortran 的缺点主要是上述优点之一的反面; Fortran 有着悠久的历史。优点:大量优秀的图书馆。缺点:大量的历史包袱。

如果您必须进行大量的数字运算,Fortran 仍然是最佳选择之一,这就是为什么在世界各地的超级计算中心运行的许​​多最复杂的模拟代码都是用它编写的。但当然,用它来编写网络浏览器将是一种非常非常糟糕的语言。针对每项任务都有其工具。

Fortran is, for better or worse, the only major language out there specifically designed for scientific numerical computing. It's array handling is nice, with succinct array operations on both whole arrays and on slices, comparable with matlab or numpy but super fast. The language is carefully designed to make it very difficult to accidentally write slow code -- pointers are restricted in such a way that it's immediately obvious if there might be aliasing, as the standard example -- and so the optimizer can go to town on your code. Current incarnations have things like coarray fortran, and do concurrent and forall built into the language, allowing distributed memory and shared memory parallelism, and vectorization.

The downsides of Fortran are mainly the flip side of one of the upsides mentioned; Fortran has a huge long history. Upside: tonnes of great libraries. Downsides: tonnes of historical baggage.

If you have to do a lot of number crunching, Fortran remains one of the top choices, which is why many of the most sophisticated simulation codes run at supercomputing centres around the world are written in it. But of course it would be a terrible, terrible, language to write a web browser in. To each task its tool.

樱娆 2025-01-04 22:45:04

对我来说,主要原因是漂亮的数组表示法,以及许多其他设计决策,使编写和调试科学代码变得更容易。事实上,就相关任务(数组操作)的性能而言,它通常是最佳选择,这也没有什么坏处:)

老实说,我不会认为大多数被列为 Fortran 真正竞争对手的语言——Java 和 Ruby 相差甚远,在便利性和性能方面都远远落后,而 C++ 是一种过于复杂和棘手的语言,不适合推荐给过去几年主要工作不是日常用 C++ 编程的人。 Python 和 numpy 可能是一个选择。我个人并不是这门语言的忠实粉丝,但我认识很多经常使用 numpy 的人,并且似乎对它非常满意。

我看到的真正的竞争不是来自这些,而是​​来自 Matlab、R 和类似的语言,它们提供类似的便利性,并与许多标准库相结合。幸运的是,通常可以在 R 或 Matlab 中启动一个项目,然后在 Fortran 中编写性能关键的部分。

The main reason for me is the nice array notation, and many other design decisions that make writing and debugging scientific code easier. The fact that it is usually the best choice in terms of performance on the relevant tasks (array operations) does not hurt either :)

Honestly, I would not consider most the languages cited as real competitors for Fortran -- Java and Ruby are far, far behind in terms of both convenience and performance, while C++ is much too complex and tricky a language to recommend to anyone whose main job for the last few years has been anything other than daily programming in C++. Python with numpy could be an option though. I am personally not a huge fan of the language, but I know a number of people who use numpy regularly and seem quite happy with it.

Real competition I see is not from these, but from Matlab, R, and similar languages, that offer similar convenience, combined with many standard libraries. Luckily, it is usually possible to start a project in R or Matlab, and write performance-critical parts in Fortran later.

心头的小情儿 2025-01-04 22:45:04

很少有项目是全新的项目。我不确定它是否特定于科学计算,但至少在这个领域,您倾向于基于现有的(科学)模型(可能是由其他团体/人产生的)构建应用程序。无论您是否愿意,您总是需要处理一定数量的遗留代码。

Fortran 语言是许多科学家学习的语言,也是他们需要的许多库的实现语言。他们中的许多人可能不是计算机科学家或 IT 人员,而是更多的计算科学家。他们的主要目标很少是计算,而是他们的科学第一。
虽然大量程序员一有机会(包括在业余时间)就会倾向于学习新的编程语言或框架,但大多数科学家会利用这段时间探索有关他们的科学的新想法。

接受过 Fortran(或任何语言)培训并且周围都是处于类似情况的人的领域专家将没有动力离开它。
现在,其他语言不仅可以在性能方面与 Fortran 一样好,而且还需要变得更好:需要有一个充分的理由来摆脱您所拥有和知道的东西。

这在某种程度上也是一个“恶性”循环。我一直发现 Java 和 Fortran 之间的比较有点困难,仅仅是因为许多 Java 科学应用程序不是用 Java 方式编程的。一些 Java Grande 基准应用程序看起来很像 Fortran 程序变成了 C 程序,复制/粘贴/调整到 Java 程序中(在一种方法中) ,将数组的长度作为数组本身旁边的额外参数传递给出了线索​​,如果我没记错的话)。因此,Java(例如)在科学界并没有获得很好的声誉,尽管它的性能越来越好。例如,这样做的结果是 HPC 专家和 Java 专家之间几乎没有重叠。即使来自硬件供应商或库实现者,用户的需求很少也会导致提供的支持很少,这反过来又阻止了可能有兴趣转向其他语言的用户。

请注意,这并不妨碍相同(或其他)科学家将其他语言用于其他目的(例如工作流程管理、数据管理、使用 Matlab、Numpy 进行更快的建模……)。

Few projects are completely new projects. I'm not sure it's specific to scientific computing, but at least in this field, you tend to build your applications based on existing (scientific) models, perhaps produced by other groups/people. You will always have to deal with some amount of legacy code, whether you want it or not.

Fortran is what a lot of scientists have been taught with and what a lot of the libraries they need are implemented in. A number of them might not be computer scientists or IT people, more computational scientists. Their primary goal is rarely computing, it's their science first.
While a large number of programmers would have a tendency to learn a new programming language or framework whenever they get a chance (including during their spare time), most scientists would use that time exploring new ideas regarding their science.

A domain expert who's trained in Fortran (or any language) and surrounded by people who are in a similar situation will have no incentive to move away from it.
It's not just that now other languages can be as good as Fortran in terms of performance, they need to be much better: there needs to be a good reason to move away from what you have and know.

It's also a "vicious" circle to a degree. I've always found comparisons between Java and Fortran a bit difficult, simply because a number of Java scientific applications are not programmed in a Java way. Some of the Java Grande benchmark applications look clearly like Fortran programs turned into C programs, copied/pasted/tweaked into Java programs (in a method, passing the length of the array as an extra parameter next to the array itself gives a clue, if I remember well). Because of this, Java (for example) hasn't got a great reputation in the scientific community, even though its performance is getting better. A consequence of that is that there is little overlap between HPC experts and Java experts, for example. Even from the hardware vendors or libraries implementors, little demand from users leads to little support offered, which in turns deters users who would potentially be interested in moving to other languages.

Note that this doesn't preclude the same (or other) scientists from using other languages for other purposes (e.g. workflow management, data management, quicker modeling with Matlab, Numpy, ...).

别在捏我脸啦 2025-01-04 22:45:04

据我了解,有些库是其可用算法的最有效实现,这使得 Fortran 在此类工作中很受欢迎,尽管该语言有其局限性。

As I understand it, there are libraries that are some of the most efficient implementations of their algorithms available, which makes Fortran popular for this kind of work in spite of the language's limitations.

荭秂 2025-01-04 22:45:04

原因之一在于数组的构建方式。与大多数其他语言不同,它们是专栏专业。这为他们的计算提供了更快的计算速度。

One reason is in how the arrays were constructed. They are column major, unlike most other languages. This provides faster computation for their calculations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文