# Why does compiling a function with ConstantArray give an error when used in parallel?

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?

-
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

1. Not all functions can be compiled. This extremely interesting thread discusses this. ConstantArray can't be compiled; Table can.
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.