I write a code that creates a compiled function, and then call that function over and over to generate a list. I run this code on a remote server via a batch job, and will run several instances of it. Sometimes when I make changes to the code, I make a mistake, and inside the compiled function is an undefined variable, such that when the function is called I get the following error messages (repeated several times)

 CompiledFunction::cfse: Compiled expression w should be a machine-size complex number.

 CompiledFunction::cfex: Could not complete external evaluation at instruction 18; proceeding with uncompiled evaluation.

This causes massive memory usage (which puts me on the system administrator's bad side), and the results are garbage if since there was a mistake in the code. Is there any way to force the code to abort and quit the program rather than proceed with uncompiled evaluation?

  • 1
    $\begingroup$ you can also do fc = Compile[{{z, _Real}}, Sin[z]]; f[x_?NumericQ] := fc[x] $\endgroup$ – acl Jul 21 '14 at 20:07
  • $\begingroup$ But that only fixes it if the function argument isn't numeric. My mistake involved an undefined variable inside the expression I feed to Compile. $\endgroup$ – Jason B. Jul 21 '14 at 20:15
  • $\begingroup$ OK, I thought you were worried about passing the wrong argument, not actually forgetting to localise your variables. $\endgroup$ – acl Jul 21 '14 at 20:16

Adding this option for Compile

"RuntimeOptions" -> {"RuntimeErrorHandler" ->Function[Throw[$Failed]]}

will cause it to abort evaluation if any error messages come up. To more directly control the memory usage, and stay on the sysadmin's good side, wrap the call to the compiled function with MemoryConstrained, which causes it to abort if the memory goes above a certain threshold.

|improve this answer|||||

You can add

RuntimeOptions -> {"EvaluateSymbolically" -> False}

to your Compile function.
Consult RuntimeOptions for more details.

|improve this answer|||||
  • $\begingroup$ Using version 9,this doesn't seem to change anything. I added that option to Compile but I get the same error message. $\endgroup$ – Jason B. Jul 21 '14 at 20:00
  • $\begingroup$ I don't know why that didn't work, but I looked into RuntimeOptions and found this, which seems to work just fine: "RuntimeOptions" -> {"RuntimeErrorHandler" ->Function[Throw[$Failed]]} Thanks for your help $\endgroup$ – Jason B. Jul 21 '14 at 20:05
  • $\begingroup$ This doesn't remove the mistake in your code, but it should solve your memory issue. $\endgroup$ – Karsten 7. Jul 21 '14 at 20:06
  • 1
    $\begingroup$ @Karsten7., yes it does since it doesn't try to populate the list symbolically. $\endgroup$ – Jason B. Jul 21 '14 at 20:31
  • 3
    $\begingroup$ @JasonB Are you aware of SetSystemOptions["CompileOptions" -> "CompileReportExternal"->True]? If not, it might be useful in your case (it warns you at compile time if you reference an external variable). I might have misunderstood the problem you are trying to solve, though. $\endgroup$ – acl Jul 21 '14 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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