Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ecacd21

Browse filesBrowse files
travis matrix as hinted by @eddelbuettel
1 parent e36536e commit ecacd21
Copy full SHA for ecacd21

10 files changed

+314
-345
lines changed

‎.travis.yml

Copy file name to clipboardExpand all lines: .travis.yml
+7-4Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ before_install:
44
- curl -OL http://raw.github.com/craigcitro/r-travis/master/scripts/travis-tool.sh
55
- chmod 755 ./travis-tool.sh
66
- ./travis-tool.sh bootstrap
7-
- ./travis-tool.sh github_package testthat
8-
- ./travis-tool.sh github_package Rcpp11/Rcpp11
9-
- ./travis-tool.sh github_package Rcpp11/attributes
10-
- ./travis-tool.sh r_binary_install Rcpp
117
- ./travis-tool.sh github_package RcppCore/RcppParallel
8+
- ./travis-tool.sh github_package testthat
9+
- if [ "$RCPP" = "Rcpp" ] then ./travis-tool.sh github_package RcppCore/Rcpp; fi
10+
- if [ "$RCPP" = "Rcpp11" ] then ./travis-tool.sh github_package Rcpp11/Rcpp11; fi
11+
- if [ "$RCPP" = "Rcpp11" ] then ./travis-tool.sh github_package Rcpp11/attributes; fi
1212

1313
script:
1414
- Rscript testthat.R
@@ -18,3 +18,6 @@ notifications:
1818
on_success: change
1919
on_failure: change
2020

21+
env:
22+
- RCPP=Rcpp
23+
- RCPP=Rcpp11

‎testthat.R

Copy file name to clipboardExpand all lines: testthat.R
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,16 @@ require(methods)
22
require(RcppParallel)
33
require(testthat)
44

5+
RCPP <- Sys.getenv( "RCPP" )
6+
if( RCPP == "Rcpp" ){
7+
message( "testing against Rcpp" )
8+
require(Rcpp)
9+
} else if( RCPP == "Rcpp11" ){
10+
message( "testing against Rcpp11" )
11+
require(attributes)
12+
} else {
13+
stop( "Rcpp implementation not setup, please set the $RCPP environment variable" )
14+
}
15+
516
test_dir("testthat")
617

‎testthat/cpp/distance.cpp

