6 ms·
Fortran is an excellent choice for numerical computation. Its non-aliasing assumption allows for compiler optimizations that dramatically speed up computations.
by bmh100 10y ago
Fortran is an excellent choice for numerical computation. Its non-aliasing assumption allows for compiler optimizations that dramatically speed up computations. It is even easy to get started with concurrent code through OpenMP. When I need to run a simulation or a discontinuous optimization algorithm, I turn to Fortran first.
Here are some resources I have collected over the years.
"Introduction to Modern Fortran" (Fortran 2003): http://people.ds.cam.ac.uk/nmm1/Fortran/index.html http://people.ds.cam.ac.uk/nmm1/Fortran/index.html
Fortran Wiki: http://fortranwiki.org/fortran/show/HomePage http://fortranwiki.org/fortran/show/HomePage
List of Fortran tutorials: http://www.fortran.com/the-fortran-company-homepage/fortran-tutorials/ http://www.fortran.com/the-fortran-company-homepage/fortran-...
Fortran vs. C: http://www-cs-students.stanford.edu/~blynn/c/fortran.html http://www-cs-students.stanford.edu/~blynn/c/fortran.html
Fortran's influence on C's "restrict" keyword: http://www.drdobbs.com/the-new-c-it-all-began-with-fortran/184401313 http://www.drdobbs.com/the-new-c-it-all-began-with-fortran/1...
- castratikron 10y agoTo be fair, there are GCC built-ins that allow you to tell the compiler that arrays are nonaliasing, but you're right in that it's more difficult in general to get a compiler to spit out fast numeric code with C than it is with Fortran.
- bmh100 10y agoI feel like it's a "right tool for the job" situation. Fortran was literally built for the purpose of numerical computation. Multi-dimensional arrays, non-aliasing arrays and pointers, multi-dimensional array intrinsics, intrinsic array binary operators... all come with Fortran right out of the box. Of course, C/C++ can do all of these things, but all of the proposed methods, at least that I have seen, feel like they are trying to hammer a screw.
- pmiller2 10y agoGlad this was posted. First thing I did on loading the comment section was ^F "alias". Fortran is still used for numerics because it makes writing fast numerical code easy. C simply wasn't designed for it, just as Fortran wasn't designed for low level system/OS programming.
- smarnach 10y agoIn C99, you can use the "restrict" keyword to tell the compiler an array is not aliased, and even without restrict it's often possible to get the same optimisations in C or C++. When I was still doing HPC in academia, all our C++ code was maxing out the memory bandwidth. I think in practice the difference rarely matters these days.
- bmh100 10y agoWere you using C99 during your HPC programming in academia?
- smarnach 10y agoI used C99 for the computing kernel of one standalone project (simulation of gravitational lensing). Apart from this, it was mostly C++.
- bmh100 10y agoWhat made your work so dependent upon memory bandwidth?
- greglindahl 10y agoAs an introduction, you might want to look into John McCalpin's history as a shallow-water oceanographer and his subsequent fixation on the STREAM benchmark. TL;DR: some algorithms aren't friendly to caching. I have no idea if this applies to smarnach, but it definitely applies to other people.
- jabl 10y agoJohn McCalpin did a nice presentation at SC16 giving an overview of memory BW/latency issues in HPC, slides: http://sites.utexas.edu/jdm4372/2016/11/22/sc16-invited-talk-memory-bandwidth-and-system-balance-in-hpc-systems/ http://sites.utexas.edu/jdm4372/2016/11/22/sc16-invited-talk...
- fovc 10y agoWhat does non-aliasing mean in this context?
- shoo 10y agohttps://en.wikipedia.org/wiki/Pointer_aliasing https://en.wikipedia.org/wiki/Pointer_aliasing
- bmh100 10y agoIt means that the compiler knows that pointers/arrays don't overlap in memory. Data can therefore be safely stored in registers, instead of continually re-read from memory, to account for possible alterations between computations [1]. [1]: http://stackoverflow.com/questions/146159/is-fortran-faster-than-c#146186 http://stackoverflow.com/questions/146159/is-fortran-faster-...
- jack1243star 10y agoI wonder if Rust is fundamentally able to take advantage of its borrow checker and outperform C in this regard.
- Asooka 10y agoI have one question - if I compile and link a program written in C and Fortran, can the linker inline Fortran functions in C? I know it can do it if I link object files produced from C source, but can it do it if I link object files from separate languages?
- guitarbill 10y agoHaving never done it with Fortran, I don't know. It should be possible, because e.g. on Linux, everything adheres to the ABI. However, the ABI doesn't specify how to interpret pointers AFAIK. Array ordering [0] and indexing is different in Fortran compared to C, so I'm sure there's pitfalls you'd have to account for. [0] https://en.wikipedia.org/wiki/Row-_and_column-major_order https://en.wikipedia.org/wiki/Row-_and_column-major_order
- dfgasfjhjhsdojj 10y agoFortran arrays can be indexed however you like. Code that is designed to be called from C can be written with 0 indexed arrays.
- guitarbill 10y agoSure, but you still have to remember to do so :)
- greglindahl 10y agoMost Fortran compilers are actually Fortran/C/C++ compilers, and cross-link and cross-inline with no problems.
- Annatar 10y agoIt's not the link editor's function to inline; an optimizing compiler does that. A linker can, eventually and depending on the platform, perform some binary optimization, as is for example the case with Sun Studio compilers on SPARC (-xlinkopt=2), or with GCC (-flto), but that's not inlining.
- deleted 10y ago[deleted]
- math_and_stuff 10y agoAs a counterpoint, it is worth noting that essentially no high-performance BLAS or LAPACK implementations (e.g., GotoBLAS/OpenBLAS, ATLAS, BLIS, etc.) are implemented in Fortran and instead tend to use a combination of C (in order to use vector intrinsics) and assembly.
- deleted 10y ago[deleted]