Thanks, Romain.<br clear="all"><div><div>Best wishes!</div><div> </div><div>Honglang Wang</div><div> </div><div>Office C402 Wells Hall</div><div>Department of Statistics and Probability</div><div>Michigan State University</div>
<div>1579 I Spartan Village, East Lansing, MI 48823</div><div><a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a></div></div><br>
<br><br><div class="gmail_quote">On Fri, Dec 14, 2012 at 11:04 AM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="auto"><div>I dont know. This looks like a deeply hidden problem. </div><div><br></div><div>Maybe you can enable some rcpp debugging by adding </div><div><br></div><div>#define RCPP_DEBUG_LEVEL 1 </div><div><br></div>
<div>Before you include Rcpp.h, this will give you verbose output, maybe this will help locating the issue. </div><div><br></div><div>Romain<br><br>Envoyé de mon iPhone</div><div><br>Le 14 déc. 2012 à 16:31, Honglang Wang <<a href="mailto:wanghonglang2008@gmail.com" target="_blank">wanghonglang2008@gmail.com</a>> a écrit :<br>
<br></div><div><div class="h5"><blockquote type="cite"><div><span style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">Hi Romain,</span><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
Thanks very much. I tried your method. And now the old error has gone, which is really good for me. But a new error is :</div><div style="color:rgb(34,34,34);font-size:13px;font-family:arial,sans-serif">
<div style="color:rgb(80,0,80)">Error in betahat(ker, x, X, y, t, h, m) :</div><div> (list) object cannot be coerced to type 'double'</div><div style="color:rgb(80,0,80)"><div>Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception</div>
<div>Execution halted</div><div><br></div></div><div>What should I do? Do I have to do like:</div><div><div>arma::colvec betahat0(betahat.begin(),betahat.size()/2,false);</div><div>NumericVector betahat00(p);</div><div>betahat00 = betahat0;</div>
<div>return List::create(Named("betahat") = betahat00);</div></div><div><br></div></div><div><div>Best wishes!</div><div> </div><div>Honglang Wang</div><div> </div><div>Office C402 Wells Hall</div><div>Department of Statistics and Probability</div>
<div>Michigan State University</div><div>1579 I Spartan Village, East Lansing, MI 48823</div><div><a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a></div></div><br>
<br><br><div class="gmail_quote">On Fri, Dec 14, 2012 at 2:09 AM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Le 14/12/12 03:15, Honglang Wang a écrit :<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>
Hi,<br>
Here is the sample code:<br>
// [[Rcpp::export]]<br>
List betahat(Function ker, double t0, NumericMatrix Xr, NumericMatrix<br>
yr, NumericVector tr, double h, int m) {<br>
int n = Xr.nrow(), p = Xr.ncol();<br>
arma::mat X(Xr.begin(), n, p, false);<br>
arma::mat y(yr.begin(), n, 1, false);<br>
arma::colvec t(tr.begin(), tr.size(), false);<br>
arma::mat T = X;<br>
T.each_col() %= (t-t0)/h;<br>
arma::vec K =as<arma::vec>(ker(tr-t0,h))/<br>
m;<br>
double L1 = arma::accu(K%X.col(0)%X.col(0)<u></u>);<br>
double L2 = arma::accu(K%X.col(0)%X.col(1)<u></u>);<br>
double L3 = arma::accu(K%X.col(1)%X.col(1)<u></u>);<br>
double L4 = arma::accu(K%X.col(0)%T.col(0)<u></u>);<br>
double L5 = arma::accu(K%X.col(1)%T.col(0)<u></u>);<br>
double L6 = arma::accu(K%X.col(1)%T.col(1)<u></u>);<br>
double L7 = arma::accu(K%T.col(0)%T.col(0)<u></u>);<br>
double L8 = arma::accu(K%T.col(0)%T.col(1)<u></u>);<br>
double L9 = arma::accu(K%T.col(1)%T.col(1)<u></u>);<br>
double R1 = arma::accu(K%X.col(0)%y);<br>
double R2 = arma::accu(K%X.col(1)%y);<br>
double R3 = arma::accu(K%T.col(0)%y);<br>
double R4 = arma::accu(K%T.col(1)%y);<br>
arma::mat L(2*p,2*p);<br>
L(0,0)=L1;L(0,1)=L2;L(0,2)=L4;<u></u>L(0,3)=L5;<br>
L(1,0)=L2;L(1,1)=L3;L(1,2)=L5;<u></u>L(1,3)=L6;<br>
L(2,0)=L4;L(2,1)=L5;L(2,2)=L7;<u></u>L(2,3)=L8;<br>
L(3,0)=L5;L(3,1)=L6;L(3,2)=L8;<u></u>L(3,3)=L9;<br>
arma::mat R(2*p,1);<br>
R(0,0)=R1;R(1,0)=R2;R(2,0)=R3;<u></u>R(3,0)=R4;<br>
arma::vec betahat = arma::solve(L,R);<br>
<br></div></div><div>
arma::colvec betahat0(betahat.begin(),<u></u>betahat.size()/2,false);<br>
return List::create(Named("betahat") = betahat0);<br>
}<br>
<br>
I will call this function repeatedly, and at some point, it went wrong<br>
with the following error:<br>
Error in betahat(ker, x, X, y, t, h, m) :<br>
promise already under evaluation: recursive default argument<br>
reference or earlier problems?<br>
Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception<br>
Execution halted<br>
<br>
I have no idea what's this error. I am just wondering whether I need to<br>
free some memory in this code. Thanks.<br>
</div></blockquote>
<br>
You don't. The explicit constructor/destructor scheme of C++ takes care of this automatically for you.<br>
<br>
So there is a chance you are victim of a well hidden bug. These are usually related to garbage collection and us not making enough precaution somewhere. This is very hard to track.<br>
<br>
As a wild guess, I think it is because Function return you an unprotected SEXP. It does not matter most of the time because you consume it, but sometimes the GC will run and collect your object before as<mat> has a chance to process it.<br>
<br>
If my guess is correct, then doing yourself some protection might do the trick, something like:<br>
<br>
NumericVector result = ker(tr-t0,h) ;<br>
arma::vec K =as<arma::vec>(result)/m;<br>
<br>
will do the trick since the result of the function call then gets protected by the "result" object.<br>
<br>
If this does not work, then you can do some debugging as indicated by others.<br>
<br>
Romain<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div>
Best wishes!<br>
Honglang Wang<br>
Office C402 Wells Hall<br>
Department of Statistics and Probability<br>
Michigan State University<br>
1579 I Spartan Village, East Lansing, MI 48823<br>
</div><a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a> <mailto:<a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a>><div><br>
<br>
<br>
<br>
On Thu, Dec 13, 2012 at 4:57 PM, <<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a><br></div><div>
<mailto:<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.<u></u>com</a>>> wrote:<br>
<br>
<br>
Hello,<br>
<br>
What memory do you want to free ? Can you give an example of code<br>
where there is memory you'd like to "free" ?<br>
<br>
Romain<br>
<br>
Le 2012-12-13 22:52, Honglang Wang a écrit :<br>
<br>
Dear All,<br>
How to free memory in Rcpp? What's the command? Thanks.<br>
<br>
Best wishes!<br>
<br>
Honglang Wang<br>
<br>
Office C402 Wells Hall<br>
Department of Statistics and Probability<br>
Michigan State University<br>
1579 I Spartan Village, East Lansing, MI 48823<br></div>
<a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a> <mailto:<a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a>> [1]<br>
<br>
<br>
Links:<br>
------<br>
[1] mailto:<a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a> <mailto:<a href="mailto:wangho16@msu.edu" target="_blank">wangho16@msu.edu</a>><span><font color="#888888"><br>
</font></span></blockquote><span><font color="#888888">
<br>
<br>
-- <br>
Romain Francois<br>
Professional R Enthusiast<br>
<a href="tel:%2B33%280%29%206%2028%2091%2030%2030" value="+33628913030" target="_blank">+33(0) 6 28 91 30 30</a><br>
<br>
R Graph Gallery: <a href="http://gallery.r-enthusiasts.com" target="_blank">http://gallery.r-enthusiasts.<u></u>com</a><br>
<br>
blog: <a href="http://romainfrancois.blog.free.fr" target="_blank">http://romainfrancois.blog.<u></u>free.fr</a><br>
|- <a href="http://bit.ly/RE6sYH" target="_blank">http://bit.ly/RE6sYH</a> : OOP with Rcpp modules<br>
`- <a href="http://bit.ly/Thw7IK" target="_blank">http://bit.ly/Thw7IK</a> : Rcpp modules more flexible<br>
<br>
</font></span></blockquote></div><br>
</div></blockquote></div></div></div></blockquote></div><br>