Fortran And WebAssembly: Bringing Zippy Linear Algebra To NodeJS & Browsers

With the rise of WebAssembly (wasm) it’s become easier than ever to run native code in a browser. As mostly just another platform to target, it would be remiss if Fortran was not a part of this effort, which is why a number of projects have sought to get Fortran supported on wasm.

For the ‘why’, [George Stagg] makes the point that software packages like BLAS and LAPACK for Fortran are still great for scientific computing, while the ‘how’ is a bit more hairy, but getting better courtesy of the still-in-development LLVM front-end for Fortran (flang-new). Using it for wasm is not straightforward yet, due to the lack of a wasm32 target, but as [George] demonstrates, this is easily patched around.

We reported on Fortran and wasm back in 2016, with things having changed somewhat in the intervening eight years (yes, that long). The Fortran-to-C translator utility (f2c) is effectively EOL, while LFortran is coming along but still missing many features. The Dragonegg GCC-frontend-for-LLVM project was the best shot in 2020 for Fortran and WebAssembly, but obsolete now. Classic Flang has been in LLVM for a while, but is to be replaced with what is now called flang-new. The wish by [George] is now to find a way to get his patched flang-new code for wasm support into the project.

In the article, the diff for patching the flang-new toolchain to target wasm is provided. During compilation of the standard Fortran runtime it was then found that the flang-new code assumes that target system sizeof() results are identical to those of the host system, which of course falls flat for wasm32. One more patch (or hardcoded hack, rather) later the ‘Hello World’ example in Fortran was up and running, clearing the way to build the BLAS (Basic Linear Algebra Subprograms) and LAPACK (Linear Algebra Package) libraries and create a few example projects in Fortran-for-wasm32 which uses them.

The advantage of being able to use extremely well-optimized software packages like these when limited to a browser environment should be obvious, in addition to the benefit of using existing codebases. It is certainly [George]’s hope that flang-new will soon officially support wasm (32 and 64-bit) as targets, and he actively seeks help with making this a reality.

15 thoughts on “Fortran And WebAssembly: Bringing Zippy Linear Algebra To NodeJS & Browsers

    1. Fortran is a good language for linear algebra and scientific computing in general. FWIW it’s currently ranked 10th in the Tiobe list. The people using it know that C, C++, Python/NumPy, R, Matlab, Julia exist.

      1. JS is an ad-hoc language in my mind. The Basic language of the Browser world. I much preferred the Java Applet approach back when … until security concerns killed it. I’ll use JS when needed, but don’t have to like it :) .

        1. Job/contract interview question: Does this company use any JS on the server?

          Any answer except ‘Hell no. Are you crazy?’ you run (don’t walk) right out the door. Who would want to work for a company that makes that kind of boneheaded decision?

          JS lives in the browser ghetto. WebAssembly will save us.
          JS will never die though. The COBOL of the 24th century, along with COBOL and mainframe DOS of course.

          FORTRAN remains the only language (that I know of) that supports the calculated goto. Goto Intvar. Where Intvar contains a _line_number_.

      2. Javascript doesn’t have to be a “good” language ideologically. What we need is a good and easy way to make a web browser do whatever you want. You ever get annoyed at how every single entity you interact with wants you to download their smartphone app? I feel like when the choice was between clunky addons to make older browsers do things they weren’t designed for, or making things a separate application when they don’t need to be, it wasn’t hard to want something built-in that was good enough.

    2. Nothing wrong with Fortran. It isn’t a dead language and there is a ‘lot’ of scientific code still being used. Wasn’t that long ago, I was helping with plasma boundary applications (vehicle atmosphere re-entry) — written in Fortran. Just because it is perceived as ‘old’ doesn’t mean it is bad. It works, and works great for what it was designed for. You don’t have to write in a ‘modern’ language to get the job done — get the right answers :) .

  1. There is nothing wrong with Fortran, in the right problem space.

    I just can’t wrap my head around why my browser should be running huge linear algebra problems or the like.

    1. I would agree … But you know it seems there is a ‘push’ that everything has to run in a browser and/or in the cloud. So not surprised…. I personally would rather run on ‘bare-metal’ … not on a phone with silly apps, or in a VM in the cloud working from a thin client. No thank you.

        1. Experienced JS programmers have Stockholm syndrome.

          They are so disturbed, they think they like it. Unlikely to ever recover. Poor bastards are in library hell and don’t know better.

    2. I work on a team that writes astrodynamics algorithms for satellites. One of these algorithms does propagation. All code is done in Fortran. One of our users/customers wanted to have graphics in a browser, but having the browser go back and forth to/from the server caused time lag. We rewrote the propagation algorithm in C and converted it to wasm. No more lag. The propagation algorithm has a lot of linear algebra. Having a direct conversion from fortran to wasm would greatly benefit us.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.