Skip to content

Commit 05872c4

Browse files
committed
example using simd::abs
1 parent a39c5db commit 05872c4

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

examples/boost-simd-abssum.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// [[Rcpp::depends(RcppParallel)]]
2+
#include <RcppParallel.h>
3+
#include <Rcpp.h>
4+
using namespace Rcpp;
5+
6+
#include <boost/simd/sdk/simd/pack.hpp>
7+
#include <boost/simd/memory/allocator.hpp>
8+
#include <boost/simd/include/functions/sum.hpp>
9+
#include <boost/simd/include/functions/load.hpp>
10+
#include <boost/simd/include/functions/plus.hpp>
11+
#include <boost/simd/include/functions/multiplies.hpp>
12+
#include <boost/simd/include/functions/abs.hpp>
13+
14+
// [[Rcpp::export]]
15+
double simd_abssum(NumericVector x)
16+
{
17+
using boost::simd::pack;
18+
using boost::simd::load;
19+
using boost::simd::aligned_load;
20+
21+
typedef std::vector< double, boost::simd::allocator<double> > vector_type;
22+
typedef pack<double> packed_type;
23+
24+
vector_type data(x.begin(), x.end());
25+
26+
packed_type packed;
27+
double* it = &data[0];
28+
double* end = &data[0] + data.size();
29+
30+
while (it != end)
31+
{
32+
packed_type loaded = load<packed_type>(it);
33+
packed = packed + boost::simd::abs(loaded);
34+
it += packed_type::static_size;
35+
}
36+
37+
return sum(packed);
38+
}
39+
40+
/*** R
41+
n <- 1024 * 1000
42+
x <- rnorm(n)
43+
44+
library(microbenchmark)
45+
microbenchmark(
46+
R = sum(abs(x)),
47+
simd = simd_abssum(x)
48+
)
49+
*/

examples/boost-simd-dot.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ Value simd_dot(Value* first1, Value* last1, Value* first2)
2424
while (first1 != last1)
2525
{
2626
// Load current values from the datasets
27-
pack<Value> x1(first1);
28-
pack<Value> x2(first2);
27+
pack<Value> x1 = boost::simd::aligned_load<Value>(first1);
28+
pack<Value> x2 = boost::simd::aligned_load<Value>(first2);
2929

3030
// Computation
3131
tmp = tmp + x1 * x2;

0 commit comments

Comments
 (0)