Skip to content

Commit e22d4e0

Browse files
committed
bring back 'mapReduce'; tweak examples, etc
1 parent 224c948 commit e22d4e0

15 files changed

+317
-143
lines changed

examples/boost-simd-abssum.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ double simd_abssum(NumericVector x)
1111
using boost::simd::load;
1212
using boost::simd::aligned_load;
1313

14-
typedef std::vector< double, boost::simd::allocator<double> > vector_type;
14+
typedef std::vector<double, boost::simd::allocator<double>> vector_type;
1515
typedef pack<double> packed_type;
1616

1717
vector_type data(x.begin(), x.end());

examples/boost-simd-accumulate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
2+
#define RCPP_PARALLEL_USE_SIMD
33
#include <RcppParallel.h>
44
#include <Rcpp.h>
55
using namespace Rcpp;

examples/boost-simd-capabilities.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
2+
#define RCPP_PARALLEL_USE_SIMD
33
#include <RcppParallel.h>
44
#include <Rcpp.h>
55
using namespace Rcpp;

examples/boost-simd-dot.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
2+
#define RCPP_PARALLEL_USE_SIMD
33
#include <RcppParallel.h>
44
#include <Rcpp.h>
55
using namespace Rcpp;

examples/boost-simd-hello-world.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
2+
#define RCPP_PARALLEL_USE_SIMD
33
#include <RcppParallel.h>
44
#include <Rcpp.h>
55
using namespace Rcpp;

examples/boost-simd-transform.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
2+
#define RCPP_PARALLEL_USE_SIMD
33
#include <RcppParallel.h>
44
#include <Rcpp.h>
55
using namespace Rcpp;

examples/rcpp-simd-transform.cpp

Lines changed: 0 additions & 74 deletions
This file was deleted.

examples/simd-operations.Rmd

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,27 @@ vignette: >
1111

1212
## SIMD Basics
1313

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.
20-
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.
25-
26-
Here's a quick example of how we might compute the sum of elements in a vector,
27-
using `Boost.SIMD`.
14+
Modern CPU processors are built with new, extended
15+
instruction sets that optimize for certain operations. A
16+
class of these allow for vectorized operations, called
17+
Single Instruction / Multiple Data (SIMD) instructions.
18+
Although modern compilers will use these instructions when
19+
possible, they are often unable to reason about whether or
20+
not a particular block of code can be executed using SIMD
21+
instructions.
22+
23+
`Boost.SIMD` is a C++ header-only library that makes it
24+
possible to explicitly request the use of SIMD instructions
25+
when possible, while falling back to regular scalar
26+
operations if not. `RcppParallel` wraps and exposes this
27+
library for use with R vectors.
28+
29+
Here's a quick example of how we might compute the sum of
30+
elements in a vector, using `Boost.SIMD`.
2831

