9
$\begingroup$

Question: When an external (possibly complicated) definition f is used inside a compiled function with CompilationOption->{"InlineCompiledFunctions"->True}, does Mathematica really store the resulting code with every single instance of f replaced with its explicit definition inlined as the name of the option suggests?

For example, consider the compiled implementation of the following formula:

$f(x,a,b,c) = \text{Re }\text{Li}_2\big(\frac{(x-b^2+a^2)(a^2-c^2)}{x a^2-(b^2-a^2)(a^2-c^2)}\big) - \text{Re }\text{Li}_2\big(\frac{-(b^2-a^2)(a^2-c^2)}{x a^2-(b^2-a^2)(a^2-c^2)}\big)$

where $\text{Re }\text{Li}_2$ is the real part of the dilogarithm, whose compiled implementation is here. Now inspecting the result of compiling this secondary function,

f = Compile[{{x, _Real}, {a, _Real}, {b, _Real}, {c, _Real}}, 
    reLi2[((x - b^2 + a^2) (a^2 - c^2))/(x a^2 - (b^2 - a^2) (a^2 - c^2))]
     - reLi2[(-(b^2 - a^2) (a^2 - c^2))/(x a^2 - (b^2 - a^2) (a^2 - c^2))], 
   CompilationOptions -> {"InlineExternalDefinitions" -> True}]

it seems to me that indeed the way Mathematica stores the compiled result is by replacing both instances of reLi2 in f by the entire algorithm. Does this not require an excessive amount of memory to store such functions? Indeed, on my computer, it seems that it requires a whopping 1 MB to store this simple function:

ByteCount[f]

1 361 192

Follow up question: If this is the case, is it possible to compile functions such that outside definitions are compiled as subroutines, so that it requires less memory? It seems the most logical way to compile more complicated functions.

$\endgroup$
  • $\begingroup$ You can inspect the compiled function f with CompilePrint and see how it behaves. $\endgroup$ – user21 Oct 5 '15 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.