Does Compilation option "InlineExternalDefinitions"/"InlineCompiledFunctions" lead to unnecessarily complicated nesting?

10

1

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.

QuantumDot

Posted 2015-10-04T18:05:54.117

Reputation: 18 597

You can inspect the compiled function f with CompilePrint and see how it behaves. – user21 – 2015-10-05T06:28:08.683

No answers