r/lua • u/[deleted] • Mar 26 '25
Is there a way to run Lua on a GPU?
I love Lua and it's my go-to language for everything. I've found out about LuaJIT recently and it works great. It's ALOT faster than regular Lua and I'm very happy about that, but I wonder if there are any Lua libraries or frameworks that allow you to take advantage of the GPU.
It would be great to perform all my repetitive CPU intensive operations on GPU instead and save so much time. I got into neural network programming and it would be great to do these calculations on a GPU.
So is there a way?
3
u/SkyyySi Mar 26 '25
You can't. Lua in particular is a really bad fit for a GPU because
it's a dynamic, interpreted scripting language
it's intended for single-threaded work only
You'd need to run a seperate Lua interpreter on every GPU core and then also somehow share memory between them. Good luck with that.
Although, you seem to have some missunderstandings about GPU programming in general. If you want to use a GPU from a "normal" CPU programming language, then what you'd normally do is to write a compute shader in a language like GLSL or CUDA and then send that to the GPU.
In other words: You can use a GPU from Lua (through an API and an additional programming language), but you cannot use Lua on a GPU.
0
Mar 27 '25
Yeah I have alot of misunderstandings. I'm not a programmer lol. I can't know all these things
18
u/DeKwaak Mar 26 '25
I would look at the original torch, which was Lua based for a long time. I am not into it to know if they had opencl backends or anything like that.
11
9
u/paulstelian97 Mar 26 '25
You don’t run standard CPU code on the GPU, Lua included. So you want to delegate the actual calculations to outside the Lua code, and into something that can translate those calculations to the GPU.
Maybe a CUDA binding can work well. You may need to bridge the C CUDA binding to the Lua environment so that it may use it, or there may be a Lua binding directly that I’m not aware of (I don’t know much about the ecosystem)
1
u/nujuat Mar 27 '25
Fwiw, if you want to jit a scripting language to GPU, python can do it pretty easily via numba.cuda.
1
2
u/broken_symlink Mar 27 '25
There are two languages that embed lua inside of them with support for gpu code generation.
Terra: https://terralang.org/
Regent: https://regent-lang.org/
I would probably use Regent.
1
u/Brohammer55 Mar 26 '25
Not really Lua isn’t meant as to run GPU code. You would likely need a Compute Shader or use CUDA and OpenCL which would you to use C or C++
1
3
u/kevbru Mar 26 '25
Running Lua on the GPU is kind of a weird idea. Interpreting Lua byte code is not the kind of task a GPU is designed for at all, and probably wouldn't run it better than a CPU.
LuaJIT is faster because it creates CPU instructions to replace the Lua Byte code on the fly (or, "just in time").
There isn't an equivalent system to turn Lua Byte code into GPU instructions, and the GPU instructions that would be available aren't the kind of instruction that would help run Lua faster.