Estimation DLL: make it compatible with changes introduced in 648c3cd177 + code simplification

time-shift
Sébastien Villemot 2010-09-20 19:25:31 +02:00
parent aa8963c1db
commit fbbc21d577
3 changed files with 16 additions and 91 deletions

View File

@ -35,9 +35,9 @@ ModelSolution::ModelSolution(const std::string& dynamicDllFile, size_t n_endo_a
const std::vector<size_t>& zeta_static_arg, double INqz_criterium)
: n_endo(n_endo_arg), n_exo(n_exo_arg), // n_jcols = Num of Jacobian columns = nStat+2*nPred+3*nBoth+2*nForw+nExog
n_jcols (n_exo+n_endo+ zeta_back_arg.size() /*nsPred*/ + zeta_fwrd_arg.size() /*nsForw*/ +2*zeta_mixed_arg.size()),
jacobian (n_endo,n_jcols), residual(n_endo), Mx(2,n_exo),
jacobian (n_endo,n_jcols), residual(n_endo), Mx(1,n_exo),
decisionRules ( n_endo_arg, n_exo_arg, zeta_fwrd_arg, zeta_back_arg, zeta_mixed_arg, zeta_static_arg, INqz_criterium),
dynamicDLLp(dynamicDllFile, n_endo, n_jcols, /* nMax_lag= */ 1, n_exo),
dynamicDLLp(dynamicDllFile, n_exo),
llXsteadyState(n_jcols-n_exo)
{
Mx.setAll(0.0);
@ -78,7 +78,7 @@ ModelSolution::ComputeModelSolution(VectorView &steadyState, const Vector& deepP
llXsteadyState(zeta_back_mixed.size() + n_endo + i) = steadyState(zeta_fwrd_mixed[i]);
//get jacobian
dynamicDLLp.eval(llXsteadyState, Mx, &deepParams, 1, residual, &jacobian, NULL, NULL);
dynamicDLLp.eval(llXsteadyState, Mx, deepParams, steadyState, residual, &jacobian, NULL, NULL);
//compute rules
decisionRules.compute(jacobian,ghx, ghu);

View File

@ -17,20 +17,14 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
//#include "k_ord_dynare.hh"
#include "dynamic_dll.hh"
#include <sstream>
using namespace std;
/***********************************
* Members of DynamicModelDLL for handling loading and calling
* <model>_dynamic () function
**************************************/
DynamicModelDLL::DynamicModelDLL(const std::string &dynamicDllFile, const size_t y_length, const size_t j_cols,
const int n_max_lag, const size_t n_exog) throw (TSException) :
length(y_length), jcols(j_cols), nMax_lag(n_max_lag), nExog(n_exog)
DynamicModelDLL::DynamicModelDLL(const std::string &dynamicDllFile, size_t n_exog_arg) throw (TSException) :
n_exog(n_exog_arg)
{
std::string fName;
#if !defined(__CYGWIN32__) && !defined(_WIN32)
@ -97,57 +91,9 @@ DynamicModelDLL::~DynamicModelDLL()
}
void
DynamicModelDLL::eval(double *y, double *x, int nb_row_x, double *params,
int it_, double *residual, double *g1, double *g2, double *g3)
{
Dynamic(y, x, nb_row_x, params, it_, residual, g1, g2, g3);
}
void
DynamicModelDLL::eval(const Vector &y, const Matrix &x, const Vector *modParams,
int it_, Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException)
{
double *dresidual, *dg1 = NULL, *dg2 = NULL, *dg3 = NULL;
if ((jcols-nExog) != y.getSize())
throw TSException(__FILE__, __LINE__, "DLL Error: (jcols-nExog)!=ys.length()");
if (g1 != NULL)
{
if (g1->getRows() != length) // dummy
throw TSException(__FILE__, __LINE__, "DLL Error: g1 has wrong size");
dg1 = const_cast<double *>(g1->getData());
}
if (g2 != NULL)
dg2 = const_cast<double *>(g2->getData());
//dresidual = const_cast<double *>(residual.getData());
if (g3 != NULL)
dg3 = const_cast<double *>(g3->getData());
dresidual = const_cast<double *>(residual.getData());
double *dy = const_cast<double *>(y.getData());
double *dx = const_cast<double *>(x.getData());
double *dbParams = const_cast<double *>(modParams->getData());
Dynamic(dy, dx, nExog, dbParams, it_, dresidual, dg1, dg2, dg3);
}
void
DynamicModelDLL::eval(const Vector &y, const Matrix &x, const Vector *modParams,
DynamicModelDLL::eval(const Vector &y, const Matrix &x, const Vector &modParams, VectorView &ySteady,
Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException)
{
eval(y, x, modParams, nMax_lag, residual, g1, g2, g3);
}
void
DynamicModelDLL::eval(const Vector &y, const Vector &x, const Vector *modParams,
Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException)
{
/** ignore given exogens and create new 2D x matrix since
* when calling <model>_dynamic(z,x,params,it_) x must be equal to
* zeros(M_.maximum_lag+1,M_.exo_nbr)
**/
Matrix mx(nMax_lag+1, nExog);
mx.setAll(0); // initialise shocks to 0s
eval(y, mx, modParams, nMax_lag, residual, g1, g2, g3);
Dynamic(y.getData(), x.getData(), n_exog, modParams.getData(), ySteady.getData(), 0, residual.getData(),
g1 == NULL ? NULL : g1->getData(), g2 == NULL ? NULL : g2->getData(), g3 == NULL ? NULL : g3->getData());
}

View File

@ -17,18 +17,8 @@
* along with Dynare. If not, see <http://www.gnu.org/licenses/>.
*/
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the K_ORDER_PERTURBATION_EXPORTS
// symbol defined on the command line. this symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// K_ORDER_PERTURBATION_API functions as being imported from a DLL, wheras this DLL sees symbols
// defined with this macro as being exported.
#if defined(_WIN32) || defined(__CYGWIN32__)
# include <windows.h>
# ifdef _MSC_VER
# define K_ORDER_PERTURBATION_API __declspec(dllexport)
# endif
#else
# include <dlfcn.h> // unix/linux DLL (.so) handling routines
#endif
@ -39,8 +29,8 @@
#include "ts_exception.h"
// <model>_Dynamic DLL pointer
typedef void (*DynamicFn)
(double *y, double *x, int nb_row_x, double *params,
typedef void (*DynamicFn)
(const double *y, const double *x, int nb_row_x, const double *params, const double *steady_state,
int it_, double *residual, double *g1, double *g2, double *g3);
/**
@ -50,12 +40,8 @@ typedef void (*DynamicFn)
class DynamicModelDLL
{
private:
DynamicFn Dynamic; // pointer to the Dynamic function in DLL
const size_t length; // tot num vars = Num of Jacobian rows
const size_t jcols; // tot num var t-1, t and t+1 instances + exogs = Num of Jacobian columns
const int nMax_lag; // no of lags
const size_t nExog; // no of exogenous
DynamicFn Dynamic; // pointer to the Dynamic function in DLL
const size_t n_exog; // no of exogenous
#if defined(_WIN32) || defined(__CYGWIN32__)
HINSTANCE dynamicHinstance; // DLL instance pointer in Windows
#else
@ -64,17 +50,10 @@ private:
public:
// construct and load Dynamic model DLL
DynamicModelDLL(const std::string &dynamicDllFile, const size_t length, const size_t jcols,
const int nMax_lag, const size_t nExog) throw (TSException);
DynamicModelDLL(const std::string &dynamicDllFile, size_t n_exog_arg) throw (TSException);
virtual ~DynamicModelDLL();
// evaluate Dynamic model DLL
void eval(double *y, double *x, int nb_row_x, double *params,
int it_, double *residual, double *g1, double *g2, double *g3);
void eval(const Vector &y, const Vector &x, const Vector *params,
Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException);
void eval(const Vector &y, const Matrix &x, const Vector *params,
int it_, Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException);
void eval(const Vector &y, const Matrix &x, const Vector *params,
Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException);
//! evaluate Dynamic model DLL
void eval(const Vector &y, const Matrix &x, const Vector &params, VectorView &ySteady,
Vector &residual, Matrix *g1, Matrix *g2, Matrix *g3) throw (TSException);
};