Hello,

I have found out that the problem was in the R loop, the garbage collection of R wasn't perform efficiently. Indeed, when I force the garbage collection to be done, using R function gc(), the Rsession won't crash, although the execution of the loop will be slower. This leads me to another question. Is there anyway to force the garbage collection inside a Rcpp function?
like this
NumericVector myfunction(NumericVector x){
   for(int= i = 0; i < x.size; ++i){
//do my stuff;
gc();
}
}

Thanks for any help!

Pierre Gloaguen

pgloa...@ifremer.fr a écrit :

Hello everyone,

I am a new user of Rcpp, I use it to rewrite a whole R program.
at the end of the program, my "final function is the following"

#include <RcppArmadillo.h>
#include <Rcpp.h>
#define _USE_MATH_DEFINES
#include <math.h>
using namespace Rcpp;

// [[Rcpp::depends("RcppArmadillo")]]

List main_function_C(arma::vec X0,arma::vec XF,double t0,
                   double tF, arma::mat Gamma,
                   NumericVector piks,arma::mat muks, List Cks,
                   int max_iter = 500){
    List bounds = bounds_fun_C(piks,Cks,Gamma);
    double sup_bound = 0.5 *(as<double>(bounds["alpha_bar"])
                             + as<double>(bounds["delta_max"])
                             - as<double>(bounds["delta_min"]));
    bool accept = false;
    int kappa = 0;
    arma::mat omegas(2,2);//arbitrary size, will change in loop
    NumericVector Psi(5);//arbitrary size, will change in loop
    for(int i = 0; i < max_iter; ++i){
      kappa = rpois(1,(tF-t0)*sup_bound)[0];
      if(kappa > 0){
        Psi = stl_sort(runif(kappa,t0,tF));
        NumericVector Upsilon = runif(kappa, 0, sup_bound);
        omegas = rubb_it_C(X0, XF, t0, tF, Psi);
        NumericVector phi_omega(kappa);
        for(int i = 1; i < kappa+2; i++){
          phi_omega[i-1] = phi_C(arma::trans(omegas.row(i)),
                                 piks,muks,Cks,Gamma);
          }
        accept = all_C((phi_omega < Upsilon));
      }//end if kappa >0
      else{
        accept = true;
      }
      if(accept){
        break;
      }
    }//end for
    arma::mat res(kappa+2,3);//result matrix,
    if(kappa > 0){
      res.submat(0,0,kappa+1,1) = omegas;
      res(0,2) = t0;
      for(int i =1; i < kappa+1;++i){
       res(i,2) = Psi[i-1];
       //Psi is of size kappa the index then goes till kappa-1
      }
      res(kappa+1,2) = tF;
    }
    else{
      res(0,arma::span(0,1)) = trans(X0);
      res(1,arma::span(0,1)) = trans(XF);
      res(0,2) = t0;
      res(kappa+1,2) = tF;
    }
    return List::create(Named("skel") = res,
                        Named("accepted") = accept);
}

This function calls other funtion that I have written, all of them are attached on the cpp_funcs.cpp file.
The sourceCpp performs well and I can, from R obtain the following result

   sourceCpp("cpp_funcs.cpp")
   X0 <- c(0.5,0.5)
   XF <- c(1,1)
   t0 <- 0
   tF <- 1
   Gam <- diag(0.1,2)
   ncomp=2
   pis <- c(0.5,0.5)
   mu <- matrix(c(-1,1,
                   1,1),ncol=2,nrow=ncomp)
   cov <- list(diag(0.5,2),diag(1,2))
   set.seed(123)
 test <- main_function_C(X0,XF,t0,tF,Gam,pis,mu,cov)
 test
#$skel
           [,1]     [,2]      [,3]
#[1,]  0.5000000 0.500000 0.0000000
#[2,] -0.1261731 1.313880 0.4089769
#[3,]  0.5564577 1.069211 0.7883051
#[4,]  1.0000000 1.000000 1.0000000

#$accepted
#[1] TRUE

PROBLEM:

When I run the exact same code as above a large number of times, as this

   for(i in 1:1000){
   ## same code as above
   }

The r session aborts all the time, giving no error message but "R encountered a fatal error". I do not encounter this problem with the intermediate functions in the attached file (and, of course, I'm not asking for a debugging of those), I only have it with this one

Is this a problem with my code? with how the loop is written?The List object?
Is this a problem of the memory?

It does it either on Rstudio or Rgui
I work on windows 7, with R version 3.0.2
Rcpp 0.11.2 and RcppArmadillo 0.4.450.1.0

I also attach a R debugging file giving parameters for all intermediate functions.

Sorry for the length of the function, I'm pretty sure the problem comes from the main function, but it sadly depends on others (which, as far as I know, don't pose problems)

Thanks in advance for any help,

Pierre Gloaguen




_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to