Skip to content

Commit ef3672f

Browse files
committed
initial work on SIMD wrappers
1 parent 6c48129 commit ef3672f

File tree

5 files changed

+90
-0
lines changed

5 files changed

+90
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
.DS_Store
55
src-i386/
66
src-x64/
7+
.Rprofile

examples/rcpp-simd-transform.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// [[Rcpp::depends(RcppParallel)]]
2+
#include <RcppParallel.h>
3+
#include <Rcpp.h>
4+
5+
using namespace Rcpp;
6+
using namespace RcppParallel;
7+
8+
struct add_two_impl {
9+
10+
template <typename T>
11+
T operator()(T data) const {
12+
return data + 2.0;
13+
}
14+
15+
template <typename T>
16+
T operator()(T lhs, T rhs) const {
17+
return lhs + rhs;
18+
}
19+
};
20+
21+
// [[Rcpp::export]]
22+
NumericVector simd_add_two(NumericVector data)
23+
{
24+
return simd::transform(data, add_two_impl());
25+
}
26+
27+
// [[Rcpp::export]]
28+
NumericVector simd_add(NumericVector lhs, NumericVector rhs)
29+
{
30+
return simd::transform(lhs, rhs, add_two_impl());
31+
}
32+
33+
struct plus {
34+
template <typename T>
35+
T operator()(const T& lhs, const T& rhs) const {
36+
return lhs + rhs;
37+
}
38+
};
39+
40+
// [[Rcpp::export]]
41+
double simd_sum(NumericVector data)
42+
{
43+
return simd::accumulate(data, 0.0, plus());
44+
}
45+
46+
/*** R
47+
data <- as.numeric(1:16)
48+
simd_add_two(data)
49+
simd_add(data, data)
50+
simd_sum(data)
51+
*/

inst/include/RcppParallel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
// TinyThread implementation
66
#include "RcppParallel/TinyThread.h"
77

8+
// SIMD Wrappers
9+
#include "RcppParallel/SIMD.h"
10+
811
// Use TBB only where it's known to compile and work correctly
912
// (NOTE: Windows TBB is temporarily opt-in for packages for
1013
// compatibility with CRAN packages not previously configured

inst/include/RcppParallel/RVector.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class RVector {
4242
inline const_iterator begin() const { return begin_; }
4343
inline const_iterator end() const { return end_; }
4444

45+
inline std::size_t size() const { return end_ - begin_; }
4546
inline std::size_t length() const { return end_ - begin_; }
4647

4748
inline T& operator[](std::size_t i) {

inst/include/RcppParallel/SIMD.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#ifndef RCPP_PARALLEL_SIMD_H
2+
#define RCPP_PARALLEL_SIMD_H
3+
4+
#include <boost/simd/sdk/simd/algorithm.hpp>
5+
6+
namespace RcppParallel {
7+
namespace simd {
8+
9+
template <class T, class UnaryFunction>
10+
T transform(const T& data, UnaryFunction f)
11+
{
12+
T result(data.size());
13+
boost::simd::transform(data.begin(), data.end(), result.begin(), f);
14+
return result;
15+
}
16+
17+
template <class T, class BinaryFunction>
18+
T transform(const T& lhs, const T& rhs, BinaryFunction f)
19+
{
20+
T result(lhs.size());
21+
boost::simd::transform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), f);
22+
return result;
23+
}
24+
25+
template <class T, class U, class F>
26+
U accumulate(const T& data, U init, F f)
27+
{
28+
return boost::simd::accumulate(data.begin(), data.end(), init, f);
29+
}
30+
31+
} // namespace simd
32+
} // namespace RcppParallel
33+
34+
#endif /* RCPP_PARALLEL_SIMD_H */

0 commit comments

Comments
 (0)