Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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:

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}];

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

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?

share|improve this question
2  
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 –  Yves Klett Jun 11 at 19:10
2  
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. –  Daniel Lichtblau Jun 11 at 19:16
    
@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. –  brama Jun 11 at 19:38
    
Interesting, the link provided by Yves Klett has a rogue full-stop. mathematica.stackexchange.com/questions/24595/… –  blochwave Jun 11 at 21:01
    
I get 20 seconds with my two suggestions, and it's still running after 2-3 minutes if I omit the second one. –  Daniel Lichtblau Jun 11 at 21:31

1 Answer 1

up vote 7 down vote accepted
+50

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

Needs["CompiledFunctionTools`"];
CompilePrint[f]

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.

share|improve this answer
    
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. –  brama Jul 10 at 19:34
    
However, it will be interesting to get an insight into why the kernel crashed with the original formulation. –  brama Jul 11 at 8:29
    
@brama Yes, it would. It might take someone with knowledge of how the internals work. –  Michael E2 Jul 11 at 12:45

Your Answer

 
discard

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.