13
$\begingroup$

I recently learnt that there is something called numba in python which compiles the codes into LLVM machine language and results in incredible speedups. I also learnt Mathematica 12.3 has introduced this LLVM machine language and compiled code in that. But the documentation provides nothing in regards to whether we can get actual speed ups in Mathematica using LLVM and more importantly how to implement it etc. My question is how to use this new LLVM feature incorporated in version 12.3 and where exactly should I expect to get speed ups if I do implement it. Does it help speed up more than CompilationTarget->C?

$\endgroup$
2
  • 3
    $\begingroup$ Just for context: numba cannot compile arbitrary Python code. It recognizes a subset that it can somewhat easily translate to C (well, LLVM, which was originally a C backend) and then compiles and runs that. $\endgroup$ Jun 17, 2021 at 8:53
  • $\begingroup$ @AlexReinking Yes I am aware of that actually, lot of numpy and scipy doesn't work with numba. But then basically its like cythonizing except the numba compiler is doing it? $\endgroup$ Jun 19, 2021 at 7:32

1 Answer 1

16
$\begingroup$

No expert on this, but will share what little I know about this. This is based on my understanding of the video

Wolfram Language Version 12.3 Sneak Peek with Jon McLoone

Here is a link

enter image description here

What happens now in 12.3 is that there are many Mathematica functions that automatically have Compile wrapped around them, behind the scene, when you call them. So first time you use them, they get compiled on the fly by LLVM to machine language, and hence become much faster when used. (Next time you use them, the already compiled code is now used).

The above video lists the following functions as automatically compiled. At 09:20 time

Functions that automatically gets LLVM compiled:

Around, FindGeometricTranasform, CoordinateBounds,
FindPeaks, PeakDetect, SquarWave, SawtoothWave,TraingleWave,
BrayCurtisDistance, EuclideanDistance, etc...

Also, Mathematica have now 25 LLVM enhanced data structure. This is an example the video shows of one LLVM enhanced ds called "ImmutableVector"

Mathematica graphics

You can see the time difference.

data = {};
AbsoluteTiming[Do[data = Append[data, 1], 50000];]
ds = CreateDataStructure["ImmutableVector"];
AbsoluteTiming[Do[ds = ds["Append", 2], {50000}];]

I did not see in the above video any example on how a user can now compile their own functions using LLVM to make them faster.

The only function that shows how to compile to LLVM is FunctionCompileExportString

gives a string of textual LLVM code obtained by compiling the function specification

I have not used any of these. You can also start at this page

create-llvm-code.html

The Wolfram Compiler can generate output in a variety of native machine code formats. The LLVM Compiler Infrastructure defines a standard intermediate representation, commonly referred to as LLVM IR.

But did not see how to use this inside Mathematica. This might be something for next version of Mathematica.

$\endgroup$
2
  • $\begingroup$ Surely this is all about reference.wolfram.com/language/ref/FunctionCompile.html ? I don't have a modern enough Mathematica to answer the question, but it was my understanding that FunctionCompile did LLVM compilation. $\endgroup$ Jun 17, 2021 at 16:18
  • $\begingroup$ Thanks for the detailed answer, the FunctionCompile by Patrick is what I was aware of , but in the documentation it was mainly focussed on storing the compiled function in a machine language file. The explanation of the video and the inbuilt functions are new. I will try to restructure codes using these functions and check speedup. Maybe Mathematica13 will have what I want. $\endgroup$ Jun 19, 2021 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.