|
| 1 | +--- |
| 2 | +title: "SIMD Operations" |
| 3 | +author: "Kevin Ushey" |
| 4 | +date: "`r Sys.Date()`" |
| 5 | +output: rmarkdown::html_vignette |
| 6 | +vignette: > |
| 7 | + %\VignetteIndexEntry{SIMD Operations} |
| 8 | + %\VignetteEngine{knitr::rmarkdown} |
| 9 | + %\VignetteEncoding{UTF-8} |
| 10 | +--- |
| 11 | + |
| 12 | +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. |
| 13 | + |
| 14 | +`Boost.SIMD` is a (proposed) C++ header-only libary, 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. |
| 15 | + |
| 16 | +Here's a quick example of how we might compute the sum of elements in a vector, using `Boost.SIMD`. |
| 17 | + |
| 18 | +```{r, engine='Rcpp'} |
| 19 | +// [[Rcpp::depends(RcppParallel)]] |
| 20 | +#include <RcppParallel.h> |
| 21 | +#include <Rcpp.h> |
| 22 | +
|
| 23 | +using namespace RcppParallel; |
| 24 | +using namespace Rcpp; |
| 25 | +
|
| 26 | +// Define a functor -- a C++ class which defines a 'function call' |
| 27 | +// operator -- to perform the addition of two pieces of data. |
| 28 | +struct add_two { |
| 29 | + template <typename T> |
| 30 | + T operator()(const T& lhs, const T& rhs) { |
| 31 | + return lhs + rhs; |
| 32 | + } |
| 33 | +}; |
| 34 | +
|
| 35 | +// [[Rcpp::export]] |
| 36 | +double simd_sum(NumericVector x) { |
| 37 | + return simd::accumulate(x, 0.0, add_two()); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +```{r} |
| 42 | +data <- rnorm(1024 * 1000) |
| 43 | +all.equal(simd_sum(data), sum(data)) |
| 44 | +if (requireNamespace("microbenchmark", quietly = TRUE)) { |
| 45 | + microbenchmark::microbenchmark(sum(data), simd_sum(data)) |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +Boost.SIMD provides two primary abstractions for the implementation of SIMD algorithms: |
| 50 | + |
| 51 | +- `simd::accumulate()`, for vector -> scalar transformations, and |
| 52 | +- `simd::transform()`, for vector -> vector transformations. |
| 53 | + |
| 54 | +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. |
0 commit comments