3 ms·
Is it sensible to implement math heavy part of C application in Fortran and call it via FFI? Or is -Ofast enough in most cases without the costs (multiple, beca
by hawski 9y ago
Is it sensible to implement math heavy part of C application in Fortran and call it via FFI? Or is -Ofast enough in most cases without the costs (multiple, because performance may be one of them) of FFI?
- jabl 9y agoNot for speed reasons alone. It's certainly possible to tune C code to have roughly equivalent performance to Fortran. The real appeal for using Fortran is that for beginners it's a much easier language to pick up than C (much less footguns, for one), and that working with (dense) multidimensional arrays is very nice, roughly on par with high-level languages like numpy, R, or MATLAB.
- m_mueller 9y agoTo expand on this, I think the footguns you're talking about are specifically performance related. Just an example: How do you specify a fast readonly array in C? If I recall correctly it's restrict const * const int my_array[n] In Fortran? integer(4), intent(in) :: my_array(n) Not even starting with array ops, multidimensionals, array slicing etc., that's already a big win in readability. Having a pass-by-reference language by default is refreshing for HPC purposes. That being said there are disadvantages compared to C. Biggest one IMO is the lack of inline declarations, which especially makes privatizing things in OpenMP and OpenACC code a bit more tricky, i.e. it needs to be explicit.
- smitherfield 9y ago> How do you specify a fast readonly array in C? Generally in the function declaration rather than the variable declaration, but to declare a non-aliasing pointer to array of constant ints would be const int (restrict *my_array)[N]; or int const (*restrict my_array)[N]; and the other two variations thereof.
- int_19h 9y agoGentle reminder to future language designers: whatever else you decide to take from C, please, please don't take declarator syntax. This thread showcases why.
- wombatpm 9y agoThats what I did in grad school. C front end for datafile reading, writing, and data munging. Fortran for the heavy lifting and solver libraries.
- alephnil 9y agoActually one of the reasons that Fortran is fast is not that the code you write in it is inherently faster than C. It is more that the idiomatic way of programming it is fast. This is not necessarily the case in C, where it is quite natural to use pointer structures, while in Fortran you would in most cases use large arrays that consists of continuously allocated memory instead. It is of cause possible to do that in C as well, but it is not as natural. You can use pointer structures in modern Fortrans as well (as in F90 and later). Again, the difference is that idiomatic Fortran often lead to faster code. One real difference is that Fortran do not allow aliased memory in arrays unless that is specifically declared using the "equivalence" statement, while in C and C++ that is the default assumption. This allows for some optimizations that can't be done safely in C. In C99 and later, you can use the "restrict" keyword to state the opposite, namely that an array is not aliased, but usually you won't do that outside of tight loops if at all.