[HN Gopher] Fortran on WebAssembly
___________________________________________________________________
Fortran on WebAssembly
Author : georgestagg
Score : 143 points
Date : 2024-04-05 16:21 UTC (6 hours ago)
(HTM) web link (gws.phd)
(TXT) w3m dump (gws.phd)
| math_dandy wrote:
| 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!
| georgestagg wrote:
| 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
| certik wrote:
| 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
| droelf wrote:
| Weird that it doesn't go into LFortran more, they even have an
| excellent online and mindblowing WASM example.
| https://dev.lfortran.org/
| andsoitis wrote:
| > 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 athttps://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 wrote:
| 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!
| georgestagg wrote:
| 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 wrote:
| 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 wrote:
| Grats for the amazing work! LFortran is very cool project.
| certik wrote:
| Thank you!
| superjan wrote:
| Just curious but what is keeping you from compiling x*3?
| certik wrote:
| 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/69d
| 488b1d1fd26b163....
| floxy wrote:
| 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?
| Onavo wrote:
| Also of course LPython
|
| https://lpython.org/
| pklausler wrote:
| 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 wrote:
| 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 wrote:
| 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 wrote:
| 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.
| pjmlp wrote:
| 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
| coldcode wrote:
| 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.
| tomcam wrote:
| Did you work at Hughes in Orange County?
| wch wrote:
| 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/
| pklausler wrote:
| 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.
| QuantumG wrote:
| Source to source, F77 to JavaScript is already pretty good but
| WASM is better.
| 0cf8612b2e1e wrote:
| 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.
| kevingadd wrote:
| 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.
| norman784 wrote:
| 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.
| ur-whale wrote:
| The translator gets translated.
| tomasreimers wrote:
| 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...
| mhh__ wrote:
| 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.
| KRAKRISMOTT wrote:
| Do we have a BLAS equivalent on WebGPU or WebNN?
| ein0p wrote:
| 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?
| rad_gruchalski wrote:
| 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 wrote:
| 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 ???
| georgestagg wrote:
| 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 wrote:
| 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 wrote:
| Jupyterlite looks pretty cool, thanks for making me aware of
| it
| sanxiyn wrote:
| 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
| amirhirsch wrote:
| 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)_
| amirhirsch wrote:
| Does using all-caps FORTRAN say something about me? Fortran
| looks wrong to me.
| necheffa wrote:
| Yes, it says you probably got started before the f90
| standard.
___________________________________________________________________
(page generated 2024-04-05 23:00 UTC)