Commit Graph

499 Commits (master)

Author SHA1 Message Date
Sébastien Villemot f55019c41e
Bytecode: rename several classes and class members, for consistency and clarity 2023-12-14 16:17:22 +01:00
Sébastien Villemot 22709f8225
Move bytecode stuff into a dedicated namespace, for better code separation 2023-12-14 15:48:22 +01:00
Sébastien Villemot 22675728aa
No longer call std::move on trivially-copyable types
Automatically detected by clang-tidy with performance-move-const-arg check.

Do not make the modification for Tokenizer::location type, since we have no
guarantee that the type will remain trivially-copyable in the future.
2023-12-13 15:37:07 +01:00
Sébastien Villemot 7cfe226e58
When possible, use a more efficient overload of std::string::find()
Automatically detected by clang-tidy using the performance-faster-string-find
check.
2023-12-13 14:21:11 +01:00
Sébastien Villemot c2d6ab8ee0
Simplify a few loops using std::ranges::reverse_view
Automatically detected using clang-tidy with modernize-loop-convert check.
2023-12-01 15:06:40 +01:00
Sébastien Villemot 5e89921ea1
Use emplace() and emplace_back() at several places
Automatically detected using clang-tidy with modernize-use-emplace check.
2023-12-01 14:30:03 +01:00
Sébastien Villemot d463607e90
Reformat C++ source code using clang-format 15 2023-11-30 15:58:32 +01:00
Sébastien Villemot 46aa6610ab
model_replace, model_remove: allow selecting an equation with several (conjunct) tags
NB: does not (yet) works with Occbin regime-specific equations.

Ref. dynare#1890
2023-11-15 12:05:06 +01:00
Sébastien Villemot 1ad7dd9672
Refactor parsing of equation tags in a more functional way
Incidentally, makes tag names case-insensitive in “model_replace” and
“model_remove”, which is consistent with tag declarations.
2023-11-14 15:55:30 +01:00
Sébastien Villemot 63f04636ee
New structure M_.block_structure.block().bytecode_jacobs_cols_to_sparse
It maps column indices in the Jacobian returned by bytecode MEX in evaluate
mode (which only contains nonzero columns, but also incorporate recursive
variables), into column indices in the sparse representation (which has 3×n
columns for two-boundaries blocks and n columns for one-boundary blocks).
Columns unused in the sparse representation are indicated by a zero.
2023-11-07 15:32:12 +01:00
Sébastien Villemot 3c7b02e87e
🐛 Bytecode+block+mfs>0: fix Jacobian computation in “evaluate” mode
When parsing the FSTPG{2,3} opcodes, the bytecode MEX internally constructs a
Jacobian with as many lines as feedback variables. But the preprocessor would
also output instructions for filling derivatives of equations corresponding to
recursive variables in the “evaluate” mode, thus leading to memory corruption
in the bytecode MEX.
2023-11-07 15:31:51 +01:00
Sébastien Villemot 17ba27330a
Minor simplification 2023-10-25 14:38:43 +02:00
Sébastien Villemot 6ec7f580d5
New option “static_mfs” to “model” block (and “model_options” command)
Currently defaults to 0.
2023-10-16 11:50:26 -04:00
Sébastien Villemot 43fc59a2bc
The “mfs” option of the “model” block no longer affects the static model
This is a restoration of the behaviour that was present in 5.x.
2023-10-16 11:49:30 -04:00
Sébastien Villemot dec60b25e6
Do not write bytecode for discretionary optimal policy models
Those models do not have as many variables as equations, and this case is not
supported by bytecode.

The present commit is an improvement over commit
a8ea57dd63, which had already removed bytecode
for the planner objective model.
2023-09-27 14:49:06 +02:00
Sébastien Villemot a8ea57dd63
Fix illegal memory access when doing Ramsey
The preprocessor would try to write bytecode for the planner objective. But
bytecode only works when there are as many endogenous as equations, which is
not the case for the PlannerObjective object derived from StaticModel.
2023-09-27 11:23:18 +02:00
Johannes Pfeifer 0990492e2f Remove unused exo_names_orig_ord 2023-06-23 08:36:37 -04:00
Sébastien Villemot 150547b560
🐛 Bytecode + block decomposition: temporary terms were not correctly output
Temporary terms computed in previous blocks were not used in the bytecode
output of a given block. This was inefficient (because this means that
expressions already computed and store in the temporary terms vector would be
recomputed), and incidentally it would break the external functions
output (because it would trigger a lookup in the “TEF terms”, which would thus
fail).

