There is this n-body benchmark at n-body - Which programs are fastest? (Benchmarks Game), right now C/C++ is winning, Fortran is 6th, 60% slower. I figured, why don’t we fix it?
Here is an example of the “C++” winner on my machine (full code here):
template<size_t k = 0>
inline void KernelLoop1() {
__m256d x0 = _mm256_mul_pd(r[k], r[k]);
__m256d x1 = _mm256_mul_pd(r[k + 1], r[k + 1]);
__m256d x2 = _mm256_mul_pd(r[k + 2], r[k + 2]);
__m256d x3 = _mm256_mul_pd(r[k + 3], r[k + 3]);
__m256d t0 = _mm256_hadd_pd(x0, x1);
__m256d t1 = _mm256_hadd_pd(x2, x3);
__m256d y0 = _mm256_permute2f128_pd(t0, t1, 0x21);
__m256d y1 = _mm256_blend_pd(t0, t1, 0b1100);
__m256d z = _mm256_add_pd(y0, y1);
z = _mm256_rsqrt_pd(z);
_mm256_store_pd(&w[k], z);
if constexpr ((k + 4) < PAIRS) KernelLoop1<k+4>();
}
I don’t know how many of you can tell right away what is going on there. Plus it doesn’t even run on my Apple M4, since it’s hand-optimized in assembly for x86. But winner is a winner, so that has to be respected.
So the first thing we do is to write it in readable Fortran (full code here):
do k = 1, npair
d2 = r(1,k)*r(1,k) + r(2,k)*r(2,k) + r(3,k)*r(3,k)
w(k) = 1 / sqrt(real(d2))
w(k) = w(k) * (1.5_dp - 0.5_dp * d2 * w(k) * w(k))
w(k) = tstep * w(k) * w(k) * w(k)
end do
The C++ code is doing the same four lines in the body, but spread over several functions. Overall the code is equivalent, the same result. The Fortran code can be simplified more I think, but it’s a start, at 150 lines. The C++ code is 237 lines.
Let’s benchmark on AMD Ryzen 9 9955HX:
$ time ./nbody.cxx 50000000
-0.169075164
-0.169059907
real 0m1.027s
user 0m1.024s
sys 0m0.003s
$ time ./nbody.lfortran-opt 50000000
-0.169075164
-0.169059907
real 0m1.018s
user 0m1.017s
sys 0m0.001s
Wait, is LFortran faster? Well it is on my machine!
Let’s see how other Fortran compilers are doing:
$ time ./nbody.flang 50000000
-0.169075164
-0.169059907
real 0m1.488s
user 0m1.485s
sys 0m0.002s
$ time ./nbody.gfortran 50000000
-0.169075164
-0.169059907
real 0m2.264s
user 0m2.263s
sys 0m0.001s
Not too bad.
Now let’s benchmark on Apple M4:
$ time ./nbody.lfortran23 50000000
-0.169075164
-0.169059907
real 1.306s
user 1.297s
sys 0.006s
cpu 99.8%
$ time ./nbody.flang 50000000
-0.169075164
-0.169059907
real 1.346s
user 1.338s
sys 0.005s
cpu 99.8%
$ time ./nbody.gfortran 50000000
-0.169075164
-0.169059907
real 2.594s
user 2.587s
sys 0.006s
cpu 99.9%
The C++ code doesn’t even run on my M4, since it’s hardwired with assembly intrinsics for x86.
I think the result is about right, the frequency of the single M4 core is slightly lower than the AMD Ryzen core, so it seems we are getting a pretty good performance. I would say that’s true performance portability.
I used --fast for LFortran and -O3 -march=native -ffast-math for Flang/GFortran. I used these versions:
$ lfortran --version
LFortran version: 0.68.0-977-g01dce0b346
Status: alpha (expected to fail on third-party codes)
Platform: Linux
LLVM: 11.1.0
Default target: x86_64-unknown-linux-gnu
$ flang --version
flang version 23.1.4 (https://github.com/conda-forge/clangdev-feedstock babf9b0ec04c97fffe9fc24ec045deeb91a2c910)
Target: x86_64-conda-linux-gnu
Thread model: posix
InstalledDir: /home/ondrej/.pixi/envs/flang/bin
Configuration file: /home/ondrej/.pixi/envs/flang/bin/x86_64-conda-linux-gnu-flang.cfg
$ gfortran --version
GNU Fortran (conda-forge gcc 16.2.0-7) 16.2.0
Btw, I was using LLVM 11 on AMD Ryzen, and LLVM 23 on Apple M4. Usually newer LLVM is better, but on the Ryzen I was able to get state-of-the-art performance with very old LLVM 11 (released in 2020), showing that we are not doing anything fancy, just standard compiler optimizations.
And yes, we started implementing some optimizations into LFortran, just to get some decent performance before we release beta.
This is obviously all quite new and likely doesn’t translate to every code yet, but we’ll get there. I just wanted to let you know about this little (or big?) win, and put across the idea that we should be writing these high-performance algorithms in readable Fortran, not low level C++ with assembly instrinsics, and have the compiler optimize the code well on every platform.