Bytecode: remove stuff for creating a standalone debugging executable

This code no longer compiles. And it’s possible to debug the MEX directly.
time-shift
Sébastien Villemot 2021-02-01 14:43:52 +01:00
parent bb5f58f55f
commit 743469bfd7
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
9 changed files with 106 additions and 1012 deletions

View File

@ -32,11 +32,6 @@
#include <cmath>
#include <utility>
#ifdef DEBUG_EX
# include <math.h>
# include "mex_interface.hh"
#endif
#ifdef OCTAVE_MEX_FILE
# define CHAR_LENGTH 1
#else

View File

@ -29,11 +29,7 @@
#ifdef LINBCG
# include "linbcg.hh"
#endif
#ifndef DEBUG_EX
# include <dynmex.h>
#else
# include "mex_interface.hh"
#endif
#include <dynmex.h>
#include "ErrorHandling.hh"
#define pow_ pow

View File

@ -31,11 +31,7 @@
#ifdef LINBCG
# include "linbcg.hh"
#endif
#ifndef DEBUG_EX
# include <dynmex.h>
#else
# include "mex_interface.hh"
#endif
#include <dynmex.h>
//#define DEBUGC

View File

@ -1,5 +1,5 @@
/*
* Copyright © 2007-2017 Dynare Team
* Copyright © 2007-2021 Dynare Team
*
* This file is part of Dynare.
*
@ -23,11 +23,7 @@
#include "ErrorHandling.hh"
#include <vector>
#include <fstream>
#ifndef DEBUG_EX
# include <dynmex.h>
#else
# include "mex_interface.hh"
#endif
#include <dynmex.h>
//using namespace std;
struct NonZeroElem

View File

@ -22,20 +22,6 @@
#include <ctime>
#include <cmath>
#ifdef DEBUG_EX
using namespace std;
# include <sstream>
string
Get_Argument(const char *argv)
{
string f(argv);
return f;
}
#else
void (*prev_fn)(int);
string
@ -52,7 +38,6 @@ Get_Argument(const mxArray *prhs)
mxFree(first_argument);
return f;
}
#endif
//#include <windows.h>
#include <cstdio>
@ -68,20 +53,14 @@ deblank(string x)
void
Get_Arguments_and_global_variables(int nrhs,
#ifndef DEBUG_EX
const mxArray *prhs[],
#else
const char *prhs[],
#endif
int &count_array_argument,
double *yd[], size_t &row_y, size_t &col_y,
double *xd[], size_t &row_x, size_t &col_x,
double *params[],
double *steady_yd[], size_t &steady_row_y, size_t &steady_col_y,
unsigned int &periods,
#ifndef DEBUG_EX
mxArray *block_structur[],
#endif
bool &steady_state, bool &evaluate, int &block,
mxArray *M_[], mxArray *oo_[], mxArray *options_[], bool &global_temporary_terms,
bool &print,
@ -91,114 +70,108 @@ Get_Arguments_and_global_variables(int nrhs,
{
size_t pos;
*extended_path = false;
#ifdef DEBUG_EX
for (int i = 2; i < nrhs; i++)
#else
for (int i = 0; i < nrhs; i++)
#endif
{
#ifndef DEBUG_EX
if (!mxIsChar(prhs[i]))
{
switch (count_array_argument)
{
case 0:
*yd = mxGetPr(prhs[i]);
row_y = mxGetM(prhs[i]);
col_y = mxGetN(prhs[i]);
break;
case 1:
*xd = mxGetPr(prhs[i]);
row_x = mxGetM(prhs[i]);
col_x = mxGetN(prhs[i]);
break;
case 2:
*params = mxGetPr(prhs[i]);
break;
case 3:
*steady_yd = mxGetPr(prhs[i]);
steady_row_y = mxGetM(prhs[i]);
steady_col_y = mxGetN(prhs[i]);
break;
case 4:
periods = int (mxGetScalar(prhs[i]));
break;
case 5:
*block_structur = mxDuplicateArray(prhs[i]);
break;
case 6:
global_temporary_terms = true;
*GlobalTemporaryTerms = mxDuplicateArray(prhs[i]);
break;
default:
mexPrintf("Unknown argument count_array_argument=%d\n", count_array_argument);
break;
}
count_array_argument++;
}
else
#endif
if (Get_Argument(prhs[i]) == "static")
steady_state = true;
else if (Get_Argument(prhs[i]) == "dynamic")
steady_state = false;
else if (Get_Argument(prhs[i]) == "evaluate")
evaluate = true;
else if (Get_Argument(prhs[i]) == "global_temporary_terms")
global_temporary_terms = true;
else if (Get_Argument(prhs[i]) == "print")
print = true;
else if (Get_Argument(prhs[i]) == "no_print_error")
print_error = false;
else
for (int i = 0; i < nrhs; i++)
{
if (!mxIsChar(prhs[i]))
{
switch (count_array_argument)
{
pos = 0;
if (Get_Argument(prhs[i]).substr(0, 5) == "block")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 5);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 5;
block = atoi(Get_Argument(prhs[i]).substr(pos, string::npos).c_str())-1;
}
else if (Get_Argument(prhs[i]).substr(0, 13) == "extended_path")
{
*extended_path = true;
if ((i+1) >= nrhs)
*ep_struct = nullptr;
else
{
*ep_struct = mxDuplicateArray(prhs[i + 1]);
i++;
}
}
else if (Get_Argument(prhs[i]).substr(0, 6) == "pfplan")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 6);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 6;
*pfplan_struct_name = deblank(Get_Argument(prhs[i]).substr(pos, string::npos));
}
else if (Get_Argument(prhs[i]).substr(0, 4) == "plan")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 4);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 4;
*plan_struct_name = deblank(Get_Argument(prhs[i]).substr(pos, string::npos));
}
else
{
ostringstream tmp;
tmp << " in main, unknown argument : " << Get_Argument(prhs[i]) << "\n";
throw FatalExceptionHandling(tmp.str());
}
case 0:
*yd = mxGetPr(prhs[i]);
row_y = mxGetM(prhs[i]);
col_y = mxGetN(prhs[i]);
break;
case 1:
*xd = mxGetPr(prhs[i]);
row_x = mxGetM(prhs[i]);
col_x = mxGetN(prhs[i]);
break;
case 2:
*params = mxGetPr(prhs[i]);
break;
case 3:
*steady_yd = mxGetPr(prhs[i]);
steady_row_y = mxGetM(prhs[i]);
steady_col_y = mxGetN(prhs[i]);
break;
case 4:
periods = int (mxGetScalar(prhs[i]));
break;
case 5:
*block_structur = mxDuplicateArray(prhs[i]);
break;
case 6:
global_temporary_terms = true;
*GlobalTemporaryTerms = mxDuplicateArray(prhs[i]);
break;
default:
mexPrintf("Unknown argument count_array_argument=%d\n", count_array_argument);
break;
}
}
count_array_argument++;
}
else
if (Get_Argument(prhs[i]) == "static")
steady_state = true;
else if (Get_Argument(prhs[i]) == "dynamic")
steady_state = false;
else if (Get_Argument(prhs[i]) == "evaluate")
evaluate = true;
else if (Get_Argument(prhs[i]) == "global_temporary_terms")
global_temporary_terms = true;
else if (Get_Argument(prhs[i]) == "print")
print = true;
else if (Get_Argument(prhs[i]) == "no_print_error")
print_error = false;
else
{
pos = 0;
if (Get_Argument(prhs[i]).substr(0, 5) == "block")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 5);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 5;
block = atoi(Get_Argument(prhs[i]).substr(pos, string::npos).c_str())-1;
}
else if (Get_Argument(prhs[i]).substr(0, 13) == "extended_path")
{
*extended_path = true;
if ((i+1) >= nrhs)
*ep_struct = nullptr;
else
{
*ep_struct = mxDuplicateArray(prhs[i + 1]);
i++;
}
}
else if (Get_Argument(prhs[i]).substr(0, 6) == "pfplan")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 6);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 6;
*pfplan_struct_name = deblank(Get_Argument(prhs[i]).substr(pos, string::npos));
}
else if (Get_Argument(prhs[i]).substr(0, 4) == "plan")
{
size_t pos1 = Get_Argument(prhs[i]).find("=", pos + 4);
if (pos1 != string::npos)
pos = pos1 + 1;
else
pos += 4;
*plan_struct_name = deblank(Get_Argument(prhs[i]).substr(pos, string::npos));
}
else
{
ostringstream tmp;
tmp << " in main, unknown argument : " << Get_Argument(prhs[i]) << "\n";
throw FatalExceptionHandling(tmp.str());
}
}
}
if (count_array_argument > 0 && count_array_argument < 5)
{
if (count_array_argument == 3 && steady_state)
@ -234,24 +207,13 @@ Get_Arguments_and_global_variables(int nrhs,
}
}
#ifdef DEBUG_EX
int
main(int nrhs, const char *prhs[])
#else
/* The gateway routine */
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
#endif
void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
mxArray *M_, *oo_, *options_;
mxArray *GlobalTemporaryTerms;
#ifndef DEBUG_EX
mxArray *block_structur = nullptr;
#else
int nlhs = 0;
char *plhs[1];
load_global((char *) prhs[1]);
#endif
mxArray *pfplan_struct = nullptr;
ErrorMsg error_msg;
size_t i, row_y = 0, col_y = 0, row_x = 0, col_x = 0, nb_row_xd = 0;
@ -287,9 +249,7 @@ main(int nrhs, const char *prhs[])
&params,
&steady_yd, steady_row_y, steady_col_y,
periods,
#ifndef DEBUG_EX
&block_structur,
#endif
steady_state, evaluate, block,
&M_, &oo_, &options_, global_temporary_terms,
print, print_error, &GlobalTemporaryTerms,
@ -902,7 +862,6 @@ main(int nrhs, const char *prhs[])
clock_t t1 = clock();
if (!steady_state && !evaluate && print)
mexPrintf("Simulation Time=%f milliseconds\n", 1000.0*(double (t1)-double (t0))/double (CLOCKS_PER_SEC));
#ifndef DEBUG_EX
bool dont_store_a_structure = false;
if (nlhs > 0)
{
@ -1029,9 +988,6 @@ main(int nrhs, const char *prhs[])
}
}
#else
Free_global();
#endif
if (x)
mxFree(x);
if (y)

