Settings

Theme

Fortran on WebAssembly

gws.phd

235 points by georgestagg 2 years ago · 63 comments

Reader

wch 2 years ago

A little context: this dive into Fortran is part of the excellent work George has been doing on WebR, to get R running in the browser. The R sources contain a fair bit of Fortran code, and I believe WebR originally used f2c to compile the Fortran to C first, before compiling that to wasm.

With the patches to LLVM Flang, WebR can be built with a real Fortran compiler.

I think George didn't want to say it directly in the blog post, but he has said that he's hoping that Flang would take his patches or implement better ones. That would be a win-win -- these patches wouldn't need to be maintained separately, and since unmodified Flang would be able to compile to wasm, it would benefit other projects out there that use Fortran.

https://docs.r-wasm.org/webr/latest/

amirhirsch 2 years ago

I worked on compiling FORTRAN at Xilinx 20 years ago. The only thing I remember is that the header file for f2c.h contains a definition of barf:

/* f2c.h -- Standard Fortran to C header file /

/* barf [ba:rf] 2. "He suggested using FORTRAN, and everybody barfed."

(https://www.netlib.org/clapack/f2c.h)

math_dandy 2 years ago

I love the "simplest nontrivial example" approach to exposition. I think I learned a lot from the article because it was grounded in the concrete problem "call a BLAS function from Javascript". Great post!

pklausler 2 years ago

I don't know whether to be impressed or horrified. Maybe both.

I would recommend using top-of-tree llvm-project/main sources for building f18; we are a fast-moving project and it would be a waste of time for anybody to debug a problem that has already been fixed, or miss a feature that has already been implemented.

  • tomcam 2 years ago

    I was unable to understand the llvm source well enough to understand your point. Are they working on a WebAssembly port that will get their intermediate code to a point where Fortran works?

    • pklausler 2 years ago

      I meant that we are actively developing a new implementation of the Fortran language itself here. We are still adding features and fixing bugs. Fortran code that compiles and runs today with llvm-project/main HEAD may well not compile or not run with older release branches.

    • sanxiyn 2 years ago

      The article uses LLVM Flang from LLVM 18.1.1. pklausler's point is that it is counterproductive and LLVM HEAD should be used instead.

0cf8612b2e1e 2 years ago

I am pretty ignorant of Web Assembly development.

Does Web Assembly have anything to offer me today as a consumer? Or is all of this still setting the groundwork for a future where programs are truly portable?

I have heard some rumblings that the WA machinery makes it easier to restrict access (network, files) but I do not know if those are theoretical or implemented today.

  • norman784 2 years ago

    Basically Wasm is a virtual machine, is is very similar to JVM that is portable, but the key difference is that Wasm does not have any std nor expose any IO function, so you can build your own host (the VM) that expose functions that can be imported from the Wasm binary, that means that the Wasm binary can have access to the external world only through these functions.

    Also I would say an advantage is that the binary format is not proprietary and there's a spec, so anyone could implement their own Wasm VM.

    But right now is not in a good place yet, is too early and there are a lot of new functionality that is being standardized by a group (similar to W3C) and the process is very slow.

  • kevingadd 2 years ago

    As a developer or someone shipping products, if you want robust sandboxing, WASM is probably the best option available to you right now. And there are ways to deploy it or cross-compile it for most targets.

  • flohofwoe 2 years ago

    If implemented well you wouldn't notice it as a customer (just as you don't notice nor should care much whether your computer has an ARM or x86 CPU), so it's hard to say whether it offers anything over other solutions (unless you care about details like whether a program runs as native code or via a VM like JVM, .NET or WASM) - typically one will only notice the bad apples which then may turn into memes (like "all Electron programs are a bloated resource hungry mess, while all native applications are automatically an efficient marvel of software engineering").

coldcode 2 years ago

I wish I had kept my Fortran 78 code from 1981/82 to see if I could get it to run on this. It was a Jovial programming language source code formatter. Not exactly what you should use Fortran for, but that's the only choice I had.

mhh__ 2 years ago

Is there a somewhat "production"-ready ecosystem for linear algebra in javascript?

Upon googling I often find some ~decade old port of one of the old familiars to javascript (e.g. via emscripten), wondering if I'm missing something.

droelf 2 years ago

Weird that it doesn't go into LFortran more, they even have an excellent online and mindblowing WASM example. https://dev.lfortran.org/

  • andsoitis 2 years ago

    > Weird that it doesn't go into LFortran more, they even have an excellent online and mindblowing WASM example.

    They write: The LFortran compiler has made great strides over the last few years. In 2020, it was missing a lot of features and only supported a very small subset of Fortran. Now it now supports a much wider range of language features and can be used to compile a reasonable amount of Fortran code. It can even compile to WebAssembly out of the box!

    However, there are still some barriers that make using LFortran a little rough. The project is currently considered to be in alpha phase and the developers state that issues compiling real-world code are expected. While it can successfully compile some projects, such as MINPACK, the full Fortran specification is not yet supported and so many larger projects still cannot be compiled.

    The LFortran developers are targeting full support for Fortran 2018, and its standout feature is an interactive Jupyter-like Fortran REPL. With a few more years of development, I expect that LFortran will be an excellent choice for compiling Fortran code for WebAssembly.

    and

    Check out the LFortran demo at https://dev.lfortran.org. While extremely impressive, note that the first thing I tried was changing x * 2 to x * 3 and saw that such a change is currently not supported by the code generator.

    • certik 2 years ago

      The author of LFortran here.

      The demo at https://dev.lfortran.org uses our direct WASM backend that does not use LLVM. It is currently more limited, and indeed, we currently do not support the cubic power x**3 there, only square power x**2. Our most advanced backend is LLVM, and that of course supports x**3 and a very wide subset of Fortran (such as 60% of all SciPy packages fully compile and all SciPy tests pass). However, LLVM is huge and relatively slow, so we do not use LLVM in the online demo, which runs the compiler itself in the browser.

      For offline LLVM based WASM compilation I think LFortran is ready be tried. We'll be happy to help!

      • georgestaggOP 2 years ago

        I’ll definitely be trying out more of LFortran in the future.

        For this post I really wanted to go deeper into the approach we’ve taken with flang, but I can see that LFortran is also a very strong choice here for running Fortran on Wasm.

        • certik 2 years ago

          Thanks. Please report all bugs that you find. I talked to my collaborators, we'll try to get some simple demo of Fortran->LLVM->WASM working soon, we need to figure out the runtime library issue (like you did), hook it into the driver, etc. I was in fact thinking about exactly this just last week, to easily distribute my simple computational codes online via static pages.

          I think exactly the approach that you took with Flang should work with LFortran also.

      • forgotpwd16 2 years ago

        Grats for the amazing work! LFortran is very cool project.

      • superjan 2 years ago

        Just curious but what is keeping you from compiling x*3?

        • certik 2 years ago

          Nothing, we can compile x*3. We can't compile x**3, because we do not have a runtime library setup for WASM yet (Flang above had the same issue) and WASM can do x**2, but arbitrary power, such as x**3, requires a runtime power function that we haven't implemented yet. If you want to help, you can fix it probably quite easily right here: https://github.com/lfortran/lfortran/blob/69d488b1d1fd26b163....

          • selimthegrim 2 years ago

            How about cube roots of fractions and rational numbers? (TI and Casio fans will get the deep cut)

            • certik 2 years ago

              The LLVM backend just does the usual floating point calculation for those.

      • floxy 2 years ago

        Wow, this looks great. Seems like there is a mini-renaissance of Fortran that I've been seeing lately. I see from the main page:

        "LLVM makes it possible to run LFortran on diverse hardware and take advantage of native Fortran language constructs (such as do concurrent) on multi-core CPUs and GPUs."

        ...is LFortran close to using coarrays, etc, and farming it out as appropriate to the GPU cores?

        • certik 2 years ago

          We are progressing. We'll tackle parallel loops very soon, and get some GPU offloading working. Our main focus is still on just compiling Fortran codes via LLVM. Once we can compile most codes, we'll focus on the various other backends, including GPU, running in the browser and Jupyter.

    • Onavo 2 years ago

      Also of course LPython

      https://lpython.org/

pjmlp 2 years ago

Fortran on .NET and Java,

https://www.silverfrost.com/14/ftn95/ftn95_fortran_95_for_mi...

https://dl.acm.org/doi/10.1145/376656.376833

tomasreimers 2 years ago

I remember when I was working on https://medium.com/@tomasreimers/compiling-tensorflow-for-th... I thanked my lucky stars that TF used Eigen and not one of the popular math libraries (BLAS, Lapack) written in fortran b/c that would have been A LOT more work...

ein0p 2 years ago

I’ve been reading these articles for years, but I’ve yet to experience any practical use of webassembly outside of contrived demos. Where do people use all this stuff? Does anyone use it?

  • georgestaggOP 2 years ago

    WebAssembly is generally most useful when you want to write high performance web applications using languages like C, C++ or Rust.

    WebAssembly sits in the background quietly powering the web-based versions of products like Adobe Photoshop, AutoCAD, Figma, Canva, and likely others. By using Wasm components combined with other browser technologies such as HTML canvas and webGL, app performance and responsiveness can be improved.

    WebAssembly also powers the Pyodide and webR projects, enabling Python and R code to run in a browser without a supporting computational server. Where I’ve seen this used most effectively so far is in teaching materials, particularly for teaching data science, where interactive R and Python examples can be embedded directly into teaching materials without the educator having to worry about the time or expense to deploy a powerful backend service to evaluate learner’s code.

  • homerowilson 2 years ago

    As an educator, one great use for me is classroom use. Students can run R/Python/Fortran in the browser on any OS without installing any software:

    https://docs.r-wasm.org/webr/latest/ https://github.com/jupyterlite/jupyterlite https://dev.lfortran.org/

    There are rough edges to be sure, but the potential is great in education I think.

    • ein0p 2 years ago

      Jupyterlite looks pretty cool, thanks for making me aware of it

  • azakai 2 years ago

    If you used Zoom on the Web then you have used WebAssembly without knowing it. The same for a long list of other stuff, big and small, from parts of Wikipedia to games to many other things.

    When you use something like the Video element on the Web then it's obvious - you see a video playing - but wasm is just a technical detail that you might not notice as a user. But it often makes things faster or easier to port or to develop, and it is used quite widely (though far less widely than JavaScript, for example).

    • ein0p 2 years ago

      Didn’t know about this, very cool, and makes a lot of sense.

  • sanxiyn 2 years ago

    WebAssembly is used in the real world and uses are very diverse. I recommend "An Empirical Study of Real-World WebAssembly Binaries". (The paper is from 2021, and it would be great to get an update, but I guess academia does not reward such work because it is not "novel" even if it is clearly valuable.)

    https://www.software-lab.org/publications/www2021.pdf

  • rad_gruchalski 2 years ago

    Envoy proxy uses wasm filters, thus Istio uses wasm filters. Everyone who uses Istio in non-ambient-mesh uses them. They can write their own in a language of their choice.

  • csjh 2 years ago

    Cloudflare workers use them for portability/v8 isolate compatibility/speed/isolation, Figma uses for the main editor, a bunch of Web3 stuff uses them for ???

ur-whale 2 years ago

The translator gets translated.

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection