[Rcpp-commits] r2157 - pkg/Rcpp/inst/examples/ConvolveBenchmarks

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Sep 24 11:31:28 CEST 2010


Author: romain
Date: 2010-09-24 11:31:28 +0200 (Fri, 24 Sep 2010)
New Revision: 2157

Added:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.cpp
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.h
Modified:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
Log:
another version, using a header file. still faster

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 09:14:28 UTC (rev 2156)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-24 09:31:28 UTC (rev 2157)
@@ -16,6 +16,7 @@
 R CMD SHLIB convolve5_cpp.cpp
 R CMD SHLIB convolve8_cpp.cpp
 R CMD SHLIB convolve9_cpp.cpp
+R CMD SHLIB convolve10_cpp.cpp
 
 # call R so that we get an interactive session
 Rscript exampleRCode.r

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.cpp	2010-09-24 09:31:28 UTC (rev 2157)
@@ -0,0 +1,36 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// this version expands convolve8_cpp by making Vec mimic the structure of 
+// NumericVector. It peforms well, so this is is not the structure of 
+// NumericVector that is the problem. So what is it then ?
+//
+// could it be because NumericVector is in a different library than 
+// this code, so that operator[] is not inlined ?
+// 
+// clues: 
+// - http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka3538.html
+
+#include <Rcpp.h>
+
+#include "convolve10_cpp.h"
+
+RcppExport SEXP convolve10cpp(SEXP a, SEXP b){
+    Rcpp::NumericVector xa(a);
+    Rcpp::NumericVector xb(b);
+    int n_xa = xa.size() ;
+    int n_xb = xb.size() ;
+    int nab = n_xa + n_xb - 1;
+    Rcpp::NumericVector xab(nab);
+    
+    Vec vab(xab.begin()), va(xa.begin()), vb(xb.begin()) ;
+    
+    for (int i = 0; i < n_xa; i++)
+        for (int j = 0; j < n_xb; j++) 
+            vab[i + j] += va[i] * vb[j];
+
+    return xab ;
+}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve10cpp)
+

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.h
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.h	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve10_cpp.h	2010-09-24 09:31:28 UTC (rev 2157)
@@ -0,0 +1,27 @@
+
+class Cache{
+public:
+    typedef double& proxy ;
+    typedef double* iterator ;
+    
+    Cache( iterator data_) : data(data_){}
+    
+    inline proxy ref(int i){ return data[i] ; }
+    inline proxy ref(int i) const { return data[i] ; }
+            
+private:    
+    iterator data ;
+} ;
+
+class Vec {
+public:
+    typedef double& proxy ;
+    
+    Vec( double* data_ ) : cache(data_){}
+    inline proxy operator[]( int i){ return cache.ref(i) ; }
+    inline proxy operator[]( int i) const { return cache.ref(i) ; }
+    
+private:
+    Cache cache ;
+} ;
+

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 09:14:28 UTC (rev 2156)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-24 09:31:28 UTC (rev 2157)
@@ -15,6 +15,7 @@
 
 dyn.load("convolve8_cpp.so")
 dyn.load("convolve9_cpp.so")
+dyn.load("convolve10_cpp.so")
 
 ## now run each one once for comparison of results,
 ## and define test functions
@@ -28,6 +29,7 @@
 R_API_naive <- function(n,a,b) .Call("convolve7__loop", n, a, b)
 Rcpp_New_std_2 <- function(n,a,b) .Call("convolve8cpp__loop", n, a, b)
 Rcpp_New_std_3 <- function(n,a,b) .Call("convolve9cpp__loop", n, a, b)
+Rcpp_New_std_4 <- function(n,a,b) .Call("convolve10cpp__loop", n, a, b)
 
 
 v1 <- R_API_optimised(1L, a, b )
@@ -55,6 +57,7 @@
                 Rcpp_New_sugar(REPS,a,b),
                 Rcpp_New_std_2(REPS,a,b),
                 Rcpp_New_std_3(REPS,a,b),
+                Rcpp_New_std_4(REPS,a,b),
                 columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
                 order="relative",
                 replications=1)



More information about the Rcpp-commits mailing list