Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 208690

Rcpp segmentation fault creating armadillo matrix

$
0
0

I'm having a really hard time to debug this error. The situation is the following:

  1. I have a pretty small C++ library, all contained in a 'src' directory (and subfolders), that links to armadillo
  2. by using pybind11, I successfully created a Python binding for the library
  3. when using Rcpp to bind to R, the same exact code that runs in C++ standalone executables and pybinded functions, suddenly crashes

On top of that, I tried to create a MWE to showcase the error, but it seems to work fine!

Here's the file used to generate the python binding

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#include <armadillo>
#include <tuple>
#include "my_class.hpp"

std::tuple<std::vector<double>, std::vector<std::vector<double>>, double>
runFromPython(std::vector<double> data, int nrep, double theta,
          double sigma, double m0, double k0, double a0, double b0,
          double eps, int p, std::string dist="sorting") {

    arma::vec datavec = arma::conv_to<arma::vec>::from(data);

    Myclass abc(datavec, theta, sigma, eps, a0, b0, k0, m0, dist);
    std::tuple<arma::vec, arma::mat, double> out = abc.run(nrep);

    int nrows = std::get<1>(out).n_rows;
    std::vector<std::vector<double>> parts(nrows);
    for (int i=0; i < nrows; i++) {
        parts[i] = arma::conv_to<std::vector<double>>::from(
            std::get<1>(out).row(i));
    }

    return std::make_tuple(
        arma::conv_to<std::vector<double>>::from(std::get<0>(out)),
        parts,
        std::get<2>(out));
}

PYBIND11_MODULE(mod, m) {
    m.doc() = "aaa";

    m.def("runFromPython", &runFromPython,
          "...");

}

Here instead is 'rcpp_functions.cpp' that is placed in src/

#include "RcppArmadillo.h"
// [[Rcpp::depends(RcppArmadillo)]]

#include <tuple>
#include "my_class.hpp"

// [[Rcpp::export]]
Rcpp::List runAbcMCMC_univ_R(
          Rcpp::NumericVector data, int nrep=1, double theta=1.0,
          double sigma=0.2, double m0=0.0, double k0=0.5, double a0=2.0,
          double b0=2.0, double eps=10.0, int p=1,
          std::string dist="sorting") {
  arma::vec datavec(data.begin(), data.size(), false);
  Myclass abc(
      datavec, theta, sigma, eps, a0, b0, k0, m0, dist);
  std::tuple<arma::vec, arma::mat, double> out = abc.run(nrep);

  Rcpp::List res;
  res["1"] = std::get<0>(out);
  res["2"] = std::get<1>(out);
  res["3"] = std::get<2>(out);
  return res;
}

I try to build the package from R using the following commands

pkgbuild::clean_dll()
Rcpp::compileAttributes(".")
devtools::load_all()

The segmentation fault occurs when calling the class constructor abc(...)

Here is the weird part: if I look at the size of data inside the class constructor, it tells me that it is a HUGE matrix, even if I pass a vector of just 100 elements from R

The constructor is as follows

Myclass::Myclass(const arma::mat &data_, double theta, double sigma, double eps0,
                 double a0, double b0, double k0, double m0, std::string distance) {

   Rcpp::Rcout << "data_: "<< data_.n_rows << " x "<< data_.n_cols << std::endl;
   this->data = data;
}

Running R with the gbd debugger, the displayed informations are

data_: 4294967396 x 281479271678052

error: Mat::init(): requested size is too large

So basically I have no clue on how to proceed.

It would be great if someone with more experience than me in developing with Rcpp could point me to the right direction.

Thanks a lot!


Viewing all articles
Browse latest Browse all 208690

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>