[Rcpp-commits] r2188 - in pkg/Rcpp/inst: . examples/ConvolveBenchmarks include include/Rcpp

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Sep 26 12:56:42 CEST 2010


Author: romain
Date: 2010-09-26 12:56:42 +0200 (Sun, 26 Sep 2010)
New Revision: 2188

Added:
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve13_cpp.cpp
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve14_cpp.cpp
   pkg/Rcpp/inst/include/Rcpp/Fast.h
Modified:
   pkg/Rcpp/inst/ChangeLog
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
   pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
   pkg/Rcpp/inst/include/Rcpp.h
Log:
added Rcpp::Fast for faster indexing, disambiguating the compiler, used in convolve14 

Modified: pkg/Rcpp/inst/ChangeLog
===================================================================
--- pkg/Rcpp/inst/ChangeLog	2010-09-26 07:56:02 UTC (rev 2187)
+++ pkg/Rcpp/inst/ChangeLog	2010-09-26 10:56:42 UTC (rev 2188)
@@ -1,5 +1,9 @@
 2010-09-25  Romain Francois <romain at r-enthusiasts.com>
 
+        * inst/include/Rpp/Fast.h: new helper class Rcpp::Fast that 
+
+2010-09-25  Romain Francois <romain at r-enthusiasts.com>
+
         * inst/include/Rcpp/routines.h: declare routines that are registered
 
         * src/Rcpp_init.c: register routines

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-26 07:56:02 UTC (rev 2187)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh	2010-09-26 10:56:42 UTC (rev 2188)
@@ -19,6 +19,7 @@
 R CMD SHLIB convolve10_cpp.cpp
 R CMD SHLIB convolve11_cpp.cpp
 R CMD SHLIB convolve12_cpp.cpp
+R CMD SHLIB convolve14_cpp.cpp
 
 # call R so that we get an interactive session
 Rscript exampleRCode.r

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve13_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve13_cpp.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve13_cpp.cpp	2010-09-26 10:56:42 UTC (rev 2188)
@@ -0,0 +1,27 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// This is a rewrite of the 'Writing R Extensions' section 5.10.1 example
+
+#include <Rcpp.h>
+
+template <typename T>
+T convolve( const T& a, const T& b ){
+    int na = a.size() ; int nb = b.size() ;
+    T out(na + nb - 1);
+    typename T::iterator iter_a(a.begin()), iter_b(b.begin()), iter_ab( out.begin() ) ;
+    
+    for (int i = 0; i < na; i++)
+        for (int j = 0; j < nb; j++) 
+            iter_ab[i + j] += iter_a[i] * iter_b[j];
+
+    return out ;
+}
+
+
+RcppExport SEXP convolve13cpp(SEXP a, SEXP b){
+    return convolve( Rcpp::NumericVector(a), Rcpp::NumericVector(b) ) ;
+}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve13cpp)
+

Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve14_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve14_cpp.cpp	                        (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve14_cpp.cpp	2010-09-26 10:56:42 UTC (rev 2188)
@@ -0,0 +1,25 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// This is a rewrite of the 'Writing R Extensions' section 5.10.1 example
+
+#include <Rcpp.h>
+
+using namespace Rcpp ;
+RcppExport SEXP convolve14cpp(SEXP a, SEXP b){
+    NumericVector xa(a), xb(b);
+    int n_xa = xa.size() ;
+    int n_xb = xb.size() ;
+    int nab = n_xa + n_xb - 1;
+    NumericVector xab(nab);
+    Fast<NumericVector> fa(xa), fb(xb), fab(xab) ;
+    
+    for (int i = 0; i < n_xa; i++)
+        for (int j = 0; j < n_xb; j++) 
+            fab[i + j] += fa[i] * fb[j];
+
+    return xab ;
+}
+
+#include "loopmacro.h"
+LOOPMACRO_CPP(convolve14cpp)
+

Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-26 07:56:02 UTC (rev 2187)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r	2010-09-26 10:56:42 UTC (rev 2188)
@@ -19,6 +19,7 @@
 dyn.load("convolve10_cpp.so")
 dyn.load("convolve11_cpp.so")
 dyn.load("convolve12_cpp.so" )
+dyn.load("convolve14_cpp.so" )
 
 ## now run each one once for comparison of results,
 ## and define test functions
@@ -31,23 +32,27 @@
 Rcpp_New_sugar <- function(n,a,b) .Call("convolve5cpp__loop", n, a, b)
 Rcpp_New_sugar_nona <- function(n,a,b) .Call("convolve11cpp__loop", n, a, b)
 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_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)
 Rcpp_New_std_it <- function(n,a,b) .Call("convolve12cpp__loop", n, a, b )
