[Rcpp-devel] beginner questions on MinGW, Rcpp and inline packages

ravi rv15i at yahoo.se
Fri Jul 2 23:07:04 CEST 2010


Hi,
I am fairly familiar with R but a total newcomer to C++. Still, I can see the new possibilities that are opened up by the integration of R with C++. I am excited by the new developments in Rcpp and related packages in recent months. I 
have decided to at least begin by wetting my feet in these topics.
 
If this is not the forum for the questions that I 
am raising here, please let me know whom I should contact. 
 
Right now, I am on a PC with win XP. I would like to have some help in knowing what tools I need to get started. I have Rtools 2.11 installed. Is it 
sufficient to have this to access the C compilers? I even have a 
seperate installation of MinGW. The following commands show the versions of gcc and g++ (and g77 too) :
 
> system("gcc --version")
gcc.exe (GCC) 
4.2.1-sjlj (mingw32-2)

> system("g++ --version")
G__~1.EXE 
(GCC) 4.2.1-sjlj (mingw32-2)

> system("g77 --version")
GNU Fortran (GCC) 3.4.5 (mingw special)
Copyright (C) 2004 Free Software Foundation, Inc.

But I am confused by 
the MingW installation procedure. I tried to install MinGW with the most recent 
version (5.1.6) from SourceForge. But this led to a older version of gcc and g++ (v 3.4.5 if I remember correctly). And it was difficult to 
locate the MSYS installation file. My Norton Internet security program 
warned of security risks with the file that I located. I have now gone 
back to the installation with MinGW 5.1.4 and MSYS 1.0.1.
 
I 
tried to test my set of tools with the examples in the reference manual 
of the inline package. Except for the fortran bit, I find that I am able to run the commands. I am reproducing here the results from the R 
console. How do I solve the problem that I experience with Fortran code? What am I missing? Are the results OK for the other commands? How can I upgrade to the latest version of Mingw? I find some of the 
documentation on the Mingw site a little too difficult to follow? Can I 
use the mingw files on Rtools (and upgrade to Rtools 2.12)?
 
I first show the results with 
sessionInfo() and then show the results from running the inline 
examples.
########################################
> 
sessionInfo()
R version 2.11.0 (2010-04-22) 
i386-pc-mingw32 
locale:
[1] LC_COLLATE=Swedish_Sweden.1252  LC_CTYPE=Swedish_Sweden.1252    
LC_MONETARY=Swedish_Sweden.1252 LC_NUMERIC=C                   
[5] 
LC_TIME=Swedish_Sweden.1252    
attached base packages:
[1] 
grDevices datasets  splines   graphics  stats     tcltk     utils     
methods   base     
other attached packages:
 [1] 
RcppArmadillo_0.2.3 Rcpp_0.8.2          inline_0.3.5        
svSocket_0.9-48     TinnR_1.0.3         R2HTML_2.0.0        
Hmisc_3.8-0        
 [8] survival_2.35-8     rcom_2.2-1          
rscproxy_1.3-1     
loaded via a namespace (and not attached):
[1] cluster_1.12.3 grid_2.11.0    lattice_0.18-5 svMisc_0.9-57  
tools_2.11.0  
###########################################
> 
library(inline)
Warning message:
package 'inline' was built under R version 2.11.1 
> x <- as.numeric(1:10)
> n <- 
as.integer(10)
> code <- "
+ integer i
+ do 1 i=1, n(1)
+ 1 x(i) = x(i)**3
+ "
> cubefn <- 
cfunction(signature(n="integer", x="numeric"), code, 
convention=".Fortran")
ERROR(s) during compilation: source code 
errors or compiler configuration errors!
Program source:
  1: 
  2:        SUBROUTINE file678418be ( n, x )
  3:        INTEGER n(*)
  4:       DOUBLE PRECISION x(*)
  5: integer i
  6: do 1 i=1, n(1)
  7: 1 x(i) = x(i)**3
  8: 
  9:       RETURN
 10:       END
Error in compileCode(f, code, language, verbose) : 
  Compilation ERROR, 
function(s)/method(s) not created!
> cubefn(n, x)$x
Error: 
could not find function "cubefn"
> sigSq <- 
signature(n="integer", x="numeric")
> codeSq <- "
+ for (int i=0; i < *n; i++) {
+ x[i] = x[i]*x[i];
+ }"
> sigQd 
<- signature(n="integer", x="numeric")
> codeQd <- "
+ 
squarefn(n, x);
+ squarefn(n, x);
+ "
> fns <- cfunction( list(squarefn=sigSq, quadfn=sigQd),
+ list(codeSq, codeQd),
+ 
convention=".C")
> squarefn <- fns[["squarefn"]]
> quadfn <- fns[["quadfn"]]
> squarefn(n, x)$x
 [1]   1   4   9  16  25  36  49  64  81 100
> quadfn(n, x)$x
 [1]     1    16    
81   256   625  1296  2401  4096  6561 10000
> 
setCMethod(c("squarefn", "quadfn"), list(sigSq, sigQd),
+ 
list(codeSq, codeQd), convention=".C")
Warning message:
In 
getPackageName(environment(fdef)) :
  Created a package name, 
"2010-07-01 16:05:30", when none found
> squarefn(n, x)$x
 [1]   1   4   9  16  25  36  49  64  81 100
> quadfn(n, x)$x
 [1]     1    16    81   256   625  1296  2401  4096  6561 10000
