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.

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
1  
you can also do fc = Compile[{{z, _Real}}, Sin[z]]; f[x_?NumericQ] := fc[x] –  acl Jul 21 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. –  Jason B Jul 21 at 20:15
    
OK, I thought you were worried about passing the wrong argument, not actually forgetting to localise your variables. –  acl Jul 21 at 20:16

2 Answers 2

up vote 7 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. –  Jason B Jul 21 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 –  Jason B Jul 21 at 20:05
    
This doesn't remove the mistake in your code, but it should solve your memory issue. –  Karsten 7. Jul 21 at 20:06
1  
@Karsten7., yes it does since it doesn't try to populate the list symbolically. –  Jason B Jul 21 at 20:31
3  
@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 at 22:57

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.