Group Abstract Group Abstract

Message Boards Message Boards

0
|
9.4K Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

RuntimeErrorHandler and Check[] in combination with CompiledFunction[]

Posted 12 years ago

Hi, I use Mathematica 10.0 and have following code where I want to stop the calculation if a CompiledFunction generates an error message:

offset = 4;(*3=ok, 4=error*)
func1 = Compile[{{v, _Real}}, Block[{r}, r = Sqrt[v - offset]], "RuntimeOptions" -> {"RuntimeErrorHandler" -> Function[Throw[$Failed]]}];
Off[General::stop];(*enable or disable, does not matter*)
Map[Catch[func1[#]] &, Range[10]]
On[General::stop];

Previous code works fine as long as no more than 3 messages are created, but starting from the 4th message only the RuntimeErrorHandler seems to be executed although the result should be valid. Switching off the "General::stop" message has no effect on the behaviour of the above code.

When I use Check[] instead of the RuntimeErrorHandler the behaviour is such that the first 3 messages are catched by Check[] but no further message is generated at all. The function returns complex numbers which must have been evaluated silently by non-compiled code.

offset = 5;(*3=ok, 4=ok(?), 5=complex results from non-compliled code?*)
func2 = Compile[{{v, _Real}}, Block[{r}, r = Sqrt[v - offset]]];
Off[General::stop];(*enable or disable,does not matter*)
Map[Check[func2[#], "error"] &, Range[10]]
On[General::stop];

A note why I want to catch messages from compiled code: I thought its a clever idea to use a compiled function with variables set to Reals which will generate an error whenever it starts to generate complex numbers - which are useless for me. In this case I would like to abort any evaluation immediately without loss of time (I am doing a computational heavy simulation).

What is your opinion? Is it a bug or do I do something wrong? Thanks a lot for your comments. Andreas

3 Replies

Hi Udo, thank you for your work-around!

I can confirm your observations (on Win7 64bit, Mathematica 10.0.2.0). Your work-around works like a charm.

It also works with a throwing error handler:

In[351]:= Remove[func27]
func27 = Compile[{{v, _Real}}, Sqrt[v - 27], "RuntimeOptions" -> {"RuntimeErrorHandler" -> Function[Throw[$Failed]]}];
If[True,(*if True unfreak the error handler, otherwise get always $Failed below*)
  Catch[func27[#]] & /@ {1, 27}
  ];
(*works if True above*)
Catch[func27[#]] & /@ Range[23, 30]
Out[354]= {$Failed, $Failed, $Failed, $Failed, 0., 1., 1.41421, 1.73205}

The work-around works also using Check:

In[313]:=Remove[func28]
func28 = Compile[{{v, _Real}}, Block[{r}, r = Sqrt[v - 28]]];
If[True,(*if True unfreak the error handler, otherwise get complex numbers below after 3x"error"*)
  func28[#] & /@ {1, 28}
  ];
Map[Check[func28[#], "error"] &, Range[24, 30]]    
Out[316]= {"error", "error", "error", "error", 0., 1., 1.41421}

I have sent a bug report to Wolfram Support. Andreas

As a work-around you can do this

In[6]:= Remove[func17]
func17 = Compile[{{v, _Real}}, Sqrt[v - 17], "RuntimeOptions" -> {"RuntimeErrorHandler" -> Function[$Failed]}];
Off[CompiledFunction::cfn]
(* unfreak the error handler *)
func17 /@ {1, 20};

In[10]:= func17 /@ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 21, 22, 23,24}
Out[10]= {$Failed, $Failed, $Failed, $Failed, $Failed, $Failed, \
$Failed, $Failed, $Failed, $Failed, $Failed, $Failed, $Failed, 2., \
2.23607, 2.44949, 2.64575}

to have it working like a charme. But that work-around seems to fail with a throwing error handler.

POSTED BY: Udo Krause
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard