News for hackers. Stuff that matters.
Re:Fortran by Majromax on Monday August 28, 2023 @08:37AM (#37293051)
> the "winning" strategy is to have high-level scripting languages where you can ignore most of the details, which call into hyper-optimized, high performance libraries. For instance, when you're using Scipy, you call into Fortran and C almost interchangeably.

That gets you about 70% of the way there, but the remaining 30% is tricky. Unfortunately, the library-call stage doesn't easily (with generality) let you fuse operations. For example, consider the example of a finite-volume code, where the flux is some nonlinear function of the interface value, taken to be the average of left and right cells:

  flux = nonlinear_flux_op(f[:-1] + f[1:]) # Compute interface fluxes
  f[1:-1] += dt/dx*(flux[:-1] - flux[1:]) # Update cell volumes based on flux differences
  # Do something special for the boundaries

Each of the substeps here is fast. Sub-indexing these 1D arrays is trivial, and their local operations are highly vectorized. The nonlinear flux operator operates element-wise, so it's easy to compile it into a version that properly (CPU) vectorizes (and/or it might already be provided, like np.exp or *2). This code is also very readable.

However, real performance would require fusing operations. There's no need to compute a full temporary array for the interface values, but that's exactly what would happen in naive NumPy code. Likewise, there's no need to compute all of the fluxes before taking their differences.

A hand-optimized code would perform the full computation in a single scan of the array, keeping all of the data in CPU cache (blocking by a cache line or so in order to use vector registers effectively). Even if given the code naively written, an optimizing compiler would try loop fusion and get most of the way to the hand-optimized result.

Code such as this is relatively FLOPS-light compared to the amount of memory accessed, so effective use of cache can lead to an order of magnitude speed-up.

With a fair amount of work you can still achieve this result in Python, but to do so you need to use one of the optimizing Python compilation tools like numba. Unfortunately, this is not easy for the not-a-programmer domain scientist, since working with numba means understanding the intersection of Python and numba's imposed compilation rules.

Re:Fortran by thanatropism on Monday August 28, 2023 @09:50AM (#37293940)
ChatGPT, please write a song to the approx tune of "All you need is love" about numpy.linalg.einsum

---

(Verse 1) Gather 'round, fellow coders, lend an ear, A tale of performance, crystal clear, It's about `numpy.linalg.einsum`, you see, A bridge to speed that sets your code free.

(Chorus) Numpy linalg einsum, unlocking the door, To performance realms we've hungered for, With C and Fortran, it dances in tune, Efficiency's treasure, like the brightest of moons.

(Verse 2) Python loops and arrays, they have their grace, But when it comes to speed, they're in a different space, Einsum brings us closer, to libraries so bold, C and Fortran's might, where performance unfolds.

(Chorus) Numpy linalg einsum, a liaison so grand, Linking Python's elegance with performance's land, From indices to memory, it bridges the span, Oh, the power it wields in a programmer's hand.

(Bridge) Clever indexing and Pythonic style, Are wonderful tools that make us smile, But beneath them, a world of raw speed resides, Numpy linalg einsum, where performance abides.

(Verse 3) Linear algebra's puzzles, intricate and vast, With einsum's prowess, we reach new heights so fast, Python's beauty combined with Fortran's swift glide, Numpy linalg einsum, where two worlds coincide.

(Chorus) Numpy linalg einsum, the key to the gate, Where performance and elegance conjugate, Let's embrace the speed that we've dreamed of so long, With C and Fortran, together we're strong.

(Outro) So heed this ode to efficiency's call, Numpy linalg einsum, the ace of them all, In the realm of coding, where performance takes flight, We cherish the union that powers our might.

Re:Fortran by blt on Monday August 28, 2023 @03:27PM (#37299237)
Numba is quite restrictive... to someone comfortable in C++, coercing my code into a form Numba likes sometimes feels no easier than writing an extension using Eigen types and pybind11.