-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples-transform.cpp
More file actions
49 lines (42 loc) · 929 Bytes
/
examples-transform.cpp
File metadata and controls
49 lines (42 loc) · 929 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// A simple example demonstration how 'simdTransform()'
// can be used.
// [[Rcpp::depends(RcppNT2)]]
#include <RcppNT2.h>
using namespace RcppNT2;
#include <Rcpp.h>
using namespace Rcpp;
struct plus
{
template <class T>
T operator()(const T& lhs, const T& rhs) const
{
return lhs + rhs;
}
};
// [[Rcpp::export]]
NumericVector simd_add(NumericVector lhs, NumericVector rhs)
{
NumericVector result = no_init(lhs.size());
simdTransform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), plus());
return result;
}
// [[Rcpp::export]]
NumericVector cpp_add(NumericVector lhs, NumericVector rhs)
{
return lhs + rhs;
}
/***R
set.seed(123)
n <- 1024 * 1000
lhs <- rnorm(n)
rhs <- rnorm(n)
simd <- simd_add(lhs, rhs)
cpp <- cpp_add(lhs, rhs)
stopifnot(all.equal(simd, cpp))
library(microbenchmark)
microbenchmark(
simd = simd_add(lhs, rhs),
cpp = cpp_add(lhs, rhs),
R = lhs + rhs
)
*/