Skip to content

Commit 72a47bd

Browse files
committed
automatically attempt to use Boost.SIMD (C++11 check will disable it on platforms where it's not supported)
1 parent db3517d commit 72a47bd

10 files changed

+21
-25
lines changed

R/build.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
# PKG_CXXFLAGS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "RcppParallel::CxxFlags()")
88
#
9-
CxxFlags <- function(simd = FALSE) {
9+
CxxFlags <- function(simd = TRUE) {
1010
cat(tbbCxxFlags(simd = simd))
1111
}
1212

@@ -40,7 +40,7 @@ inlineCxxPlugin <- function() {
4040
)
4141
}
4242

43-
tbbCxxFlags <- function(simd = FALSE) {
43+
tbbCxxFlags <- function(simd = TRUE) {
4444

4545
# request use of C++11 when possible
4646
flags <- "$(CXX1XSTD)"

examples/boost-simd-abssum.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/boost-simd-accumulate.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/boost-simd-capabilities.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/boost-simd-dot.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/boost-simd-hello-world.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/boost-simd-transform.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54
using namespace Rcpp;

examples/rcpp-simd-transform.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// [[Rcpp::depends(RcppParallel)]]
2-
#define RCPP_PARALLEL_USE_SIMD 1
32
#include <RcppParallel.h>
43
#include <Rcpp.h>
54

examples/simd-operations.Rmd

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@ Modern CPU processors are built with new, extended instruction sets that optimiz
1515

1616
`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.
1717

18-
Here's a quick example of how we might compute the sum of elements in a vector, using `Boost.SIMD`. Note that we've added `#define RCPP_PARALLEL_USE_SIMD 1` before including `RcppParallel.h` since SIMD features are not included by default.
18+
Here's a quick example of how we might compute the sum of elements in a vector, using `Boost.SIMD`.
1919

2020
```{r, engine='Rcpp'}
2121
// [[Rcpp::depends(RcppParallel)]]
22-
#define RCPP_PARALLEL_USE_SIMD 1
2322
#include <RcppParallel.h>
2423
#include <Rcpp.h>
2524
@@ -65,7 +64,6 @@ These functions operate like their `std::` counterparts, but expect a functor wi
6564

6665
```{r, engine='Rcpp'}
6766
// [[Rcpp::depends(RcppParallel)]]
68-
#define RCPP_PARALLEL_USE_SIMD 1
6967
#include <RcppParallel.h>
7068
#include <Rcpp.h>
7169
@@ -84,8 +82,9 @@ NumericVector simd_min(NumericVector data) {
8482

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

87-
1. Add definitions to `PKG_CXXFLAGS` to opt-in to Boost.SIMD
85+
1. Make sure you call the `CxxFlags` and `LdFlags` functions in `Makevars`
8886
2. Add the **BH** package as a LinkingTo dependency
87+
3. Add C++11 as a SystemRequirement
8988

9089
Here's the complete recipe for using RcppParallel with Boost.SIMD in an R package:
9190

@@ -94,7 +93,7 @@ Here's the complete recipe for using RcppParallel with Boost.SIMD in an R packag
9493
```yaml
9594
Imports: RcppParallel
9695
LinkingTo: RcppParallel, BH
97-
SystemRequirements: GNU make
96+
SystemRequirements: GNU make, C++11
9897
```
9998
10099
**NAMESPACE**
@@ -106,52 +105,56 @@ importFrom(RcppParallel, RcppParallelLibs)
106105
**src/Makevars**
107106

108107
```make
109-
PKG_CXXFLAGS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::CxxFlags(simd = TRUE)")
108+
PKG_CXXFLAGS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::CxxFlags()")
110109
PKG_LIBS += $(shell ${R_HOME}/bin/Rscript -e "RcppParallel::LdFlags()")
111110
```
112111

113112
**src/Makevars.win**
114113

115114
```make
116-
PKG_CXXFLAGS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \
117-
-e "RcppParallel::CxxFlags(simd = TRUE)")
118-
PKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \
119-
-e "RcppParallel::LdFlags()")
115+
PKG_CXXFLAGS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" \ -e "RcppParallel::CxxFlags()")
116+
PKG_LIBS += $(shell "${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" -e "RcppParallel::LdFlags()")
120117
```
121118

122119
### Platform Compatibility
123120

124121
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.
125122

126-
The result of specifying `simd = TRUE` in Makevars to to define the `RCPP_PARALLEL_USE_SIMD` preprocessor variable. This is interpreted as a *request* for Boost.SIMD. If the current compiler doesn't support C++11 (as determined by `__cplusplus <= 199711L`) the variable will be undefined. This allows you to write code like this:
123+
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. This allows you to write code like this:
127124

128125
```cpp
129-
#define RCPP_PARALLEL_USE_SIMD
130126
#include <RcppParallel.h>
131127

132128
#if RCPP_PARALLEL_USE_SIMD
133129

134130
#include <boost/simd/sdk/simd/pack.hpp>
135131

136-
IntegerVector transformData(IntegerVector x) {
132+
IntegerVector transformDataImpl(IntegerVector x) {
137133

138134
// Implement with Boost.SIMD
139135

140136
}
141137

142138
#else
143139

144-
IntegerVector transformData(IntegerVector x) {
140+
IntegerVector transformDataImpl(IntegerVector x) {
145141

146142
// Implement without Boost.SIMD
147143

148144
}
149145

150146
#endif
147+
148+
// [[Rcpp::export]]
149+
IntegerVector transformData(IntegerVector x) {
150+
return transformDataImpl(x);
151+
}
152+
151153
```
152154
153-
Note that the two functions have the same name (only one will be compiled and linked based on whether the target platform supports Boost.SIMD).
155+
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.
154156
157+
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).
155158
156159
157160

man/RcppParallelFlags.Rd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Compilation flags for RcppParallel
99
Output the compiler or linker flags required to build against RcppParallel.
1010
}
1111
\usage{
12-
CxxFlags(simd = FALSE)
12+
CxxFlags(simd = TRUE)
1313
LdFlags()
1414
RcppParallelLibs()
1515
}

0 commit comments

Comments
 (0)