[Rcpp-devel] How to increase the coding efficiency

Honglang Wang wanghonglang2008 at gmail.com
Wed Dec 12 15:56:17 CET 2012


And actually, i got three different types of errors:

1)Error in betahat(ker, inv[l], cbind(inv[1:ll], inv[(ll + 1):(2 * ll)]),  :
  unused argument(s) (n = 1)
Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception
Execution halted

2)Error in betahat(ker, inv[l], cbind(inv[1:ll], inv[(ll + 1):(2 * ll)]),  :
  promise already under evaluation: recursive default argument reference or
ear\
lier problems?
Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception
Execution halted

3)*** caught segfault ***
address 0x1, cause 'memory not mapped'
Error in betahat(ker, inv[l], cbind(inv[1:ll], inv[(ll + 1):(2 * ll)]),  :
  badly formed function expression
Calls: system.time ... apply -> FUN -> betahat -> .External -> cpp_exception
Execution halted

And I really have no idea what is the problem.

Best wishes!

Honglang Wang

Office C402 Wells Hall
Department of Statistics and Probability
Michigan State University
1579 I Spartan Village, East Lansing, MI 48823
wangho16 at msu.edu



On Tue, Dec 11, 2012 at 10:16 AM, Honglang Wang
<wanghonglang2008 at gmail.com>wrote:

> Dear All,
> I have modified the code as following. Since I found the old code had the
> memory allocation issue, I calculated each term of the matrix with dim 4.
> The speed is pretty much good now. But since I am not familiar with Rcpp,
> the code sometimes will get error, like
>
>  *** caught segfault ***
> address 0x16, cause 'memory not mapped'
> Error in betahat(ker, inv[l], cbind(inv[1:ll], inv[(ll + 1):(2 * ll)]),  :
>   badly formed function expression
> Calls: system.time ... apply -> FUN -> betahat -> .External ->
> cpp_exception
> Execution halted
>
> I am not sure whether my following code has some big issue mentioned in
> the above error hint. I repeatedly call the following function in my entire
> code.
>
>
> // [[Rcpp::depends(RcppArmadillo)]]
>
> #include <RcppArmadillo.h>
>
> using namespace Rcpp;
>
> // [[Rcpp::export]]
> List betahat(Function ker, double t0, NumericMatrix Xr, NumericMatrix yr,
> NumericVector tr, double h, int m) {
>   int n = Xr.nrow(), p = Xr.ncol();
>   arma::mat X(Xr.begin(), n, p, false);
>   arma::mat y(yr.begin(), n, 1, false);
>   arma::colvec t(tr.begin(), tr.size(), false);
>   arma::mat T = X;
>   T.each_col() %= (t-t0)/h;
>   arma::vec K =as<arma::vec>(ker(tr-t0,h))/m;
>   double L1 = arma::accu(K%X.col(0)%X.col(0));
>   double L2 = arma::accu(K%X.col(0)%X.col(1));
>   double L3 = arma::accu(K%X.col(1)%X.col(1));
>   double L4 = arma::accu(K%X.col(0)%T.col(0));
>   double L5 = arma::accu(K%X.col(1)%T.col(0));
>   double L6 = arma::accu(K%X.col(1)%T.col(1));
>   double L7 = arma::accu(K%T.col(0)%T.col(0));
>   double L8 = arma::accu(K%T.col(0)%T.col(1));
>   double L9 = arma::accu(K%T.col(1)%T.col(1));
>   double R1 = arma::accu(K%X.col(0)%y);
>   double R2 = arma::accu(K%X.col(1)%y);
>   double R3 = arma::accu(K%T.col(0)%y);
>   double R4 = arma::accu(K%T.col(1)%y);
>   arma::mat L(2*p,2*p);
>   L(0,0)=L1;L(0,1)=L2;L(0,2)=L4;L(0,3)=L5;
>   L(1,0)=L2;L(1,1)=L3;L(1,2)=L5;L(1,3)=L6;
>   L(2,0)=L4;L(2,1)=L5;L(2,2)=L7;L(2,3)=L8;
>   L(3,0)=L5;L(3,1)=L6;L(3,2)=L8;L(3,3)=L9;
>   arma::mat R(2*p,1);
>   R(0,0)=R1;R(1,0)=R2;R(2,0)=R3;R(3,0)=R4;
>   arma::vec betahat = arma::solve(L,R);
>
>   arma::colvec betahat0(betahat.begin(),betahat.size()/2,false);
>   return List::create(Named("betahat") = betahat0);
> }
>
> Best wishes!
>
> Honglang Wang
>
> Office C402 Wells Hall
> Department of Statistics and Probability
> Michigan State University
> 1579 I Spartan Village, East Lansing, MI 48823
> wangho16 at msu.edu
>
>
>
> On Thu, Dec 6, 2012 at 2:10 AM, Darren Cook <darren at dcook.org> wrote:
>
>> > this part will always make your code crawl along:
>>
>> >>   arma::vec betahat = arma::inv(Inv)*arma::trans(D)*W*y;
>>
>> I'd be very interested to see the before/after version of your code
>> code, Honglang. With timings. It'll make a good blog post or academic
>> paper (depending on just how many learnings you get from the process) :-)
>>
>> Darren
>>
>>
>> --
>> Darren Cook, Software Researcher/Developer
>>
>> http://dcook.org/work/ (About me and my work)
>> http://dcook.org/blogs.html (My blogs and articles)
>> _______________________________________________
>> Rcpp-devel mailing list
>> Rcpp-devel at lists.r-forge.r-project.org
>> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121212/7a047615/attachment.html>


More information about the Rcpp-devel mailing list