[Rcpp-devel] New self-contained OpenMP example (Was: Stack imbalance warning when using Rcpp and OpenMP)
Davor Cubranic
cubranic at stat.ubc.ca
Fri Aug 26 17:37:52 CEST 2011
On August 25, 2011 05:32:45 PM Dirk Eddelbuettel wrote:
> Long story short I just committed a new self-contained example to the Rcpp
> source which you can look at (and copy) via the URL
[...]
> The results bear this out. On my (standard i7, four cores hyperthreaded)
> box:
>
> edd at max:~$ r ~/svn/rcpp/pkg/Rcpp/inst/examples/OpenMP/OpenMPandInline.r
> Loading required package: methods
> test replications elapsed relative user.self sys.self
> 2 funOpenMP(z) 100 3.219 1.000000 25.26 0.07
> 3 funSerialRcpp(z) 100 9.030 2.805219 9.43 0.32
> 4 funSugarRcpp(z) 100 9.423 2.927307 9.06 0.35
> 1 funSerial(z) 100 9.601 2.982603 9.59 0.00
> edd at max:~$
>
[...]
> Rcpp sugar has no real leg up on manual loops, but is the shortest
> implementation in two lines. Looping over an Rcpp vector is a little faster
> than looping over a C++ STL vector (which incurs a copy).
I know you wanted to keep all code looking the same, but with std::transform
all serial code is very short:
serialStdAlgCode <- '
std::vector<double> x = Rcpp::as<std::vector< double > >(xs);
std::transform(x.begin(), x.end(), x.begin(), ::log);
return Rcpp::wrap(x);
'
funSerialStdAlg <- cxxfunction(signature(xs="numeric"), body=serialStdAlgCode,
plugin="Rcpp")
serialStdAlgRcppCode <- '
Rcpp::NumericVector x = Rcpp::NumericVector(xs);
std::transform(x.begin(), x.end(), x.begin(), ::log);
return x;
'
funSerialStdAlgRcpp <- cxxfunction(signature(xs="numeric"),
body=serialStdAlgRcppCode, plugin="Rcpp")
The results (without OpenMP because I'm currently on a single-core CPU):
test replications elapsed relative user.self sys.self
3 funSerialStdAlgRcpp(z) 20 4.236 1.000000 3.792 0.252
4 funSerialRcpp(z) 20 4.312 1.017941 3.792 0.272
5 funSugarRcpp(z) 20 4.537 1.071058 3.744 0.588
2 funSerialStdAlg(z) 20 5.514 1.301700 4.329 0.884
1 funSerial(z) 20 5.536 1.306893 4.480 0.808
Davor
More information about the Rcpp-devel
mailing list