With LUA2C/LUA2VM you can speed up your applications which use a lot of LUA-Callbacks.
Both LUA2C and LUA2VM first parse your code. Then LUA2C creates a C file, compiles it with GCC and creates a library from it. This library is then dynamically linked to ug4. That means code which is compiled by LUA2C has the same speed as native C code.
- Warning
- The LUA2C compile/load process is not available on all platforms, especially clusters. For those, you can choose LUA2VM.
LUA2VM creates special code which can be interpreted with a special virtual machine on every machine/cluster.
Keep in mind that LUA2C/LUA2VM only implement a subset of LUA's functionality. Some restrictions are:
- Global variables: They work, but they are assume to be constant.
- All other variables have to be declared local (if they are not a paramter of the function).
- all variables are assumed to be doubles or convertible to doubles (like ints). That means: no strings, no tables. so if you have code like
if Modell == "1" then ...
you should replace it with something like if Modell == 1.0 then
.
- A lot of other fancy stuff like Closures are not possible.
- Calling other C functions is not possible at the moment.
- However, you can call other LUA functions (if they can be parsed, too). Those Subfunctions are restricted to returning exactly one double.
- All other functions may return an arbitrary number of doubles.
- build-in math functions from LUA: math.cos, math.sin, math.exp, math.abs, math.log, math.log10, math.sqrt, math.floor, math.ceil, math.pow, math.max, math.min, math.math.pi.
- control structures:
- if x then y elseif a then b else z end
- for a=start,stop do statement (break) end
- not possible: while
- comparison operators:
and or >= <= == ~=
- multi-line comments –[[ ]]– are not possible at the moment, howoever single-line (– comment) fworks
- Note
- If you function can not be parsed, LUA2C/LUA2VM won't be used. Instead, the normal LUA functionality is used for this function. This is a fallback strategy to keep things safe.
- Warning
- LUA2C/LUA2VM assumes at the moment that global variables are constant at the moment of the first parsing.
You enable LUA2C at the beginning of your script with
void EnableLUA2C(bool b)
Definition: info_commands.cpp:1224
and LUA2VM with
void EnableLUA2VM(bool b)
Definition: info_commands.cpp:1234
Note that you can't use both at the same time.
If you want to disable LUA2C/LUA2VM for some functions, use –LUACompiler:ignore
--LUACompiler:ignore
function Diffusion(x, y, t)
local a = MyCFunction(x, y)
return a, 0, 0, a
end
int local(bglp_vertex_descriptor p)
Definition: parallel_matrix.h:57
–LUACompiler:ignore
can be either on the line directly before the function or somewhere inside the function. Please make sure to use exactly –LUACompiler:ignore
.
For debugging, you can use
void SetDebugLevel(const char *strTag, int level)
Definition: misc_bridge.cpp:411
This way you are getting more information about successful/unsuccessful parsing. You can also set this to 5 and get even more information (generated C/VM Code and so on).
- Note
- Use LUA2VM on clusters and in parallel settings. Don't use LUA2C on clusters/parallel.