<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Hi all,<div class=""><br class=""></div><div class="">I am very new to Rcpp and I wrote a function which I now want to parallelize.</div><div class="">The function is working fine, and is much faster than in R, however, it really is slow for bigger datasets.</div><div class=""><br class=""></div><div class="">Since the function is running I don’t need to explain what it does, however, it is has </div><div class="">3 nested for loops. It loops through columns and within each column if loops through the rows and in a third loop produces pair comparisons… </div><div class=""><br class=""></div><div class="">So the parallelisation should parallelize the column loop.</div><div class=""><br class=""></div><div class="">I found the RcppParallel package and for the beginning wanted to run on of the example to understand the</div><div class="">workflow first. However, I already have issues running the code below:</div><div class=""><br class=""></div><div class="">following is sourced with: Rcpp::sourceCpp("src/par_example.cpp")</div><div class="">However, I get the error: </div><div class=""><br class=""></div><div class=""><div class=""><b class="">par_example.cpp:6:10: fatal error: 'RcppParallel.h' file not found</b></div><div class=""><b class="">#include <RcppParallel.h></b></div><div class=""><b class="">         ^</b></div><div class=""><b class="">1 error generated.</b></div><div class=""><b class="">make: *** [par_example.o] Error 1</b></div></div><div class=""><br class=""></div><div class=""><div class="">I would much appreciate if someone could give me a start with this!</div><div class=""><br class=""></div><div class="">Cheers, </div><div class="">Franz</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">#include <Rcpp.h></div><div class=""><br class=""></div><div class="">using namespace Rcpp;</div><div class="">#include <cmath></div><div class="">#include <algorithm></div><div class="">#include <RcppParallel.h></div><div class=""><br class=""></div><div class="">// [[Rcpp::export]]</div><div class="">NumericMatrix matrixSqrt(NumericMatrix orig) {</div><div class="">  // allocate the matrix we will return</div><div class="">  NumericMatrix mat(orig.nrow(), orig.ncol());</div><div class="">  // transform it</div><div class="">  std::transform(orig.begin(), orig.end(), mat.begin(), ::sqrt);</div><div class="">  // return the new matrix</div><div class="">  return mat;</div><div class="">}</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">using namespace RcppParallel;</div><div class="">struct SquareRoot : public Worker {</div><div class="">  const RMatrix<double> input;</div><div class="">  // source matrix RMatrix<double> output; // destination matrix</div><div class="">  // initialize with source and destination</div><div class="">  SquareRoot(const NumericMatrix input, NumericMatrix output)</div><div class="">    : input(input), output(output) {}</div><div class="">  // take the square root of the range of elements requested</div><div class="">  void operator()(std::size_t begin, std::size_t end) { std::transform(input.begin() + begin,</div><div class="">    input.begin() + end,</div><div class="">    output.begin() + begin,</div><div class="">    ::sqrt);</div><div class="">  }</div><div class="">};</div><div class=""><br class=""></div><div class="">// [[Rcpp::export]]</div><div class="">NumericMatrix parallelMatrixSqrt(NumericMatrix x) {</div><div class="">  // allocate the output matrix</div><div class="">  NumericMatrix output(x.nrow(), x.ncol());</div><div class="">  // SquareRoot functor (pass input and output matrixes)</div><div class="">  SquareRoot squareRoot(x, output);</div><div class="">  // call parallelFor to do the work</div><div class="">  parallelFor(0, x.length(), squareRoot);</div><div class="">  // return the output matrix</div><div class="">  return output; }</div></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div></body></html>