Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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

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?

share|improve this question
you can also do fc = Compile[{{z, _Real}}, Sin[z]]; f[x_?NumericQ] := fc[x] – acl Jul 21 '14 at 20:07
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. – JasonB Jul 21 '14 at 20:15
OK, I thought you were worried about passing the wrong argument, not actually forgetting to localise your variables. – acl Jul 21 '14 at 20:16
up vote 8 down vote accepted

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.

share|improve this answer

You can add

RuntimeOptions -> {"EvaluateSymbolically" -> False}

to your Compile function.
Consult RuntimeOptions for more details.

share|improve this answer
Using version 9,this doesn't seem to change anything. I added that option to Compile but I get the same error message. – JasonB Jul 21 '14 at 20:00
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 – JasonB Jul 21 '14 at 20:05
This doesn't remove the mistake in your code, but it should solve your memory issue. – Karsten 7. Jul 21 '14 at 20:06
@Karsten7., yes it does since it doesn't try to populate the list symbolically. – JasonB Jul 21 '14 at 20:31
@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. – acl Jul 21 '14 at 22:57

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.