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