Skip to content

Commit 57faf42

Browse files
committed
using 'simd::accumulate' with templated functor
1 parent 05872c4 commit 57faf42

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

examples/boost-simd-accumulate.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// [[Rcpp::depends(RcppParallel)]]
2+
#include <RcppParallel.h>
3+
#include <Rcpp.h>
4+
using namespace Rcpp;
5+
6+
#include <boost/simd/sdk/simd/algorithm.hpp>
7+
8+
struct plus
9+
{
10+
template <class T>
11+
T operator()(const T& lhs, const T& rhs) const
12+
{
13+
return lhs + rhs;
14+
}
15+
};
16+
17+
// [[Rcpp::export]]
18+
double simd_sum(NumericVector x)
19+
{
20+
return boost::simd::accumulate(
21+
x.begin(),
22+
x.end(),
23+
0.0,
24+
plus()
25+
);
26+
}
27+
28+
/***R
29+
n <- 1024 * 1000
30+
data <- rnorm(n)
31+
simd_sum(data)
32+
microbenchmark(R = sum(data), simd = simd_sum(data))
33+
*/

0 commit comments

Comments
 (0)