New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
segfault after repeated builds #1
Comments
Well, this is weird. The prior pdf function, which is not used in this simplified package at all, contains currently following code:
If I remove the assignment with |
Hmm. Cleaning cache with |
Wild guess: Maybe the repeated use of |
Yes that is the issue, what I am trying to do here is to simulate the case with |
We could maybe add a new argument besides I think we simply run into trouble with the You could also try to see and spy what Stan, Nimble, ... and other users do. |
This is the shortest version I managed to get so far: code <- '
// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
void func() {
double infinite = arma::datum::inf;
}
// [[Rcpp::export]]
SEXP create_pntrs() {
typedef void (*funcPtr)();
return Rcpp::XPtr<funcPtr>(new funcPtr(&func));
}'
for (i in 1:10) {
print(paste0("iteration ", i))
Rcpp::sourceCpp(code = code, rebuild = TRUE)
pntrs <- create_pntrs()
# this seems to be crucial
a <- matrix(1000^2, 1000, 1000)
} |
Another issue: changing code under the same filename may be a bad idea. Maybe just append the iteration number, or the |
I don't understand what's the problem with code <- '
#include <Rcpp.h>
class Datum_helper {
public:
static double inf() { return 0; }
};
template<typename eT>
class Datum {
public:
static const eT inf;
};
template<typename eT> const eT Datum<eT>::inf = Datum_helper::inf();
void func() {
double infinite = Datum<double>::inf;
}
// [[Rcpp::export]]
SEXP create_pntrs() {
typedef void (*funcPtr)();
return Rcpp::XPtr<funcPtr>(new funcPtr(&func));
}'
for (i in 1:10) {
print(paste0("iteration ", i))
Rcpp::sourceCpp(code = code, rebuild = TRUE)
pntrs <- create_pntrs()
# this seems to be crucial
a <- matrix(1000^2, 1000, 1000)
} |
And for the record, I'm unable to cause a segfault with clang. |
I don't think the issue is really about I can't be certain, but I have a feeling that we also have some (complex) cases where we get a segfault even when we are not compiling the same file twice but two different files which contain (some) functions with same names. Just to be clear, I am not doing any automatic modifications to cpp files or any iterations in loop, true workflow is more like here:
Of course it could make more sense to always save each model version as a new file but in exploratory phase that feels like old school version control. ;) And I'm pretty sure one of my coworkers experiencing this issue is using clang with OSX. |
Could you alter the code to not repeat function names / file names when doing repeated compiliations? I have the feeling we are violating some unspoken agreement. |
Yes I could, and although I had a feeling that I previously got segfault with different file names, at least so far I haven't been able to reproduce that. So yes it seems to caused by repeated compilation of same file (modified or not). |
Have you double-checked this? Just to be reasonably sure that this is not a compiler issue. |
I am in a process of double checking it, but actually after looking old emails he is probably not using clang after all as he had some problems with clang and openmp in the past. |
I'm not a C++ expert, but calling a class static method from a templated static initialisation sounds like trouble to me. (Edited: forget the code, the initialisation cannot take place in a constructor, of course) |
Hmm, perhaps, I'm no C++ expert either, but I'll repeat tomorrow some real workflows without inf and see how it goes. Meanwhile, I ran Removing previous |
Avoiding the code <- '
#include <Rcpp.h>
template<typename eT>
class Datum {
public:
static const eT inf;
};
template<typename eT> const eT Datum<eT>::inf =
std::numeric_limits<eT>::has_infinity ?
std::numeric_limits<eT>::infinity() : std::numeric_limits<eT>::max();
void func() {
double infinite = Datum<double>::inf;
}
// [[Rcpp::export]]
SEXP create_pntrs() {
typedef void (*funcPtr)();
return Rcpp::XPtr<funcPtr>(new funcPtr(&func));
}'
for (i in 1:100) {
print(paste0("iteration ", i))
Rcpp::sourceCpp(code = code, rebuild = TRUE)
pntrs <- create_pntrs()
# this seems to be crucial
a <- matrix(1000^2, 1000, 1000)
} So, I would try using |
I now changed all the instances of |
Interesting. I don't quite see why this would bite, but good to know you are making progress... |
I am seeing segfaults again in more complex setting, so infinity wasn't the issue (which felt really strange anyway). |
Just to be sure, is there any other mention to |
Yes there is one instance of |
Yes, try replacing them, because as I said, the implementation for |
I now changed both the |
Then I would try to simplify the code as much as possible to try to find similitudes with the case above. It's not rocket science, but it's clear that you are hitting a quite rare memory problem in a quite complex setup and we don't even have a clue about where it is. It could be in R, in Armadillo, in Rcpp or even in the compiler itself. From the information we have now, I would bet on the first two, but there's not evidence enough to support this. |
Yeah, it is pretty difficult to get the segfault by command in such complex setting, it feels like it has something to do with garbage collection or something similar as typically I am running some relatively big MCMC runs between the compilations when the segfault happens. And the segfault sometimes happens when I am back in R side doing some stuff after the rebuilding the model and running the MCMC. Like:
But unlinking the previous |
Yes, it is clearly related to your use case which is a little special and a good stress case. Removing the file, or randomizing fiile names, or ... may also work to be sure to really get 'fresh' code for fresh models. |
I get segfault when I repeatedly build the model. Valgrind reports:
Script, not minimal but does the job:
The text was updated successfully, but these errors were encountered: