Live data from Hacker News

Learning Fortran (2024)

uncenter.dev

61–70 of 85 posts

Re: Learning Fortran (2024)

#62
post #8

The article did not discuss this, but to me, one of the bigger differences between Fortran and more modern languages is the difference between functions and subroutines. Yes, they are not synonyms in Fortran and serve different purposes. I think this would trip up more people initially than the clunky syntax. It is also a bit funny that the author complains about older Fortran programs requiring SCREAMING_CASE, when…

I find it annoying in Java when uppercase is used because its an acronym. URL or UUID. No man, just Url/Uuid. No need to yell just because its an acronym.

Re: Learning Fortran (2024)

#63

I actually had a fantastic experience with Fortran lately. I ported a compute kernel from python/numpy to Fortran 2018, partially due to the GIL and partly so I could use Intel's compiler. The performance improvement was tremendous. Several times faster per core, then multiplying further because I could take advantage of threading. In all, the 3 day project increased actual throughput 450x. (I considered JAX, but the…

Any reason why you couldn't do this in C/C++? I use the Eigen matrix library as a replacement for Numpy when I need more performance and the code looks surprisingly very similar.

Re: Learning Fortran (2024)

#64

Earlier quoted context omitted.

What practical difference ever existed, beyond the fact that a subroutine does not return a value? AFAIK variable scope was handled identically. Recursion was likewise identical (forbidden originally).

They are pretty similar, but they are definitely used differently. For one, you have to "call" a subroutine in one statement, but you can use multiple functions on the same statement (since they can return values). Functions (usually) do not change their arguments, but subroutines often do. In some sense, functions are closer to how mathematical functions work but subroutines are closer to labels for certain procedur…

So they are used differently, but there isn’t a language enforced difference (other than return value)?

Re: Learning Fortran (2024)

#65
post #46

I wonder: is there any reason beyond sheer curiosity* to learn Fortran in 2025? Not being snarky, genuinely curious about what Fortran brings to the table. * Nothing wrong with that as a reason, of course

Fortran is still the best way to write scientific computing codes for HPC environments. And not just because of legacy code. You know how all those language benchmarks have C as the reference class (and fastest)? Fortran is faster still, by a significant margin.

Re: Learning Fortran (2024)

#66

I actually had a fantastic experience with Fortran lately. I ported a compute kernel from python/numpy to Fortran 2018, partially due to the GIL and partly so I could use Intel's compiler. The performance improvement was tremendous. Several times faster per core, then multiplying further because I could take advantage of threading. In all, the 3 day project increased actual throughput 450x. (I considered JAX, but the…

> Suppose you pass a parameter, N, and you also would like to pass a tensor, and you would like to specify the tensor's shape (N, N).

You can do that, and it might be cleaner and less lines of code that way.

But you don't necessarily need to pass the array dimensions as a parameter, as you can call `size` or `shape` to query it inside your function.

    program main
      implicit none
    
      real :: a(2, 2) = reshape([1., 2., 3., 4.], [2, 2])
    
      call print_array(a)
    
    contains
    
      subroutine print_array(a)
        real, intent(in) :: a(:, :)
        integer :: n, m, i, j
    
        n = size(a, 1) ; m = size(a, 2)
        write(*, '("array dimensions:", 2i3)') [n, m]
        do i = 1, n
          do j = 1, m
            write(*, '(f6.1, 1x)', advance='no') a(i, j)
          end do
          print *
        end do
      end subroutine
    
    end program

Re: Learning Fortran (2024)

#67

Earlier quoted context omitted.

They are pretty similar, but they are definitely used differently. For one, you have to "call" a subroutine in one statement, but you can use multiple functions on the same statement (since they can return values). Functions (usually) do not change their arguments, but subroutines often do. In some sense, functions are closer to how mathematical functions work but subroutines are closer to labels for certain procedur…

So they are used differently, but there isn’t a language enforced difference (other than return value)?

Functions are called only within the context of expression evaluation, and Fortran allows a compiler to perform algebraic transformations on expressions. If you write X=Y*F(Z) and we can determine that Y is zero, the function call can be deleted. So side effects in functions are somewhat risky.

Re: Learning Fortran (2024)

#69

Very good choice. Focus on the newest standard.

... if you have access to the Intel compiler tools. gfortran does not support many of the new language features beyond Fortran 2008, even though Fortran 2018 is the defacto "current" standard.

This is true.
Post reply on HN