View File

@ -1,104 +0,0 @@
function bytecode_debug()
global M_ oo_ options_ AA;
fid = fopen([M_.fname '_options.txt'],'wt');
nfields = fieldnames(options_);
fprintf(fid, '%d %d %d\n',size(nfields,1), size(options_,1), size(options_,2));
for i=1:size(nfields, 1)
disp(nfields(i));
if iscell(nfields(i))
AA = cell2mat(nfields(i));
else
AA = nfields(i);
end;
if iscell(AA)
AA = cell2mat(AA);
end;
fprintf(fid, '%s\n', AA);
Z = getfield(options_, AA);
print_object(fid, Z);
end;
fclose(fid);
fid = fopen([M_.fname '_M.txt'],'wt');
nfields = fields(M_);
fprintf(fid, '%d %d %d\n',size(nfields,1), size(M_,1), size(M_,2));
for i=1:size(nfields, 1)
disp(nfields(i));
if iscell(nfields(i))
AA = cell2mat(nfields(i));
else
AA = nfields(i);
end;
fprintf(fid, '%s\n', AA);
print_object(fid, getfield(M_, AA));
end;
fclose(fid);
fid = fopen([M_.fname '_oo.txt'],'wt');
nfields = fields(oo_);
fprintf(fid, '%d %d %d\n',size(nfields,1), size(oo_,1), size(oo_,2));
for i=1:size(nfields, 1)
disp(nfields(i));
if iscell(nfields(i))
AA = cell2mat(nfields(i));
else
AA = nfields(i);
end;
if iscell(AA)
AA = cell2mat(AA);
end;
fprintf(fid, '%s\n', AA);
print_object(fid, getfield(oo_, AA));
end;
fclose(fid);
function print_object(fid, object_arg)
if iscell(object_arg)
object = cell2mat(object_arg);
else
object = object_arg;
end;
if isa(object,'float') == 1
fprintf(fid, '%d ', 0);
fprintf(fid, '%d %d\n',size(object,1), size(object,2));
fprintf(fid, '%f\n', object);
%for i=1:size(object, 2)
%for j=1:size(object, 1)
%fprintf(fid, '%f\n', object(i,j));
%end;
%end;
elseif isa(object,'char') == 1
fprintf(fid, '%d ', 3);
fprintf(fid, '%d %d\n',size(object,1), size(object,2));
%object
for i=1:size(object, 1)
%for i=1:size(object, 2)
fprintf(fid, '%s ', object(i,:));
%end;
%fprintf(fid, '\n');
end;
fprintf(fid, '\n');
elseif isa(object,'struct') == 1
fprintf(fid, '%d ', 5);
nfields = fields(object);
fprintf(fid, '%d %d %d\n',size(nfields,1), size(object,1), size(object,2));
for j=1:size(object, 1) * size(object, 2)
nfields = fields(object(j));
for i=1:size(nfields, 1)
if iscell(nfields(i))
AA = cell2mat(nfields(i));
else
AA = nfields(i);
end;
fprintf(fid, '%s\n', AA);
print_object(fid, getfield(object, AA));
end;
end;
else
disp(['type ' object 'note handle']);
end;

