[Rcpp-devel] parallel distance matrix calculation

James Bullard scientificb at gmail.com
Sat Jul 12 01:20:38 CEST 2014

Hi All,

I'm attempting to paralellize some Rcpp code I have now, but I'm having a
hard time understanding the best route for this. I'm looking for some
guidance about how things need to be restructured to take advantage of the
parallelFor (if that would be the speediest option).

I have the following two functions:

double kl_divergence(NumericVector vec1, NumericVector vec2)
NumericMatrix js_distance(NumericMatrix mat)

Right now, the code for js_distance is:

NumericMatrix rmat(mat.nrow(), mat.nrow());
  for (int i = 0; i < rmat.nrow(); i++) {
    for (int j = 0; j < i; j++) {
      NumericVector avg = (mat(i,_) + mat(j,_))/2;
      double d1 = kl_divergence(mat(i,_), avg);
      double d2 = kl_divergence(mat(j,_), avg);
      rmat(i,j) = sqrt(.5 * (d1 + d2));

Which, by the way, is amazingly short and nice. I wanted to parallelize the
outer loop, but I'm not finding a mechanism to use iterators to go over the
rows of mat. I've looked at the examples on RcppParallel, but
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140711/b195bd99/attachment-0001.html>

More information about the Rcpp-devel mailing list