-
Notifications
You must be signed in to change notification settings - Fork 39
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
Stack overflow during code generation #22
Comments
Are you using ModelCSourceGen<>.setRelatedDependents? |
No, I'm not using ModelCSourceGen<>.setRelatedDependents . #include <string.h>
#include <cppad/cg.hpp>
#include <cppad/cg/support/cppadcg_eigen.hpp>
using namespace CppAD;
using namespace CppAD::cg;
typedef CppAD::cg::CG<double> CGD;
typedef CppAD::AD<CGD> ADCG;
typedef Eigen::Matrix<ADCG, Eigen::Dynamic, 1> ADCGvec;
void ThisAdFunc(ADCGvec& adscalar, const ADCGvec& advec, int dim1, int dim2, int dim3) {
adscalar(0) = 0;
for (int ii = 0; ii < dim2; ++ii) {
for (int jj = 0; jj < dim1; ++jj) {
for (int kk = 0; kk < dim3; ++kk) {
adscalar(0) = adscalar(0) + advec((ii*dim1 + jj) * dim3 + kk)*advec((ii*dim1 + jj) * dim3 + kk);
}
}
}
}
int main(void) {
int dim1 = 35;
int dim2 = 29;
int dim3 = 3;
ADCGvec thisAdvec(dim1*dim2*dim3);
ADCGvec thisAdscalar(1);
CppAD::Independent(thisAdvec);
ThisAdFunc(thisAdscalar, thisAdvec, dim1, dim2, dim3);
CppAD::ADFun<CGD> adfun(thisAdvec, thisAdscalar);
CppAD::cg::ModelCSourceGen<double> cgen(adfun, "adModel");
cgen.setCreateJacobian(true);
CppAD::cg::ModelLibraryCSourceGen<double> libcgen(cgen);
CppAD::cg::DynamicModelLibraryProcessor<double> dynLibProc(libcgen, "<path of dynamic lib.>");
CppAD::cg::GccCompiler<double> compiler("<compiler path>");
string storageDir = "<code storage directory>";
compiler.setSaveToDiskFirst(true);
compiler.setTemporaryFolder(storageDir );
compiler.setSourcesFolder(storageDir );
dynLibProc.createDynamicLibrary(compiler);
return 0;
} |
I can't reproduce the issue on my side. I'm attaching the code that I compiled and ran under valgrind: error.zip Can you try to run your program under valgrind? ps: I was able to bring dim2 up to 100: generated_sources_100.zip |
For CppADCodeGen I have used the version from 28.10.2019 and for CppAD the version 20180000.
What stack size have you used? |
I'm running native Ubuntu 18.04.2 LTS (64bit) with a stack limit of 8192 kB. int dim1 = 40;
int dim2 = 100;
int dim3 = 4; Increasing the size of dim2 even further will lead to a very long execution time (the sparse Jacobian has 8000 non-zero elements for |
Do you think part of the problem is the amount of memory being used by CppAD ?If so, perhaps the following functions will help diagnose the problem: |
Settting |
Thank you a lot for tackling this! |
I've updated LanguageC to use a non-recursive algorithm and it is now able to generate sources for |
Thanks! I have tried it out and got the same result, ...unfortunately...
... or is there perhaps already some implemented option/functionality that allows you to create such temporary variables on demand, which I'm not aware of yet? P.s.: For possible future updates perhaps the following post is also of interest: #23 |
Temporary variables are already created automatically but only when a variable/value is used in more than one location. |
There is now an option to define the maximum number of operations per variable assignment: ModelCSourceGen<Base>::setMaxOperationsPerAssignment(size_t); |
That's great! The current version passes all the concerned tests now (even with dim2 around 10000). |
Hello,
calling "createDynamicLibrary(...)" causes fatal stack overflow
when the code (e.g. for a "zero-order forward") is to be generated for
more complex models.
These contain a one-deimensional "dependent" vector and a high dimensional "independent" one.
The dependencies in the respective function are implemented, i.a., via nested loops (of nesting order > 2) .
Dependent on the complexity of the function body everything works fine below a particular low threshold dimension of the "independent" vector.
Above such a threshold dimension, however, the stack overflow occurs.
This problem is obviously originated in the recursion methods in code_handler_impl.hpp and /lang/c/language_c.hpp. Among the very bad behaving methods seems to be "markCodeBlockUsed(...)" in code_handler_impl.hpp .
The text was updated successfully, but these errors were encountered: