[Rcpp-devel] New self-contained OpenMP example (Was: Stack imbalance warning when using Rcpp and OpenMP)
Dirk Eddelbuettel
edd at debian.org
Fri Aug 26 17:56:13 CEST 2011
On 26 August 2011 at 08:37, Davor Cubranic wrote:
| 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:
Good thinking, I'll make that change!
| 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")
That's much nicer. And quicker. Nice :)
| 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
Thanks for the suggestion.
Dirk
--
Two new Rcpp master classes for R and C++ integration scheduled for
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php
More information about the Rcpp-devel
mailing list