<div dir="ltr">Two things to consider:<div><br></div><div>(1) The parallelFor and parallelReduce functions don't require iterators -- they just take indexes which you can use for iterating over any range. In the gallery examples they are used to offset NumericVector.begin() to get the address of the slide of the vector or matrix to read but you could us the index for anything (i.e. the rows to process).</div>
<div><br></div><div>(2) The premise of RcppParallel is that you are reading and writing directly into C arrays in background threads (it's not safe to call into R and therefore not really safe to call into Rcpp). So to interact with a Matrix/Vector you need to calculate the appropriate offsets from matrix.begin() to get the slices (rows/columns) of the matrix you want.</div>
<div><br></div><div>#2 is based on my conservative assumption about what's thread-safe in Rcpp. Romain may tell us that it's perfectly safe to call vector iterators and matrix::operator(,) from a background thread but I don't want to assume that's okay without confirmation. If those things _aren't_ okay (or might not be okay in the future) then we either need to provide good examples for offsetting into vectors and matrixes or perhaps provide some lightweight helper classes or functions for doing the same.</div>
<div><br></div><div>Romain, what do you think?</div><div><br></div><div>In terms of parallelizing the outer vs. inner computations, I think you'd have to just benchmark. Fewer thread creations and fewer context switches is better though so my gut would be that the outer loop is the one to parallelize.</div>
<div><br></div><div>J.J.</div><div> </div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Jul 11, 2014 at 7:20 PM, James Bullard <span dir="ltr"><<a href="mailto:scientificb@gmail.com" target="_blank">scientificb@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi All,<div><br></div><div>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).</div>

<div><br></div><div>I have the following two functions:</div><div><br></div><div>double kl_divergence(NumericVector vec1, NumericVector vec2)<br></div><div>NumericMatrix js_distance(NumericMatrix mat)<br></div><div><br></div>

<div>Right now, the code for js_distance is:</div><div><br></div><div><div>NumericMatrix rmat(mat.nrow(), mat.nrow());</div><div>  for (int i = 0; i < rmat.nrow(); i++) {<br></div><div>    for (int j = 0; j < i; j++) {</div>

<div>      NumericVector avg = (mat(i,_) + mat(j,_))/2;</div><div>      double d1 = kl_divergence(mat(i,_), avg);</div><div>      double d2 = kl_divergence(mat(j,_), avg);</div><div>      rmat(i,j) = sqrt(.5 * (d1 + d2));</div>

<div>    }</div><div>  }</div></div><div><br></div><div>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</div>

<div><br></div><div><br></div><div><br></div><div><br></div><div><br></div></div>
<br>_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br></blockquote></div><br></div>