Thanks for pointing that out. I don't pretend to understand the reasons but that method seems to be much more responsive than the throw/catch method. It seems like the throw catch should be just as efficient, and should add very little overhead. It does not add overhead, but the R_CheckUserInterupt also does not appear to add significant overhead either. The throw method seems to take a very long time between when the interrupt is sent before the error is caught. Is there a way to speed up the throw/catch?<div>
<br></div><div>-Andrew<br><div><br></div><div><br><br><div class="gmail_quote">On Wed, Dec 8, 2010 at 3:42 PM, Davor Cubranic <span dir="ltr"><<a href="mailto:cubranic@stat.ubc.ca">cubranic@stat.ubc.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">How about just using R_CheckUserInterrupt()?<br>
<font color="#888888"><br>
Davor<br>
</font><div><div></div><div class="h5"><br>
<br>
On 2010-12-08, at 1:02 PM, Dirk Eddelbuettel wrote:<br>
<br>
><br>
> On 8 December 2010 at 13:41, Andrew Redd wrote:<br>
> | I have an MCMC chain that runs entirely in c++ with Rcpp. It sometimes runs<br>
> | for a vary long time and I want to interrupt it. Is there an efficient easy<br>
> | way to include catching an interupt signal and either aborting or returning<br>
> | results to that point? This might be understood well, if so I apologize.<br>
><br>
> Great question.<br>
><br>
> Here is a simple example that uses a standard C interrupt handler to set a<br>
> standard C++ exception to abort:<br>
><br>
> ----------------------------------------------------------------<br>
><br>
> #include <csignal><br>
> #include <Rcpp.h><br>
><br>
> void intHandler(int dummy=0) {<br>
> std::cerr << "In intHandler" << std::endl;<br>
> throw std::runtime_error("Interception caught");<br>
> }<br>
><br>
> RcppExport SEXP foo(void) {<br>
><br>
> try {<br>
><br>
> signal(SIGINT, intHandler);<br>
> signal(SIGKILL, intHandler);<br>
><br>
> while (true) {<br>
> sleep(1); // not doing much<br>
> }<br>
> return R_NilValue;<br>
><br>
> } catch(std::exception &ex) {<br>
> std::cerr << "In catch of std::exeception" << std::endl;<br>
> // here you insert some clenup<br>
> forward_exception_to_r(ex);<br>
><br>
> } catch(...) {<br>
> ::Rf_error("c++ exception (unknown reason)");<br>
> }<br>
><br>
> return R_NilValue;<br>
> }<br>
><br>
> ----------------------------------------------------------------<br>
><br>
> which I then quickly turned into a shared object, load and ran at the<br>
> command-line [1]<br>
><br>
> edd@max:/tmp/andrew$ PKG_CPPFLAGS=`r -e'Rcpp:::CxxFlags()'` PKG_LIBS=`r -e'Rcpp:::LdFlags()'` R CMD SHLIB int2throw.cpp<br>
> ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -fpic -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros -c int2throw.cpp -o int2throw.o<br>
> g++ -shared -o int2throw.so int2throw.o -L/usr/local/lib/R/site-library/Rcpp/lib -lRcpp -Wl,-rpath,/usr/local/lib/R/site-library/Rcpp/lib -L/usr/lib64/R/lib -lR<br>
> edd@max:/tmp/andrew$ r -e'dyn.load("int2throw.so"); .Call("foo")'<br>
> ^CIn intHandler<br>
> In catch of std::exeception<br>
> Error in cpp_exception(message = "Interception caught", class = "std::runtime_error") :<br>
> Interception caught<br>
> Execution halted<br>
> edd@max:/tmp/andrew$<br>
> edd@max:/tmp/andrew$<br>
><br>
><br>
> After lauching it from the one-line littler (r) expression, I pressed Ctrl-C<br>
> which then got us into the interrupt which threw the exception which lead to<br>
> the end.<br>
><br>
> In Rscript it looks a little more orderly on the unwind:<br>
><br>
> edd@max:/tmp/andrew$ Rscript -e 'dyn.load("int2throw.so"); .Call("foo")'<br>
> C-c C-cIn intHandler<br>
> In catch of std::exeception<br>
> Error in cpp_exception(message = "Interception caught", class = "std::runtime_error") :<br>
> Interception caught<br>
> Calls: .Call -> cpp_exception<br>
> Execution halted<br>
> edd@max:/tmp/andrew$<br>
><br>
><br>
> Hope this helps, Dirk<br>
><br>
><br>
> [1] Not sure why I didn't use inline today...<br>
><br>
><br>
> --<br>
> Dirk Eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a> | <a href="http://dirk.eddelbuettel.com" target="_blank">http://dirk.eddelbuettel.com</a><br>
</div></div><div><div></div><div class="h5">> _______________________________________________<br>
> Rcpp-devel mailing list<br>
> <a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<br>
</div></div></blockquote></div><br></div></div>