Major deviation from my original post

I realize that the long code I originally had was very discouraging to debug and did not attract any answers. So I have decided to revamp this question significantly (I hope this is acceptable). Please visit the edit history for older versions of this question.

Game Plan

I hypothesize that there are multiple errors in my script the is causing the kernel to crash. But since I can not identify them all at this time, I plan to identify one problem at a time, get solution, implement it in my code and repeat the process of identifying the next problem and posting it on this page, till all the issues are resolved.

Problem 1. The following code compiles fine:


  Max[0.,Min[(20.-p1) 5.-p2-p3,10.]-p4],RuntimeAttributes->{Listable}];


f=Compile[{},Module[{k0=Table[0.,{10}]}, ab[k0,k0,k0,k0,k0]],CompilationTarget->"C",

But when I execute f[], the kernel crashes. I am guessing, it is to do with mixing Scalars with Vectors in the a and b functions. But that is what I thought Listable takes care of. Any thoughts?

  • 2
    $\begingroup$ As soon as you revert to MainEvaluate compilation is of dubious use in any case, correct? Perhaps useful: Compiling more functions that don't call MainEvaluate./q/24595/131 $\endgroup$
    – Yves Klett
    Commented Jun 11, 2014 at 19:10
  • 2
    $\begingroup$ Here are two suggestions. (1) Add CompilationOptions -> {"InlineExternalDefinitions" -> True} (2) Define f2[x1_Integer, x2_Integer, x3_Integer] := f[x1, x2, x3] and use f2 in the call to NMinimize. Not sure if these will help but they should at least keep the compiled code from calling on the main evaluator. $\endgroup$ Commented Jun 11, 2014 at 19:16
  • $\begingroup$ @DanielLichtblau, Please look at edit 1. I actually implemented your suggestion 1. Implementing suggestion 2 takes it longer, even though same number of iterations are taken to converge. $\endgroup$
    – brama
    Commented Jun 11, 2014 at 19:38
  • $\begingroup$ Interesting, the link provided by Yves Klett has a rogue full-stop. mathematica.stackexchange.com/questions/24595/… $\endgroup$ Commented Jun 11, 2014 at 21:01
  • $\begingroup$ I get 20 seconds with my two suggestions, and it's still running after 2-3 minutes if I omit the second one. $\endgroup$ Commented Jun 11, 2014 at 21:31

1 Answer 1


Here's a workaround. Take it as evidence that compilation to C and Listable do not always get along.

a = Compile[{{p1, _Real, 0}, {p2, _Real, 0}}, 
   Max[0., Min[p1, 10.] - p2]
   (*,RuntimeAttributes -> Listable}*)];

b = Compile[{{p1, _Real, 0}, {p2, _Real, 0}, {p3, _Real, 0}, {p4, _Real, 0}}, 
   Max[0., Min[(20. - p1) 5. - p2 - p3, 10.] - p4]
   (*, RuntimeAttributes -> {Listable}*)];

ab = Compile[{{p1, _Real, 0}, {p2, _Real, 0}, {p3, _Real, 0},
              {p4, _Real, 0}, {p5, _Real, 0}}, 
      Min[a[p1, p5], b[p2, p3, p4, p5]],
      RuntimeAttributes -> {Listable},
      CompilationOptions -> {
        "InlineExternalDefinitions" -> True,
        "InlineCompiledFunctions" -> True (* necessary *)}

f = Compile[{}, 
   Module[{k0 = Table[0., {10}]}, ab[k0, k0, k0, k0, k0]], 
   CompilationTarget -> "C", 
   CompilationOptions -> {"InlineExternalDefinitions" -> True}];

The Listable attribute for a and b are not needed (in this simplified example) since ab is Listable. If you get rid of the attribute, the compiled functions can be inlined. If you don't set "InlineCompiledFunctions" -> True, the kernel crashes on me. You might want to look at


I take it that the CompiledFunctionCall in f actually is a call-back to the Virtual Machine (WVM née/né MVM), and not compiled to C. It may be that an issue in the communication between the C program and the VM leads to the crash. I do not know the answer to why the crash occurs.

  • $\begingroup$ This is insane. when I implemented your suggestion to remove Listable and inlined the upper layer functions in my code, the whole problem is resolved. $\endgroup$
    – brama
    Commented Jul 10, 2014 at 19:34
  • $\begingroup$ However, it will be interesting to get an insight into why the kernel crashed with the original formulation. $\endgroup$
    – brama
    Commented Jul 11, 2014 at 8:29
  • $\begingroup$ @brama Yes, it would. It might take someone with knowledge of how the internals work. $\endgroup$
    – Michael E2
    Commented Jul 11, 2014 at 12:45

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.