View File

@ -1,522 +0,0 @@
/*
* Copyright © 2007-2021 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mex_interface.hh"
#include <cstring>
#include <sstream>
using namespace std;
map<string, mxArray *> mxglobal;
mxArray_tag::mxArray_tag()
{
type = mxDOUBLE_CLASS;
Ir = NULL;
Jc = NULL;
Nzmax = 0;
field_name.clear();
field_array.clear();
size_1 = 0;
size_2 = 0;
data = NULL;
}
int
mexPrintf(const char *str, ...)
{
va_list args;
int retval;
va_start(args, str);
retval = vprintf(str, args);
va_end(args);
return retval;
}
void
mexErrMsgTxt(const string str)
{
perror(str.c_str());
exit(EXIT_FAILURE);
}
void
mexWarnMsgTxt(const string str)
{
perror(str.c_str());
}
void
mxFree(void *to_release)
{
free(to_release);
}
void *
mxMalloc(unsigned int amount)
{
return malloc(amount);
}
void *
mxRealloc(void *to_extend, unsigned int amount)
{
return realloc(to_extend, amount);
}
void *
mxCalloc(unsigned int nb_elements, unsigned int amount_per_element)
{
return calloc(nb_elements, amount_per_element);
}
void
mexEvalString(const string str)
{
}
double *
mxGetPr(const mxArray *b_m)
{
return b_m->data;
}
mxArray *
mxCreateDoubleMatrix(unsigned int rows, unsigned int cols, mxData_type mx_type)
{
auto *Array = new mxArray;
Array->type = mxDOUBLE_CLASS;
Array->size_1 = rows;
Array->size_2 = cols;
Array->data = (double *) mxMalloc(rows*cols*sizeof(double));
return (Array);
}
mxArray *
mxCreateCharArray(unsigned int rows, unsigned int cols, mxData_type mx_type)
{
auto *Array = new mxArray;
Array->type = mxCHAR_CLASS;
Array->size_1 = rows;
Array->size_2 = cols;
Array->data = (double *) mxMalloc(2*rows*cols*sizeof(char));
return (Array);
}
mxArray *
mxCreateSparse(unsigned int rows, unsigned int cols, unsigned int nz_max, mxData_type mx_type)
{
auto *Array = new mxArray;
Array->type = mxSPARSE_CLASS;
Array->size_1 = rows;
Array->size_2 = cols;
Array->Nzmax = nz_max;
Array->data = (double *) mxMalloc(nz_max*sizeof(double));
Array->Ir = (mwIndex *) mxMalloc(nz_max*sizeof(mwIndex));
Array->Jc = (mwIndex *) mxMalloc((cols+1)*sizeof(mwIndex));
return (Array);
}
mxArray *
mxCreateDoubleScalar(double value)
{
auto *Array = new mxArray;
Array->type = mxSINGLE_CLASS;
Array->size_1 = 1;
Array->size_2 = 1;
Array->data = (double *) mxMalloc(sizeof(double));
Array->data[0] = value;
return (Array);
}
mxArray *
mxCreatNULLMatrix()
{
return NULL;
}
mxArray *
mxCreateStructMatrix(unsigned int rows, unsigned int cols, unsigned int nfields, const vector<string> &fieldnames)
{
auto *Array = new mxArray;
Array->type = mxSTRUCT_CLASS;
Array->size_1 = rows;
Array->size_2 = cols;
for (unsigned int i = 0; i < nfields; i++)
Array->field_name.push_back(fieldnames[i]);
return Array;
}
void
mxDestroyArray(mxArray *A_m)
{
mxFree(A_m->data);
if (A_m->Ir)
mxFree(A_m->Ir);
if (A_m->Jc)
mxFree(A_m->Jc);
mxFree(A_m);
}
mxArray *
mxSetNzmax(mxArray *A_m, mwSize nz_max)
{
A_m->Nzmax = nz_max;
A_m->data = (double *) mxRealloc(A_m->data, nz_max*sizeof(double));
A_m->Ir = (mwIndex *) mxRealloc(A_m->Ir, nz_max*sizeof(mwIndex));
return (A_m);
}
void
mexCallMATLAB(unsigned int n_lhs, mxArray *matrix_lhs[], unsigned int n_rhs, mxArray *matrix_rhs[], const char *function)
{
if (strncmp(function, "disp", 4) == 0)
{
int cols = mxGetN(matrix_rhs[0]);
int rows = mxGetM(matrix_rhs[0]);
if (matrix_rhs[0]->type == mxCHAR_CLASS)
{
char *p = (char *) matrix_rhs[0]->data;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
mexPrintf("%c", p[2*(i+j*rows)]);
mexPrintf("\n");
}
}
else
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
mexPrintf("%8.4f ", matrix_rhs[0]->data[i+j*rows]);
mexPrintf("\n");
}
}
}
mxArray *
mexGetVariable(const char *space_name, const char *matrix_name)
{
if (strncmp(space_name, "global", 6) != 0)
mexErrMsgTxt("space_name not handle in mexGetVariable\n");
return mxglobal[matrix_name];
}
int
mxGetFieldNumber(const mxArray *Struct, const char *field_name)
{
auto it = find(Struct->field_name.begin(), Struct->field_name.end(), field_name);
if (it == Struct->field_name.end())
{
stringstream tmp;
tmp << "unknown field name '" << field_name << "' in mxGetFieldNumber\n";
mexErrMsgTxt(tmp.str());
}
return it - Struct->field_name.begin();
}
mxArray *
mxGetFieldByNumber(mxArray *Struct, unsigned int pos, unsigned int field_number)
{
if (pos > Struct->size_1 * Struct->size_2)
mexErrMsgTxt("index out of range in mxGetFieldByNumber\n");
if (field_number > Struct->field_name.size())
mexErrMsgTxt("field_number out of range in mxGetFieldByNumber\n");
return Struct->field_array[pos][field_number];
}
int
mxAddField(mxArray *Struct, const char *fieldname)
{
if (!mxIsStruct(Struct))
return -1;
else
{
Struct->field_name.emplace_back(fieldname);
return Struct->field_name.size();
}
}
void
mxSetFieldByNumber(mxArray *Struct, mwIndex index, unsigned int field_number, mxArray *pvalue)
{
if (index >= Struct->size_1 * Struct->size_2)
mexErrMsgTxt("index out of range in mxSetFieldByNumber\n");
unsigned int nfields = Struct->field_name.size();
if (field_number >= nfields)
mexErrMsgTxt("field_number out of range in mxSetFieldByNumber\n");
while (Struct->field_array.size() <= index)
Struct->field_array.emplace_back(nfields, NULL);
Struct->field_array[index][field_number] = pvalue;
}
int
mxGetString(const mxArray *array, char *buf, unsigned int buflen)
{
unsigned int size;
if (!mxIsChar(array))
return -1;
else if (buflen <= (size = mxGetNumberOfElements(array)))
return -1;
else
{
char *pchar = (char *) array->data;
for (unsigned int i = 0; i < size; i++)
buf[i] = pchar[2*i];
buf[size+1] = ' ';
}
return 0;
}
mxArray *
mxDuplicateArray(const mxArray *array)
{
unsigned int Size;
unsigned int i;
vector<mxArray *>::const_iterator it_v_array;
vector<string>::const_iterator it_v_string;
auto *Array = (mxArray *) mxMalloc(sizeof(mxArray));
Array->type = array->type;
Array->size_1 = array->size_1;
Array->size_2 = Array->size_2;
switch (array->type)
{
case mxDOUBLE_CLASS:
case mxSINGLE_CLASS:
Size = array->size_1*array->size_2*sizeof(double);
Array->data = (double *) mxMalloc(Size);
memcpy(Array->data, array->data, Size);
break;
case mxSTRUCT_CLASS:
for (i = 0; i < array->size_1 * array->size_2; i++)
for (it_v_array = array->field_array[i].begin(); it_v_array != array->field_array[i].end(); it_v_array++)
Array->field_array[i].push_back(mxDuplicateArray(*it_v_array));
for (it_v_string = array->field_name.begin(); it_v_string != array->field_name.end(); it_v_string++)
Array->field_name.push_back(*it_v_string);
break;
case mxSPARSE_CLASS:
Array->Nzmax = array->Nzmax;
Size = array->Nzmax * sizeof(double);
Array->data = (double *) mxMalloc(Size);
memcpy(Array->data, array->data, Size);
Size = array->Nzmax * sizeof(mwIndex);
Array->Ir = (mwIndex *) mxMalloc(Size);
memcpy(Array->Ir, array->Ir, Size);
Size = array->size_2*sizeof(mwIndex);
Array->Jc = (mwIndex *) mxMalloc(Size);
memcpy(Array->Jc, array->Jc, Size);
break;
case mxCHAR_CLASS:
Size = 2*array->size_1*array->size_2*sizeof(char);
Array->data = (double *) mxMalloc(Size);
memcpy(Array->data, array->data, Size);
break;
default:
ostringstream tmp;
tmp << "Array type not handle: " << array->type << "\n";
mexErrMsgTxt(tmp.str());
}
return (Array);
}
mxArray *
read_struct(FILE *fid)
{
unsigned int nfields = 0;
fscanf(fid, "%d", &nfields);
unsigned int size_1, size_2;
fscanf(fid, "%d", &size_1);
fscanf(fid, "%d", &size_2);
vector<string> fieldnames;
vector<mxArray *> v_Array;
vector<vector<mxArray * >> vv_Array;
for (unsigned int j = 0; j < size_1 * size_2; j++)
{
for (unsigned int i = 0; i < nfields; i++)
{
char name[512];
fscanf(fid, "%s", name);
if (j == 0)
fieldnames.emplace_back(name);
v_Array.push_back(read_Array(fid));
}
vv_Array.push_back(v_Array);
v_Array.clear();
}
mxArray *Struct;
Struct = mxCreateStructMatrix(size_1, size_2, nfields, fieldnames);
for (unsigned int j = 0; j < size_1 * size_2; j++)
{
v_Array = vv_Array[j];
for (unsigned int i = 0; i < nfields; i++)
{
mxSetFieldByNumber(Struct, j, i, v_Array[i]);
}
}
return Struct;
}
mxArray *
read_double_array(FILE *fid)
{
int size_1, size_2;
fscanf(fid, "%d", &size_1);
fscanf(fid, "%d", &size_2);
mxArray *A = mxCreateDoubleMatrix(size_1, size_2, mxREAL);
double *data = mxGetPr(A);
float f;
for (int i = 0; i < size_1*size_2; i++)
{
fscanf(fid, "%f", &f);
data[i] = f;
}
return A;
}
mxArray *
read_char_array(FILE *fid)
{
unsigned int size_1, size_2;
fscanf(fid, "%d", &size_1);
fscanf(fid, "%d", &size_2);
mxArray *A = mxCreateCharArray(size_1, size_2, mxREAL);
char *data = (char *) mxGetPr(A);
char pchar[size_2+1];
for (unsigned int i = 0; i < size_1; i++)
{
fscanf(fid, "%s", pchar);
for (unsigned int j = 0; j < strlen(pchar); j++)
data[2*(i+j*size_1)] = pchar[j];
for (unsigned int j = strlen(pchar); j < size_2; j++)
data[2*(i+j*size_1)] = ' ';
}
return A;
}
mxArray *
read_Array(FILE *fid)
{
mxArray *Array;
mxArray_type array_type;
unsigned int j;
fscanf(fid, "%d", &j);
array_type = static_cast<mxArray_type>(j);
switch (array_type)
{
case mxDOUBLE_CLASS:
case mxSINGLE_CLASS:
Array = read_double_array(fid);
break;
case mxSTRUCT_CLASS:
Array = read_struct(fid);
break;
case mxCHAR_CLASS:
Array = read_char_array(fid);
break;
default:
ostringstream tmp;
tmp << "Array type not handle in read_Array: " << array_type << "\n";
mexErrMsgTxt(tmp.str());
}
return (Array);
}
void
load_global(char *file_name)
{
FILE *fid;
ostringstream tmp_out("");
tmp_out << file_name << "_options.txt";
fid = fopen(tmp_out.str().c_str(), "r");
if (fid == NULL)
{
string s = "Can't open file ";
s += tmp_out.str().c_str();
mexErrMsgTxt(s);
}
mxglobal["options_"] = read_struct(fid);
fclose(fid);
tmp_out.str("");
tmp_out << file_name << "_M.txt";
fid = fopen(tmp_out.str().c_str(), "r");
if (fid == NULL)
{
string s = "Can't open file ";
s += tmp_out.str().c_str();
mexErrMsgTxt(s);
}
mxglobal["M_"] = read_struct(fid);
fclose(fid);
tmp_out.str("");
tmp_out << file_name << "_oo.txt";
fid = fopen(tmp_out.str().c_str(), "r");
if (fid == NULL)
{
string s = "Can't open file ";
s += tmp_out.str().c_str();
mexErrMsgTxt(s);
}
mxglobal["oo_"] = read_struct(fid);
fclose(fid);
}
void
Free_simple_array(mxArray *array)
{
mxFree(array->data);
delete array;
}
void
Free_struct_array(mxArray *array)
{
for (unsigned int i = 0; i < array->size_1 * array->size_2; i++)
for (unsigned int j = 0; j < array->field_name.size(); j++)
Free_Array(array->field_array[i][j]);
delete array;
}
void
Free_Array(mxArray *array)
{
switch (array->type)
{
case mxDOUBLE_CLASS:
case mxSINGLE_CLASS:
case mxCHAR_CLASS:
Free_simple_array(array);
break;
case mxSTRUCT_CLASS:
Free_struct_array(array);
break;
default:
mexErrMsgTxt("Array type not handle in read_Array\n");
}
}
void
Free_global()
{
for (auto & it : mxglobal)
Free_Array(it.second);
}

View File

@ -1,167 +0,0 @@
/*
* Copyright © 2007-2021 Dynare Team
*
* This file is part of Dynare.
*
* Dynare is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Dynare is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MEX_INTERFACE_HH_INCLUDED
#define MEX_INTERFACE_HH_INCLUDED
#include <cstring>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;
typedef unsigned int mwIndex;
typedef unsigned int mwSize;
enum mxData_type
{
mxREAL,
mxCOMPLEX
};
enum mxArray_type
{
mxDOUBLE_CLASS = 0,
mxSINGLE_CLASS = 1,
mxLOGICAL_CLASS = 2,
mxCHAR_CLASS = 3,
mxSPARSE_CLASS = 4,
mxSTRUCT_CLASS = 5,
mxCELL_CLASS = 6,
mxFUNCTION_CLASS = 7,
mxUINT8_CLASS = 8,
mxINT16_CLASS = 9,
mxUINT16_CLASS = 10,
mxINT32_CLASS = 11,
mxUINT32_CLASS = 12,
mxINT64_CLASS = 13,
mxUINT64_CLASS = 14
};
class mxArray_tag
{
public:
unsigned int size_1, size_2;
mwIndex *Ir, *Jc;
int Nzmax;
double *data;
mxArray_type type;
vector<string> field_name;
vector<vector<mxArray_tag *>> field_array;
mxArray_tag();
};
typedef mxArray_tag mxArray;
void load_global(char *file_name);
/*Matlab clone function*/
int mexPrintf(const char *str, ...);
void mexErrMsgTxt(const string str);
void mexWarnMsgTxt(const string str);
void *mxMalloc(unsigned int amount);
void *mxRealloc(void *to_extend, unsigned int amount);
void *mxCalloc(unsigned int nb_elements, unsigned int amount_per_element);
void mxFree(void *to_release);
void mexEvalString(const string str);
double *mxGetPr(const mxArray *b_m);
inline mwIndex *
mxGetIr(const mxArray *A_m)
{
return (mwIndex *) A_m->Ir;
};
inline mwIndex *
mxGetJc(const mxArray *A_m)
{
return (mwIndex *) A_m->Jc;
};
mxArray *mxSetNzmax(mxArray *A_m, mwSize nz_max);
inline int
mxGetNzmax(const mxArray *A_m)
{
return A_m->Nzmax;
};
inline int
mxGetM(const mxArray *A_m)
{
return A_m->size_1;
};
inline int
mxGetN(const mxArray *A_m)
{
return A_m->size_2;
};
inline bool
mxIsChar(const mxArray *A)
{
return A->type == mxCHAR_CLASS;
};
inline bool
mxIsFloat(const mxArray *A)
{
return A->type == mxDOUBLE_CLASS || A->type == mxSINGLE_CLASS;
};
inline bool
mxIsStruct(const mxArray *A)
{
return A->type == mxSTRUCT_CLASS;
};
mxArray *mxCreateDoubleMatrix(unsigned int rows, unsigned int cols, mxData_type mx_type);
mxArray *mxCreateSparse(unsigned int rows, unsigned int cols, unsigned int nz_max, mxData_type mx_type);
mxArray *mxCreateCharArray(unsigned int rows, unsigned int cols, mxData_type mx_type);
mxArray *mxCreateDoubleScalar(double value);
mxArray *mxCreateStructMatrix(unsigned int rows, unsigned int cols, unsigned int nfields, const string &fieldnames);
inline mxArray *
mxCreateStructArray(unsigned int rows, mwSize *cols, int nfields, const string &fieldnames)
{
return mxCreateStructMatrix(rows, *cols, nfields, fieldnames);
};
mxArray *mxCreatNULLMatrix();
void mexCallMATLAB(unsigned int n_lhs, mxArray *lhs[], unsigned int n_rhs, mxArray *rhs[], const char *function);
void mxDestroyArray(mxArray *A_m);
mxArray *read_struct(FILE *fid);
mxArray *read_Array(FILE *fid);
mxArray *read_double_array(FILE *fid);
mxArray *mexGetVariable(const char *space_name, const char *matrix_name);
int mxGetFieldNumber(const mxArray *Struct, const char *field_name);
mxArray *mxGetFieldByNumber(mxArray *Struct, unsigned int pos, unsigned int field_number);
void mxSetFieldByNumber(mxArray *Struct, mwIndex index, unsigned int field_number, mxArray *pvalue);
int mxAddField(mxArray *Struct, const char *field_name);
void mxSetFieldByNumber(mxArray *Struct, mwIndex index, unsigned int fieldnumber, mxArray *pvalue);
inline unsigned int
mxGetNumberOfElements(const mxArray *A)
{
return A->size_1 * A->size_2;
};
int mxGetString(const mxArray *array, char *buf, unsigned int buflen);
inline double
mxGetScalar(const mxArray *array)
{
if (!mxIsFloat(array))
mexErrMsgTxt("not a float array\n");
return array->data[0];
};
mxArray *mxDuplicateArray(const mxArray *array);
void Free_simple_array(mxArray *array);
void Free_struct_array(mxArray *array);
void Free_Array(mxArray *array);
void Free_global();
#endif

View File

@ -1,52 +0,0 @@
function simulate_debug(steady_state)
global M_ oo_ options_;
fid = fopen([M_.fname '_options.txt'],'wt');
if steady_state~=1
fprintf(fid,'%d\n',options_.periods);
end;
fprintf(fid,'%d\n',options_.simul.maxit);
fprintf(fid,'%6.20f\n',options_.slowc);
fprintf(fid,'%6.20f\n',options_.markowitz);
fprintf(fid,'%6.20f\n',options_.dynatol.f);
fprintf(fid,'%d\n',options_.minimal_solving_periods);
fclose(fid);
fid = fopen([M_.fname '_M.txt'],'wt');
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);
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);
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));
fprintf(fid,'%d\n',size(oo_.steady_state,2));
fprintf(fid,'%d\n',size(oo_.exo_steady_state,1));
fprintf(fid,'%d\n',size(oo_.exo_steady_state,2));
fprintf(fid,'%6.20f\n',M_.params);
fclose(fid);
fid = fopen([M_.fname '_oo.txt'],'wt');
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);