[Rcpp-devel] C++ issue only spotted by CRAN and not locally or via rhub

Sokol Serguei serguei.sokol at gmail.com
Mon Dec 5 11:32:45 CET 2022


Hi,

I don't know what the exact source of this problem is but I can suggest 
to use Armadillo which has a reputation to be better suited for 
matrix/vector operation than plain Rcpp.
Something like:

%<------ lse.cpp
#define RCPP_ARMADILLO_RETURN_COLVEC_AS_VECTOR
#include <RcppArmadillo.h>
using namespace arma;

// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
vec logsumexp_arma(const mat  &x, const vec &a)
{
// Efficiently computes log-sum-exp(x+a)
  vec lse(x.n_rows);
  rowvec xa(x.n_cols);
  double m;

  for (int i = 0; i < x.n_rows; i++)
  {
    xa = x.row(i) + a.t();
    m = max(xa);
    lse[i] = m + log(sum(exp(xa-m)));
  }
  return lse;
}

/*** R

x = structure(c(-4.19768936334846, -23.3334911845962, -5.36851445858848,
-15.2896460085004, -3.06018772423303, -13.2857737610833, -3.69968442181734,
-5.33468420156765, -22.954092839643, -3.03420360101199, -23.3405056884397,
-2.6395810621981, -16.4338853853632, -3.23305725595493, -50.7400647615373,
-7.76487486677727, -57.9522847161203, -26.7659048640944, -2.41249310267583,
-44.9591733534474), dim = c(10L, 2L))
pro = c(0.644072589572232, 0.355927410427768)

logsumexp_arma(x, log(pro))
*/
%<------
It si also slightly more performant.

Best,
Serguei.

Le 05/12/2022 à 10:07, Luca Scrucca a écrit :
> Dear all,
>
> the "mclustAddons" R package contains some C++ functions included via Rcpp.
> It's not the first time I've done this and, although I'm not a C++ expert, I've been able to get it right thanks to the wonderful Rcpp facilities.
>
> Before submitting to CRAN the new version, I tested the package both locally (on MacOS - Intel machine) and via rhub on many platforms (debian, fedora, ubuntu, windows) with different versions of R (release, develop), including memory sanitizers/valgrind.
> The "mclustAddons" package gives me no problems on any tested platform/version.
> Unfortunately when I submit it to CRAN the problems start to arise. Below are the links to the log files:
>
> - clang-ASAN <https://www.stats.ox.ac.uk/pub/bdr/memtests/clang-ASAN/mclustAddons>
> - gcc-ASAN <https://www.stats.ox.ac.uk/pub/bdr/memtests/gcc-ASAN/mclustAddons>
>
> After several e-mails with CRAN maintainers they suggest me to set up an environment to safely reproduce the ASAN reports:
> See https://www.stats.ox.ac.uk/pub/bdr/memtests/README.txt
> This of course would be the next thing to do, and I have already asked for help to colleagues at the Physics Dept.
>
> The code causing the problem is reported below and the offending line is the one with "xa = x.row(i) + a;".
> Since I can't replicate the issue and test it, I can't resubmit the package to CRAN.
> Perhaps a C++ expert will spot the problem at first sight...
>
> However, the main problem I have (with CRAN) is that, despite all the efforts that one can reasonably made, it is impossible for a maintainer of a package to be sure that the package will be tested correctly on CRAN.
>
> Thanks in advance for your help.
>
> Luca
>



More information about the Rcpp-devel mailing list