Commit 0efd5b16 authored by ferhat's avatar ferhat
Browse files

"bytecode" option can be used without "block" option

git-svn-id: https://www.dynare.org/svn/dynare/trunk@3373 ac1d8469-bf42-47a9-8791-bf33cf982152
parent 98de32df
......@@ -78,11 +78,15 @@ if(options_.block)
eval([M_.fname '_dynamic']);
end;
else
if M_.maximum_endo_lag ==1 & M_.maximum_endo_lead <= 1
sim1 ;
if(options_.bytecode)
oo_.endo_simul=bytecode('dynamic');
else
simk ;
end
if M_.maximum_endo_lag ==1 & M_.maximum_endo_lead <= 1
sim1 ;
else
simk ;
end;
end;
end;
dyn2vec;
......
......@@ -118,7 +118,7 @@ elseif options_.block && ~options_.bytecode
[oo_.exo_steady_state; ...
oo_.exo_det_steady_state], M_.params);
end
elseif options_.block && options_.bytecode
elseif options_.bytecode
[oo_.steady_state,check] = bytecode('static');
else
[oo_.steady_state,check] = dynare_solve([M_.fname '_static'],...
......
......@@ -21,6 +21,7 @@
#include "Interpreter.hh"
#define BIG 1.0e+8;
#define SMALL 1.0e-5;
//#define DEBUG
Interpreter::Interpreter(double *params_arg, double *y_arg, double *ya_arg, double *x_arg, double *steady_y_arg, double *steady_x_arg,
double *direction_arg, int y_size_arg,
......@@ -169,6 +170,11 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
Stack.push(y[var]);
#ifdef DEBUG
tmp_out << " y[" << var << "](" << y[var] << ")";
if(var<0 || var>= y_size)
{
mexPrintf("y[%d]=",var);
mexErrMsgTxt("End of bytecode");
}
#endif
break;
case eExogenous:
......@@ -238,6 +244,7 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
//load a temporary variable in the processor
var = ((FLDT_ *) it_code->second)->get_pos();
#ifdef DEBUG
mexPrintf("T[it_=%d var=%d, y_kmin=%d, y_kmax=%d == %d]=>%f\n", it_, var, y_kmin, y_kmax, var*(periods+y_kmin+y_kmax)+it_, var);
tmp_out << " T[" << it_ << ", " << var << "](" << T[var*(periods+y_kmin+y_kmax)+it_] << ")";
#endif
Stack.push(T[var*(periods+y_kmin+y_kmax)+it_]);
......@@ -274,7 +281,7 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
break;
case FLDZ:
//load 0 in the processor
Stack.push(0);
Stack.push(0.0);
#ifdef DEBUG
tmp_out << " 0";
#endif
......@@ -397,6 +404,7 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
var += Per_u_;
u[var] = Stack.top();
#ifdef DEBUG
mexPrintf("FSTPU\n");
tmp_out << "=>";
mexPrintf(" u[%d](%f)=%s\n", var, u[var], tmp_out.str().c_str());
tmp_out.str("");
......@@ -406,6 +414,10 @@ Interpreter::compute_block_time(int Per_u_, bool evaluate, int block_num)
case FSTPSU:
//store in u variable from the processor
var = ((FSTPSU_ *) it_code->second)->get_pos();
#ifdef DEBUG
if(var>=u_count_alloc || var<0)
mexPrintf("Erreur var=%d\n",var);
#endif
u[var] = Stack.top();
#ifdef DEBUG
tmp_out << "=>";
......@@ -1288,6 +1300,7 @@ Interpreter::simulate_a_block(const int size, const int type, string file_name,
Per_u_ = (it_-y_kmin)*u_count_int;
Per_y_ = it_*y_size;
it_code = begining;
error_not_printed = true;
compute_block_time(Per_u_, false, block_num);
if (isnan(res1) || isinf(res1))
{
......
......@@ -326,20 +326,12 @@ SparseMatrix::Read_SparseMatrix(string file_name, const int Size, int periods, i
for (j = 0; j < Size; j++)
SaveCode.read(reinterpret_cast<char *>(&index_vara[j]), sizeof(*index_vara));
if (periods+y_kmin+y_kmax > 1)
{
for (i = 1; i < periods+y_kmin+y_kmax; i++)
{
for (j = 0; j < Size; j++)
{
index_vara[j+Size*i] = index_vara[j+Size*(i-1)]+y_size;
}
}
}
for (i = 1; i < periods+y_kmin+y_kmax; i++)
for (j = 0; j < Size; j++)
index_vara[j+Size*i] = index_vara[j+Size*(i-1)]+y_size;
index_equa = (int *) mxMalloc(Size*sizeof(int));
for (j = 0; j < Size; j++)
{
SaveCode.read(reinterpret_cast<char *>(&index_equa[j]), sizeof(*index_equa));
}
SaveCode.read(reinterpret_cast<char *>(&index_equa[j]), sizeof(*index_equa));
}
void
......@@ -367,13 +359,11 @@ SparseMatrix::Simple_Init(int it_, int y_kmin, int y_kmax, int Size, map<pair<pa
i = Size*sizeof(int);
NbNZRow = (int *) mxMalloc(i);
NbNZCol = (int *) mxMalloc(i);
i = Size*sizeof(*b);
it4 = IM.begin();
eq = -1;
//#pragma omp parallel for num_threads(atoi(getenv("DYNARE_NUM_THREADS")))
for (i = 0; i < Size; i++)
{
b[i] = 0;
line_done[i] = 0;
FNZE_C[i] = 0;
FNZE_R[i] = 0;
......@@ -961,12 +951,9 @@ SparseMatrix::bksub(int tbreak, int last_period, int Size, double slowc_l)
nb_var--;
int eq = index_vara[j]+y_size;
yy = 0;
ostringstream tmp_out;
tmp_out.clear();
for (k = 0; k < nb_var; k++)
{
yy += y[index_vara[first->c_index]+cal_y]*u[first->u_index];
tmp_out << "y[" << index_vara[first->c_index]+cal_y << "]" << "(" << y[index_vara[first->c_index]+cal_y] << ")*u[" << first->u_index << "](" << u[first->u_index] << ")+";
first = first->NZE_R_N;
}
yy = -(yy+y[eq]+u[b[pos]]);
......@@ -994,13 +981,9 @@ SparseMatrix::simple_bksub(int it_, int Size, double slowc_l)
nb_var--;
int eq = index_vara[i];
yy = 0;
ostringstream tmp_out;
tmp_out.clear();
tmp_out << "|";
for (k = 0; k < nb_var; k++)
{
yy += y[index_vara[first->c_index]+it_*y_size]*u[first->u_index];
tmp_out << "y[" << index_vara[first->c_index]+it_*y_size << "](" << y[index_vara[first->c_index]+it_*y_size] << ")*u[" << first->u_index << "](" << u[first->u_index] << ")+";
first = first->NZE_R_N;
}
yy = -(yy+y[eq+it_*y_size]+u[b[pos]]);
......@@ -1046,7 +1029,7 @@ SparseMatrix::simulate_NG(int blck, int y_size, int it_, int y_kmin, int y_kmax,
else
mexPrintf(" variable %d at time %d = %f direction = %f\n", j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]);
}
mexPrintf("res1=%5.25\n", res1);
mexPrintf("res1=%5.10f\n", res1);
mexPrintf("The initial values of endogenous variables are too far from the solution.\n");
mexPrintf("Change them!\n");
mexEvalString("drawnow;");
......@@ -1063,35 +1046,40 @@ SparseMatrix::simulate_NG(int blck, int y_size, int it_, int y_kmin, int y_kmax,
mexErrMsgTxt(filename.c_str());
}
}
if (slowc_save < 1e-8)
if (fabs(slowc_save) < 1e-8)
{
for (j = 0; j < y_size; j++)
{
bool select = false;
for (int i = 0; i < Size; i++)
if (j == index_vara[i])
{
select = true;
break;
}
if (select)
mexPrintf("-> variable %d at time %d = %f direction = %f\n", j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]);
else
mexPrintf(" variable %d at time %d = %f direction = %f\n", j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]);
}
mexPrintf("Dynare cannot improve the simulation in block %d at time %d (variable %d)\n", blck+1, it_+1, max_res_idx);
mexEvalString("drawnow;");
mxFree(piv_v);
mxFree(pivj_v);
mxFree(pivk_v);
mxFree(NR);
if (steady_state)
return false;
if (slowc_save>0)
slowc_save = -slowc;
else
{
mexEvalString("st=fclose('all');clear all;");
filename += " stopped";
mexErrMsgTxt(filename.c_str());
for (j = 0; j < y_size; j++)
{
bool select = false;
for (int i = 0; i < Size; i++)
if (j == index_vara[i])
{
select = true;
break;
}
if (select)
mexPrintf("-> variable %d at time %d = %f direction = %f\n", j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]);
else
mexPrintf(" variable %d at time %d = %f direction = %f\n", j+1, it_, y[j+it_*y_size], direction[j+it_*y_size]);
}
mexPrintf("Dynare cannot improve the simulation in block %d at time %d (variable %d)\n", blck+1, it_+1, max_res_idx);
mexEvalString("drawnow;");
mxFree(piv_v);
mxFree(pivj_v);
mxFree(pivk_v);
mxFree(NR);
if (steady_state)
return false;
else
{
mexEvalString("st=fclose('all');clear all;");
filename += " stopped";
mexErrMsgTxt(filename.c_str());
}
}
}
slowc_save /= 2;
......@@ -1484,8 +1472,8 @@ SparseMatrix::simulate_NG1(int blck, int y_size, int it_, int y_kmin, int y_kmax
{
mexPrintf("slowc_save=%g\n", slowc_save);
for (j = 0; j < y_size; j++)
mexPrintf("variable %d at time %d = %f\n", j+1, it_, y[j+it_*y_size]);
mexPrintf("Dynare cannot improve the simulation in block %d at time %d (variable %d)\n", blck+1, it_+1, max_res_idx);
mexPrintf("variable %d at time %d = %f direction = %f variable at last step = %f b = %f\n", j+1, it_+1, y[j+it_*y_size], direction[j+it_*y_size], ya[j+it_*y_size], u[pivot[j+it_*y_size]]);
mexPrintf("Dynare cannot improve the simulation in block %d at time %d (variable %d max_res = %f, res1 = %f)\n", blck+1, it_+1, max_res_idx, max_res, res1);
mexEvalString("drawnow;");
mexEvalString("st=fclose('all');clear all;");
filename += " stopped";
......
......@@ -165,12 +165,12 @@ private:
int *g_save_op;
int first_count_loop;
int g_nop_all;
int u_count_alloc, u_count_alloc_save;
double markowitz_c_s;
double res1a;
long int nop_all, nop1, nop2;
map<pair<pair<int, int>, int>, int> IM_i;
protected:
int u_count_alloc, u_count_alloc_save;
double *u, *y, *ya;
double res1, res2, max_res, max_res_idx;
double slowc, slowc_save, markowitz_c;
......
......@@ -88,8 +88,9 @@ main(int argc, const char *argv[])
}
}
fid = fopen(tmp_out.str().c_str(), "r");
int periods;
fscanf(fid, "%d", &periods);
int periods = 1;
if (!steady_state)
fscanf(fid, "%d", &periods);
int maxit_;
fscanf(fid, "%d", &maxit_);
fscanf(fid, "%f", &f_tmp);
......
function simulate_debug()
function simulate_debug(steady_state)
global M_ oo_ options_;
fid = fopen([M_.fname '_options.txt'],'wt');
fprintf(fid,'%d\n',options_.periods);
......@@ -14,10 +14,19 @@ fprintf(fid,'%d\n',M_.maximum_lag);
fprintf(fid,'%d\n',M_.maximum_lead);
fprintf(fid,'%d\n',M_.maximum_endo_lag);
fprintf(fid,'%d\n',M_.param_nbr);
fprintf(fid,'%d\n',size(oo_.exo_simul, 1));
fprintf(fid,'%d\n',size(oo_.exo_simul, 2));
if steady_state==1
fprintf(fid,'%d\n',size(oo_.exo_steady_state, 1));
fprintf(fid,'%d\n',size(oo_.exo_steady_state, 2));
else
fprintf(fid,'%d\n',size(oo_.exo_simul, 1));
fprintf(fid,'%d\n',size(oo_.exo_simul, 2));
end;
fprintf(fid,'%d\n',M_.endo_nbr);
fprintf(fid,'%d\n',size(oo_.endo_simul, 2));
if steady_state==1
fprintf(fid,'%d\n',size(oo_.steady_state, 2));
else
fprintf(fid,'%d\n',size(oo_.endo_simul, 2));
end;
fprintf(fid,'%d\n',M_.exo_det_nbr);
fprintf(fid,'%d\n',size(oo_.steady_state,1));
......@@ -29,8 +38,13 @@ fprintf(fid,'%6.20f\n',M_.params);
fclose(fid);
fid = fopen([M_.fname '_oo.txt'],'wt');
fprintf(fid,'%6.20f\n',oo_.endo_simul);
fprintf(fid,'%6.20f\n',oo_.exo_simul);
if steady_state==1
fprintf(fid,'%6.20f\n',oo_.steady_state);
fprintf(fid,'%6.20f\n',oo_.exo_steady_state);
else
fprintf(fid,'%6.20f\n',oo_.endo_simul);
fprintf(fid,'%6.20f\n',oo_.exo_simul);
end;
fprintf(fid,'%6.20f\n',oo_.steady_state);
fprintf(fid,'%6.20f\n',oo_.exo_steady_state);
fclose(fid);
......@@ -82,6 +82,7 @@ enum Tags
FSTPR, //!< Loads a residual from the stack - 12
FSTPG, //!< Loads a derivative from the stack - 13
FSTPG2, //!< Loads a derivative matrix from the stack - 13
FUNARY, //!< A Unary operator - 14
FBINARY, //!< A binary operator - 15
......@@ -533,6 +534,28 @@ public:
};
};
class FSTPG2_ : public TagWithTwoArguments<unsigned int, unsigned int>
{
public:
inline FSTPG2_() : TagWithTwoArguments<unsigned int, unsigned int>::TagWithTwoArguments(FSTPG2, 0, 0)
{
};
inline FSTPG2_(const unsigned int pos_arg1, const unsigned int pos_arg2) : TagWithTwoArguments<unsigned int, unsigned int>::TagWithTwoArguments(FSTPG2, pos_arg1, pos_arg2)
{
};
inline unsigned int
get_row()
{
return arg1;
};
inline unsigned int
get_col()
{
return arg2;
};
};
class FUNARY_ : public TagWithOneArgument<uint8_t>
{
public:
......@@ -728,7 +751,7 @@ public:
op_code = FBEGINBLOCK; size = 0; type = UNKNOWN; /*variable = NULL; equation = NULL;*/
is_linear = false; endo_nbr = 0; Max_Lag = 0; Max_Lead = 0; u_count_int = 0;
};
inline FBEGINBLOCK_(unsigned int &size_arg, BlockSimulationType &type_arg, int unsigned first_element, int unsigned &block_size,
inline FBEGINBLOCK_(unsigned int size_arg, BlockSimulationType type_arg, int unsigned first_element, int unsigned block_size,
const vector<int> &variable_arg, const vector<int> &equation_arg,
bool is_linear_arg, int endo_nbr_arg, int Max_Lag_arg, int Max_Lead_arg, int &u_count_int_arg)
{
......
......@@ -878,7 +878,7 @@ PlannerObjectiveStatement::checkPass(ModFileStructure &mod_file_struct)
void
PlannerObjectiveStatement::computingPass()
{
model_tree->computingPass(eval_context_type(), false, true, false);
model_tree->computingPass(eval_context_type(), false, true, false, false);
}
void
......
......@@ -81,6 +81,18 @@ DynamicModel::compileChainRuleDerivative(ofstream &code_file, int eqr, int varr,
}
}
void
DynamicModel::initializeVariablesAndEquations()
{
for(int j=0; j<equation_number(); j++)
{
equation_reordered.push_back(j);
variable_reordered.push_back(j);
}
}
void
DynamicModel::computeTemporaryTermsOrdered()
{
......@@ -190,14 +202,21 @@ DynamicModel::computeTemporaryTermsOrdered()
it->second->collectTemporary_terms(temporary_terms, temporary_terms_in_use, block);
v_temporary_terms_inuse[block] = temporary_terms_in_use;
}
// Add a mapping form node ID to temporary terms order
int j = 0;
for (temporary_terms_type::const_iterator it = temporary_terms.begin();
it != temporary_terms.end(); it++)
map_idx[(*it)->idx] = j++;
computeTemporaryTermsMapping();
}
}
void
DynamicModel::computeTemporaryTermsMapping()
{
// Add a mapping form node ID to temporary terms order
int j = 0;
for (temporary_terms_type::const_iterator it = temporary_terms.begin();
it != temporary_terms.end(); it++)
map_idx[(*it)->idx] = j++;
}
void
DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
{
......@@ -710,7 +729,118 @@ DynamicModel::writeModelEquationsOrdered_M(const string &dynamic_basename) const
}
void
DynamicModel::writeModelEquationsCodeOrdered(const string file_name, const string bin_basename, map_idx_type map_idx) const
DynamicModel::writeModelEquationsCode(const string file_name, const string bin_basename, map_idx_type map_idx) const
{
ostringstream tmp_output;
ofstream code_file;
bool file_open = false;
string main_name = file_name;
main_name += ".cod";
code_file.open(main_name.c_str(), ios::out | ios::binary | ios::ate);
if (!code_file.is_open())
{
cout << "Error : Can't open file \"" << main_name << "\" for writing\n";
exit(EXIT_FAILURE);
}
int count_u;
int u_count_int = 0;
BlockSimulationType simulation_type;
if ((max_endo_lag > 0) && (max_endo_lead > 0))
simulation_type = SOLVE_TWO_BOUNDARIES_COMPLETE;
else if ((max_endo_lag >= 0) && (max_endo_lead == 0))
simulation_type = SOLVE_FORWARD_COMPLETE;
else
simulation_type = SOLVE_BACKWARD_COMPLETE;
Write_Inf_To_Bin_File(file_name, u_count_int, file_open, simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE, symbol_table.endo_nbr() );
file_open = true;
//Temporary variables declaration
FDIMT_ fdimt(temporary_terms.size());
fdimt.write(code_file);
FBEGINBLOCK_ fbeginblock(symbol_table.endo_nbr(),
simulation_type,
0,
symbol_table.endo_nbr(),
variable_reordered,
equation_reordered,
false,
symbol_table.endo_nbr(),
0,
0,
u_count_int
);
fbeginblock.write(code_file);
compileTemporaryTerms(code_file, temporary_terms, map_idx, true, false);
compileModelEquations(code_file, temporary_terms, map_idx, true, false);
FENDEQU_ fendequ;
fendequ.write(code_file);
vector<vector<pair<pair<int, int>, int > > > derivatives;
derivatives.resize(symbol_table.endo_nbr());
count_u = symbol_table.endo_nbr();
for (first_derivatives_type::const_iterator it = first_derivatives.begin();
it != first_derivatives.end(); it++)
{
int deriv_id = it->first.second;
if (getTypeByDerivID(deriv_id) == eEndogenous)
{
NodeID d1 = it->second;
unsigned int eq = it->first.first;
int symb = getSymbIDByDerivID(deriv_id);
unsigned int var = symbol_table.getTypeSpecificID(symb);
int lag = getLagByDerivID(deriv_id);
if (!derivatives[eq].size())
derivatives[eq].clear();
derivatives[eq].push_back(make_pair(make_pair(var, lag), count_u));
d1->compile(code_file, false, temporary_terms, map_idx, true, false);
FSTPU_ fstpu(count_u);
fstpu.write(code_file);
count_u++;
}
}
for (int i = 0; i < symbol_table.endo_nbr(); i++)
{
FLDR_ fldr(i);
fldr.write(code_file);
for(vector<pair<pair<int, int>, int> >::const_iterator it = derivatives[i].begin();
it != derivatives[i].end(); it++)
{
FLDU_ fldu(it->second);
fldu.write(code_file);
FLDV_ fldv(eEndogenous, it->first.first, it->first.second);
fldv.write(code_file);
FBINARY_ fbinary(oTimes);
fbinary.write(code_file);
if (it != derivatives[i].begin())
{
FBINARY_ fbinary(oPlus);
fbinary.write(code_file);
}
}
FBINARY_ fbinary(oMinus);
fbinary.write(code_file);
FSTPU_ fstpu(i);
fstpu.write(code_file);
}
FENDBLOCK_ fendblock;
fendblock.write(code_file);
FEND_ fend;
fend.write(code_file);
code_file.close();
}
void
DynamicModel::writeModelEquationsCode_Block(const string file_name, const string bin_basename, map_idx_type map_idx) const
{
struct Uff_l
{
......@@ -766,7 +896,7 @@ DynamicModel::writeModelEquationsCodeOrdered(const string file_name, const strin
if (simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE || simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE
|| simulation_type == SOLVE_BACKWARD_COMPLETE || simulation_type == SOLVE_FORWARD_COMPLETE)
{
Write_Inf_To_Bin_File(file_name, bin_basename, block, u_count_int, file_open,
Write_Inf_To_Bin_File_Block(file_name, bin_basename, block, u_count_int, file_open,
simulation_type == SOLVE_TWO_BOUNDARIES_COMPLETE || simulation_type == SOLVE_TWO_BOUNDARIES_SIMPLE);
file_open = true;
}
......@@ -1109,7 +1239,7 @@ DynamicModel::reform(const string name1) const
}
void
DynamicModel::Write_Inf_To_Bin_File(const string &dynamic_basename, const string &bin_basename, const int &num,
DynamicModel::Write_Inf_To_Bin_File_Block(const string &dynamic_basename, const string &bin_basename, const int &num,
int &u_count_int, bool &file_open, bool is_two_boundaries) const
{
int j;
......@@ -1940,7 +2070,7 @@ DynamicModel::collect_first_order_derivatives_endogenous()
void
DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivatives, bool paramsDerivatives,
const eval_context_type &eval_context, bool no_tmp_terms, bool block, bool use_dll)
const eval_context_type &eval_context, bool no_tmp_terms, bool block, bool use_dll, bool bytecode)
{
assert(jacobianExo || !(hessian || thirdDerivatives || paramsDerivatives));
......@@ -2025,7 +2155,11 @@ DynamicModel::computingPass(bool jacobianExo, bool hessian, bool thirdDerivative
}
else
if (!no_tmp_terms)
computeTemporaryTerms(!use_dll);
{
computeTemporaryTerms(!use_dll);
if (bytecode)
computeTemporaryTermsMapping();
}
}
map<pair<pair<int, pair<int, int> >, pair<int, int> >, int>
......@@ -2274,7 +2408,9 @@ DynamicModel::writeDynamicFile(const string &basename, bool block, bool bytecode
{
int r;
if (block && bytecode)
writeModelEquationsCodeOrdered(basename + "_dynamic", basename, map_idx);
writeModelEquationsCode_Block(basename + "_dynamic", basename, map_idx);
else if (!block && bytecode)
writeModelEquationsCode(basename + "_dynamic", basename, map_idx);
else if (block && !bytecode)
{
#ifdef _WIN32
......
......@@ -96,7 +96,10 @@ private:
//! Writes the Block reordred structure of the model in M output
void writeModelEquationsOrdered_M(const string &dynamic_basename) const;
//! Writes the code of the Block reordred structure of the model in virtual machine bytecode
void writeModelEquationsCodeOrdered(const string file_name, const string bin_basename, map_idx_type map_idx) const;
void writeModelEquationsCode_Block(const string file_name, const string bin_basename, map_idx_type map_idx) const;
//! Writes the code of the model in virtual machine bytecode
void writeModelEquationsCode(const string file_name, const string bin_basename, map_idx_type map_idx) const;
//! Computes jacobian and prepares for equation normalization
/*! Using values from initval/endval blocks and parameter initializations:
- computes the jacobian for the model w.r. to contemporaneous variables