I am slightly puzzled by now and am not able to figure out what's going on inside of LibraryLink (used by LTemplate) to justify what I recognized:
I have C++ code that is way too extensive to be shown here, but that mainly only consists of if-else-constructs and *
and +
many terms. If I arrange my data as Mathematica Lists and use LTemplate
which uses LibraryLink
the data is handled as IntTensorRef
and RealTensorRef
. The LTemplate
code is compiled by clang
and — when executed in Mathematica — runs orders of magnitude faster than when I compile the C++ code directly with the same compiler and the same compiler options and replace IntTensorRef
and RealTensorRef
by pure C
arrays.
Since my code will run on HPC
clusters, using Mathematica is unfortunately not an option, so I am forced to use C++. As I have no idea what the underlying data structures are doing my question is: How do IntTensorRef
and RealTensorRef
achieve such a performance boost over a pure C
array? Do they prefetch? Do they increase the number of cache hits and lower cache misses?
IntTensorRef
andRealTensorRef
just wrap anMTensor
of integer or real type. $\endgroup$LTemplate
hook in to Mathematica's automatic threading/parallelization processes? That's the only thing I can think of that'd make Mathematica actually work faster. Or maybe it finds a way to remove some unnecessary casts that ended up being really expensive somehow. $\endgroup$