+Rcpp_New_std_Fast <- function(n,a,b) .Call("convolve14cpp__loop", n, a, b )
 
+
 v1 <- R_API_optimised(1L, a, b )
 v2 <- Rcpp_Classic(1L,a,b)[[1]]
 v3 <- Rcpp_New_std(1L, a, b)
 v4 <- Rcpp_New_ptr(1L, a, b)
 v5 <- Rcpp_New_sugar(1L, a, b )
 v7 <- R_API_naive(1L, a, b)
+v11 <- Rcpp_New_sugar_nona(1L, a, b)
 
 stopifnot(all.equal(v1, v2))
 stopifnot(all.equal(v1, v3))
 stopifnot(all.equal(v1, v4))
 stopifnot(all.equal(v1, v5))
 stopifnot(all.equal(v1, v7))
+stopifnot(all.equal(v1, v11))
 
 ## load benchmarkin helper function
 suppressMessages(library(rbenchmark))
@@ -60,10 +65,11 @@
                 Rcpp_New_ptr(REPS,a,b),
                 Rcpp_New_sugar(REPS,a,b),
                 Rcpp_New_sugar_nona(REPS,a,b),
-#                Rcpp_New_std_2(REPS,a,b),
+                Rcpp_New_std_2(REPS,a,b),
 #                Rcpp_New_std_3(REPS,a,b),
 #                Rcpp_New_std_4(REPS,a,b),
 		Rcpp_New_std_it(REPS,a,b),
+		Rcpp_New_std_Fast(REPS,a,b),
                 columns=c("test", "elapsed", "relative", "user.self", "sys.self"),
                 order="relative",
                 replications=1)

Added: pkg/Rcpp/inst/include/Rcpp/Fast.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/Fast.h	                        (rev 0)
+++ pkg/Rcpp/inst/include/Rcpp/Fast.h	2010-09-26 10:56:42 UTC (rev 2188)
@@ -0,0 +1,40 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+//
+// Fast.h: Rcpp R/C++ interface class library -- faster vectors (less interface)
+//
+// Copyright (C) 2010	Dirk Eddelbuettel and Romain Francois
+//
+// This file is part of Rcpp.
+//
+// Rcpp is free software: you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 2 of the License, or
+// (at your option) any later version.
+//
+// Rcpp is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with Rcpp.  If not, see <http://www.gnu.org/licenses/>.
+
+#ifndef Rcpp__Fast_h
+#define Rcpp__Fast_h
+
+namespace Rcpp {
+template <typename VECTOR>
+class Fast {
+public:
+    typedef typename VECTOR::stored_type value_type ;
+    Fast( VECTOR& v) : data( v.begin() ){} 
+    
+    inline value_type& operator[]( int i){ return data[i] ; }
+        
+private:
+    value_type* data ;
+    
+} ;
+}
+
+#endif 

Modified: pkg/Rcpp/inst/include/Rcpp.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp.h	2010-09-26 07:56:02 UTC (rev 2187)
+++ pkg/Rcpp/inst/include/Rcpp.h	2010-09-26 10:56:42 UTC (rev 2188)
@@ -58,6 +58,7 @@
 #include <Rcpp/DateVector.h>
 #include <Rcpp/Datetime.h>
 #include <Rcpp/DatetimeVector.h>
+#include <Rcpp/Fast.h>
 
 #ifdef RCPP_ENABLE_MODULES
 #include <Rcpp/Module.h>



More information about the Rcpp-commits mailing list