Skip to content

Commit f781ff4

Browse files
committed
include most boost.SIMD headers in SIMD.h
1 parent 13fd5dc commit f781ff4

File tree

7 files changed

+42
-78
lines changed

7 files changed

+42
-78
lines changed

examples/boost-simd-abssum.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,6 @@
33
#include <Rcpp.h>
44
using namespace Rcpp;
55

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-
146
// [[Rcpp::export]]
157
double simd_abssum(NumericVector x)
168
{

examples/boost-simd-accumulate.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <Rcpp.h>
44
using namespace Rcpp;
55

6-
#include <boost/simd/sdk/simd/algorithm.hpp>
7-
86
struct plus
97
{
108
template <class T>

examples/boost-simd-dot.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
using namespace Rcpp;
55

66
// http://nt2.metascale.fr/doc/html/tutorials/processing_data_the_simd_way.html
7-
#include <boost/simd/memory/allocator.hpp>
8-
#include <boost/simd/sdk/simd/pack.hpp>
9-
#include <boost/simd/include/functions/sum.hpp>
10-
#include <boost/simd/include/functions/load.hpp>
11-
#include <boost/simd/include/functions/plus.hpp>
12-
#include <boost/simd/include/functions/multiplies.hpp>
137

148
template <typename Value>
159
Value simd_dot_impl(Value* first1, Value* last1, Value* first2)

examples/boost-simd-hello-world.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@
44
using namespace Rcpp;
55

66
// http://nt2.metascale.fr/doc/html/tutorials/simd_hello_world.html
7-
#include <boost/simd/sdk/simd/pack.hpp>
8-
#include <boost/simd/sdk/simd/io.hpp>
9-
#include <boost/simd/include/functions/splat.hpp>
10-
#include <boost/simd/include/functions/plus.hpp>
11-
#include <boost/simd/include/functions/multiplies.hpp>
12-
#include <iostream>
137

148
// [[Rcpp::export]]
159
void HelloWorld()

examples/boost-simd-transform.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include <Rcpp.h>
44
using namespace Rcpp;
55

6-
#include <boost/simd/sdk/simd/algorithm.hpp>
7-
86
struct plus
97
{
108
template <class T>

examples/simd-operations.Rmd

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,20 @@ vignette: >
1111

1212
## SIMD Basics
1313

14-
Modern CPU processors are built with new, extended instruction sets that optimize for certain operations. A class of these allow for vectorized operations, called Single Instruction / Multiple Data (SIMD) instructions. Although modern compilers will use these instructions when possible, it is often not possible for the compiler to reason about whether or not a particular block of code can be executed using SIMD instructions.
14+
Modern CPU processors are built with new, extended instruction sets that
15+
optimize for certain operations. A class of these allow for vectorized
16+
operations, called Single Instruction / Multiple Data (SIMD) instructions.
17+
Although modern compilers will use these instructions when possible, they are
18+
often unable to reason about whether or not a particular block of code can be
19+
executed using SIMD instructions.
1520

16-
`Boost.SIMD` is a C++ header-only library that makes it possible to explicitly request the use of SIMD instructions when possible, while falling back to regular scalar operations if not. `RcppParallel` wraps and exposes this library for use with R vectors.
21+
`Boost.SIMD` is a C++ header-only library that makes it possible to explicitly
22+
request the use of SIMD instructions when possible, while falling back to
23+
regular scalar operations if not. `RcppParallel` wraps and exposes this library
24+
for use with R vectors.
1725

18-
Here's a quick example of how we might compute the sum of elements in a vector, using `Boost.SIMD`.
26+
Here's a quick example of how we might compute the sum of elements in a vector,
27+
using `Boost.SIMD`.
1928

2029
```{r, engine='Rcpp'}
2130
// [[Rcpp::depends(RcppParallel)]]
@@ -25,7 +34,7 @@ Here's a quick example of how we might compute the sum of elements in a vector,
2534
using namespace RcppParallel;
2635
using namespace Rcpp;
2736
28-
// Define a functor -- a C++ class which defines a 'function call'
37+
// Define a functor -- a C++ class which defines a templated 'function call'
2938
// operator -- to perform the addition of two pieces of data.
3039
struct add_two {
3140
template <typename T>
@@ -36,9 +45,8 @@ struct add_two {
3645
3746
// [[Rcpp::export]]
3847
double simd_sum(NumericVector x) {
39-
// Use 'simd::accumulate', to sum our vector (by successively adding
40-
// up pairwise components of the vector)
41-
return simd::accumulate(x, 0.0, add_two());
48+
49+
return boost::simd::accumulate(x.begin(), x.end(), 0.0, add_two());
4250
}
4351
```
4452

@@ -60,24 +68,6 @@ Boost.SIMD provides two primary abstractions for the implementation of SIMD algo
6068

6169
These functions operate like their `std::` counterparts, but expect a functor with a templated call operator. By making the call operator templated, `Boost.SIMD` can generate code using its own optimized SIMD functions when appropriate, and fall back to a default implementation (based on the types provided) when not.
6270

63-
`Boost.SIMD` also provides a number of built in functions, which can be used effectively with `simd::accumulate()` or `simd::transform()`. For example, let's use SIMD instructions to compute the minimum:
64-
65-
```{r, engine='Rcpp'}
66-
// [[Rcpp::depends(RcppParallel)]]
67-
#include <RcppParallel.h>
68-
#include <Rcpp.h>
69-
70-
#include <boost/simd/arithmetic/functions/min.hpp>
71-
72-
using namespace RcppParallel;
73-
using namespace Rcpp;
74-
75-
// [[Rcpp::export]]
76-
NumericVector simd_min(NumericVector data) {
77-
return simd::transform(data, boost::simd::tag::min_());
78-
}
79-
```
80-
8171
## Using SIMD in an R Package
8272

8373
To build an R package that uses Boost.SIMD you need to make some modifications to the standard RcppParallel configuration:

inst/include/RcppParallel/SIMD.h

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
#ifndef RCPP_PARALLEL_SIMD_H
22
#define RCPP_PARALLEL_SIMD_H
33

4+
// Bring in the headers that automatically include the most
5+
// commonly used SIMD functions. We do this for a couple reasons:
6+
//
7+
// 1. Including each desired header 'by hand' can be tedious
8+
// for the user,
9+
//
10+
// 2. Certain headers depend on others in such a way that attempting
11+
// to include one without the other can lead to obscure compiler
12+
// errors.
13+
//
14+
// The downside is the increased compile time, but this is something that users
15+
// have implicitly accepted given the heavy use of template meta-programming in
16+
// Boost.SIMD and its dependent libraries.
417
#include <boost/simd/sdk/simd/algorithm.hpp>
518

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 */
19+
#include <boost/simd/memory/memory.hpp>
20+
#include <boost/simd/memory/allocator.hpp>
21+
22+
#include <boost/simd/arithmetic/arithmetic.hpp>
23+
24+
#include <boost/simd/bitwise/bitwise.hpp>
25+
26+
#include <boost/simd/boolean/boolean.hpp>
27+
28+
#include <boost/simd/reduction/reduction.hpp>
29+
30+
#include <boost/simd/swar/swar.hpp>
31+
32+
#endif /* RCPP_PARALLEL_SIMD_H */

0 commit comments

Comments
 (0)