Closes: #115
2023-05-12 17:24:31 +02:00
Sébastien Villemot a53426fc1f
Use ModelTree::addAuxEquation() method instead of directly manipulating ModelTree::aux_equations 2023-04-24 16:52:10 +02:00
Sébastien Villemot 34edd4e143
Bytecode: remove useless FBEGINBLOCK_::endo_nbr data member 2023-04-18 16:54:35 +02:00
Sébastien Villemot 1b7b70ec93
Build system: enable the dangling else warning (-Wdangling-else)
Adapt the code accordingly.
2023-04-11 14:22:32 +02:00
Sébastien Villemot e22d9049ee
Optimization: use std::unordered_map instead of std::map for caching chain rule derivation
Improves performance on very very large models (tens of thousands of equations).
2023-04-05 19:49:16 +02:00
Sébastien Villemot 1ef5feec15
Factorize methods for writing set auxiliary variables/series files 2023-03-28 16:37:05 +02:00
Sébastien Villemot 0169240f76
Ramsey: write derivatives of static model w.r.t. Lagrange multipliers in a new file
The computing of the Ramsey steady state relies on the fact that Lagrange
multipliers appear linearly in the system to be solved. Instead of directly
solving for the Lagrange multipliers along with the other variables,
dyn_ramsey_static.m reduces the size of the problem by always computing the
value of the multipliers that minimizes the residuals, given the other
variables (using a minimum norm solution, easy to compute because of the
linearity property). That function thus needs the derivatives of the optimality
FOCs with respect to the multipliers. The problem is that, when multipliers
appear in an auxiliary variable related to a lead/lag, then those derivatives
need to be retrieved by a chain rule derivation, which cannot be easily done
with the regular static file.

This commit implements the creation of a new file,
ramsey_multipliers_static_g1.{m,mex}, that provides exactly the needed
derivatives w.r.t. Lagrange multipliers through chain rule derivation.

Ref. dynare#633, dynare#1119, dynare#1133
2023-03-28 14:27:51 +02:00
Sébastien Villemot 5ff503a964
Use map::try_emplace() instead of map::emplace() to simplify some calls
By the way, remove a redundant Cluster{} constructor call.
2023-03-23 16:10:42 +01:00
Sébastien Villemot bff80c0eaf
use_dll: under Windows, append MinGW location to the PATH variable only once
Previously, the MinGW location was appended multiple times to the PATH
variable, which in some cases would make the variable too long and thus
dysfunctional.

The variable is now initialized once when the worker threads are created.

By the way, move the macOS+Octave environment variable initializations to the
same place, for consistency.
2023-03-20 18:27:33 +01:00
Sébastien Villemot 7acf278370
Performance improvement of chain rule derivation
Commit 23b0c12d8e introduced caching in chain
rule derivation (used by block decomposition), which increased speed for mfs >
0, but actually decreased it for mfs=0.

This patch introduces the pre-computation of derivatives which are known to be
zero using symbolic a priori (similarly to what is done in the non-chain rule
context). The algorithms are now identical between the two contexts (both
symbolic a priori + caching), the difference being that in the chain rule
context, the symbolic a priori and the cache are not stored within the ExprNode
class, since they depend on the list of recursive variables.

