2
$\begingroup$

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?

$\endgroup$
6
  • $\begingroup$ For those unfamiliar with LTemplate, IntTensorRef and RealTensorRef just wrap an MTensor of integer or real type. $\endgroup$
    – Szabolcs
    Commented Nov 9, 2018 at 13:03
  • 2
    $\begingroup$ I have no idea why this would be the case (to be frank, I am quite skeptical that the speed difference is due to using Mathematica). MTensor simply stores data contiguously. Are you sure that something doesn't go wrong when using Mathematica that prevents your C++ code from running at all? Some silent failure? $\endgroup$
    – Szabolcs
    Commented Nov 9, 2018 at 13:04
  • $\begingroup$ @Szabolcs Thank you for your kind answer. And yes, I am sceptical as well considering the speed differences. There is no doubt that the C++ code is indeed run within Mathematica since the calculations rely on output generated by this code. The C++ code (run itself) and the MMA code both produce the same output - with the only difference being time. Since C arrays should be contiguously stored as well the problem must lay somewhere different. Though, thank you for your help! :) $\endgroup$
    – pbx
    Commented Nov 9, 2018 at 13:59
  • 1
    $\begingroup$ @Szabolcs can 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$
    – b3m2a1
    Commented Nov 9, 2018 at 18:10
  • 1
    $\begingroup$ I would be very surprised if this is not due to a simple mistake on the OP's part. (I made such a mistake myself today and banged my head against the wall for an hour trying to figure out why a function is faster than a subroutine it calls. Well, of course it was not faster.) $\endgroup$
    – Szabolcs
    Commented Nov 9, 2018 at 19:17

0

Your Answer

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

Browse other questions tagged or ask your own question.