<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Hi, <br><br>I changed the function as indicated by Dirk and I modify the functions and the program does work now.<br>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><br>So I was wondering whether it was a question of declaration of object in my code?<br><br>------------------------------------------------------------------------------------------------------------------<br><br>sourceCpp("Rcpp/test.cpp") #<br>qwe = matrix(runif(10000), nrow = 100)<br>a = contrib1(qwe)<br>b = qnorm(qwe)<br>a - b<br><br>for (i in 1:20000) a = contrib1(qwe)<br>----------------------------------------------------------<br>// test.cpp<br><br>#include <RcppArmadillo.h><br>#include <cmath><br>#include <algorithm><br>#include <RcppParallel.h><br>#include <boost/math/distributions/inverse_gaussian.hpp><br> <br>using namespace Rcpp;<br>using namespace arma;<br>using namespace std;<br>using namespace RcppParallel;<br> <br>// [[Rcpp::depends(RcppArmadillo, RcppParallel, BH)]]<br> <br>double qnorm_f(const double& x_q) {<br>    boost::math::normal s;<br>    return boost::math::quantile(s, x_q);<br>};<br><br> <br> <br>struct Qnorm : public Worker<br>{<br>   // source matrix<br>   const RMatrix<double> input_q;<br>    <br>   // destination matrix<br>   RMatrix<double> output_q;<br>    <br>   // initialize with source and destination<br>   Qnorm(const NumericMatrix input_q, NumericMatrix output_q)<br>      : input_q(input_q), output_q(output_q) {}<br>    <br>   // take the Pnorm of the range of elements requested<br>   void operator()(std::size_t begin, std::size_t end) {<br>      std::transform(input_q.begin() + begin,<br>                     input_q.begin() + end,<br>                     output_q.begin() + begin,<br>                     ::qnorm_f);<br>   }<br>};<br> <br>mat pQnorm(mat xx_q) {<br> <br>    NumericMatrix x_q = Rcpp::as<Rcpp::NumericMatrix>(wrap(xx_q));<br>   <br>    // allocate the output matrix<br>    const NumericMatrix output_q(x_q.nrow(), x_q.ncol());<br>   <br>    // Pnorm functor (pass input and output matrices)<br>    Qnorm qnorm_temp(x_q, output_q);<br>   <br>    // call parallelFor to do the work<br>    parallelFor(0, x_q.length(), qnorm_temp);<br>   <br>    // return the output matrix<br>    mat outmat_q(output_q.begin(), output_q.nrow(),output_q.ncol());<br>    return outmat_q;<br> <br>}<br> <br>// [[Rcpp::export]]<br>mat contrib1(mat X1) {<br> <br>    mat test    = pQnorm(X1);<br>    mat results = test;<br><br>    return results;<br>}<br><br>----------------------------------------------------------<br><br><div>> Date: Tue, 9 Dec 2014 09:07:10 -0600<br>> To: qkou@umail.iu.edu<br>> CC: maxime.to@outlook.fr; rcpp-devel@lists.r-forge.r-project.org<br>> Subject: Re: [Rcpp-devel] Rcpp Parallel and Rcpp Armadillo<br>> From: edd@debian.org<br>> <br>> <br>> On 9 December 2014 at 09:46, Qiang Kou wrote:<br>> | What do you mean by "doesn't work" ? Compiling error or the result is not<br>> | right?<br>> | <br>> | I just tried the code, and it seems the code can compile and work.<br>> <br>> I am generally very careful about calling back to anything related to R from<br>> functions to be parallelized.  So for<br>> <br>>      inline double f(double x) { return ::Rf_pnorm5(x, 0.0, 1.0, 1, 0); }<br>> <br>> I think going with an equivalent pnorm() function from Boost / Bh may be better.<br>> <br>> But I am shooting from my hip here as I have not had time to look at this,<br>> having been out way too late at a nice concert :) <br>> <br>> Dirk<br>> <br>> -- <br>> http://dirk.eddelbuettel.com | @eddelbuettel | edd@debian.org<br></div>                                       </div></body>
</html>