Closed Zilong-Li closed 1 month ago
Thanks for taking the time to write up a report. However, this has been clearly documented for many years so no there is no expectation to change it. I think we should just close this.
PS One of the places where it is discussed is Question 5.1 in the Rcpp FAQ.
All good. Just so people are aware.
The underlying SEXP
is a pointer so this is hard to close. We did it, if memory serves, to some extent in RcppArmadillo where you can declare a signature arma_test(const arma:::mat& m)
at which point the compiler stops you. But it's a lot of work to carry it across the other two packages, plus the interface is 'out there' as it is so ... status quo remains.
Mmmh, my understanding is that @Zilong-Li does want the side effect to happen. The issue is that not all the elements in the 1e6 x 1e4 input matrix are being filled with 1's. More specifically, only 1410065408 elements are filled with 1's, i.e. a 14%, whereas the previous version filled 100%. Maybe it has something to do with the size_t
change we did?
Here's a reprex that doesn't involve any side effect. I get:
Rcpp::cppFunction("
Rcpp::NumericMatrix test_rcpp(double M, double N) {
Rcpp::NumericMatrix A(M, N);
A.fill(1);
return A;
}")
sum(test_rcpp(1e5, 1e4))
#> [1] 1e+09
sum(test_rcpp(1e6, 1e4)) # requires 80 GB of memory
#> [1] 1410065408
And removing this static_cast
solves the issue here.
@eddelbuettel Do you remember why it was needed?
My apologies. I completely misread the bug report about being on the old, known side effect when it is not.
And I verified that this passes on 1.0.12, and fails on 1.0.13. The RcppArmadillo issue is seperate (but to be looked at too) and RcppEigen seems to work.
I think we had compiler warnings there. Which no longer appear but now we seem to be smaller int size only. From glancing at it it seems to be #1307.
So the better fix, it seems, would have been
modified inst/include/Rcpp/vector/Vector.h
@@ -331,7 +331,7 @@ public:
}
inline iterator begin() { return cache.get() ; }
- inline iterator end() { return cache.get() + static_cast<int>(size()) ; }
+ inline iterator end() { return cache.get() + static_cast<R_xlen_t>(size()) ; }
inline const_iterator begin() const{ return cache.get_const() ; }
inline const_iterator end() const{ return cache.get_const() + size() ; }
inline const_iterator cbegin() const{ return cache.get_const() ; }
It passes with it, and I am not getting compilation warnings. I still have the same stanza in ~/.R/Makevars
(comment-out, now re-activated) (and also re-using CCACHE
etc defined above)
CLANGVER=-17
# #CLANGLIB=-stdlib=libc++
CXX=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)
CXX11=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)
CXX14=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)
CXX17=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)
CXX20=$(CCACHE) clang++$(CLANGVER) $(CLANGLIB)
CC=$(CCACHE) clang$(CLANGVER)
SHLIB_CXXLD=clang++$(CLANGVER) $(CLANGLIB)
CLANG_CXX_FLAGS=-Wconversion -Wno-sign-conversion #-Wno-unused-but-set-variable -Wno-delee-non-abstract-non-virtual-dtor
#CLANG_FLAGS=-Wconversion -Wno-sign-conversion -Wno-float-conversion -Wno-implicit-int-conversion -Wno-implicit-int-float-conversion -Wno-implicit-float-conversion #-Wno-unused-but-set-variable -Wno-delee-non-abstract-non-virtual-dtor
CXXFLAGS=-Wall -O3 -pedantic $(CLANG_CXX_FLAGS)
CXX11FLAGS=-Wall -O3 -pedantic $(CLANG_CXX_FLAGS)
CXX14FLAGS=-Wall -O3 -pedantic $(CLANG_CXX_FLAGS)
CXX17FLAGS=-Wall -O3 -pedantic $(CLANG_CXX_FLAGS)
CXX20FLAGS=-Wall -O3 -pedantic $(CLANG_CXX_FLAGS)
Thanks for looking into it!
Few R projects I contributed to failed to pass CI when using Rcpp==1.0.13
. I was too lazy to look into them, and simply disabled Rcpp==1.0.13
in the conda envrionment. Once you fixed it, I'll re-try Rcpp=1.0.13
.
@Zilong-Li It's a really helpful bug report, but I got distracted by some aspects. Maybe next time stress the preferred aspect of a minimally complete reproducible example: returning the large matrix and showing sum()
not being equal to the number of elements would have been more concise. Which helps readers like myself with attention deficit disorder :wink:
R package development happens mostly outside of Conda, and you will be able to get a new minor release from the Rcpp drat and/or the r-universe builds soon. Because Rcpp has such a tail of dependencies we may wait until the scheduled 1.0.14 release in January to get this to CRAN.
I also double-checked RcppArmadillo. We're good as long as ARMA_64BIT_WORD is defined (which we may want to consider as a default now):
#define ARMA_64BIT_WORD 1
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
arma::mat test_arma(int n, int k) {
arma::mat m(n,k);
m.fill(1);
return m;
}
/*** R
sum(test_arma(1e6,1e4))
*/
$ Rscript -e 'Rcpp::sourceCpp("armacheck.cpp")'
> sum(test_arma(1e+06, 10000))
[1] 1e+10
$
Problem: update matrix passed by reference from R
Code:
Results and Sessioninfo:
Reproducible conda env:
Btw, with Rcpp=1.0.12, the above code reported: