<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">Some pointers. <div class=""><br class=""></div><div class="">When you use an arma::mat passed by value in an Rcpp::export, this means copying all of the data of the underlying R object into armadillo. I’d suggest you use a reference to const to avoid that, i.e. </div><div class=""><br class=""></div><div class=""><div dir="ltr" class="" style="font-family: Calibri;">mat contrib1(const mat& X1) { … }</div></div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">Then in pQnorm, you do: </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;"><div dir="ltr" class="" style="font-family: Calibri;">NumericMatrix x_q = Rcpp::as<Rcpp::NumericMatrix>(wrap(xx_q));</div></div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">That is yet again, copying all of the data from the arma::mat into an Rcpp matrix. </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">You then return a arma::mat, which data is copied implicitly as the return of contrib1. </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">I’d suggest you do all this without armadillo, which you don’t really use except for inducing a lot of extra copies of data. </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">To anwser your last question, R uses a garbage collector, so the memory is not automatically reclaimed as soon as it is no longer needed. </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">Hope this helps. </div><div dir="ltr" class="" style="font-family: Calibri;"><br class=""></div><div dir="ltr" class="" style="font-family: Calibri;">Romain</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">Le 10 déc. 2014 à 15:01, Maxime To <<a href="mailto:maxime.to@outlook.fr" class="">maxime.to@outlook.fr</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">Hi,<span class="Apple-converted-space"> </span><br class=""><br class="">I changed the function as indicated by Dirk and I modify the functions and the program does work now.<br class="">However, I am still puzzled by the memory use of the program. when I run a loop of my function in R as in the code below, it seems that the program does not free the memory used in the previous iterations... which is annoying when I need to optimize on my final object.<br class=""><br class="">So I was wondering whether it was a question of declaration of object in my code?<br class=""><br class="">------------------------------------------------------------------------------------------------------------------<br class=""><br class="">sourceCpp("Rcpp/test.cpp") #<br class="">qwe = matrix(runif(10000), nrow = 100)<br class="">a = contrib1(qwe)<br class="">b = qnorm(qwe)<br class="">a - b<br class=""><br class="">for (i in 1:20000) a = contrib1(qwe)<br class="">----------------------------------------------------------<br class="">// test.cpp<br class=""><br class="">#include <RcppArmadillo.h><br class="">#include <cmath><br class="">#include <algorithm><br class="">#include <RcppParallel.h><br class="">#include <boost/math/distributions/inverse_gaussian.hpp><br class=""> <br class="">using namespace Rcpp;<br class="">using namespace arma;<br class="">using namespace std;<br class="">using namespace RcppParallel;<br class=""> <br class="">// [[Rcpp::depends(RcppArmadillo, RcppParallel, BH)]]<br class=""> <br class="">double qnorm_f(const double& x_q) {<br class="">    boost::math::normal s;<br class="">    return boost::math::quantile(s, x_q);<br class="">};<br class=""><br class=""> <br class=""> <br class="">struct Qnorm : public Worker<br class="">{<br class="">   // source matrix<br class="">   const RMatrix<double> input_q;<br class="">   <span class="Apple-converted-space"> </span><br class="">   // destination matrix<br class="">   RMatrix<double> output_q;<br class="">   <span class="Apple-converted-space"> </span><br class="">   // initialize with source and destination<br class="">   Qnorm(const NumericMatrix input_q, NumericMatrix output_q)<br class="">      : input_q(input_q), output_q(output_q) {}<br class="">   <span class="Apple-converted-space"> </span><br class="">   // take the Pnorm of the range of elements requested<br class="">   void operator()(std::size_t begin, std::size_t end) {<br class="">      std::transform(input_q.begin() + begin,<br class="">                     input_q.begin() + end,<br class="">                     output_q.begin() + begin,<br class="">                     ::qnorm_f);<br class="">   }<br class="">};<br class=""> <br class="">mat pQnorm(mat xx_q) {<br class=""> <br class="">    NumericMatrix x_q = Rcpp::as<Rcpp::NumericMatrix>(wrap(xx_q));<br class="">  <span class="Apple-converted-space"> </span><br class="">    // allocate the output matrix<br class="">    const NumericMatrix output_q(x_q.nrow(), x_q.ncol());<br class="">  <span class="Apple-converted-space"> </span><br class="">    // Pnorm functor (pass input and output matrices)<br class="">    Qnorm qnorm_temp(x_q, output_q);<br class="">  <span class="Apple-converted-space"> </span><br class="">    // call parallelFor to do the work<br class="">    parallelFor(0, x_q.length(), qnorm_temp);<br class="">  <span class="Apple-converted-space"> </span><br class="">    // return the output matrix<br class="">    mat outmat_q(output_q.begin(), output_q.nrow(),output_q.ncol());<br class="">    return outmat_q;<br class=""> <br class="">}<br class=""> <br class="">// [[Rcpp::export]]<br class="">mat contrib1(mat X1) {<br class=""> <br class="">    mat test    = pQnorm(X1);<br class="">    mat results = test;<br class=""><br class="">    return results;<br class="">}<br class=""><br class="">----------------------------------------------------------<br class=""><br class=""><div class="">> Date: Tue, 9 Dec 2014 09:07:10 -0600<br class="">> To:<span class="Apple-converted-space"> </span><a href="mailto:qkou@umail.iu.edu" class="">qkou@umail.iu.edu</a><br class="">> CC:<span class="Apple-converted-space"> </span><a href="mailto:maxime.to@outlook.fr" class="">maxime.to@outlook.fr</a>;<span class="Apple-converted-space"> </span><a href="mailto:rcpp-devel@lists.r-forge.r-project.org" class="">rcpp-devel@lists.r-forge.r-project.org</a><br class="">> Subject: Re: [Rcpp-devel] Rcpp Parallel and Rcpp Armadillo<br class="">> From:<span class="Apple-converted-space"> </span><a href="mailto:edd@debian.org" class="">edd@debian.org</a><br class="">><span class="Apple-converted-space"> </span><br class="">><span class="Apple-converted-space"> </span><br class="">> On 9 December 2014 at 09:46, Qiang Kou wrote:<br class="">> | What do you mean by "doesn't work" ? Compiling error or the result is not<br class="">> | right?<br class="">> |<span class="Apple-converted-space"> </span><br class="">> | I just tried the code, and it seems the code can compile and work.<br class="">><span class="Apple-converted-space"> </span><br class="">> I am generally very careful about calling back to anything related to R from<br class="">> functions to be parallelized. So for<br class="">><span class="Apple-converted-space"> </span><br class="">> inline double f(double x) { return ::Rf_pnorm5(x, 0.0, 1.0, 1, 0); }<br class="">><span class="Apple-converted-space"> </span><br class="">> I think going with an equivalent pnorm() function from Boost / Bh may be better.<br class="">><span class="Apple-converted-space"> </span><br class="">> But I am shooting from my hip here as I have not had time to look at this,<br class="">> having been out way too late at a nice concert :)<span class="Apple-converted-space"> </span><br class="">><span class="Apple-converted-space"> </span><br class="">> Dirk<br class="">><span class="Apple-converted-space"> </span><br class="">> --<span class="Apple-converted-space"> </span><br class="">> <a href="http://dirk.eddelbuettel.com" class="">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org" class="">edd@debian.org</a><br class=""></div></div><span style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">_______________________________________________</span><br style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class="">Rcpp-devel mailing list</span><br style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" class="">Rcpp-devel@lists.r-forge.r-project.org</a></span><br style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><span style="font-family: Calibri; font-size: 16px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; float: none; display: inline !important;" class=""><a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" class="">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></span></div></blockquote></div><br class=""></div></body></html>