Skip to main content

compile - Compiling more functions that don't call MainEvaluate


I would like to use Compile with functions defined outside Compile.


For example if I have the two basic functions F and G


F[x_] := x + 2
G[x_] := x

And I want to compute F[G[x]] in Compile


compiledFunction = Compile[{{x, _Real, 0}}, F[G[x]] ]


The resulting compiled function calls MainEvaluate


FastCompiledFunctionQ[function_CompiledFunction]:=
(
Needs["CompiledFunctionTools`"];
StringFreeQ[CompiledFunctionTools`CompilePrint@function,"MainEvaluate"]
)

compiledFunction // FastCompiledFunctionQ


This returns False, where FastCompiledFunctionQ[] checks if a compiled function calls MainEvaluate in order to use normal Mathematica code instead of compiled code, which is usually slower than compiled code.


Is there a way around this?


More generally I want to compile almost any numerical Mathematica code that calls user-defined functions (which themselves can call other user-defined functions) and doesn't use symbolic computations.



Answer



Yes there is a way to use functions that use external non compiled functions.


It uses the step function of Mr.Wizard defined in the post How do I evaluate only one step of an expression?, in order to recursively expand the code that we want to compile until it uses only functions that Mathematica can compile. The technique discussed in the post How to inject an evaluated expression into a held expression? is also used.


The function ExpandCode needs two functions that tell it when a function should be expanded and when a function should be evaluated during the expansion.


Using the functions defined below we can do to solve the question


code = Hold[F[G[x]]]
codeExpand = ExpandCode[code]

compiledFunction2 = Function[codeExpanded, Compile[{{x, _Real}}, codeExpanded], HoldFirst] @@ codeExpand

The $CriteriaFunction used here is that a function name (symbol) should have upper case letters only. Note the use of pure function with HoldFirst attribute in order to avoid evaluation leaks.


And now the function compiledFunction2 doesn't call MainEvaluate and returns the right answer


compiledFunction2 // FastCompiledFunctionQ
compiledFunction2[2]

A more streamlined version of this for common cases using a function defined below


CompileExpand[{{x, _Real}}, F[G[x]]] // FastCompiledFunctionQ


Here's the main code and some advice are after it.


SetAttributes[STEP, {Flat, OneIdentity, HoldFirst}];
STEP[expr_] :=
Module[{P},
P = (P = Return[# (*/. HoldForm[x_] :> Defer[STEP[x]]*), TraceScan] &) &;
TraceScan[P, expr, TraceDepth -> 1]
];

ReleaseAllHold[expr_,firstLevel_:0,lastLevel_:Infinity] := Replace[expr, (Hold|HoldForm|HoldPattern|HoldComplete)[e___] :> e, {firstLevel, lastLevel}, Heads -> True];


SetAttributes[EVALUATE,HoldFirst];
EVALUATE[x_]:=x;

$CriteriaFunction =Function[symbol,UpperCaseQ@SymbolName@symbol,HoldFirst];
$FullEvalFunction=Function[symbol,symbol===EVALUATE,HoldFirst];

ExpandCode[code_]:=ReleaseAllHold[Quiet@ExpandCodeAux[code,$CriteriaFunction ,$FullEvalFunction], 1];

ExpandCodeAux[code_,criteria_,fullEval_]:=
code /.

(expr:(x_Symbol[___]) /; criteria@x :>
RuleCondition[
If[fullEval@x,
expr
,
With[{oneStep = HoldForm@Evaluate@STEP@expr},
If[oneStep===HoldForm@expr,
oneStep
,
ExpandCodeAux[oneStep,criteria,fullEval]

]
]
]
]
);

SetAttributes[CompileExpand,HoldAll];
CompileExpand[variables_,code_,otherVariables___]:=
Function[
codeExpanded

,
Compile[variables,codeExpanded,otherVariables]
,
HoldFirst
] @@ ExpandCode[Hold@code];

FastCompiledFunctionQ[function_CompiledFunction]:=
(
Needs["CompiledFunctionTools`"];
StringFreeQ[CompiledFunctionTools`CompilePrint@function,"MainEvaluate"]

)

(*Example*)
SetAttributes[{F,G},HoldAll];
F[x_] := G[x] + 2;
G[x_] := 3 x;
compiledFunction3=CompileExpand[{{x,_Real}},F[G[x]]+EVALUATE@Range@5,CompilationTarget->"WVM"]
compiledFunction3//FastCompiledFunctionQ
compiledFunction3[2]


Comments



  • You need to specify the type of the variables even if they are Real numbers (for example {{x,_Real}} and not x for a function of just one variable).

  • Works with any type of values : DownValues, UpValues, SubValues ... which means you can use auxiliary functions that use the pattern matcher in their definitions instead of just already compiled functions that sometimes don't mix well together, and still be able to compile without calls to MainEvaluate.

  • A function to be expanded can contain calls to other functions that will be expanded.

  • In order to avoid problems the functions that you want to expand should have a HoldAll attribute (SetAttributes[F,HoldAll] for example).

  • Some useful Compile arguments for speed {Parallelization->True,RuntimeAttributes->{Listable},CompilationTarget->"WVM",RuntimeOptions->"Speed",CompilationOptions->{"ExpressionOptimization"->True,"InlineCompiledFunctions"->True,"InlineExternalDefinitions"->True}

  • If you call many times a same relatively big function (for example an interpolation function that you have written), it can be best to use a CompiledFunctionCall as explained in this answer in order to avoid an exploding code size after code expansion.

  • It can be best to avoid "ExpressionOptimization" when the CompilationTarget target is "WVM" (the compilation is faster, especially as the size of the expanded code can be very big). When it's "C" it's better to optimize the expression.



  • Numeric functions don't have a HoldAll attribute and pose problems if you want to expand a function that is inside a numeric one. You can use InheritedBlock to circumvent this. For example


    blockedFunctions={Abs,Log,Power,Plus,Minus,Times,Max,UnitStep,Exp};

    With[{blockedFunctions=blockedFunctions},
    Internal`InheritedBlock[blockedFunctions,
    SetAttributes[#,HoldAll]&/@blockedFunctions;
    ExpandCode[....]
    ]
    ]



  • If you use constant strings in your code you can replace them inside the code expanded with Real numbers (in order to return them together with a Real result in a List which will compile correctly, as you can't mix types in the result of a compiled function). For example


    Module[{cacheString,stringIndex=0.,codeExpandWithStringsReplaced},
    c:cacheString[s_] := c = ++stringIndex;
    codeExpandWithStringsReplaced=codeExpand/.s_String:>RuleCondition[cacheString[s]];
    ...
    ]

    And then cacheString can be used to convert the results returned by the compiled function back into strings. You need to access the keys and the values of cacheString, see here, or you can use and manipulate an Association in V10 instead of a symbol for cacheString.





  • A simple way to fully evaluate an expression during the code expansion is to enclose the expression between an EVALUATE function equal to the identity function.


    SetAttributes[EVALUATE,HoldFirst];
    EVALUATE[x_]:=x;
    $FullEvalFunction = Function[symbol,symbol===EVALUATE,HoldFirst];

    for example


    EVALUATE[Range@5]  

    EVALUATE also lets you avoid using With in order to insert constant parameters into the compiled code.





  • This code expansion can be used in order to have a fast compiled DSL (Domain Specific Language).




  • If you modify the $CriteriaFunction you can use Apply. This is an easier way to use Apply with Compile than in this question: Using Apply inside Compile.


    $CriteriaFunction=Function[symbol,UpperCaseQ@SymbolName@symbol||symbol===Apply,HoldFirst];  

    f=Compile[{{x,_Real}},F@@{x}]
    f // FastCompiledFunctionQ (*False*)


    f=CompileExpand[{{x,_Real}},F@@{x}]
    f // FastCompiledFunctionQ (*True*)

    You can also use this syntax instead of redefining $CriteriaFunction.


    f = CompileExpand[{{x, _Real}}, STEP[F @@ {x}]] 
    f // FastCompiledFunctionQ (*True*)


Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.