> code 
<- "
+ SEXP res;
+ int nprotect = 0, nx, ny, nz, x, y;
+ 
PROTECT(res = Rf_duplicate(a)); nprotect++;
+ nx = 
INTEGER(GET_DIM(a))[0];
+ ny = INTEGER(GET_DIM(a))[1];
+ nz = 
INTEGER(GET_DIM(a))[2];
+ double sigma2 = REAL(s)[0] * REAL(s)[0], d2 ;
+ double cx = REAL(centre)[0], cy = REAL(centre)[1], *data, 
*rdata;
+ for (int im = 0; im < nz; im++) {
+ data = 
&(REAL(a)[im*nx*ny]); rdata = &(REAL(res)[im*nx*ny]);
+ for 
(x = 0; x < nx; x++)
+ for (y = 0; y < ny; y++) {
+ d2 = 
(x-cx)*(x-cx) + (y-cy)*(y-cy);
+ rdata[x + y*nx] = data[x + y*nx] * 
exp(-d2/sigma2);
+ }
+ }
+ UNPROTECT(nprotect);
+ return 
res;
+ "
> funx <- cfunction(signature(a="array", 
s="numeric", centre="numeric"), code)
> x <- 
array(runif(50*50), c(50,50,1))
> res <- funx(a=x, s=10, 
centre=c(25,15))
> head(res)
[1] 9.168592e-05 2.790754e-04 
6.208501e-05 7.655548e-04 7.868005e-04 3.288564e-04
> if 
(interactive()) image(res[,,1])
> setCMethod("funy", 
signature(a="array", s="numeric", centre="numeric"), code, verbose=TRUE)
Compilation argument:
 C:\PROGRA~1\R\R-211~1.0/bin/R CMD SHLIB file276e6f8.cpp 
g++ -I"C:/PROGRA~1/R/R-211~1.0/include"         -O2 -Wall  -c 
file276e6f8.cpp -o file276e6f8.o
g++ -shared -s -static-libgcc -o 
file276e6f8.dll tmp.def file276e6f8.o -LC:/PROGRA~1/R/R-211~1.0/bin -lR
Program source:
  1: #include <R.h>
  2: #include 
<Rdefines.h>
  3: #include <R_ext/Error.h>
  4: 
  
5: 
  6: 
  7: 
  8: extern "C" {
  9:   SEXP funy ( SEXP a, SEXP s, SEXP centre );
 10: }
 11: 
 12: SEXP funy ( SEXP a, 
SEXP s, SEXP centre ) {
 13: 
 14: SEXP res;
 15: int nprotect = 0, nx, ny, nz, x, y;
 16: PROTECT(res = Rf_duplicate(a)); 
nprotect++;
 17: nx = INTEGER(GET_DIM(a))[0];
 18: ny = 
INTEGER(GET_DIM(a))[1];
 19: nz = INTEGER(GET_DIM(a))[2];
 20: 
double sigma2 = REAL(s)[0] * REAL(s)[0], d2 ;
 21: double cx = 
REAL(centre)[0], cy = REAL(centre)[1], *data, *rdata;
 22: for (int 
im = 0; im < nz; im++) {
 23: data = &(REAL(a)[im*nx*ny]); 
rdata = &(REAL(res)[im*nx*ny]);
 24: for (x = 0; x < nx; x++)
 25: for (y = 0; y < ny; y++) {
 26: d2 = (x-cx)*(x-cx) + 
(y-cy)*(y-cy);
 27: rdata[x + y*nx] = data[x + y*nx] * 
exp(-d2/sigma2);
 28: }
 29: }
 30: UNPROTECT(nprotect);
 31: return res;
 32: 
 33:   warning("your C program does not return 
anything!");
 34:   return R_NilValue;
 35: }
The following 
methods are now defined:
Function: funy (package .GlobalEnv)
a="array", s="numeric", centre="numeric"
> res <- funy(x, 10, c(35,35))
> head(res)
[1] 1.031789e-11 3.835913e-11 1.042300e-11 1.569789e-10 
1.970555e-10 1.005979e-10
> if (interactive()) { x11(); 
image(res[,,1]) }
> fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ return ScalarReal( INTEGER(x)[0] * REAL(y)[0] ) ;
+ ' )
> fx( 2L, 5 )
[1] 10
> if( require( Rcpp ) ){
+ fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ return wrap( as<int>(x) * as<double>(y) ) ;
+ ', plugin = "Rcpp" )
+ fx( 2L, 5 )
+ }
Loading required package: Rcpp
[1] 10
Warning message:
package 'Rcpp' was built under R version 
2.11.1 
> fx(6L,7)
[1] 42
> if( require( RcppArmadillo ) 
){
+ fx <- cxxfunction( signature(x = "integer", y = "numeric" ) , '
+ int dim = as<int>( x ) ;
+ arma::mat z = 
as<double>(y) * arma::eye<arma::mat>( dim, dim ) ;
+ 
return wrap( arma::accu(z) ) ;
+ ', plugin = "RcppArmadillo" )
+ 
fx( 2L, 5 )
+ }
Loading required package: RcppArmadillo
[1] 10
Warning message:
package 'RcppArmadillo' was built under R version 2.11.1 
###########################################
I would appreciate any help that I can get.
Thanking you,
Ravi

PS : Instead of sending a long stream of text, I attempted to get the 
output in a file with the following commands :
setwd("M:\\CtoRf\\Rcpp")
sink("inlineExOut.txt") # send output to inlineExOut.txt
source("inlineExamples.r",echo=TRUE) #inlineExamples.r is the file with the script
sink()

This 
gave the following error :
Error in compileCode(f, code, language, 
verbose) : 
  Compilation ERROR, function(s)/method(s) not created!
Is there any workaround?




More information about the Rcpp-devel mailing list