<div dir="ltr"><div>I have good news and bad news.</div><div><br></div><div>I found the source of the error. In your wrapper function</div><div><br></div><div>logsumexp <- function(x, a = NULL)<br>{<br>  x <- as.matrix(x)<br>  a <- if(is.null(a)) rep(double(0), ncol(x)) else as.vector(a)<br>  logsumexp_Rcpp(x,a)<br>}</div><div><br></div><div>the expression rep(double(0), ncol(x)) is not doing what you think it is doing. It returns a numeric vector of length 0, whereas you actually want a vector of zeros of length ncol(x). Note that the same issue affects your softmax function.<br></div><div><br></div><div>I don't know what Rcpp does when you try add a numeric vector of length 0, but at R level it is quite a destructive operation</div><div><br></div><div>> c(1,2) + numeric(0)<br>numeric(0)</div><div><br></div><div>This is not the behaviour you want. Here is a modified wrapper that makes the ASAN error message go away and adds some checks that the arguments are conforming.<br></div><div><br></div><div>logsumexp <- function(x, a = NULL)<br>{<br>  x <- as.matrix(x)<br>  a <- if(is.null(a)) rep(0.0, ncol(x)) else as.vector(a)<br>  if (length(a) != ncol(x)) {<br>      stop("Non-conforming arguments in logsumexp")<br>  }<br>  logsumexp_Rcpp(x,a)<br>}</div><div><br></div><div>That's the good news. The bad news is that you have a lot of memory leaks, so this is still not going to pass R CMD check with ASAN. You're going to need Dirk's container.</div><div><br></div><div>Martyn<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, 5 Dec 2022 at 09:07, Luca Scrucca <<a href="mailto:luca.scrucca@unipg.it">luca.scrucca@unipg.it</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Dear all,<br>
<br>
the "mclustAddons" R package contains some C++ functions included via Rcpp. <br>
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. <br>
<br>
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.<br>
The "mclustAddons" package gives me no problems on any tested platform/version.<br>
Unfortunately when I submit it to CRAN the problems start to arise. Below are the links to the log files:<br>
<br>
- clang-ASAN <<a href="https://www.stats.ox.ac.uk/pub/bdr/memtests/clang-ASAN/mclustAddons" rel="noreferrer" target="_blank">https://www.stats.ox.ac.uk/pub/bdr/memtests/clang-ASAN/mclustAddons</a>><br>
- gcc-ASAN <<a href="https://www.stats.ox.ac.uk/pub/bdr/memtests/gcc-ASAN/mclustAddons" rel="noreferrer" target="_blank">https://www.stats.ox.ac.uk/pub/bdr/memtests/gcc-ASAN/mclustAddons</a>><br>
<br>
After several e-mails with CRAN maintainers they suggest me to set up an environment to safely reproduce the ASAN reports:<br>
See <a href="https://www.stats.ox.ac.uk/pub/bdr/memtests/README.txt" rel="noreferrer" target="_blank">https://www.stats.ox.ac.uk/pub/bdr/memtests/README.txt</a><br>
This of course would be the next thing to do, and I have already asked for help to colleagues at the Physics Dept. <br>
<br>
The code causing the problem is reported below and the offending line is the one with "xa = x.row(i) + a;". <br>
Since I can't replicate the issue and test it, I can't resubmit the package to CRAN.<br>
Perhaps a C++ expert will spot the problem at first sight...<br>
<br>
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. <br>
<br>
Thanks in advance for your help.<br>
<br>
Luca<br>
<br>
-- <br>
--------------------------------------<br>
Luca Scrucca, PhD<br>
Associate Professor of Statistics<br>
Department of Economics <br>
University of Perugia<br>
Via A. Pascoli, 20<br>
06123 Perugia (Italy)<br>
Tel +39-075-5855231<br>
Fax +39-075-5855950<br>
E-mail <a href="mailto:luca.scrucca@unipg.it" target="_blank">luca.scrucca@unipg.it</a><br>
Web page <a href="http://www.stat.unipg.it/luca" rel="noreferrer" target="_blank">http://www.stat.unipg.it/luca</a><br>
--------------------------------------<br>
<br>
<br>
<br>
-- logsumexp.cpp ------------------------------------------------------<br>
#include <Rcpp.h><br>
using namespace Rcpp;<br>
<br>
// [[Rcpp::export]]<br>
NumericVector logsumexp_Rcpp(NumericMatrix x, NumericVector a)<br>
{<br>
// Efficiently computes log-sum-exp(x+a)<br>
// x = matrix (n x d)<br>
// a = vector (d)<br>
<br>
 double n = x.nrow();<br>
 double d = x.ncol();<br>
 NumericVector lse(n);<br>
 NumericVector xa(d);<br>
 double m = 0.0;<br>
 // <br>
 for (int i = 0; i < n; i++)<br>
 {<br>
   xa = x.row(i) + a;<br>
   m = max(xa);<br>
   lse[i] = m + log(sum(exp(xa-m)));<br>
 }<br>
 return lse;<br>
}<br>
<br>
/*** R<br>
<br>
x = structure(c(-4.19768936334846, -23.3334911845962, -5.36851445858848, <br>
-15.2896460085004, -3.06018772423303, -13.2857737610833, -3.69968442181734, <br>
-5.33468420156765, -22.954092839643, -3.03420360101199, -23.3405056884397, <br>
-2.6395810621981, -16.4338853853632, -3.23305725595493, -50.7400647615373, <br>
-7.76487486677727, -57.9522847161203, -26.7659048640944, -2.41249310267583, <br>
-44.9591733534474), dim = c(10L, 2L))<br>
pro = c(0.644072589572232, 0.355927410427768)<br>
<br>
logsumexp_Rcpp(x, log(pro))<br>
*/<br>
<br>
-----------------------------------------------------------------------<br>
<br>
<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a></blockquote></div>