This patch brings a significant performant improvement for all values of the
“mfs” option (the improvement is greater for small values of “mfs”).
2023-03-02 17:52:36 +01:00
Sébastien Villemot ae83974e0e
Minor simplification using if with initialization 2023-02-28 16:27:31 +01:00
Sébastien Villemot 389a2647d3
Block decomposition: make mfs=1 the default
NB: mfs=3 is nevertheless used when “block” option has not been used, and the
model is purely backward/forward or static, for solve_algo={12,14}.
2023-01-25 18:38:58 +01:00
Sébastien Villemot 1c813a1cf9
Run detrending engine if trend variables are present, even if unused in a var(deflator=…) statement
Closes: #113
2023-01-24 14:14:40 +01:00
Sébastien Villemot 74e5bbf537
Drop dynamic_g1_nz.m generated file
No longer needed since perfect_foresight_problem MEX has moved to the sparse
representation.
2023-01-23 16:20:12 +01:00
Sébastien Villemot 4f9241442c
Fix size of dynamic derivatives (legacy representation) when there are unused exogenous and “nostrict” option is given 2023-01-19 11:00:32 +01:00
Sébastien Villemot 9013a5cb1b
No longer create +<basename>/+block/ folder (now empty) 2023-01-17 18:42:40 +01:00
Sébastien Villemot 4bba274f39
Block decomposition: drop unused max lead/lag fields
Note that DynamicModel::determineBlockDerivativesType(), it’s legitimate to
replace max_{lead,lag} by max_endo_{lead,lag}, because for exogenous
lag=lead=0, and we no longer compute derivatives w.r.t. to endogenous that do
not belong to the block (so-called “other” endogenous).
2023-01-17 14:42:29 +01:00
Sébastien Villemot 2e09df90e7
Bytecode w/ block decomposition: no longer output derivatives w.r.t. exogenous and endogenous outside the block 2023-01-17 14:23:40 +01:00
Sébastien Villemot bbdbd0807b
Sparse representation: Octave compatibility fix
Octave does not support “private” subdir inside plus folders (“+namespace”).

See https://savannah.gnu.org/bugs/?45444
2023-01-16 17:12:10 +01:00
Sébastien Villemot 93b9ed6957
Drop the legacy representation of the block-decomposed model
As a consequence, and as a temporary measure, always output the
non-block-decomposed legacy representation.

Also drop the block kalman filter output, and drop now useless variables in
M_.block_structure.
2023-01-13 16:57:06 +01:00
Sébastien Villemot ec3ce156b4
Bytecode: always produce block and non-block versions
The block decomposed version is now under <BASENAME>/bytecode/block/.
2023-01-09 13:35:49 +01:00
Sébastien Villemot cf493ef6a6
Add two missing checks for file opening failures 2023-01-05 17:21:30 +01:00
Sébastien Villemot 253fbbe7d6
Use std::filesystem::path in more places 2023-01-05 17:21:28 +01:00
Sébastien Villemot 9658d82cc6
EquationTags::getEqnByTag() now returns an optional<int> instead of throwing 2023-01-04 16:45:12 +01:00
Sébastien Villemot e2c72a1b75
Drop ModelTree::getEquationTags(), and replace it by calls to better interfaces 2023-01-04 16:20:50 +01:00
Sébastien Villemot bdb5d37dec
EquationTags::getTagValueByEqnAndKey() now return an optional<string>
By the way, optimize the implementation.
2023-01-04 16:03:49 +01:00
Sébastien Villemot 2758f46c83
Fix indexing in M_.endo_trends
It would use 0-based indexing, instead of 1-based.

Ref. dynare#1648

Closes: #109
2022-12-14 12:05:12 +01:00
Sébastien Villemot c48248fc0d
Implement time-recursive block decomposition tuned for purely backward/forward/static models
Such a decomposition has to be simulated with periods as the outer loop and
blocks as the inner loop.

It is enabled by default for purely backward/forward/static models, as long as
the “block” option is not given. In that case, “mfs” is also set to 3 by
default (until that value becomes the global default).

M_.time_recursive_block_decomposition is set to “true” when that decomposition
has been performed, “false” otherwise for the traditional decomposition (the
latter has to be simulated with blocks as the outer loop and periods as the
inner loop).
2022-11-30 14:43:44 +01:00
Sébastien Villemot bb3475d548
Remove call to setup_solvers.m
It is no longer used in the new implementation of solve_algo={12,14}.

However, the M_.lhs field that was created by setup_solvers.m is used from
other places, so reimplement it at the preprocessor level.
2022-11-30 14:43:44 +01:00
Sébastien Villemot 2e171e28de
New files for helping a user to debug their model
The files are created under <basename>/+debug/dynamic_resid.m and
<basename>/+debug/static_resid.m.

Their purpose is to evaluate separately the LHS and RHS of each equation.
2022-11-16 11:18:09 +01:00
Sébastien Villemot 23b0c12d8e
Performance improvement of chain rule derivation, using caching
Useful for mfs > 0 on large models.
2022-11-08 12:34:05 +01:00
Sébastien Villemot 1430ab9cc2
Implement new sparse model representation in C
Ref. dynare#1859
2022-11-04 14:23:29 +01:00
Sébastien Villemot 4ab3e937ea
Implement new sparse model representation in MATLAB/Octave
Ref. dynare#1859
2022-11-04 14:23:29 +01:00