[Rcpp-devel] tracking volatile bug

Romain François romain at r-enthusiasts.com
Tue Nov 18 16:39:25 CET 2014


> Le 18 nov. 2014 à 14:42, Serguei Sokol <serguei.sokol at gmail.com> a écrit :
> 
> Hi everybody,
> 
> I am facing a volatile bug which appears and disappears in
> identical calls on identical data.
> 
> My RcppArmadilla code is expm_cpp() (it is obtained by rex2arma tool
> which I have presented before, cf. attached file). It is compared and
> benchmarked vs its R counterpart expm.higham() (also attached). Both
> are computing matrix exponential by a same particular method.
> 
> The results are identical for R and Rcpp on small and medium sized
> matrices nxn n=10:100 but on large matrices (n > 800) various error messages
> can interrupt (or not) a normal run of expm_cpp().
> 
> Sometimes message says (in French) "type 'char' indisponible dans 'eval'",
> I suppose in English it must be "unimplemented type 'char' in 'eval'"
> I have seen (thanks to Google) a similar error message in the following snippet:
> 
> /* call this R command: source("FileName") */
>    int errorOccurred;
>    SEXP e = lang2(install("source"), mkString("FileName"));
>        /* mkChar instead of mkString would lead to this runtime error:
> 	 * Error in source(FileName) : unimplemented type 'char' in 'eval' */
>    R_tryEval(e, R_GlobalEnv, &errorOccurred);

e is not protected here. Does the problem go away if you protect it: 

SEXP e = PROTECT( lang2(install("source"), mkString("FileName"))) ;


Or more R/C++ idiomatically, which is both nicer and safer. 

Language e( "source", "FileName" ) ;

Romain


> which suggests that somewhere in Rcpp or RcppArmadillo there is
> a mkChar() call instead of mkString().
> 
> Other times, error message can say something like
> "argument type[1]='x' must be one of 'M','1','O','I','F' or 'E'"
> or "argument type[1]='character' must be a one-letter character string"
> This latter message is somewhat volatile per se. The part of message
> just after "type[1]=" can be 'character' (as above) or 'method' or 'ANY' etc.

That kind of problem usually is related to premature GC. 

> I have found these messages in the Matrix package
> https://r-forge.r-project.org/scm/viewvc.php/pkg/Matrix/src/Mutils.c?view=markup&root=matrix&pathrev=2614
> function "char La_norm_type(const char *typstr)"
> Seemingly, the argument *typstr is getting corrupted somewhere
> on the road.
> 
> It is useless to say that debugging is of no help here.
> If run in a debugger, the program stops normally with
> or without error messages cited above.
> 
> I have also tried to low the level of gcc optimization
> both in compiling R and the Rcpp code but it didn't help.
> 
> Anybody has an experience in tracking down similar cases?
> 
> This example can be run as:
> library(expm)
> library(Rcpp)
> source("expm_cpp.R")
> source("expm.higham.R")
> n=1000
> As=matrix(rnorm(n*n), n)
> stopifnot(diff(range(expm.higham(As, balancing=TRUE)-expm_cpp(As, balancing=TRUE))) < 1.e-14)
> 
> The last command may be run several times before an error shows up.
> 
> Cheers,
> Serguei.
> <expm_cpp.R><expm.higham.R>_______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel



More information about the Rcpp-devel mailing list