Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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?

share|improve this question
The general consensus is that new questions should not be tagged bugs until there is confirmation by the community, at large. – rcollyer Mar 5 '13 at 14:09
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. – Leonid Shifrin Mar 5 '13 at 14:10
Side comment: you should use f =, not f := , otherwise f will be recompiled every time it's called – Szabolcs Mar 5 '13 at 14:11
Not a bug, for the reason Leonid states. Top-level evaluation is not thread-safe, so these calls cannot be parallelized. – Oleksandr R. Mar 5 '13 at 14:15
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]. – Szabolcs Mar 5 '13 at 16:06
up vote 7 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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