7
$\begingroup$

Here is a sample compiled function:

f := Compile[{x}, ConstantArray[0., 10]; x^2, RuntimeAttributes -> {Listable},
        Parallelization->True,  CompilationTarget -> "C"]

Using with a single argument works fine:

f[10.0] = 100.0;

But using it in parallel gives an error message:

f[{10.0,20.0}]

CompiledFunction::pext: Instruction 1 in «original function» calls ordinary Mathematica code that can be evaluated on only one thread at a time. >>

The problem is in the ConstantArray[0.,10], which is not even being used. Replacing it with Table[0.,{10}] fixes the problem.

Any ideas?

$\endgroup$
11
  • 1
    $\begingroup$ The general consensus is that new questions should not be tagged bugs until there is confirmation by the community, at large. $\endgroup$
    – rcollyer
    Commented Mar 5, 2013 at 14:09
  • 4
    $\begingroup$ ConstantArray is not compilable, even though this is not very apparent from looking at the instructions (f[[6]]), but it can be seen with CompilePrint - there is a callback to MainEvaluate. This is what the message warns about. Use Table to get the fully compiled code. $\endgroup$ Commented Mar 5, 2013 at 14:10
  • 2
    $\begingroup$ Side comment: you should use f =, not f := , otherwise f will be recompiled every time it's called $\endgroup$
    – Szabolcs
    Commented Mar 5, 2013 at 14:11
  • 2
    $\begingroup$ Not a bug, for the reason Leonid states. Top-level evaluation is not thread-safe, so these calls cannot be parallelized. $\endgroup$ Commented Mar 5, 2013 at 14:15
  • 2
    $\begingroup$ A bit of extra info on CompilePrint: it will show you a human readable form of the byte code that Compile compiles to. The C code is generated from this byte code as well, so it's useful to look at it even if your target is C (it's easier to read than the C Mathematica generates). To use CompilePrint, you'll need to load <<CompiledFunctionTools` first, then CompilePrint[f]. $\endgroup$
    – Szabolcs
    Commented Mar 5, 2013 at 16:06

1 Answer 1

11
$\begingroup$

The answer, compiled [sic] from the comments above, arises from the following considerations:

  1. Not all functions can be compiled. This extremely interesting thread discusses this. ConstantArray can't be compiled; Table can.
  2. Functions that cannot be compiled will be dealt with via a call out to the top-level interpreter.
  3. The Mathematica interpreter is not thread-safe, or at least not reentrant.
  4. Your code asks for parallelization, which is accomplished in this case through multithreading.
  5. The Listable attribute causes the call of f[{1,2}] to be interpreted as {f[1],f[2]} which Mathematica attempts to parallelize.

The CompiledFunction::pext message is produced here to warn you that parallelization could not be accomplished as a result of the five facts mentioned above.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.