Commit cffe121a authored by MichelJuillard's avatar MichelJuillard

fixing extended-preprocessor C++ API and test case

parent b32bf860
......@@ -2956,153 +2956,6 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block_de
}
void
DynamicModel::writeCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
{
int lag_presence[3];
// Loop on endogenous variables
vector<int> zeta_back, zeta_mixed, zeta_fwrd, zeta_static;
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
{
int varID;
// Loop on periods
for (int lag = 0; lag <= 2; lag++)
{
lag_presence[lag] = 1;
try
{
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
}
catch (UnknownDerivIDException &e)
{
lag_presence[lag] = 0;
}
}
if (lag_presence[0] == 1)
if (lag_presence[2] == 1)
zeta_mixed.push_back(endoID);
else
zeta_back.push_back(endoID);
else if (lag_presence[2] == 1)
zeta_fwrd.push_back(endoID);
else
zeta_static.push_back(endoID);
}
output << "nstatic = " << zeta_static.size() << ";" << endl
<< "nfwrd = " << zeta_fwrd.size() << ";" << endl
<< "nback = " << zeta_back.size() << ";" << endl
<< "nmixed = " << zeta_mixed.size() << ";" << endl;
output << "zeta_static[" << zeta_static.size() << "] = {";
for (vector<int>::iterator i = zeta_static.begin(); i != zeta_static.end(); ++i)
{
if ( i != zeta_static.begin() )
output << ",";
output << *i;
}
output << "};" << endl;
output << "zeta_back[" << zeta_back.size() << "] = {";
for (vector<int>::iterator i = zeta_back.begin(); i != zeta_back.end(); ++i)
{
if ( i != zeta_back.begin() )
output << ",";
output << *i;
}
output << "};" << endl;
output << "zeta_fwrd[" << zeta_fwrd.size() << "] = {";
for (vector<int>::iterator i = zeta_fwrd.begin(); i != zeta_fwrd.end(); ++i)
{
if ( i != zeta_fwrd.begin() )
output << ",";
output << *i;
}
output << "};" << endl;
output << "zeta_mixed[" << zeta_mixed.size() << "] = {";
for (vector<int>::iterator i = zeta_mixed.begin(); i != zeta_mixed.end(); ++i)
{
if ( i != zeta_mixed.begin() )
output << ",";
output << *i;
}
output << "};" << endl;
// Write number of non-zero derivatives
// Use -1 if the derivatives have not been computed
output << "int *NNZDerivatives[3] = {";
switch (order)
{
case 0:
output << NNZDerivatives[0] << ",-1,-1};" << endl;
break;
case 1:
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << ",-1};" << endl;
break;
case 2:
output << NNZDerivatives[0] << "," << NNZDerivatives[1] << "," << NNZDerivatives[2] << "};" << endl;
break;
default:
cerr << "Order larger than 3 not implemented" << endl;
exit(EXIT_FAILURE);
}
}
void
DynamicModel::writeCCOutput(ostream &output, const string &basename, bool block_decomposition, bool byte_code, bool use_dll, int order, bool estimation_present) const
{
int lag_presence[3];
// Loop on endogenous variables
for (int endoID = 0; endoID < symbol_table.endo_nbr(); endoID++)
{
int varID;
// Loop on periods
for (int lag = 0; lag <= 2; lag++)
{
lag_presence[lag] = 1;
try
{
varID = getDerivID(symbol_table.getID(eEndogenous, endoID), lag-1);
}
catch (UnknownDerivIDException &e)
{
lag_presence[lag] = 0;
}
}
if (lag_presence[0] == 1)
if (lag_presence[2] == 1)
output << "zeta_mixed.push_back(" << endoID << ");" << endl;
else
output << "zeta_back.push_back(" << endoID << ");" << endl;
else if (lag_presence[2] == 1)
output << "zeta_fwrd.push_back(" << endoID << ");" << endl;
else
output << "zeta_static.push_back(" << endoID << ");" << endl;
}
output << "nstatic = zeta_static.size();" << endl
<< "nfwrd = zeta_fwrd.size();" << endl
<< "nback = zeta_back.size();" << endl
<< "nmixed = zeta_mixed.size();" << endl;
// Write number of non-zero derivatives
// Use -1 if the derivatives have not been computed
output << endl
<< "NNZDerivatives.push_back(" << NNZDerivatives[0] << ");" << endl;
if (order > 1)
{
output << "NNZDerivatives.push_back(" << NNZDerivatives[1] << ");" << endl;
if (order > 2)
output << "NNZDerivatives.push_back(" << NNZDerivatives[2] << ");" << endl;
else
output << "NNZDerivatives.push_back(-1);" << endl;
}
else
output << "NNZDerivatives.push_back(-1);" << endl
<< "NNZDerivatives.push_back(-1);" << endl;
}
map<pair<int, pair<int, int > >, expr_t>
DynamicModel::collect_first_order_derivatives_endogenous()
{
......
This diff is collapsed.
......@@ -53,7 +53,8 @@ dynare_m_SOURCES = \
SteadyStateModel.cc \
WarningConsolidation.hh \
WarningConsolidation.cc \
ExtendedPreprocessorTypes.hh
ExtendedPreprocessorTypes.hh \
ExternalFiles.cc
# The -I. is for <FlexLexer.h>
dynare_m_CPPFLAGS = $(BOOST_CPPFLAGS) -I.
......
......@@ -820,183 +820,4 @@ ModFile::writeOutputFiles(const string &basename, bool clear_all, bool no_log, b
cout << "done" << endl;
}
void
ModFile::writeModelC(const string &basename, bool cuda) const
{
string filename = basename + ".c";
ofstream mDriverCFile;
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
if (!mDriverCFile.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
exit(EXIT_FAILURE);
}
mDriverCFile << "/*" << endl
<< " * " << filename << " : Driver file for Dynare C code" << endl
<< " *" << endl
<< " * Warning : this file is generated automatically by Dynare" << endl
<< " * from model file (.mod)" << endl
<< " */" << endl
<< endl
<< "#include \"dynare_driver.h\"" << endl
<< endl
<< "struct" << endl
<< "{" << endl;
// Write basic info
symbol_table.writeCOutput(mDriverCFile);
mDriverCFile << endl << "params.resize(param_nbr);" << endl;
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present);
// if (!no_static)
// static_model.writeCOutput(mOutputFile, block);
}
// Print statements
for (vector<Statement *>::const_iterator it = statements.begin();
it != statements.end(); it++)
(*it)->writeCOutput(mDriverCFile, basename);
mDriverCFile << "} DynareInfo;" << endl;
mDriverCFile.close();
// Write informational m file
ofstream mOutputFile;
if (basename.size())
{
string fname(basename);
fname += ".m";
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
if (!mOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname
<< " for writing" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << "ERROR: Missing file name" << endl;
exit(EXIT_FAILURE);
}
mOutputFile << "%" << endl
<< "% Status : informational m file" << endl
<< "%" << endl
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl
<< "disp('The following C file was successfully created:');" << endl
<< "ls preprocessorOutput.c" << endl << endl;
mOutputFile.close();
}
void
ModFile::writeModelCC(const string &basename, bool cuda) const
{
string filename = basename + ".cc";
ofstream mDriverCFile;
mDriverCFile.open(filename.c_str(), ios::out | ios::binary);
if (!mDriverCFile.is_open())
{
cerr << "Error: Can't open file " << filename << " for writing" << endl;
exit(EXIT_FAILURE);
}
mDriverCFile << "/*" << endl
<< " * " << filename << " : Driver file for Dynare C++ code" << endl
<< " *" << endl
<< " * Warning : this file is generated automatically by Dynare" << endl
<< " * from model file (.mod)" << endl
<< " */" << endl
<< endl
<< "#include \"dynare_cpp_driver.hh\"" << endl
<< endl
<< "DynareInfo::DynareInfo(void)" << endl
<< "{" << endl;
// Write basic info
symbol_table.writeCCOutput(mDriverCFile);
mDriverCFile << endl << "params.resize(param_nbr);" << endl;
if (dynamic_model.equation_number() > 0)
{
dynamic_model.writeCOutput(mDriverCFile, basename, block, byte_code, use_dll, mod_file_struct.order_option, mod_file_struct.estimation_present);
// if (!no_static)
// static_model.writeCOutput(mOutputFile, block);
}
// Print statements
for (vector<Statement *>::const_iterator it = statements.begin();
it != statements.end(); it++)
(*it)->writeCOutput(mDriverCFile, basename);
mDriverCFile << "};" << endl;
mDriverCFile.close();
// Write informational m file
ofstream mOutputFile;
if (basename.size())
{
string fname(basename);
fname += ".m";
mOutputFile.open(fname.c_str(), ios::out | ios::binary);
if (!mOutputFile.is_open())
{
cerr << "ERROR: Can't open file " << fname
<< " for writing" << endl;
exit(EXIT_FAILURE);
}
}
else
{
cerr << "ERROR: Missing file name" << endl;
exit(EXIT_FAILURE);
}
mOutputFile << "%" << endl
<< "% Status : informational m file" << endl
<< "%" << endl
<< "% Warning : this file is generated automatically by Dynare" << endl
<< "% from model file (.mod)" << endl << endl
<< "disp('The following C++ file was successfully created:');" << endl
<< "ls preprocessorOutput.cc" << endl << endl;
mOutputFile.close();
}
void
ModFile::writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const
{
writeModelC(basename, cuda);
steady_state_model.writeSteadyStateFileC(basename, mod_file_struct.ramsey_model_present, cuda);
dynamic_model.writeDynamicFile(basename, block, byte_code, use_dll, mod_file_struct.order_option);
if (!no_static)
static_model.writeStaticFile(basename, false, false, true);
// static_model.writeStaticCFile(basename, block, byte_code, use_dll);
// static_model.writeParamsDerivativesFileC(basename, cuda);
// static_model.writeAuxVarInitvalC(mOutputFile, oMatlabOutsideModel, cuda);
// dynamic_model.writeResidualsC(basename, cuda);
// dynamic_model.writeParamsDerivativesFileC(basename, cuda);
dynamic_model.writeFirstDerivativesC(basename, cuda);
if (output == second)
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
else if (output == third)
{
dynamic_model.writeSecondDerivativesC_csr(basename, cuda);
dynamic_model.writeThirdDerivativesC_csr(basename, cuda);
}
}
......@@ -138,13 +138,16 @@ public:
, bool cygwin, bool msvc
#endif
) const;
// Functions located in ExternalFiles.cc
void writeExternalFiles(const string &basename, FileOutputType output, LanguageOutputType language) const;
void writeExternalFilesC(const string &basename, FileOutputType output) const;
void writeExternalFilesCC(const string &basename, FileOutputType output) const;
//! Writes C output files only => No further Matlab processing
void writeCOutputFiles(const string &basename) const;
void writeModelC(const string &basename, bool cuda) const;
void writeModelC(const string &basename) const;
//! Writes Cpp output files only => No further Matlab processing
void writeCCOutputFiles(const string &basename) const;
void writeModelCC(const string &basename, bool cuda) const;
void writeExternalFiles(const string &basename, FileOutputType output, bool cuda) const;
void writeModelCC(const string &basename) const;
};
#endif // ! MOD_FILE_HH
......@@ -153,7 +153,7 @@ SteadyStateModel::writeSteadyStateFile(const string &basename, bool ramsey_model
}
void
SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const
SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_model) const
{
string filename = basename + "_steadystate.c";
......@@ -167,9 +167,6 @@ SteadyStateModel::writeSteadyStateFileC(const string &basename, bool ramsey_mode
output << "#include <math.h>" << endl;
if (cuda)
output << "__global__ ";
output << "void steadystate("
<< "const double *exo_, const double *params, double *ys_, int *info)" << endl
<< "// Steady state file generated by Dynare preprocessor" << endl
......
......@@ -49,7 +49,7 @@ public:
\param[in] ramsey_model Is there a Ramsey model in the MOD file? If yes, then use the "ys" in argument of the steady state file as initial values
*/
void writeSteadyStateFile(const string &basename, bool ramsey_model) const;
void writeSteadyStateFileC(const string &basename, bool ramsey_model, bool cuda) const;
void writeSteadyStateFileC(const string &basename, bool ramsey_model) const;
};
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment