<div dir="ltr">What do you mean by "doesn't work" ? Compiling error or the result is not right?<div><br></div><div>I just tried the code, and it seems the code can compile and work.</div><div><br></div><div>Best,</div><div><br></div><div>KK</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 9, 2014 at 6:33 AM, Maxime To <span dir="ltr"><<a href="mailto:maxime.to@outlook.fr" target="_blank">maxime.to@outlook.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div><div dir="ltr">Hi,<br><br>I started to work with Rcpp and I have some trouble to adapt some code. I copy below the following code:<br><br>- I want to write a function named contrib1, that is simplified version here, but the idea is that that function makes a large use of the pnorm (and qnorm) on marices and I would like to make it faster by applying the function using Rcpp parallel.<br><br>- Then I started with an example that I tried to adapt <a href="http://gallery.rcpp.org/articles/parallel-matrix-transform/" target="_blank">(http://gallery.rcpp.org/articles/parallel-matrix-transform/</a>) to build the structure Pnorm and the function pPnorm. I also created the function f (I wasn't able to use directly the pnorm function as would Sugar allow...)<br><br>The example doesn't work. And I don't really understand why. I guess that it is a problem of memory allocation... Can anyone help me on that point?? Moreover, I was able to build a previous version that was working, but once I started to iterate on the contrib1 function (I execute an optimization algorithm), the RAM memory saturated I wasn't able to figure out why the program was not freeing memory after each execution of the function.<br><br>I attach the code below. I am a Rcpp novice, so don't hesitate to comment on strange things....<br>Thanks for your answers!<br><br>Maxime<br><br>--------------------------------------<br><br>#include <RcppArmadillo.h><br>#include <cmath><br>#include <algorithm><br>#include <RcppParallel.h><br><br>using namespace Rcpp;<br>using namespace arma;<br>using namespace std;<br>using namespace RcppParallel;<br><br>// [[Rcpp::depends(RcppArmadillo, RcppParallel)]]<br><br>inline double f(double x) { return ::Rf_pnorm5(x, 0.0, 1.0, 1, 0); }<br><br>struct Pnorm : public Worker<br>{<br>   // source matrix<br>   mat input;<br>   <br>   // destination matrix<br>   mat output;<br>   <br>   // initialize with source and destination<br>   Pnorm(mat input, mat output) <br>      : input(input), output(output) {}<br>   <br>   // take the square root of the range of elements requested<br>   void operator()(std::size_t begin, std::size_t end) {<br>      std::transform(input.begin() + begin, <br>                     input.begin() + end, <br>                     output.begin() + begin, <br>                     f);<br>   }<br>};<br><br>mat pPnorm(mat x) {<br>  <br>    // allocate the output matrix<br>    mat output(x.n_rows, x.n_cols);<br>  <br>    // Pnorm functor (pass input and output matrixes)<br>    Pnorm ppnorm(x, output);<br>  <br>    // call parallelFor to do the work<br>    parallelFor(0, x.n_elem, ppnorm);<br>  <br>    // return the output matrix<br>    mat outmat(output.begin(), output.n_rows, output.n_cols);<br>      return outmat;<br>}<br><br>// [[Rcpp::export]]<br>mat contrib1(mat my_matrix) {<br><br>        mat pbound2    = pPnorm(my_matrix);<br>    return pbound2;<br>}<br><br><br>------------------------------------------<br>### Main R code<br><br>rm(list=ls(all=TRUE))<br><br>library(Rcpp)<br>library(RcppGSL)<br>library(RcppParallel)<br>library(RcppArmadillo)<br>setwd('U:/testParallel')<br><br>sourceCpp("test.cpp")<br><br>asd = matrix(runif(1000), ncol = 100)<br>contrib1(asd)<br><br><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><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr">Qiang Kou<div><a href="mailto:qkou@umail.iu.edu" target="_blank">qkou@umail.iu.edu</a><br><div>School of Informatics and Computing, Indiana University</div><div><br></div></div></div></div>
</div>