Copy file name to clipboard
+128Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
#include <cmath>
5+
#include <algorithm>
6+
7+
// generic function for kl_divergence
8+
template <typename InputIterator1, typename InputIterator2>
9+
inline double kl_divergence(InputIterator1 begin1, InputIterator1 end1,
10+
InputIterator2 begin2) {
11+
12+
// value to return
13+
double rval = 0;
14+
15+
// set iterators to beginning of ranges
16+
InputIterator1 it1 = begin1;
17+
InputIterator2 it2 = begin2;
18+
19+
// for each input item
20+
while (it1 != end1) {
21+
22+
// take the value and increment the iterator
23+
double d1 = *it1++;
24+
double d2 = *it2++;
25+
26+
// accumulate if appropirate
27+
if (d1 > 0 && d2 > 0)
28+
rval += std::log(d1 / d2) * d1;
29+
}
30+
return rval;
31+
}
32+
33+
// helper function for taking the average of two numbers
34+
inline double average(double val1, double val2) {
35+
return (val1 + val2) / 2;
36+
}
37+
38+
// [[Rcpp::export]]
39+
NumericMatrix rcpp_js_distance(NumericMatrix mat) {
40+
41+
// allocate the matrix we will return
42+
NumericMatrix rmat(mat.nrow(), mat.nrow());
43+
44+
for (int i = 0; i < rmat.nrow(); i++) {
45+
for (int j = 0; j < i; j++) {
46+
47+
// rows we will operate on
48+
NumericMatrix::Row row1 = mat.row(i);
49+
NumericMatrix::Row row2 = mat.row(j);
50+
51+
// compute the average using std::tranform from the STL
52+
std::vector<double> avg(row1.size());
53+
std::transform(row1.begin(), row1.end(), // input range 1
54+
row2.begin(), // input range 2
55+
avg.begin(), // output range
56+
average); // function to apply
57+
58+
// calculate divergences
59+
double d1 = kl_divergence(row1.begin(), row1.end(), avg.begin());
60+
double d2 = kl_divergence(row2.begin(), row2.end(), avg.begin());
61+
62+
// write to output matrix
63+
rmat(i,j) = std::sqrt(.5 * (d1 + d2));
64+
}
65+
}
66+
67+
return rmat;
68+
}
69+
70+
// [[Rcpp::depends(RcppParallel)]]
71+
#include <RcppParallel.h>
72+
using namespace RcppParallel;
73+
74+
struct JsDistance : public Worker {
75+
76+
// input matrix to read from
77+
const RMatrix<double> mat;
78+
79+
// output matrix to write to
80+
RMatrix<double> rmat;
81+
82+
// initialize from Rcpp input and output matrixes (the RMatrix class
83+
// can be automatically converted to from the Rcpp matrix type)
84+
JsDistance(const NumericMatrix mat, NumericMatrix rmat)
85+
: mat(mat), rmat(rmat) {}
86+
87+
// function call operator that work for the specified range (begin/end)
88+
void operator()(std::size_t begin, std::size_t end) {
89+
for (std::size_t i = begin; i < end; i++) {
90+
for (std::size_t j = 0; j < i; j++) {
91+
92+
// rows we will operate on
93+
RMatrix<double>::Row row1 = mat.row(i);
94+
RMatrix<double>::Row row2 = mat.row(j);
95+
96+
// compute the average using std::tranform from the STL
97+
std::vector<double> avg(row1.length());
98+
std::transform(row1.begin(), row1.end(), // input range 1
99+
row2.begin(), // input range 2
100+
avg.begin(), // output range
101+
average); // function to apply
102+
103+
// calculate divergences
104+
double d1 = kl_divergence(row1.begin(), row1.end(), avg.begin());
105+
double d2 = kl_divergence(row2.begin(), row2.end(), avg.begin());
106+
107+
// write to output matrix
108+
rmat(i,j) = sqrt(.5 * (d1 + d2));
109+
}
110+
}
111+
}
112+
};
113+
114+
// [[Rcpp::export]]
115+
NumericMatrix rcpp_parallel_js_distance(NumericMatrix mat) {
116+
117+
// allocate the matrix we will return
118+
NumericMatrix rmat(mat.nrow(), mat.nrow());
119+
120+
// create the worker
121+
JsDistance jsDistance(mat, rmat);
122+
123+
// call it with parallelFor
124+
parallelFor(0, mat.nrow(), jsDistance);
125+
126+
return rmat;
127+
}
128+

‎testthat/cpp/innerproduct.cpp

Copy file name to clipboard
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
#include <algorithm>
5+
6+
// [[Rcpp::export]]
7+
double innerProduct(NumericVector x, NumericVector y) {
8+
return std::inner_product(x.begin(), x.end(), y.begin(), 0.0);
9+
}
10+
11+
// [[Rcpp::depends(RcppParallel)]]
12+
#include <RcppParallel.h>
13+
using namespace RcppParallel;
14+
15+
struct InnerProduct : public Worker
16+
{
17+
// source vectors
18+
const RVector<double> x;
19+
const RVector<double> y;
20+
21+
// product that I have accumulated
22+
double product;
23+
24+
// constructors
25+
InnerProduct(const NumericVector x, const NumericVector y)
26+
: x(x), y(y), product(0) {}
27+
InnerProduct(const InnerProduct& innerProduct, Split)
28+
: x(innerProduct.x), y(innerProduct.y), product(0) {}
29+
30+
// process just the elements of the range I have been asked to
31+
void operator()(std::size_t begin, std::size_t end) {
32+
product += std::inner_product(x.begin() + begin,
33+
x.begin() + end,
34+
y.begin() + begin,
35+
0.0);
36+
}
37+
38+
// join my value with that of another InnerProduct
39+
void join(const InnerProduct& rhs) {
40+
product += rhs.product;
41+
}
42+
};
43+
44+
// [[Rcpp::export]]
45+
double parallelInnerProduct(NumericVector x, NumericVector y) {
46+
47+
// declare the InnerProduct instance that takes a pointer to the vector data
48+
InnerProduct innerProduct(x, y);
49+
50+
// call paralleReduce to start the work
51+
parallelReduce(0, x.length(), innerProduct);
52+
53+
// return the computed product
54+
return innerProduct.product;
55+
}
56+

