Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran vs C++, does Fortran still hold any advantage in numerical analysis these days? [closed]

Tags:

c++

c

cuda

fortran

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?

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?

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.

like image 229
user0002128 Avatar asked Oct 25 '12 23:10

user0002128


People also ask

Is Fortran better than C?

Judging the performance of programming languages, usually C is called the leader, though Fortran is often faster. New programming languages commonly use C as their reference and they are really proud to be only so much slower than C.

Is Fortran still useful?

With its 66 years of legacy, Fortran is still considered alive for many reasons. One of the primary ones is the valuable legacy that Fortran code has in critical software systems like weather prediction, hurricane or storm surge prediction as well as traffic monitoring.

Is Fortran still faster than C++?

The benchmarks where Fortran is much slower than C++ involve processes where most of the time is spent reading and writing data, for which Fortran is known to be slow. So, altogether, C++ is just as fast as Fortran and often a bit faster.

Is Fortran still used in 2020?

Fortran is a language that is specialized for high-performance computing. Believe it or not, it's still alive and evolving. Fortran is still used in high-performance computing.


1 Answers

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.

The programming languages shootout should not be taken too seriously, but of the 15 benchmarks, Fortran ranks #1 for speed on four of 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:

  • spectral norm 27% faster
  • fasta 67% faster
  • mandelbrot 56% faster
  • pidigits 18% faster

Counterexample:

  • k-nucleotide 500% slower (this benchmark focuses heavily on more sophisticated data structures and string processing, which is not Fortran's strength)

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.

So the questions you will need to ask yourself are:

  1. Is the speed of this function so critical that reimplementing it in Fortran is worth my time?

  2. Is performance so important that my investment in learning Fortran will pay off?

  3. Is it possible to use a library like ATLAS instead of writing the code myself?

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.

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.

like image 120
Dietrich Epp Avatar answered Sep 22 '22 02:09

Dietrich Epp