[Bioc-devel] R_CheckUserInterrupt guidelines (was Re: R_CheckUserInterrupt in IRanges)
To 2nd what Seth's says, I think it is worth showing an explicit
example of how to check at regular intervals, e.g. for (i = 0; i < n;
i++) { if (i %% 100 == 0) R_CheckUserInterrupt(); ... }
/Henrik
On Mon, Jan 11, 2010 at 3:50 PM, Seth Falcon <sfalcon at fhcrc.org> wrote:
On 1/11/10 1:31 PM, Patrick Aboyoun wrote:
I just modified the C or Fortran code section of package guidelines to reflect this new entry: http://wiki.fhcrc.org/bioc/Package_Guidelines#c-or-fortran-code
This looks reasonable. It is worth noting, however, that calling R_CheckUserInterrupt is not entirely free. ?If you have a tight loop it might make more sense to check at intervals rather than each time through and the choice of when to check will depend very much on the computation. Here's an example using the inline package (code is at the bottom) with a silly double loop to compare checking vs not: With R_CheckUserInterrupt inner loop:
system.time(z <- funx(10000L, TRUE))
? user ?system elapsed
?1.310 ? 0.001 ? 1.313
No checking:
system.time(z <- funx(10000L, FALSE))
? user ?system elapsed
? 0.08 ? ?0.00 ? ?0.08
Now this may be completely unrealistic, but I suspect that a sensible take
away message is that we should be making sure our C code that loops does
call R_CheckUserInterrupt, but depending on the computation, not at every
loop iteration.
+ seth
## example code is below.
library("inline")
code <- "
int i, j, sum = 0, int n = INTEGER(max)[0];
int check_inter = LOGICAL(do_check)[0];
SEXP ans;
PROTECT(ans = allocVector(INTSXP, n));
for (i = 0; i < n; i++) {
? ?INTEGER(ans)[i] = i;
? ?for (j = 0; j < n; j++) {
? ? ? ?sum = i + j;
? ? ? ?if (check_inter) {
? ? ? ? ? ?R_CheckUserInterrupt();
? ? ? ?}
? ?}
}
UNPROTECT(1);
return ans;
"
funx <- cfunction(signature(max="integer", do_check="logical"), code)
system.time(z <- funx(10000L, TRUE))
system.time(z <- funx(10000L, FALSE))
--
Seth Falcon
Bioconductor Core Team | FHCRC
_______________________________________________ Bioc-devel at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/bioc-devel