Hacker News new | past | comments | ask | show | jobs | submit login
Fortran on WebAssembly (gws.phd)
235 points by georgestagg 9 months ago | hide | past | favorite | 51 comments



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/


Pull requests are always welcome (https://github.com/llvm/llvm-project), and one can contact the general LLVM Fortran development community (https://discourse.llvm.org/c/subprojects/flang/33) for help. I am focused on things needed to complete development for Nvidia's Fortran product and don't have any time left for things like this, myself.


Source to source, F77 to JavaScript is already pretty good but WASM is better.


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)


Does using all-caps FORTRAN say something about me? Fortran looks wrong to me.


Yes, it says you probably got started before the f90 standard.


Glad it’s not just me.


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!


Thanks! There’s a series of books I really like, The Theoretical Minimum[1], that also takes that kind of approach but for teaching physics.

[1] https://en.m.wikipedia.org/wiki/The_Theoretical_Minimum


Yes, I also like The Theoretical Minimum. Ha, I didn't realize there is more than one book! I think I only have the classical mechanics book. Need to buy the other ones.

I tried to organize many physics subjects in a similar manner, with many worked out examples (minimal, but non-trivial/complete):

https://www.theoretical-physics.com/dev/index.html


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.


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?


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.


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.


I’m trying to understand why though


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.


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.


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.


Any good pointer to this direction?


https://github.com/WebAssembly/wabt/blob/main/wasm2c/README.... is a straightforward way to take an untrusted application (compiled already to wasm) and turn it into C that you can embed into your application or compile to a linkable DLL. I believe this approach has been used to sandbox untrusted libraries in production by Mozilla: https://hacks.mozilla.org/2021/12/webassembly-and-back-again...


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").


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.


Did you work at Hughes in Orange County?


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.


Do we have a BLAS equivalent on WebGPU or WebNN?


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


> 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.


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!


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.


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.


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


Thank you!


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


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....


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


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


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?


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.


Also of course LPython

https://lpython.org/



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...


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?


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.


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.


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


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).


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


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


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.


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 ???


The translator gets translated.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: