Adding the simulate files.

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1448 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
ferhat 2007-11-20 23:27:30 +00:00
parent d884d8a397
commit d90393f9df
7 changed files with 5236 additions and 43 deletions

View File

@ -0,0 +1,72 @@
#ifndef INTERPRETER_HH_INCLUDED
#define INTERPRETER_HH_INCLUDED
#include <stack>
#include <vector>
#include <string>
#include <cmath>
#include "CodeInterpreter.hh"
#include "SymbolTableTypes.hh"
#include "ExprNode.hh"
//#include "Mem_Mngr.hh"
#include "SparseMatrix.hh"
#include "LinBCG.hh"
#include "mex.h"
//#define DEBUGC
using namespace std;
typedef struct Block_contain_type
{
int Equation, Variable, Own_Derivative;
};
typedef struct Block_type
{
int begin, end, size, type;
};
#define pow_ pow
#define get_code_int *((int*)Code); Code+=sizeof(int);
#define get_code_double *((double*)Code); Code+=sizeof(double);
#define get_code_pdouble (double*)Code; Code+=sizeof(double);
#define get_code_bool *((bool*)(Code++))
#define get_code_char *((char*)(Code++))
#define get_code_pos (int)Code
#define get_code_pointer Code
#define set_code_pointer(pos) Code=pos
class Interpreter : SparseMatrix
{
protected :
double pow1(double a, double b);
void compute_block_time();
void simulate_a_block(int size,int type, string file_name, string bin_basename, bool Gaussian_Elimination);
double *T;
vector<Block_contain_type> Block_Contain;
vector<Block_type> Block;
char *Code;
stack<double> Stack;
int Block_Count, Per_u_, Per_y_;
int it_, nb_row_x, nb_row_xd, maxit_, size_of_direction;
double *g1, *r;
//double max_res, res1, res2, slowc, markowitz_c;
double solve_tolf;
bool GaussSeidel;
double *x, *params;
//double *y, *ya, *x, *direction;
map<pair<pair<int, int> ,int>, int> IM_i;
string filename;
public :
//ReadBinFile read_bin_file;
Interpreter(double *params_arg, double *y_arg, double *ya_arg, double *x_arg, double *direction_arg, int y_size_arg, int nb_row_x_arg,
int nb_row_xd_arg, int periods_arg, int y_kmin_arg, int y_kmax_arg, int maxit_arg_, double solve_tolf_arg, int size_o_direction_arg,
double slowc_arg, int y_decal_arg, double markowitz_c_arg, string &filename_arg);
void compute_blocks(string file_name, string bin_basename);
};
#endif

View File

@ -0,0 +1,48 @@
#ifndef MEM_MNGR_HH_INCLUDED
#define MEM_MNGR_HH_INCLUDED
#include <vector>
#include <fstream>
#include "mex.h"
using namespace std;
typedef struct NonZeroElem
{
int u_index;
int r_index, c_index, lag_index;
NonZeroElem *NZE_R_N, *NZE_C_N;
};
typedef vector<NonZeroElem*> v_NonZeroElem;
class Mem_Mngr
{
public:
void write_swp_f(int *save_op_all,long int *nop_all);
bool read_swp_f(int **save_op_all,long int *nop_all);
void close_swp_f();
void Print_heap();
void init_Mem();
void mxFree_NZE(void* pos);
NonZeroElem* mxMalloc_NZE();
void init_CHUNK_BLCK_SIZE(int u_count);
void Free_All();
Mem_Mngr();
void fixe_file_name(string filename_arg);
int* malloc_std(long int nop);
int* realloc_std(int* save_op_o, long int &nopa);
void chk_avail_mem(int **save_op_all,long int *nop_all,long int *nopa_all,int add, int t);
bool swp_f;
//bool verbose;
private:
v_NonZeroElem Chunk_Stack;
int CHUNK_SIZE, CHUNK_BLCK_SIZE, Nb_CHUNK;
int CHUNK_heap_pos/*, CHUNK_heap_max_size*/;
NonZeroElem** NZE_Mem_add;
NonZeroElem* NZE_Mem;
int swp_f_b;
fstream SaveCode_swp;
string filename;
};
#endif

View File

