# [Rcpp-devel] parallel distance matrix calculation

JJ Allaire jj.allaire at gmail.com
Sat Jul 12 18:37:23 CEST 2014

```James,

If you could send the full source code to your example (including
js_distance and whatever R code you are using to test/exercise the
functions) I'll see if I can come up with the code you'd use to parallelize
the outer loop. Depending on how it turns out perhaps we can even convert
this into another gallery article!

J.J.

On Fri, Jul 11, 2014 at 7:20 PM, James Bullard <scientificb at gmail.com>
wrote:

> 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
>
>
>
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140712/a06fd880/attachment.html>
```