‎testthat/cpp/sum.cpp

Copy file name to clipboard
+49Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <Rcpp.h>
2+
#include <RcppParallel.h>
3+
4+
// [[Rcpp::depends(RcppParallel)]]
5+
using namespace RcppParallel;
6+
using namespace Rcpp;
7+
8+
struct Sum : public Worker
9+
{
10+
// source vector
11+
const RVector<double> input;
12+
13+
// accumulated value
14+
double value;
15+
16+
// constructors
17+
Sum(const NumericVector input) : input(input), value(0) {}
18+
Sum(const Sum& sum, Split) : input(sum.input), value(0) {}
19+
20+
// accumulate just the element of the range I have been asked to
21+
void operator()(std::size_t begin, std::size_t end) {
22+
value += std::accumulate(input.begin() + begin, input.begin() + end, 0.0);
23+
}
24+
25+
// join my value with that of another Sum
26+
void join(const Sum& rhs) {
27+
value += rhs.value;
28+
}
29+
};
30+
31+
// [[Rcpp::export]]
32+
double parallelVectorSum(NumericVector x) {
33+
34+
// declare the SumBody instance
35+
Sum sum(x);
36+
37+
// call parallel_reduce to start the work
38+
parallelReduce(0, x.length(), sum);
39+
40+
// return the computed sum
41+
return sum.value;
42+
}
43+
44+
// [[Rcpp::export]]
45+
double vectorSum(NumericVector x) {
46+
return std::accumulate(x.begin(), x.end(), 0.0);
47+
}
48+
49+

‎testthat/cpp/transform.cpp

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <Rcpp.h>
2+
using namespace Rcpp;
3+
4+
#include <cmath>
5+
#include <algorithm>
6+
7+
// [[Rcpp::export]]
8+
NumericMatrix matrixSqrt(NumericMatrix orig) {
9+
10+
// allocate the matrix we will return
11+
NumericMatrix mat(orig.nrow(), orig.ncol());
12+
13+
// transform it
14+
std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);
15+
16+
// return the new matrix
17+
return mat;
18+
}
19+
// [[Rcpp::depends(RcppParallel)]]
20+
#include <RcppParallel.h>
21+
using namespace RcppParallel;
22+
23+
struct SquareRoot : public Worker
24+
{
25+
// source matrix
26+
const RMatrix<double> input;
27+
28+
// destination matrix
29+
RMatrix<double> output;
30+
31+
// initialize with source and destination
32+
SquareRoot(const NumericMatrix input, NumericMatrix output)
33+
: input(input), output(output) {}
34+
35+
// take the square root of the range of elements requested
36+
void operator()(std::size_t begin, std::size_t end) {
37+
std::transform(input.begin() + begin,
38+
input.begin() + end,
39+
output.begin() + begin,
40+
::sqrt);
41+
}
42+
};
43+
44+
// [[Rcpp::export]]
45+
NumericMatrix parallelMatrixSqrt(NumericMatrix x) {
46+
47+
// allocate the output matrix
48+
NumericMatrix output(x.nrow(), x.ncol());
49+
50+
// SquareRoot functor (pass input and output matrixes)
51+
SquareRoot squareRoot(x, output);
52+
53+
// call parallelFor to do the work
54+
parallelFor(0, x.nrow() * x.ncol(), squareRoot);
55+
56+
// return the output matrix
57+
return output;
58+
}
59+

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.