@ -0,0 +1,149 @@
#ifndef SPARSEMATRIX_HH_INCLUDED
#define SPARSEMATRIX_HH_INCLUDED
#include <fstream>
#include <stack>
#include <math.h>
#include <map>
#include <time.h>
#include "Mem_Mngr.hh"
#define NEW_ALLOC
#define MARKOVITZ
//#define MEMORY_LEAKS
using namespace std;
typedef struct t_save_op_s
{
short int lag, operat;
int first, second;
};
const int IFLD =0;
const int IFDIV =1;
const int IFLESS=2;
const int IFSUB =3;
const int IFLDZ =4;
const int IFMUL =5;
const int IFSTP =6;
const int IFADD =7;
const double eps=1e-7;
const double very_big=1e24;
const int alt_symbolic_count_max=1;
typedef struct t_table_y
{
int index, nb;
int *u_index, *y_index;
};
typedef struct t_table_u
{
t_table_u* pNext;
unsigned char type;
int index;
int op1, op2;
};
class SparseMatrix
{
public:
SparseMatrix();
int simulate_NG1(int blck, int y_size, int it_, int y_kmin, int y_kmax, int Size, int periods, bool print_it, bool cvg, int &iter);
void Direct_Simulate(int blck, int y_size, int it_, int y_kmin, int y_kmax, int Size, int periods, bool print_it, int iter);
void fixe_u(double **u, int u_count_int, int max_lag_plus_max_lead_plus_1);
//void initialize(int periods_arg, int nb_endo_arg, int y_kmin_arg, int y_kmax_arg, int y_size_arg, int u_count_arg, int u_count_init_arg, double *u_arg, double *y_arg, double *ya_arg, double slowc_arg, int y_decal_arg, double markowitz_c_arg, double res1_arg, double res2_arg, double max_res_arg);
void Read_SparseMatrix(string file_name, int Size, int periods, int y_kmin, int y_kmax);
void Read_file(string file_name, int periods, int u_size1, int y_size, int y_kmin, int y_kmax, int &nb_endo, int &u_count, int &u_count_init, double* u);
private:
void Init(int periods, int y_kmin, int y_kmax, int Size, map<pair<pair<int, int> ,int>, int> IM);
void ShortInit(int periods, int y_kmin, int y_kmax, int Size, map<pair<pair<int, int> ,int>, int> IM);
void End(int Size);
bool compare( int *save_op, int *save_opa, int *save_opaa, int beg_t, int periods, long int nop4, int Size
#ifdef PROFILER
, long int *ndiv, long int *nsub
#endif
);
void Insert(int r, int c, int u_index, int lag_index);
void Delete(int r,int c, int Size, int *b);
int At_Row(int r, NonZeroElem **first);
int At_Pos(int r, int c, NonZeroElem **first);
int At_Col(int c, NonZeroElem **first);
int At_Col(int c, int lag, NonZeroElem **first);
int NRow(int r);
int NCol(int c);
int Union_Row(int row1, int row2);
void Print(int Size,int *b);
int Get_u();
void Delete_u(int pos);
void Clear_u();
void Print_u();
int complete(int beg_t, int Size, int periods, int *b);
double bksub( int tbreak, int last_period, int Size, double slowc_l
#ifdef PROFILER
, long int *nmul
#endif
);
void run_triangular(int nop_all,int *op_all);
void run_it(int nop_all,int *op_all);
void run_u_period1(int periods);
void close_swp_file();
void read_file_table_u(t_table_u **table_u, t_table_u **F_table_u, t_table_u **i_table_u, t_table_u **F_i_table_u, int *nb_table_u, bool i_to_do, bool shifting, int *nb_add_u_count, int y_kmin, int y_kmax, int u_size);
void read_file_table_y(t_table_y **table_y, t_table_y **i_table_y, int *nb_table_y, bool i_to_do, bool shifting, int y_kmin, int y_kmax, int u_size, int y_size);
void close_SaveCode();
stack<double> Stack;
int nb_prologue_table_u, nb_first_table_u, nb_middle_table_u, nb_last_table_u;
int nb_prologue_table_y, nb_first_table_y, nb_middle_table_y, nb_last_table_y;
int middle_count_loop;
char type;
t_table_u *prologue_table_u, *first_table_u, *first_i_table_u, *middle_table_u, *middle_i_table_u, *last_table_u;
t_table_y *prologue_table_y, *first_table_y, *middle_table_y, *middle_i_table_y, *last_table_y;
t_table_u *F_prologue_table_u, *F_first_table_u, *F_first_i_table_u, *F_middle_table_u, *F_middle_i_table_u, *F_last_table_u;
fstream SaveCode;
string filename;
int max_u, min_u;
clock_t time00;
Mem_Mngr mem_mngr;
vector<int> u_liste;
int *NbNZRow, *NbNZCol;
NonZeroElem **FNZE_R, **FNZE_C;
int nb_endo, u_count_init;
int *pivot, *pivotk;
double *pivotv, *pivotva;
int *b;
bool *line_done;
bool symbolic, alt_symbolic;
int alt_symbolic_count;
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, /*nopa_all,*/ nop1, nop2;
map<pair<pair<int, int> ,int>, int> IM_i;
protected:
double *u, *y, *ya;
double res1, res2, max_res;
double slowc, slowc_save, markowitz_c;
int y_kmin, y_kmax, y_size, periods, y_decal;
//public:
int *index_vara, *index_equa;
int u_count, tbreak_g;
int iter;
double *direction;
};
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,43 +1,51 @@
CPP = g++
CPPFLAGS = -Wall
MATLABPATH = C:/MATLAB7
ifeq ($(shell uname -o), Cygwin)
# Detection of uninitialized variables is buggy in Cygwin and generates spurious warnings
CPPFLAGS += -Wno-uninitialized
CPPFLAGS += -mno-cygwin
endif
ifeq ($(CROSS_WIN32), yes)
CPP = i586-mingw32msvc-g++
# Detection of uninitialized variables is buggy in MinGW and generates spurious warnings
CPPFLAGS += -Wno-uninitialized
endif
ifeq ($(DEBUG),yes)
CPPFLAGS += -ggdb
else
CPPFLAGS += -O3 -DMATLAB_MEX_FILE -malign-double -fno-exceptions -mtune=pentium
endif
ifeq ($(VALGRIND), yes)
CPPFLAGS = -Wall -O -g -fno-inline
endif
################################################################################
### Build ######################################################################
################################################################################
all: simulate.o
dlltool --def c:/matlab7/extern/include/libmex.def --output-lib temp1.a
dlltool --def c:/matlab7/extern/include/libmx.def --output-lib temp2.a
$(CPP) -mno-cygwin -shared simulate.o -o ../../matlab/simulate.dll temp1.a temp2.a -lstdc++
rm -f temp1.a temp2.a simulate.d simulate.P
################################################################################
### Compile ####################################################################
################################################################################
simulate.o : ../simulate.cc
$(CPP) $(CPPFLAGS) -MD -I ../include -I $(MATLABPATH)/extern/include -c ../simulate.cc
@cp $*.d $*.P;
CPP = g++
CPPFLAGS = -Wall
MATLABPATH = C:/MATLAB7
ifeq ($(shell uname -o), Cygwin)
# Detection of uninitialized variables is buggy in Cygwin and generates spurious warnings
CPPFLAGS += -Wno-uninitialized
CPPFLAGS += -mno-cygwin
endif
ifeq ($(CROSS_WIN32), yes)
CPP = i586-mingw32msvc-g++
# Detection of uninitialized variables is buggy in MinGW and generates spurious warnings
CPPFLAGS += -Wno-uninitialized
endif
ifeq ($(DEBUG),yes)
CPPFLAGS += -ggdb
else
CPPFLAGS += -O3 -DMATLAB_MEX_FILE -malign-double -fno-exceptions -mtune=pentium
endif
ifeq ($(VALGRIND), yes)
CPPFLAGS = -Wall -O -g -fno-inline
endif
################################################################################
### Build ######################################################################
################################################################################
all: simulate.o linbcg.o sparsematrix.o mem_mngr.o interpreter.o
dlltool --def c:/matlab7/extern/include/libmex.def --output-lib temp1.a
dlltool --def c:/matlab7/extern/include/libmx.def --output-lib temp2.a
$(CPP) -mno-cygwin -shared simulate.o linbcg.o sparsematrix.o mem_mngr.o interpreter.o -o ../../matlab/simulate.dll temp1.a temp2.a -lstdc++
rm -f temp1.a temp2.a simulate.d simulate.P
################################################################################
### Compile ####################################################################
################################################################################
mem_mngr.o : mem_mngr.cc
$(CPP) $(CPPFLAGS) -MD -I include -I $(MATLABPATH)/extern/include -c mem_mngr.cc
linbcg.o : ../linbcg.cc
$(CPP) $(CPPFLAGS) -MD -I ../include -I $(MATLABPATH)/extern/include -c ../linbcg.cc
sparsematrix.o : sparsematrix.cc
$(CPP) $(CPPFLAGS) -MD -I include -I $(MATLABPATH)/extern/include -c sparsematrix.cc
interpreter.o : interpreter.cc
$(CPP) $(CPPFLAGS) -MD -I include -I $(MATLABPATH)/extern/include -c interpreter.cc
simulate.o : ../simulate.cc
$(CPP) $(CPPFLAGS) -MD -I ../include -I $(MATLABPATH)/extern/include -c ../simulate.cc
@cp $*.d $*.P;

