
Gpu.js – GPU Accelerated JavaScript - apo
http://gpu.rocks
======
graedus
Warning: this hard locked my MBP. More detail after I’m back at my machine.

Edit: System details

Retina Mid-2015 15" \- MJLT2LL/A - MacBookPro11,5 - A1398 - 2910

Firefox 65.0.1 (64-bit)

The hang occurred when running the benchmark with Texture Mode enabled. The
spinning gear froze after a few seconds, then the display went black and the
fan was maxed.

It was a pain in the ass to recover: my only option seemed to be to power down
and power back up again, at which point OSX helpfully remembered the state my
machine was in and dropped me back into the blacked-out display and maxed fan.
Some combination of NVRAM reset, SPC reset and safe mode was required to get
back in.

~~~
tambourine_man
This is nuts. There’s no way a webpage (or app for that matter) should be able
to crash an entire OS.

Something's gone very wrong with ours software.

~~~
tokyodude
Blame Apple. They have the worst graphics stack when it comes to stuff like
this.

Most GPUs are not pre-emptable (maybe that's changing?). Because they are not
pre-emptable if you give them something to do they will do it until finished.
There is no interrupting them, saving state, switching to something else like
with a CPU.

Microsoft realized this and so Microsoft built into the OS a timeout. If the
GPU is given something to do and doesn't come back in a few seconds then they
reset the GPU (like shut the power off and turn it back on, or rather send a
PCI reset). You need to be aware of this in Windows programming if you have
data you need on the GPU. Most games don't and can just re-upload their data
if told their data was ejected but some GPGPU calculation will at least get
told "sorry, GPU reset, start over"

Apple didn't implement anything like that in OSX (now MacOS) for the longest
time. The entire OS is built around the idea that all apps are well behaved
(LOL). If a single app gives the GPU 30minutes of work to then the GPU is
going to do 30 minutes of work and your option was only hold the power button.

They tried retrofitting in a timeout but AFAICT there's just too many parts of
the OS that expect no one will ever hog the GPU and it filters back to CPU
land when the CPU tries to do something (queue drawing some windows) and the
GPU doesn't respond for .5 a second, 1 second, 20 seconds, etc and stuff piles
up etc...

Linux had (has?) exactly the same problem. I don't know how much progress has
been made on either MacOS nor Linux to deal with this.

Now comes WebGL which you guys won't like but basically any page can give your
GPU something that takes a few minutes to run and your machine (if it's not
Windows) may crash. The mitigating factor is it doesn't help the bad guys.
It's not like they can steal your info. The most they can do is convince you
never to visit their site again so it's a mostly self correcting problem. The
hard spots are if the person making the site was targeting a top end GPU and
you visit with a low-end GPU.

There are really only 2 real solutions. The OSes need to handle the fact that
GPUs are non-preemptable or we need preemptable GPUs.

Some will argue get rid of WebGL. I'd argue the issue is mostly self
correcting and that Google Maps is pretty awesome and it gets that awesomeness
from being able to render with the GPU. There's plenty of other 2D and 3D
visualizations WebGL enables. So I'm happy to live with the self correcting
problem for the benefits while I wait for pre-emptable GPUs for the real fix.

This is actually one place where I'm surprised there are cloud GPUs since
they'd have the same issue. I've never used one but they must have some way of
being reset. They must also not be shared? Or you just have to accept that
some other process on the machine can completely hog the machine.

~~~
gammagoblin
>Linux had (has?) exactly the same problem. I don't know how much progress has
been made on either MacOS nor Linux to deal with this.

All my AMD GPUs have been issued GPU resets for years. I know that in 2013 my
Radeon 5870 (the card I had at the time) was issued soft resets, which was
logged in dmesg as a success. The problem with that is that whatever program
caused the GPU lockup would continue to run (unless it crashes, which is
frequently the case) until the kernel had reset the GPU so many times, the
only option left is a hard reset.

On Windows, the way GPU lockups are dealt with is also by resetting the GPU,
and it's the same there too, if it keeps locking up several times within a
certain time span the kernel panics and you either get a BSOD or a complete
system lockup.

------
peterkelly
I love this comment in the code:
[https://github.com/gpujs/gpu.js/blob/develop/src/backend/web...](https://github.com/gpujs/gpu.js/blob/develop/src/backend/web-
gl2/fragment-shader.js#L40)

    
    
        // Here be dragons!
        // DO NOT OPTIMIZE THIS CODE
        // YOU WILL BREAK SOMETHING ON SOMEBODY\'S MACHINE
        // LEAVE IT AS IT IS, LEST YOU WASTE YOUR OWN TIME

~~~
gubbrora
I don't love it because let's say I really did want to change it, it doesn't
tell me how much effort has previously gone into it and what happened. Maybe
it means 10h have been spent in vain maybe 10000h. If I want to try my luck
that's important.

------
jingwen
I've built an animated raytracer with an early version of this.
[http://raytracer.crypt.sg/](http://raytracer.crypt.sg/)

~~~
techsin101
How does one go about learning web gl

~~~
tomlagier
[https://webgl2fundamentals.org/](https://webgl2fundamentals.org/) is a good
resource, as is [https://github.com/stackgl/shader-
school](https://github.com/stackgl/shader-school)

------
na85
How can I block this so that websites aren't mining crypto on my system?

Is it a matter of blocking js entirely or is there a way to achieve a bit
finer granularity?

~~~
Etheryte
This paired with the recent web workers exploit [1] shows the direction
malicious and pwned sites are very quickly moving. I think a good solution
would be to implement resource control on domain level in browsers and give
the user the ability to set the preferred values.

[1] [https://www.zdnet.com/article/new-browser-attack-lets-
hacker...](https://www.zdnet.com/article/new-browser-attack-lets-hackers-run-
bad-code-even-after-users-leave-a-web-page/)

~~~
untog
That worker exploit doesn't make any sense, as written:

> Technically, Service Workers are an update to an older API called Web
> Workers. However, unlike web workers, a service worker, once registered and
> activated, can live and run in the page's background, without requiring the
> user to continue browsing through the site that loaded the service worker.

This isn't true. A service worker will be automatically shut down when it
isn't attached to a browsing context. Not immediately, but it definitely
doesn't support what the article describes.

~~~
Etheryte
This is covered in the article.

> The attack routine consists of registering a service worker [...] and then
> abusing the Service Worker SyncManager interface to keep the service worker
> alive after the user navigates away.

------
Twirrim
Firefox manages to be both significantly slower for CPU benchmark and
significantly faster for GPU, compared to Chrome.

The former gets me wondering what is going on with the javascript. Sure, v8 is
usually faster than Firefox, but not 8x faster.

Chrome GPU does it in 0.053s, where Firefox is hitting 0.005s, a tenth of the
time.

This is on a 2015 Mac Pro.

~~~
travisoneill1
This is anecdotal, but when I built a JS image segmentation tool I found that
FF was significantly faster than Chrome when working with the lower level
stuff like ArrayBuffer, etc. I assume they are using a lot of this here since
they are probably dealing with the WebGL API which needs to receive data as a
buffer.

~~~
blotter_paper
Maybe because FF started optimizing for asm.js earlier? Just an ignorant
guess.

~~~
masswerk
Chrome was always significantly slower with typed arrays. I remember early
benchmarks, where array buffers were actually slower than plain arrays (which
also aligned with my own tests).

------
turdnagel
I used this for a simple implementation of Conway's Game of Life:
[https://turdnagel.com/cells/](https://turdnagel.com/cells/)

------
dtrailin
I've used this for a project and works pretty well. What's pretty incredible
is that it was actually made a hackathon in Singapore. My main gripe I had
this the debugging errors is very difficult if they happen in the graphics
portion. Sometimes you will have to switch in the CPU mode which works
differently the GPU mode, which means the results can be different for unknown
reasons. Another annoyance (and this is more to do with web standards than
anything) is that because a Canvas data is represented as RGBA in a single
array any conversions to matrix form are very slow because any dimension past
a certain size fails in GPU.js.

------
numtel
GLSL isn't that much different syntax that Javascript. If you write it
directly you get the fun of using types. And WebGL2's support of GLSL ES 3.0
brings features that make it much more handy for compute operations like
bitwise operators. It's really not that complicated to write a compute
function without any kind of library. Just throw up 2 triangles that cover the
output and scale your canvas to the match the number of pixels you want to
each 32 bits of output.

See my proof of work generator for the Nano currency that implements the
Blake2B hash using these techniques: [https://github.com/numtel/nano-webgl-
pow](https://github.com/numtel/nano-webgl-pow)

Send data in as an array of integers on a uniform then receive data out as a
bitmap encoded how you please.

------
terrortrain
It would be interesting to compare some calculations between Gpu.js and
[https://js.tensorflow.org/](https://js.tensorflow.org/)

------
voltagex_
250x faster than CPU on the Samsung Note 9, but what's really interesting is
that it's 380x faster when I turn off all the power saving modes.

------
lost_my_pwd
Caution: this just hung Firefox for a ~30s and then my MBP rebooted.

~~~
redeemedfadi
Same here.

------
amelius
Why do we need such hacks to access basic compute infrastructure?

~~~
kllrnohj
The better question is why would you care? GPGPU has nearly no use outside of
HPC applications, and nobody in that world is going to bother trying to make
their supercomputer run Chrome.

Otherwise for anything "GPGPU" in usage it tends to really just be "GPU" \-
like applying filters to images. That's just running a shader on a texture,
it's something you can do quite naturally in WebGL already.

GPGPU had a lot of buzz a few years back, but it's basically just a niche
usage these days.

~~~
maeln
GPGPU is definitely not having "no use outside of HPC". Almost any modern game
engine that you use nowadays use GPGPU in some ways (for AI, Physics, some
terrain processing, ...). GPGPU is also insanely popular in modern AI these
days. Almost any AI framework has backend with CUDA or OpenCL. A lot of
Offline renderer also use GPGPU nowadays.

And this is just a few example that come to mind. The number of application
that benefit from high-latency, high-throughput computations are plenty. Why
do you think NVidia is still pushing and updating CUDA ?

> Otherwise for anything "GPGPU" in usage it tends to really just be "GPU"

It's like saying most turing-complete usage tend to be just "CPU". You can
absolutely do GPGPU with just "normal" GPU usage. After-all, what is an array
of data if not a texture ? A lot of processing are implemented this way: A
texture and a pixel shader. But it doesn't mean that it is not GPGPU. GPGPU
means that the GPU doesn't limit us to displaying polygons on the screen.

~~~
kllrnohj
> Almost any modern game engine that you use nowadays use GPGPU in some ways
> (for AI, Physics)

No, they don't. Those are failed applications. The round-trip time kills it,
along with the GPU being generally over-subscribed in the first place while
the CPU sits idle.

For some stuff like terrain sure, which is why compute shaders exist in
directx & vulkan. But it's a rather different usage in practice, and a very
different "how you program with it".

> Why do you think NVidia is still pushing and updating CUDA ?

Because they still sell Tesla cards to the HPC market? Have you not noticed
that the GTX cards massively cut CUDA capability because it didn't help games?

> GPGPU means that the GPU doesn't limit us to displaying polygons on the
> screen.

OK but that's clearly not what I was talking about? GPGPU in this context was
"run non-graphics math written in JS on the GPU". Similar to something like
CUDA. Not "here's an actual pixel shader, run it in WebGL". The _how_ matters
here.

> After-all, what is an array of data if not a texture ?

A texture is an array of color channels, and often swizzled by the driver.
They are not equivalent to a generic array of data, although if you squint you
can get close treating them as similar.

But textures are treated differently, which is why actual GPGPU systems exist,
because the hack of "treat a texture as an array of data" doesn't really work
well.

------
tyfon
Firefox on Arch Linux with a Vega 64 card + i7 4770k @ 3.5 ghz:

CPU: 0.955s ±0.5% GPU: 0.003s ±4.3% (295.22 times faster!)

Seems like decent speedup.

It's a bit scary to have a web page have access to my GPU memory though, how
does that work really? Does firefox clear the GPU memory before using it for
tasks like this?

~~~
politelemon
Similar (FF + Ubuntu 1804, GTX 1080Ti, i7 4790K):

CPU: 0.863s ±1.4%

GPU: 0.003s ±4.0% (279.34 times faster!)

I believe the library is using WebGL with buffers; browsers can make use of
hardware for rendering and this library simply takes advantage of that

------
sidcool
This is pretty cool. I don't know its practical applications. Honestly I don't
even care. It's just very nice to see such tinkering. Great job.

------
AnnoyingSwede
Debian 9 x86_64/NVIDIA Corporation GK107 [NVS 510] hung like never before
too.. Happend after clicking Enabled and running the test.

------
m3kw9
When will they have FPGA accelerated JavaScript?

~~~
theblackcat1002
this is a joke right?

------
anotherrobot
WARNING anyone trying. Crashed my MBP running 10.14.3 with FF 65.0.1. Lost
some of my work

------
lprd
Welp, this blew up my 2017 MBP (10.14.3). Hanged after about 5 seconds of
running.

------
throwaway66666
GPU 491 times faster. Something's iffy, but I love the idea.

------
KingFelix
I went to Plaid!

------
elipsey
Crashed my tab real nice.

------
angel_j
Tensorflow.js is pretty good.

------
ringaroll
Works fine in Chrome. Crashes Firefox.

------
FlowNote
Will this solve the problem of my linter running Babel and its hundred plugins
completely destroying my CPU just so I can get access to spreads?

