(评论)
(comments)

原始链接: https://news.ycombinator.com/item?id=39944275

作者表达了通过简单例子教授复杂概念的热情。 他们从一篇演示从 JavaScript 调用 BLAS 函数的文章中发现了价值。 尽管很难理解引用的 LLVM 源代码,但他们分享了对 Fortran 语言和 LFortran 编译器的持续开发的见解。 目前处于 alpha 阶段,LFortran 的目标是通过类似 Jupyter 的 REPL 完全支持 Fortran 2018,但由于缺少功能而在编译大型项目时面临挑战。 进展包括并行循环、GPU 卸载以及最终支持除通过 LLVM 编译之外的多个后端。 值得注意的是,WebAssembly 在演示之外的实际应用有限,主要用于云服务,例如用于速度和隔离的 Cloudflare、用于编辑的 Figma 以及用于未知目的的 Web3 计划。

相关文章

原文


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/



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!


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.


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.



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


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.


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.



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.


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



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.


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.



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



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 ???
联系我们 contact @ memedata.com