2932
```{r, engine='Rcpp'}
3033
// [[Rcpp::depends(RcppParallel)]]
31-
#define RCPP_PARALLEL_USE_SIMD 1
34+
#define RCPP_PARALLEL_USE_SIMD
3235
#include <RcppParallel.h>
3336
#include <Rcpp.h>
3437
@@ -46,7 +49,6 @@ struct add_two {
4649
4750
// [[Rcpp::export]]
4851
double simd_sum(NumericVector x) {
49-
5052
return boost::simd::accumulate(x.begin(), x.end(), 0.0, add_two());
5153
}
5254
```
@@ -62,48 +64,65 @@ if (requireNamespace("microbenchmark", quietly = TRUE)) {
6264

6365
## SIMD Algorithms
6466

65-
Boost.SIMD provides two primary abstractions for the implementation of SIMD algorithms:
67+
`Boost.SIMD` provides two primary abstractions for the
68+
implementation of SIMD algorithms:
6669

6770
- `simd::accumulate()`, for vector -> scalar transformations, and
6871
- `simd::transform()`, for vector -> vector transformations.
6972

70-
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.
73+
These functions operate like their `std::` counterparts, but
74+
expect a functor with a templated call operator. By making
75+
the call operator templated, `Boost.SIMD` can generate code
76+
using its own optimized SIMD functions when appropriate, and
77+
fall back to a default implementation (based on the types
78+
provided) when not.
7179

7280
## Using SIMD in an R Package
7381

74-
To build an R package that uses Boost.SIMD you need to make some modifications to the standard RcppParallel configuration:
75-
76-
1. Add the **BH** package as a LinkingTo dependency
77-
2. Add C++11 as a SystemRequirement
82+
To build an R package that uses `Boost.SIMD`, you need to
83+
make some modifications to the standard RcppParallel
84+
configuration. Within the `DESCRIPTION` file of your package,
85+
you need to:
7886

87+
1. Add the **BH** package as a `LinkingTo` dependency, and
88+
2. Add `C++11` as a `SystemRequirement`
7989

8090
### Platform Compatibility
8191

82-
Note that Boost.SIMD requires a C++11 conformant compiler. This means that packages making use of SIMD features may not compile on platforms with older compilers including Windows and RedHat/CentOS Linux. You can however create a package that takes advantage of Boost.SIMD where available and falls back to a non-SIMD implementation elsewhere.
83-
84-
You can test for the availability of Boost.SIMD on a given platform using the `RCPP_PARALLEL_USE_SIMD` preprocessor variable. If the current compiler doesn't support C++11 (as determined by `__cplusplus <= 199711L`) the variable will be undefined (even if you defined it explicitly). This allows you to write code like this:
92+
`Boost.SIMD` requires a C++11 conformant compiler. This
93+
means that packages making use of SIMD features may not
94+
compile on platforms with older compilers, including Windows
95+
and RedHat/CentOS Linux. You can however create a package
96+
that takes advantage of `Boost.SIMD` where available and falls
97+
back to a non-SIMD implementation otherwise.
98+
99+
You can opt-in to the use of `Boost.SIMD` by defining the
100+
`RCPP_PARALLEL_USE_SIMD` macro before including
101+
`<RcppParallel.h>`, e.g.
102+
103+
#define RCPP_PARALLEL_USE_SIMD
104+
105+
You can test for the availability of `Boost.SIMD` on a given
106+
platform using the `RCPP_PARALLEL_USE_SIMD` preprocessor
107+
variable. If the current compiler doesn't support C++11 (as
108+
determined by `__cplusplus <= 199711L`) the variable will be
109+
undefined (even if you defined it explicitly). This allows
110+
you to write code like this:
85111

86112
```cpp
87-
#define RCPP_PARALLEL_USE_SIMD 1
113+
#define RCPP_PARALLEL_USE_SIMD
88114
#include <RcppParallel.h>
89115

90-
91116
#if RCPP_PARALLEL_USE_SIMD
92117

93-
#include <boost/simd/sdk/simd/pack.hpp>
94-
95118
IntegerVector transformDataImpl(IntegerVector x) {
96-
97119
// Implement with Boost.SIMD
98-
99120
}
100121

101122
#else
102123

103124
IntegerVector transformDataImpl(IntegerVector x) {
104-
105125
// Implement without Boost.SIMD
106-
107126
}
108127

109128
#endif
@@ -115,9 +134,14 @@ IntegerVector transformData(IntegerVector x) {
115134

116135
```
117136
118-
The two `transformDataImpl` functions have the same name, but only one will be compiled and linked based on whether the target platform supports Boost.SIMD.
137+
The two `transformDataImpl` functions have the same name,
138+
but only one will be compiled and linked based on whether
139+
the target platform supports `Boost.SIMD`.
119140
120-
Note that if you conditionally compile all uses of Boost.SIMD within your package then you can actually drop the `C++11` from `SystemRequirements` (it's no longer required as a result of your fallback implementation).
141+
Note that if you conditionally compile all uses of
142+
`Boost.SIMD` within your package, then you can drop
143+
the `C++11` from `SystemRequirements` (it's no longer
144+
required as a result of your fallback implementation).
121145
122146
123147

0 commit comments

Comments
 (0)