View File

@ -0,0 +1,260 @@
#include "mem_mngr.hh"
Mem_Mngr::Mem_Mngr()
{
swp_f=false;
swp_f_b=0;
//verbose=false;
}
void
Mem_Mngr::Print_heap()
{
int i;
mexPrintf("i :");
for (i=0;i<CHUNK_SIZE;i++)
mexPrintf("%3d ",i);
mexPrintf("\n");
}
void
Mem_Mngr::init_Mem()
{
Chunk_Stack.clear();
CHUNK_SIZE=0;
Nb_CHUNK=0;
NZE_Mem=NULL;
NZE_Mem_add=NULL;
CHUNK_heap_pos=0;
}
void Mem_Mngr::fixe_file_name(string filename_arg)
{
filename=filename_arg;
}
void
Mem_Mngr::init_CHUNK_BLCK_SIZE(int u_count)
{
CHUNK_BLCK_SIZE=u_count;
}
NonZeroElem*
Mem_Mngr::mxMalloc_NZE()
{
int i;
if (!Chunk_Stack.empty())
{
NonZeroElem* p1 = Chunk_Stack.back();
Chunk_Stack.pop_back();
return(p1);
}
else if (CHUNK_heap_pos<CHUNK_SIZE) /*there is enough allocated memory space available*/
{
int i=CHUNK_heap_pos++;
return(NZE_Mem_add[i]);
}
else /*We have to allocate extra memory space*/
{
CHUNK_SIZE+=CHUNK_BLCK_SIZE;
Nb_CHUNK++;
#ifdef MEM_ALLOC_CHK
mexPrintf("CHUNK_BLCK_SIZE=%d\n",CHUNK_BLCK_SIZE);
#endif
NZE_Mem=(NonZeroElem*)mxMalloc(CHUNK_BLCK_SIZE*sizeof(NonZeroElem));
#ifdef MEM_ALLOC_CHK
mexPrintf("CHUNK_SIZE=%d\n",CHUNK_SIZE);
#endif
NZE_Mem_add=(NonZeroElem**)mxRealloc(NZE_Mem_add, CHUNK_SIZE*sizeof(NonZeroElem*));
#ifdef MEM_ALLOC_CHK
mexPrintf("ok\n");
#endif
for (i=CHUNK_heap_pos;i<CHUNK_SIZE;i++)
{
NZE_Mem_add[i]=(NonZeroElem*)(NZE_Mem+(i-CHUNK_heap_pos));
}
i=CHUNK_heap_pos++;
return(NZE_Mem_add[i]);
}
}
void
Mem_Mngr::mxFree_NZE(void* pos)
{
int i, gap;
/*if(verbose)
{
mexPrintf("pos=%x Nb_CHUNK=%d CHUNK_BLCK_SIZE=%d\n",pos,Nb_CHUNK, CHUNK_BLCK_SIZE);
mexEvalString("drawnow;");
}
*/
for (i=0;i<Nb_CHUNK;i++)
{
/*if(verbose)
{
mexPrintf("i=%d\n",i);
mexEvalString("drawnow;");
mexPrintf("NZE_Mem_add[i*CHUNK_BLCK_SIZE]=%d\n",NZE_Mem_add[i*CHUNK_BLCK_SIZE]);
mexEvalString("drawnow;");
}*/
gap=(int(pos)-int(NZE_Mem_add[i*CHUNK_BLCK_SIZE]))/sizeof(NonZeroElem);
if ((gap<CHUNK_BLCK_SIZE) && (gap>=0))
break;
}
/*if(verbose)
{
mexPrintf("push_back()\n");
mexEvalString("drawnow;");
}*/
Chunk_Stack.push_back((NonZeroElem*)pos);
/*if(verbose)
{
mexPrintf("End\n");
mexEvalString("drawnow;");
}*/
}
void
Mem_Mngr::write_swp_f(int *save_op_all,long int *nop_all)
{
swp_f=true;
swp_f_b++;
mexPrintf("writing the block %d with size=%d\n",swp_f_b,*nop_all);
if (!SaveCode_swp.is_open())
{
mexPrintf("open the swp file for writing\n");
#ifdef PRINT_OUT
mexPrintf("file opened\n");
#endif
SaveCode_swp.open((filename + ".swp").c_str(), std::ios::out | std::ios::binary);
if (!SaveCode_swp.is_open())
{
mexPrintf("Error : Can't open file \"%s\" for writing\n", (filename + ".swp").c_str());
mexEvalString("st=fclose('all');clear all;");
mexErrMsgTxt("Exit from Dynare");
}
#ifdef PRINT_OUT
mexPrintf("done\n");
#endif
}
SaveCode_swp.write(reinterpret_cast<char *>(nop_all), sizeof(*nop_all));
SaveCode_swp.write(reinterpret_cast<char *>(save_op_all), (*nop_all)*sizeof(int));
(*nop_all)=0;
}
bool
Mem_Mngr::read_swp_f(int **save_op_all,long int *nop_all)
{
int j;
swp_f=true;
if (!SaveCode_swp.is_open())
{
#ifdef PRINT_OUT
mexPrintf("file opened\n");
#endif
mexPrintf("open the file %s\n",(filename + ".swp").c_str());
SaveCode_swp.open((filename + ".swp").c_str(), std::ios::in | std::ios::binary);
j=SaveCode_swp.is_open();
mexPrintf("is_open()=%d\n",j);
if (!SaveCode_swp.is_open())
{
mexPrintf("Error : Can't open file \"%s\" for reading\n", (filename + ".swp").c_str());
mexEvalString("st=fclose('all');clear all;");
mexErrMsgTxt("Exit from Dynare");
}
#ifdef PRINT_OUT
mexPrintf("done\n");
#endif
SaveCode_swp.seekg(0);
}
j=SaveCode_swp.tellg();
SaveCode_swp.read(reinterpret_cast<char *>(nop_all), sizeof(*nop_all));
(*save_op_all)=(int*)mxMalloc((*nop_all)*sizeof(int));
SaveCode_swp.read(reinterpret_cast<char *>(*save_op_all), (*nop_all)*sizeof(int));
return(SaveCode_swp.good());
}
void
Mem_Mngr::close_swp_f()
{
if (SaveCode_swp.is_open())
{
SaveCode_swp.close();
mexPrintf("close the swp file\n");
}
}
int*
Mem_Mngr::malloc_std(long int nop)
{
return((int*)malloc(nop*sizeof(int)));
}
int*
Mem_Mngr::realloc_std(int* save_op_o, long int &nopa)
{
int *save_op=(int*)realloc(save_op_o,nopa*sizeof(int));
if (!save_op)
{
int nopag=int(nopa/3);
nopa=nopa-nopag;
while (!save_op && nopag>0)
{
nopag=int(nopag*0.66);
save_op=(int*)realloc(save_op_o,nopa*sizeof(int));
}
if (!save_op)
{
mexPrintf("Memory exhausted\n");
mexEvalString("drawnow;");
mexEvalString("st=fclose('all');clear all;");
filename+=" stopped";
mexErrMsgTxt(filename.c_str());
}
}
return(save_op);
}
void
Mem_Mngr::chk_avail_mem(int **save_op_all,long int *nop_all,long int *nopa_all,int add, int t)
{
mexPrintf("Error: out of save_op_all[%d] nopa_all=%d t=%d\n",(*nop_all)+add,(*nopa_all),t);
int tmp_nopa_all=int(1.5*(*nopa_all));
int *tmp_i;
if (tmp_nopa_all*sizeof(int)<1024*1024)
{
mexPrintf("allocate %d bites save_op_all=%x\n",tmp_nopa_all*sizeof(int),*save_op_all);
tmp_i=(int*)mxRealloc(*save_op_all,tmp_nopa_all*sizeof(int));
mexPrintf("tmp_i=");
mexPrintf("%x\n",tmp_i);
}
else
tmp_i=NULL;
if (!tmp_i)
{
write_swp_f((*save_op_all),nop_all);
}
else
{
mexPrintf("allocated\n");
(*save_op_all)=tmp_i;
(*nopa_all)=tmp_nopa_all;
}
mexPrintf("end of chk\n");
}
void
Mem_Mngr::Free_All()
{
int i;
for (int i=0;i<Nb_CHUNK;i++)
{
mxFree(NZE_Mem_add[i*CHUNK_BLCK_SIZE]);
}
mxFree(NZE_Mem_add);
init_Mem();
}

File diff suppressed because it is too large Load Diff