3
$\begingroup$

This question already has an answer here:

As evidenced in my last question there is much I do not know about how to write compilable functions.

The basics are clear: one must load the appropriate packages

Needs["CCompilerDriver``"]
Needs["CompiledFunctionTools`"]

and should check the availability of a suitable compiler:

CCompilers[Full]

and it is helpful to remind oneself of the functions that can be used that avoid MainEvaluate (for which I had not recorded the source but am now happy to credit to this thread where there is also other useful information)

Compile`CompilerFunctions[] // Sort
Internal`CompileValues[];(*to trigger \
auto-load*)ClearAttributes[Internal`CompileValues, ReadProtected];
syms = DownValues[Internal`CompileValues] /. 
   HoldPattern[
     Verbatim[HoldPattern][Internal`CompileValues[sym_]] :> _] :> sym;
Complement[syms, Compile`CompilerFunctions[]]

But, once all that is done must still remember that a compiled function definition expects its arguments to be of well-defined kinds - Integer, Real, Complex and n-dimensional tensors (matrices).

What is unclear, even after re-reading documentation, is the proper specification and use of temporary variables. It appears, for example, that these too must be of simple types and that the compiler does not automatically coerce them to compatible types. Then too there is the question of using e.g. GetElement even thought Part is a compilable function, and why Do may be better than Table.

Where can such additional information be found and what guidance would the community offer re writing functions for compilation?

$\endgroup$

marked as duplicate by C. E., MarcoB, m_goldberg, Henrik Schumacher, Alex Trounev Apr 1 at 12:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ I presume you are aware of this older thread as well? $\endgroup$ – J. M. is away Mar 30 at 9:43
  • $\begingroup$ @J.M.isslightlypensive I was but did not remember the details; thanks for the prompt. I have acknowledged that input. $\endgroup$ – Julian Moore Mar 30 at 9:52
  • 1
    $\begingroup$ "It appears, for example, that these too must be of simple types [...]". Of course. How else should it be possible to generate native C code? GetElement is faster than Part but also more fragile: If not used carefulle, it may cause a kernel crash (because, e.g., the operating system detects that Mathematica tries to access memory that it is not to allowed to have access to.). Table has to allocate memory for the output while Do does not. So Do is faster if no output is to be generated. $\endgroup$ – Henrik Schumacher Mar 30 at 12:26
  • 2
    $\begingroup$ @HenrikSchumacher Since we are not writing C code but Mathematica code the neophyte (me) does not necessarily know or think about how smart the cross-compiler is, so the simple types observation might benefit others as ignorant as I. The observations re GetElement & Tables are useful, thanks. $\endgroup$ – Julian Moore Mar 30 at 12:36
  • 1
    $\begingroup$ There is a guide to the compiler that discusses available types, among other things $\endgroup$ – Michael E2 Mar 31 at 2:08