According to this question LinearSolve
can be compiled. However, CompilePrint[]
shows a call to MainEvaluate[]
but no warning is generated. It appears that LinearSolve
is not compilable, given the presence of MainEvaluate[]
. But the lack of any warning is surprising. Something more subtle appears to be going on. Consider the following.
In[1]:= SetSystemOptions[
"CompileOptions" -> "CompileReportExternal" -> True];
In[2]:= << CompiledFunctionTools`
In[3]:= v2 = Compile[{{m, _Real, 2}, {v, _Real, 1}},
LinearSolve[m, v]
];
In[4]:= CompilePrint[v2]
Out[4]= "
2 arguments
3 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
T(R2)0 = A1
T(R1)1 = A2
Result = T(R1)2
1 T(R1)2 = MainEvaluate[ Hold[LinearSolve][ T(R2)0, T(R1)1]]
2 Return
"
There are no warnings generated, but I am not sure why there is a call to MainEvaluate[]
in the CompilePrint
.
There is a much clearer warning that compiling fails when one uses options within LinearSolve
while attempting to compile. Consider the following:
In[5]:= v3 = Compile[{{m, _Real, 2}, {v, _Real, 1}},
LinearSolve[m, v, Method -> "Cholesky"]
]
During evaluation of In[5]:= Compile::extscalar: Method->Cholesky cannot
be compiled and will be evaluated externally.
The result is assumed to be of type Integer. >>
During evaluation of In[5]:= Compile::exttensor: LinearSolve[m,v,Method->Cholesky]
cannot be compiled and will be evaluated externally.
The result is assumed to be a rank 2 tensor of type Real. >>
Also, CompilePrint[]
gives the following:
In[6]:= CompilePrint[v3]
Out[6]= "
2 arguments
1 Integer register
3 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
T(R2)0 = A1
T(R1)1 = A2
Result = T(R2)2
1 T(R2)2 = MainEvaluate[ Function[{m, v}, LinearSolve[m, v,
Method -> Cholesky]][ T(R2)0, T(R1)1]]
2 Return
"
Questions:
- If
LinearSolve
can't be compiled, why is there no warning in the default case? Is there something more subtle going on (e.g. some parts of the process are compiled)? - If yes, how can one use the
Method
option within theCompiled
function to ensure that what can be compiled actually is?
LinearSolve
can't be compiled. However, you normally don't get a warning becauseInternal`CompileValues
provides complete type information about it based on the type of the arguments. (Adding aMethod
subverts matching against the patterns given byInternal`CompileValues[LinearSolve]
, so you then get the warning.) Appearance of a function in the second list I gave does not necessarily mean it can be compiled without needingMainEvaluate
! $\endgroup$