Bytecode: more simplifications and modernizations

time-shift
Sébastien Villemot 2021-02-09 15:55:36 +01:00
parent f317be1600
commit c9f6d3a626
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
10 changed files with 441 additions and 754 deletions

View File

@ -20,17 +20,22 @@
#ifndef ERROR_HANDLING
#define ERROR_HANDLING
#include <cstring>
#include <iostream>
#include <sstream>
#include <vector>
#include <utility>
#include <string>
#include <map>
#include <tuple>
#include <cstddef>
#include <sstream>
#include <iostream>
#include <stack>
#define BYTE_CODE
#include "CodeInterpreter.hh"
#define _USE_MATH_DEFINES
#include <cmath>
#include <utility>
#include "dynmex.h"
#define BYTE_CODE
#include "CodeInterpreter.hh"
#ifdef OCTAVE_MEX_FILE
# define CHAR_LENGTH 1
@ -187,9 +192,9 @@ public:
ExpressionType EQN_type;
it_code_type it_code_expr;
/*unsigned int*/ size_t nb_endo, nb_exo, nb_param;
size_t nb_endo, nb_exo, nb_param;
char *P_endo_names, *P_exo_names, *P_param_names;
size_t /*unsigned int*/ endo_name_length, exo_name_length, param_name_length;
size_t endo_name_length, exo_name_length, param_name_length;
unsigned int EQN_equation, EQN_block, EQN_block_number;
unsigned int EQN_dvar1, EQN_dvar2, EQN_dvar3;
vector<tuple<string, SymbolType, unsigned int>> Variable_list;
@ -243,10 +248,7 @@ public:
string temp;
int pos1 = -1, pos2 = -1;
string tmp_n(str.length(), ' ');
string dollar, pound, tilde;
dollar = "$";
pound = "£";
tilde = "~";
string dollar{"$"}, pound{"£"}, tilde{"~"};
for (const char & i : str)
{
if (dollar.compare(&i) != 0 && pound.compare(&i) != 0)
@ -359,7 +361,7 @@ public:
inline string
error_location(bool evaluate, bool steady_state, int size, int block_num, int it_, int Per_u_)
{
stringstream Error_loc;
ostringstream Error_loc;
if (!steady_state)
switch (EQN_type)
{
@ -406,7 +408,7 @@ public:
Error_loc << "first order derivative of equation " << EQN_equation+1 << " with respect to parameter " << get_variable(SymbolType::endogenous, EQN_dvar1) << " at time " << it_;
break;
default:
return ("???");
return "???";
}
else
switch (EQN_type)

View File

@ -17,7 +17,6 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <sstream>
#include <cmath>
#include <limits>

View File

@ -20,13 +20,13 @@
#ifndef EVALUATE_HH_INCLUDED
#define EVALUATE_HH_INCLUDED
#include <stack>
#include <vector>
#include <string>
#include <cmath>
#include "dynmex.h"
#define BYTE_CODE
#include "CodeInterpreter.hh"
#include <dynmex.h>
#include "ErrorHandling.hh"
class Evaluate : public ErrorMsg

View File

@ -17,9 +17,10 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cstring>
#include "Interpreter.hh"
constexpr double BIG = 1.0e+8, SMALL = 1.0e-5;
@ -440,7 +441,7 @@ Interpreter::simulate_a_block(const vector_table_conditional_local_type &vector_
res1 = 0;
max_res = 0;
max_res_idx = 0;
memcpy(y_save, y, y_size*sizeof(double)*(periods+y_kmax+y_kmin));
copy_n(y, y_size*(periods+y_kmax+y_kmin), y_save);
if (vector_table_conditional_local.size())
for (auto & it1 : vector_table_conditional_local)
if (it1.is_cond)
@ -450,7 +451,7 @@ Interpreter::simulate_a_block(const vector_table_conditional_local_type &vector_
if (!(isnan(res1) || isinf(res1)))
cvg = (max_res < solve_tolf);
if (isnan(res1) || isinf(res1) || (stack_solve_algo == 4 && iter > 0))
memcpy(y, y_save, y_size*sizeof(double)*(periods+y_kmax+y_kmin));
copy_n(y_save, y_size*(periods+y_kmax+y_kmin), y);
u_count = u_count_saved;
int prev_iter = iter;
Simulate_Newton_Two_Boundaries(block_num, symbol_table_endo_nbr, y_kmin, y_kmax, size, periods, cvg, minimal_solving_periods, stack_solve_algo, endo_name_length, P_endo_names, vector_table_conditional_local);
@ -736,9 +737,9 @@ Interpreter::MainLoop(const string &bin_basename, const CodeLoad &code, bool eva
mxFree(T);
if (global_temporary_terms)
{
if (GlobalTemporaryTerms == nullptr)
if (!GlobalTemporaryTerms)
{
mexPrintf("GlobalTemporaryTerms is NULL\n");
mexPrintf("GlobalTemporaryTerms is nullptr\n");
mexEvalString("drawnow;");
}
if (var != static_cast<int>(mxGetNumberOfElements(GlobalTemporaryTerms)))

View File

@ -20,15 +20,16 @@
#ifndef INTERPRETER_HH_INCLUDED
#define INTERPRETER_HH_INCLUDED
#include <stack>
#include <vector>
#include <string>
#include <cmath>
#include <cstddef>
#include "dynmex.h"
#include "ErrorHandling.hh"
#include "SparseMatrix.hh"
#define BYTE_CODE
#include "CodeInterpreter.hh"
#include "SparseMatrix.hh"
#include "Evaluate.hh"
#include <dynmex.h>
using namespace std;

View File

@ -19,6 +19,8 @@
#include "Mem_Mngr.hh"
#include "dynmex.h"
Mem_Mngr::Mem_Mngr()
{
swp_f = false;

View File

@ -20,10 +20,13 @@
#ifndef MEM_MNGR_HH_INCLUDED
#define MEM_MNGR_HH_INCLUDED
#include "ErrorHandling.hh"
#include <string>
#include <vector>
#include <fstream>
#include <dynmex.h>
#include "ErrorHandling.hh"
using namespace std;
struct NonZeroElem
{

File diff suppressed because it is too large Load Diff

View File

@ -20,16 +20,19 @@
#ifndef SPARSEMATRIX_HH_INCLUDED
#define SPARSEMATRIX_HH_INCLUDED
#include <stack>
#include <cmath>
#include <utility>
#include <vector>
#include <map>
#include <ctime>
#include <tuple>
#include "dynblas.h"
#include <stack>
#include <fstream>
#include <string>
#include <ctime>
#include "dynumfpack.h"
#include "dynmex.h"
#include "Mem_Mngr.hh"
#include "ErrorHandling.hh"
#include "Evaluate.hh"
using namespace std;
@ -50,7 +53,7 @@ class dynSparseMatrix : public Evaluate
public:
dynSparseMatrix();
dynSparseMatrix(int y_size_arg, int y_kmin_arg, int y_kmax_arg, bool print_it_arg, bool steady_state_arg, int periods_arg, int minimal_solving_periods_arg, double slowc_arg);
void Simulate_Newton_Two_Boundaries(int blck, int y_size, int y_kmin, int y_kmax, int Size, int periods, bool cvg, int minimal_solving_periods, int stack_solve_algo, unsigned int endo_name_length, char *P_endo_names, vector_table_conditional_local_type vector_table_conditional_local);
void Simulate_Newton_Two_Boundaries(int blck, int y_size, int y_kmin, int y_kmax, int Size, int periods, bool cvg, int minimal_solving_periods, int stack_solve_algo, unsigned int endo_name_length, const char *P_endo_names, const vector_table_conditional_local_type &vector_table_conditional_local);
void Simulate_Newton_One_Boundary(bool forward);
void fixe_u(double **u, int u_count_int, int max_lag_plus_max_lead_plus_1);
void Read_SparseMatrix(const string &file_name, int Size, int periods, int y_kmin, int y_kmax, bool two_boundaries, int stack_solve_algo, int solve_algo);
@ -93,7 +96,6 @@ private:
void solve_non_linear(int block_num, int y_size, int y_kmin, int y_kmax, int size);
string preconditioner_print_out(string s, int preconditioner, bool ss);
bool compare(int *save_op, int *save_opa, int *save_opaa, int beg_t, int periods, long nop4, int Size);
void Grad_f_product(int n, mxArray *b_m, double *vectr, mxArray *A_m, SuiteSparse_long *Ap, SuiteSparse_long *Ai, double *Ax, double *b);
void Insert(int r, int c, int u_index, int lag_index);
void Delete(int r, int c);
int At_Row(int r, NonZeroElem **first) const;

View File

@ -16,13 +16,13 @@
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstring>
#include "Interpreter.hh"
#include "ErrorHandling.hh"
#include <ctime>
#include <cmath>
#include <cstring>
void (*prev_fn)(int);
#include "Interpreter.hh"
#include "ErrorHandling.hh"
string
Get_Argument(const mxArray *prhs)
@ -39,9 +39,6 @@ Get_Argument(const mxArray *prhs)
return f;
}
//#include <windows.h>
#include <cstdio>
string
deblank(string x)
{
@ -254,82 +251,42 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (extended_path)
{
if (extended_path_struct == nullptr)
{
string tmp = "The 'extended_path' option must be followed by the extended_path descriptor";
mexErrMsgTxt(tmp.c_str());
}
if (!extended_path_struct)
mexErrMsgTxt("The 'extended_path' option must be followed by the extended_path descriptor");
mxArray *date_str = mxGetField(extended_path_struct, 0, "date_str");
if (date_str == nullptr)
{
string tmp = "date_str";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!date_str)
mexErrMsgTxt("The extended_path description structure does not contain the member: date_str");
int nb_periods = mxGetM(date_str) * mxGetN(date_str);
mxArray *constrained_vars_ = mxGetField(extended_path_struct, 0, "constrained_vars_");
if (constrained_vars_ == nullptr)
{
string tmp = "constrained_vars_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!constrained_vars_)
mexErrMsgTxt("The extended_path description structure does not contain the member: constrained_vars_");
mxArray *constrained_paths_ = mxGetField(extended_path_struct, 0, "constrained_paths_");
if (constrained_paths_ == nullptr)
{
string tmp = "constrained_paths_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!constrained_paths_)
mexErrMsgTxt("The extended_path description structure does not contain the member: constrained_paths_");
mxArray *constrained_int_date_ = mxGetField(extended_path_struct, 0, "constrained_int_date_");
if (constrained_int_date_ == nullptr)
{
string tmp = "constrained_int_date_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!constrained_int_date_)
mexErrMsgTxt("The extended_path description structure does not contain the member: constrained_int_date_");
mxArray *constrained_perfect_foresight_ = mxGetField(extended_path_struct, 0, "constrained_perfect_foresight_");
if (constrained_perfect_foresight_ == nullptr)
{
string tmp = "constrained_perfect_foresight_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!constrained_perfect_foresight_)
mexErrMsgTxt("The extended_path description structure does not contain the member: constrained_perfect_foresight_");
mxArray *shock_var_ = mxGetField(extended_path_struct, 0, "shock_vars_");
if (shock_var_ == nullptr)
{
string tmp = "shock_vars_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!shock_var_)
mexErrMsgTxt("The extended_path description structure does not contain the member: shock_vars_");
mxArray *shock_paths_ = mxGetField(extended_path_struct, 0, "shock_paths_");
if (shock_paths_ == nullptr)
{
string tmp = "shock_paths_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!shock_paths_)
mexErrMsgTxt("The extended_path description structure does not contain the member: shock_paths_");
mxArray *shock_int_date_ = mxGetField(extended_path_struct, 0, "shock_int_date_");
if (shock_int_date_ == nullptr)
{
string tmp = "shock_int_date_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!shock_int_date_)
mexErrMsgTxt("The extended_path description structure does not contain the member: shock_int_date_");
mxArray *shock_str_date_ = mxGetField(extended_path_struct, 0, "shock_str_date_");
if (shock_str_date_ == nullptr)
{
string tmp = "shock_str_date_";
tmp.insert(0, "The extended_path description structure does not contain the member: ");
mexErrMsgTxt(tmp.c_str());
}
if (!shock_str_date_)
mexErrMsgTxt("The extended_path description structure does not contain the member: shock_str_date_");
int nb_constrained = mxGetM(constrained_vars_) * mxGetN(constrained_vars_);
int nb_controlled = 0;
mxArray *options_cond_fcst_ = mxGetField(extended_path_struct, 0, "options_cond_fcst_");
mxArray *controlled_varexo = nullptr;
if (options_cond_fcst_ != nullptr)
if (options_cond_fcst_)
{
controlled_varexo = mxGetField(options_cond_fcst_, 0, "controlled_varexo");
nb_controlled = mxGetM(controlled_varexo) * mxGetN(controlled_varexo);
@ -337,7 +294,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexErrMsgTxt("The number of exogenized variables and the number of exogenous controlled variables should be equal.");
}
double *controlled_varexo_value = nullptr;
if (controlled_varexo != nullptr)
if (controlled_varexo)
controlled_varexo_value = mxGetPr(controlled_varexo);
double *constrained_var_value = mxGetPr(constrained_vars_);
sconditional_extended_path.resize(nb_constrained);
@ -372,7 +329,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
int *constrained_int_date = static_cast<int *>(mxMalloc(nb_local_periods * sizeof(int)));
error_msg.test_mxMalloc(constrained_int_date, __LINE__, __FILE__, __func__, nb_local_periods * sizeof(int));
if (nb_periods < nb_local_periods)
mexErrMsgTxt((string{"The total number of simulation periods ("} + to_string(nb_periods)
mexErrMsgTxt(("The total number of simulation periods (" + to_string(nb_periods)
+ ") is lesser than the number of periods in the shock definitions ("
+ to_string(nb_local_periods)).c_str());
@ -426,10 +383,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
char *buf = static_cast<char *>(mxCalloc(buflen, sizeof(char)));
int info = mxGetString(mxGetCell(date_str, i), buf, buflen);
if (info)
{
string tmp = "Can not allocated memory to store the date_str in the extended path descriptor";
mexErrMsgTxt(tmp.c_str());
}
mexErrMsgTxt("Can not allocated memory to store the date_str in the extended path descriptor");
dates.emplace_back(buf); //string(Dates[i]);
mxFree(buf);
}
@ -437,12 +391,8 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (plan.length() > 0)
{
mxArray *plan_struct = mexGetVariable("base", plan.c_str());
if (plan_struct == nullptr)
{
string tmp = plan;
tmp.insert(0, "Can't find the plan: ");
mexErrMsgTxt(tmp.c_str());
}
if (!plan_struct)
mexErrMsgTxt(("Can't find the plan: " + plan).c_str());
size_t n_plan = mxGetN(plan_struct);
splan.resize(n_plan);
for (int i = 0; i < static_cast<int>(n_plan); i++)
@ -460,12 +410,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (variable_type == SymbolType::exogenous || variable_type == SymbolType::exogenousDet)
splan[i].var_num = exo_num;
else
{
string tmp = name;
tmp.insert(0, "the variable '");
tmp.append("' defined as var in plan is not an exogenous or a deterministic exogenous\n");
mexErrMsgTxt(tmp.c_str());
}
mexErrMsgTxt(("The variable '" + string{name} + "' defined as var in plan is not an exogenous or a deterministic exogenous\n").c_str());
}
tmp = mxGetField(plan_struct, i, "var");
if (tmp)
@ -478,12 +423,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (variable_type == SymbolType::endogenous)
splan[i].exo_num = exo_num;
else
{
string tmp = name;
tmp.insert(0, "the variable '");
tmp.append("' defined as exo in plan is not an endogenous variable\n");
mexErrMsgTxt(tmp.c_str());
}
mexErrMsgTxt(("The variable '" + string{name} + "' defined as exo in plan is not an endogenous variable\n").c_str());
}
tmp = mxGetField(plan_struct, i, "per_value");
if (tmp)
@ -532,12 +472,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (variable_type == SymbolType::exogenous || variable_type == SymbolType::exogenousDet)
splan[i].var_num = exo_num;
else
{
string tmp = name;
tmp.insert(0, "the variable '");
tmp.append("' defined as var in pfplan is not an exogenous or a deterministic exogenous\n");
mexErrMsgTxt(tmp.c_str());
}
mexErrMsgTxt(("The variable '" + string{name} + "' defined as var in pfplan is not an exogenous or a deterministic exogenous\n").c_str());
}
tmp = mxGetField(pfplan_struct, i, "exo");
if (tmp)
@ -550,12 +485,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
if (variable_type == SymbolType::endogenous)
spfplan[i].exo_num = exo_num;
else
{
string tmp = name;
tmp.insert(0, "the variable '");
tmp.append("' defined as exo in pfplan is not an endogenous variable\n");
mexErrMsgTxt(tmp.c_str());
}
mexErrMsgTxt(("The variable '" + string{name} + "' defined as exo in pfplan is not an endogenous variable\n").c_str());
}
tmp = mxGetField(pfplan_struct, i, "per_value");
if (tmp)
@ -828,11 +758,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
}
else
{
int out_periods;
if (extended_path)
out_periods = max_periods + y_kmin;
else
out_periods = row_y;
int out_periods = extended_path ? max_periods + y_kmin : row_y;
plhs[0] = mxCreateDoubleMatrix(out_periods, static_cast<int>(col_y), mxREAL);
pind = mxGetPr(plhs[0]);
for (i = 0; i < out_periods*col_y; i++)
@ -841,11 +767,7 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
}
else
{
int out_periods;
if (extended_path)
out_periods = max_periods + y_kmin;
else
out_periods = col_y;
int out_periods = extended_path ? max_periods + y_kmin : col_y;
plhs[0] = mxCreateDoubleMatrix(static_cast<int>(row_y), out_periods, mxREAL);
pind = mxGetPr(plhs[0]);
if (evaluate)
@ -862,7 +784,8 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (evaluate)
{
int jacob_field_number = 0, jacob_exo_field_number = 0, jacob_exo_det_field_number = 0, jacob_other_endo_field_number = 0;
int jacob_field_number = 0, jacob_exo_field_number = 0,
jacob_exo_det_field_number = 0, jacob_other_endo_field_number = 0;
if (!block_structur)
{
const char *field_names[] = {"g1", "g1_x", "g1_xd", "g1_o"};
@ -895,18 +818,16 @@ mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
mexErrMsgTxt("Fatal error in bytecode: in main, cannot add extra field jacob_other_endo to the structArray\n");
}
if (!dont_store_a_structure)
{
for (int i = 0; i < nb_blocks; i++)
{
mxSetFieldByNumber(plhs[1], i, jacob_field_number, interprete.get_jacob(i));
if (!steady_state)
{
mxSetFieldByNumber(plhs[1], i, jacob_exo_field_number, interprete.get_jacob_exo(i));
mxSetFieldByNumber(plhs[1], i, jacob_exo_det_field_number, interprete.get_jacob_exo_det(i));
mxSetFieldByNumber(plhs[1], i, jacob_other_endo_field_number, interprete.get_jacob_other_endo(i));
}
}
}
for (int i = 0; i < nb_blocks; i++)
{
mxSetFieldByNumber(plhs[1], i, jacob_field_number, interprete.get_jacob(i));
if (!steady_state)
{
mxSetFieldByNumber(plhs[1], i, jacob_exo_field_number, interprete.get_jacob_exo(i));
mxSetFieldByNumber(plhs[1], i, jacob_exo_det_field_number, interprete.get_jacob_exo_det(i));
mxSetFieldByNumber(plhs[1], i, jacob_other_endo_field_number, interprete.get_jacob_other_endo(i));
}
}
}
else
{