What are the advantages of modern Fortran over modern C ++?

I am trying to decide between Fortran and C ++ for an application in scientific computing. I don’t understand if Fortran has any advantages over other languages ​​when it comes to performance. For example, I believe that since Fortran uses a strict alias, more efficient optimizations can be made by the compiler compared to C to C99. I'm not sure how C ++ fits here.

Any directions?

+28
c ++ fortran language-comparisons
Jan 27 2018-11-21T00:
source share
10 answers

I took a look at some of the material in the latest Fortran standards , and frankly, I'm impressed. Many of what I hated about language 20 years ago have disappeared now. No more line numbers and special columns (maybe they burn in hell).

Fortran has been widely used in engineering for 50 years. This gives you two benefits if you work in these circles. First of all, these people care a lot about optimization. This means that Fortran compilers have better optimizers. The language itself is much more optimized than the Cish languages, due to the lack of aliases.

The second advantage is that Fortran library support for crunching a number simply cannot be beaten. The best code will almost always be well-debugged code that you don't need to write .

If your application does not fall under the scientific, engineering, or quantitative crunch in general, then none of the above actions will be a big problem for you, so you might be better off looking elsewhere.

+41
Jan 27 2018-11-21T00:
source share

Another important issue is the learning curve, which is very large for C ++ and extremely small for Fortran (90 and later). Fortran is similar to MATLAB with operations such as ...

  • B'DB matmul( matmul(transpose(B), D), B )
  • L2 norm of the norm2(x) vector norm2(x)
  • SVD matrices using LAPACK call gesvd(A,S,u,vt)

Fortran also has pointers, dynamic memory, custom data types, etc.

It is well supported by major vendors (Intel / Sun / IBM / Cray / PGI / NAG, etc.), open source communities (gfortan / g95), and numerical library / API developers such as PETSc, MPI, etc. .

Suppose the new standard (Fortran 2008) even has shared arrays for parallel programming without the need for MPI / OpenMP, and some Fortran compilers already support it (g95 and Cray).

In principle, it has all the good qualities necessary for numerical calculations, easier than MATLAB, it is standardized, free, scalable (with MPI / OpenMP and co-arrays), it creates fast / parallel code.

For numbers, nothing compares to Fortran, but unfortunately for anything else Fortan hits. Therefore, if you are a scientist with safe work and only numerical calculations / HPC, then stick to Fortran, otherwise study and use C ++, since it is widely used for non-numerical software.

+19
Jan 28 '11 at 17:24
source share

Fortran allows you to perform operations on the entire array, as well as operations on sections of the array. There are C ++ classes for arrays, but I don’t think you can reference a slice like x (:, 2 :, 1: N3: 2) as easily as in Fortran. This allows you to briefly express some algorithms.

The convenience of Fortran array operations extends to arrays of derived types. Suppose you have an array of dates:

enter date
integer :: month, day, year
end date type

type (date) :: x (1000)

Then x refers to an array of dates, x% month refers to an array of months, and pack (x, x% month == 1) applies to all dates in January. How many other programming languages ​​does this convenience offer?

Some of Fortran's earlier comments - "old and disgusting" - are biased and should be discounted accordingly. Let me object. In my opinion, the free Fortran 90 format looks better than the C and C ++ syntax, with curly braces and semicolons. Leaving them or inserting them incorrectly, errors can occur in C and C ++ that have no analogues in Fortran.

+12
Feb 07 '11 at 17:57
source share

Fortran has been optimized for mathematical (especially matrix) operations.

C ++ has been optimized for using objects.

Which is more important to you.

As noted below, C ++ has an optimized matrix library.
But Fortran's whole goal is to optimize mathematical processes (especially matrix operations). The fact that these optimizations are built into the core of the language (and not libraries) and have begun two decades on C ++ research, I doubt (but do not know the fact) that Fortran is going to win hands in this area.

+11
Jan 27 2018-11-21T00:
source share

Advantages of fortran95 and higher over C ++ (2003):

  • As previously mentioned (by user4562) a short learning curve is mentioned (my first language was C, and I still can’t master it, similarly true for C ++)
  • (My personal opinion) It is easy to switch code from Octave (in this respect Matlab) the same syntax, the same modularity [I use Octave to prototype the program and rewrite in fortran95 for speed], although you can directly use octave code in C ++.
  • dynamic memory allocation is quite straightforward (f77 didn’t!) Library support
  • (u can do this in C ++ too, but its natural use is fortran)
  • Support for parallel computing from the side of the array (only pity cray supports them as of February 2011. gfortran started with gfortran4.6, but still a long way to go)

