Skip to content

Commit 1c579e0

Browse files
committed
initial work on vignette for SIMD
1 parent ef3672f commit 1c579e0

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
src-i386/
66
src-x64/
77
.Rprofile
8+
inst/doc

DESCRIPTION

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@ Description: High level functions for doing parallel programming with 'Rcpp'.
1818
For example, the parallelFor() function can be used to convert the work of
1919
a standard serial "for" loop into a parallel one and the parallelReduce()
2020
function can be used for accumulating aggregate or other values.
21-
Suggests: Rcpp, RUnit
21+
Suggests:
22+
Rcpp,
23+
RUnit,
24+
knitr,
25+
rmarkdown
2226
SystemRequirements: GNU make, Windows: cmd.exe and cscript.exe, Solaris: g++ is required
2327
License: GPL-2
2428
URL: http://rcppcore.github.io/RcppParallel, https://github.com/RcppCore/RcppParallel
2529
Collate:
2630
'build.R'
2731
'hooks.R'
2832
'options.R'
29-
33+
VignetteBuilder: knitr

vignettes/simd-operations.Rmd

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

Comments
 (0)