<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>