in short, if your program or application is purely scientific computing, use fortran 95 and above if calculating multiple numbers is only part of the story using C ++ (or something like u is better)

+8
Feb 14 2018-11-11T00:
source share

My experience with Fortran is that it is easy to learn, clean (being very modular), and therefore well suited for non-programmers whose main task is to do highly optimized numerical calculations. Although equal optimization can be performed in C ++ (even, possibly, to a greater extent), it takes a lot of instinctive understanding to achieve this level of optimization. When it comes to matrix computing, because of the box, the Fortran compiler will usually exit the C ++ compiler. I will also add that Fortran has keywords that are specifically designed to help the programmer squeeze more performance out of the numerical procedure. C ++ also has this, but not to the extent that Fortran does.

Another advantage is that Fortran is not specific to a system or architecture. In other words, Fortran code that you write on one operating system or architecture should easily be ported to another, where there is a Fortran compiler.

Another advantage is that modern Fortran is generally backward compatible with old Fortran code bases. And the code base that was built over the years in Fortran is huge and extremely complex (mostly scientists and mathematicians do this).

In addition, I personally really liked the built-in file processing functions that allow you to read a data file and perform operations on it almost instantly. Many other Fortran built-in functions are designed for this convenience. C ++ offers mostly building blocks for this, and it takes a little bit of revision to read the b / c data file that you need to know about separators (where Fortran allows you to specify a separator).

Other than that, I can't come up with the benefits. Most of all, it would be string manipulations or algorithmic operations, to which C ++ as a language, and in general these are compilers, are better suited and most often will work better. A good knowledgeable programmer is likely to prefer C ++, because he / she will understand how to optimize a numerical subroutine so that it can work better than a compiled Fortran subroutine. In addition, reliable Fortran reliable compilers are not as simple as good reliable C ++ compilers.

+6
Oct 19 '12 at 20:50
source share

I am new to programming. I have been programming in the field of finite elements for about a year. After some research on the net, I decided to use fortran 2003. I learned how to program in a modular style after about ten days, studying Chapman's book. This is one year, and I wrote about four thousand lines of code in a modular format (supported, reusable and neat codes) and did not use any character variable at all. I don’t think that by learning C ++, matlab, python, java ... for ten days, you will be able to write numerical codes as efficiently as fortran. fortran 2003 also has all the necessary OOP features that I am studying right now. Thus, from the point of view of linguistic strength in numerical terms, Fortan lacks nothing (modular style, OOP style, powerful array features, powerful libraries, free and commercial to dated compilers, very easy to learn, very effective ...). Languages ​​such as python / numpy have most of these features, but are not efficient. Languages ​​like C ++ also have most of the features of fortran (although to compute an array, which is the main core of numerical computation, you need to import some libraries !!), but maybe a program written by someone like me in fortran will be more effective than one written by a C ++ programmer with over 10 years of experience.
Finally, I do my heavy numerical computations in fortran (modular or OOP format) and use python \ numpy to calculate small size (e.g. plotting, calculating small size arrays ...).

+5
Aug 28 2018-12-18T00:
source share

Given the availability of scientific computing packages such as LAPACK ++, which are already highly optimized, modern Fortran does not even have a performance advantage. C ++ may have its own errors, but performance is not one of them.

+3
Jan 27 2018-11-21T00:
source share

IMHO, the only benefit that really matters is that FORTRAN programming allows you to easily use many existing FORTRAN codes and libraries. And if you have 50 FORTRAN programmers for a project and for a limited time, will you first teach them everything in C ++, or will you agree to let them use your favorite language?

+3
Jan 27 2018-11-21T00:
source share

With the advent of meta-programming patterns (especially expression patterns ), C ++ reached the FORTRAN league in numerical computing, so speed should not be a problem . However, there is one more thing about other issues:

Pro FORTRAN . Older people may know this better than C ++.
Contra FORTRAN . This is a disgusting, old and mostly abandoned language that was already outdated when you started your project. Whoever learns to program now is unlikely to recognize FORTRAN, so you may run into problems finding programmers for a project later.

Pro C ++ : it is relatively modern, and compilers are still significantly improved. This allows you to write fairly expressive code .
Contra C ++ . Some of these template error messages will make you cry.

-one
Jan 27 2018-11-21T00:
source share



All Articles