Commit Graph

198 Commits (bffd68e0bf5b7976a911bbc98d37d09a855b327d)

Author SHA1 Message Date
Sébastien Villemot 93054cf692
Various filesystem refactorings
– DataTree::packageDir() now takes a std::string_view, returns a
  std::filesystem::path, and no longer creates that directory
– DataTree::writeToFileIfModified() now takes a std::filesystem::path as
  argument
– Do not call DataTree::writeToFileIfModified() for generating MATLAB/Octave
  files, since it does not work (the directory inside which the file is written
  has been deleted by the preprocessor just before)
– Consistently use DataTree::packageDir() everywhere (for compatibility with
  planner_objective)
2022-10-11 16:28:17 +02:00
Sébastien Villemot 5b400b69f0
Use std::filesystem::path::string() when writing paths to std::cout/std::cerr
Otherwise they will be quoted, which is not desirable, especially under
Windows (backslashes will be duplicated).
2022-10-11 11:31:15 +02:00
Sébastien Villemot 3dd40c6c42
use_dll: split generated C file into smaller files that will be compiled in parallel
By the way, factorize the code between the dynamic and static versions.

Ref. #41
2022-10-07 18:04:18 +02:00
Sébastien Villemot dd66459e5f
use_dll: parallelize compilation of multiple object files within single MEX file
In particular, it implements dependency tracking in the thread scheduler, so
that multiple MEX files can share object files.

Ref. #41
2022-10-07 18:04:17 +02:00
Sébastien Villemot 5cf4729ab0
use_dll: dynamically limit the number of concurrently running GCC processes to the number of available logical processors
Ref. #41
2022-10-07 18:04:17 +02:00
Sébastien Villemot 49a530a775
use_dll: allow more flexible output directory in interface of ModelTree::compileMEX() 2022-10-07 18:04:17 +02:00
Sébastien Villemot 4c58451d83
Factorize computing pass for block decomposition
Also add “block_decomposed” data member for tracking whether the block
decomposition has been successful.
2022-10-04 16:00:30 +02:00
Sébastien Villemot 6e69eea3cf
Minor tightening of access specifiers 2022-10-04 16:00:30 +02:00
Sébastien Villemot f6053df754
use_dll: compile MEX files in parallel
Currently two threads are used (one for the dynamic MEX, one for the static
MEX). When the sparse representation is implemented, four threads will be used.

Closes: #41
2022-10-04 16:00:30 +02:00
Sébastien Villemot 3d9ac26aa6
Remove all instances of the “map.find(key)->second” pattern
This is unsafe since the find() method can return a past-the-end iterator,
which should be tested for.

Replace most instances by calls to the std::map::at() method (which throws if
the key is unknown), and which is incidentally more readable.
2022-09-27 12:56:16 +02:00
Sébastien Villemot 18037fd29a
Drop code for supporting temporary terms corresponding to model-local variables
Since model-local variables are now substituted out of the model at an early
stage (see cfb41d291c), such temporary terms are
no longer generated.
2022-09-26 17:34:20 +02:00
Sébastien Villemot 64157d9083
Fix bug in ModelTree::writeParamsDerivativesFileHelper()
Elements of params_derivatives could be accessed without them being defined,
thus causing illegal memory read.

Ensure that these elements always exist. By the way, use std::map::at() instead
of std::map::find() to trigger an exception instead of an illegal memory read
in that case.
2022-09-26 17:17:47 +02:00
Sébastien Villemot 98d4f922ca
Cosmetics 2022-09-20 15:29:19 +02:00
Sébastien Villemot 1088cc4242
Improve informative messages about derivatives and block decomposition
When computing the derivatives or block decomposition of the planner objective,
the epilogue or the original Ramsey model, the preprocessor would talk about
dynamic/static model, which was confusing. It now uses the right terminology.
2022-09-14 17:51:36 +02:00
Sébastien Villemot d67f569035
Always compute block decomposition, even if “block” option was not passed
If block decomposition fails, error out if “block” option was passed, but not
otherwise.

This commit does not modify the generated files.

This is a preliminary step for dynare#1859.
2022-09-13 16:16:00 +02:00
Sébastien Villemot ad40f1365c
Block decomposition: code factorization 2022-07-21 18:20:35 +02:00
Sébastien Villemot a97a41f6c0
Bugfix with temporary terms in block+bytecode
There were actually two distinct bugs, leading to incorrect results in some
corner cases:

– in the “evaluate” mode of the bytecode MEX, the temporary terms of the
  derivatives “evaluate” blocks were not evaluated at runtime; but these
  temporary terms may be needed for residuals of subsequent blocks;

– when the bytecode MEX was only computing residuals of the model (and not 1st
  order derivatives), the temporary terms of the derivatives were not evaluated
  at runtime; but these temporary terms may be needed for residuals of subsequent
  blocks.
2022-07-21 17:22:08 +02:00
Sébastien Villemot 1401c85d6a
Bytecode: drop FCUML instruction
It’s redundant with FBINARY{BinaryOpcode::plus}.
2022-07-20 16:02:24 +02:00
Sébastien Villemot a58109d094
Bytecode: refactor methods for writing .cod and .bin files in the block decomposition case 2022-07-20 16:02:24 +02:00
Sébastien Villemot 35dc7fcd81
Bytecode: fix treatment of temporary terms (in non-block mode)
The “temporary_terms_union.insert(it)” statement introduced in the previous
commit was in the wrong block.

By the way, replace the “switch” by an “if constexpr”.
2022-07-14 09:17:11 +02:00
Sébastien Villemot ae150d2b1f
Bytecode: refactor routines for creating .cod file in non-block mode 2022-07-13 18:51:01 +02:00
Sébastien Villemot 89debf429a
Factorize code for writing JSON output 2022-07-12 18:04:05 +02:00
Sébastien Villemot 157c67e04b
New helpers to simplify common operations with derivation IDs 2022-07-12 16:30:45 +02:00
Sébastien Villemot 1d71386f01
Further templating of helpers for writing output files
So that more stuff is computed at compile-time (using constexpr features).
2022-07-12 16:23:28 +02:00
Sébastien Villemot 4370b76ce4
Remove unused function 2022-07-12 16:18:19 +02:00
Sébastien Villemot 9c3eeb7c8d
Refactor code for writing derivatives w.r.t. parameters 2022-07-12 16:18:16 +02:00
Sébastien Villemot 8da663a110
Further factorization in methods for writing static and dynamic files 2022-07-12 16:05:15 +02:00
MichelJuillard 3e1f9dbb82 use @inbounds blocks instead of @inbounds for each line 2022-07-12 13:07:33 +02:00
Sébastien Villemot f38c8278ae
Refactor methods for writing static and dynamic files
– factorize common code between the static and the dynamic version
– reorganise language-specific code into dedicated functions
– use a function template in the main helper to do some computations
  at compile-time (using constexpr features)
2022-07-11 22:29:28 +02:00
Sébastien Villemot 6d2d73a5d7
Bytecode: refactor output methods around an enum class similar to ExprNodeOutputType
By the way, add assertions to ensure that assignment LHS output type is only
used on VariableNode(s) and AbstractExternalFunctionNode(s).
2022-07-06 16:46:40 +02:00
Sébastien Villemot b390390a98
Remove unnecessary inline specifiers
A function defined entirely inside a class/struct/union definition is
implicitly an inline function.
2022-06-24 15:09:08 +02:00
Sébastien Villemot 9bd2973034
Refactor bytecode output around a new BytecodeWriter class and operator<< overloads 2022-06-24 13:10:04 +02:00
Sébastien Villemot 9b04c5436e
Block decomposition: fix crash when a variable appears in an equation but has zero symbolic derivative 2022-06-13 16:32:58 +02:00
Willi Mutschler fe8aaf44fd
🍎 provisions for Apple Silicon package 2022-06-08 15:11:02 +02:00
Sébastien Villemot 171cd65566
Fix bytecode compilation of external function nodes 2022-05-20 11:43:02 +02:00
Sébastien Villemot 17ba60e587
Mark copy constructor and assignment of ModelTree as protected
This limits the risk of slicing.

The same is not possible for DataTree, since that base class is directly used
in some places.
2022-05-19 12:27:39 +02:00
Sébastien Villemot 2795207802
No longer delete move constructor/assignment operator
We follow the advice given by Josuttis in his book about move semantics.

Deleting those member fuctions can be a bug if we want to allow copy semantics,
because overload resolution will no longer fallback to the copy
constructor/assignment operator when given an rvalue.

In particular, this explain why it was not possible to delete the move
assignment operator of the StaticModel class.
2022-05-18 16:33:48 +02:00
Sébastien Villemot 92d3b8166a
C++17 modernization: use std::optional for equation line numbers 2022-05-16 19:10:15 +02:00
Sébastien Villemot 54ca5d9cc0
Improve messages during model normalization
– Indicate whether we are trying to normalize the static or dynamic model
– If failed to normalize the static model, suggest to use the “no_static”
  option
– Remove a superfluous error message
2022-04-19 17:11:13 +02:00
Sébastien Villemot 0db05a886a
Move “using namespace std” statement after inclusion of standard headers
Otherwise clang emits this warning:
 using directive refers to implicitly-defined namespace 'std'
2022-01-06 14:54:57 +01:00
Sébastien Villemot ea6fb40db7
PAC: new “pac_target_info” block and “pac_target_nonstationary” operator
Ref. Madysson/estimation-codes#5
2021-12-16 15:40:47 +01:00
Sébastien Villemot 1f004584e9
New model_remove statement and model_replace block
Factorize the implementation with the existing code for the include_eqs and
exclude_eqs options (which has been refactored by the way).

Ref. #19
2021-12-15 12:11:25 +01:00
Sébastien Villemot 5fe94ed606
Simplify constant equations also in equations marked [static]
The simplifyEquations() methods had to be moved to DynamicModel, in order to
access the static_only_equations member.
2021-12-06 17:17:37 +01:00
Sébastien Villemot 3d215020ae
Fix use_dll on macOS + Octave
Enforce GCC with mkoctfile. Otherwise Clang will be used, and it does not
accept our custom optimization flags.

Ref. dynare#1797
2021-09-13 16:50:13 +02:00
Sébastien Villemot 08ac455fcd
Implement “occbin_constraints” block
Consequently drop “occbin” option to “model”.

Incidentally, allow more values in equation tag names (previously some keywords
such as “alpha” were disallowed).

Ref. #68
2021-07-16 11:34:27 +02:00
NormannR d0ec5eb155 Allows to write M_.objective_tmp_nbr in the driver.m file
Writes the number of temporary terms of the planner objective function derivatives objective_tmp_nbr in the driver.m file.
Useful for the k-order approximations of welfare.
2021-06-11 12:37:31 +02:00
Sébastien Villemot 74ef3457b4
Use secure URL for link to GNU licenses 2021-06-09 17:21:00 +02:00
Sébastien Villemot c5f9ae89e7
Implement new “occbin” option to “model” block
For the time being, the preprocessor will refuse that this option be used with
any command other than estimation.

By the way, remove occbin_likelihood and occbin_smoother options to estimation.

Ref. dynare#569
2021-05-31 10:00:59 +02:00
normann 22fbfb0fba Allows to write M_.NNZDerivatives_objective in the driver.m file
Writes the number of non-zero derivatives of the planner objective function NNZDerivatives_objective in the driver.m file. Useful for the k-order approximations of welfare.
2021-04-22 15:26:25 +02:00
Sébastien Villemot 7468b4d8d7
Remove more stuff related to unfinished decomposition in linear and nonlinear blocks
Ref. #39, dynare!1626
2021-01-25 18:13:35 +01:00
Sébastien Villemot af9bd8f109
Remove the unfinished decomposition in linear and nonlinear blocks
Also remove the det_cond_forecast interface, that was related to it.

Ref. dynare!1626
Closes: #39
2021-01-25 18:03:37 +01:00
Sébastien Villemot db8c5439f7
use_dll: higher order derivatives are now returned as sparse matrices by static/dynamic files
Previously they were returned as 3-column matrices. But this was inconsistent
with the M-file mode.
2020-06-23 17:50:50 +02:00
Sébastien Villemot ad5e196d30
Block decomposition now compatible with 'use_dll' option 2020-06-23 15:13:04 +02:00
Sébastien Villemot b402e35683
In the driver file, provide mapping between model local variables and indices in the temporary terms vector
The structures are M_.model_local_variables_{dynamic,static}_tt_idxs.

Ref. dynare#1722
2020-06-05 17:12:15 +02:00
Sébastien Villemot 90b9b6a1e4
JSON output: write model local variables in parsed model (modfile.json)
Ref. dynare#1723
2020-06-05 16:10:38 +02:00
Sébastien Villemot 7b11898689
use_dll: merge the two generated C source files into a single one
Also, do various other cosmetic/simplification changes.
2020-05-29 16:12:01 +02:00
Sébastien Villemot a6d9ba6e55
Rename EquationType::evaluate_s to evaluateRenormalized, for clarity 2020-05-20 11:49:32 +02:00
Sébastien Villemot c579a45b08
Remove helper now unused because of 1d838e96ff 2020-05-20 11:35:44 +02:00
Sébastien Villemot 4b28f1fe9c
Bytecode: remove unused data structure 2020-05-20 10:31:31 +02:00
Sébastien Villemot 2944484442
Bytecode: rename output methods for better consistency 2020-05-19 17:45:33 +02:00
Sébastien Villemot a2bea00fee
Block decomposition: another fix related to temporary terms refactoring
Temporary terms need to be computed per equation (as was done previously), and
not simply per block.

It’s necessary to track temporary terms per equation, because some equations
are evaluated instead of solved, and an equation E1 may depend on the value of
an endogenous Y computed by a previously evaluated equation E2; in this case,
if some temporary term TT of equation E2 contains Y, then TT needs to be
computed after E1, but before E2.
2020-05-13 16:58:19 +02:00
Sébastien Villemot 2e6bc7ecaa
Bytecode: fix handling of temporary terms
They were broken by 49036f166d
2020-05-13 12:53:47 +02:00
Sébastien Villemot 49036f166d
Block decomposition: refactor code for computing temporary terms
In particular, in dynamic models, temporary terms are now computed for
derivatives w.r.t. exogenous, and also w.r.t. endogenous variables that do not
belong to the block.
2020-05-12 18:39:13 +02:00
Sébastien Villemot 928aa977b6
Block decomposition: replace expr_t by BinaryOpNode * in a couple of places 2020-05-07 15:24:35 +02:00
Sébastien Villemot c699d57308
Block decomposition: no longer compute static Jacobian, it’s not used
Also remove a message about elements below the cutoff that was no longer
correct (elements below the cutoff have no impact on the incidence matrix
outside of normalization).
2020-05-06 18:11:11 +02:00
Sébastien Villemot 4f453320d1
Block decomposition: remove unneeded code for exogenous 2020-05-05 16:17:22 +02:00
Sébastien Villemot cb61ee8506
Block decomposition: make structures storing derivatives w.r.t. other endo/exo symmetric with those for endos belonging to the block 2020-04-30 18:24:24 +02:00
Sébastien Villemot a8c2defe26
Block decomposition: compute simulation types into separate function; prologue and epilogue are no longer class members
By the way, remove the BlockType stuff which was purely informative (and it’s
not worth carrying over prologue and epilogue information just for that).
2020-04-30 12:48:16 +02:00
Sébastien Villemot 26fc71ffa7
Block decomposition: store max lead/lag of all variables in the “blocks” structure
By the way, regroup all code that computes the dynamic structure of a block
into a single function.
2020-04-30 12:03:14 +02:00
Sébastien Villemot 7f57821401
Now compute blocks[].first_equation from ModelTree::computeBlockDecompositionAndFeedbackVariablesForEachBlock
Also include various cosmetic changes.
2020-04-29 18:48:42 +02:00
Sébastien Villemot 4948adf2cd
Block decomposition: remove unused data structure 2020-04-29 17:47:48 +02:00
Sébastien Villemot f39c687d85
Block decomposition: store mapping between endos/eqs and their block
Previously this computation was done at several places.
2020-04-29 17:42:46 +02:00
Sébastien Villemot 39407083be
Block decomposition: ensure that the cutoff option only impacts normalization
Previously, the cutoff option would also impact the block decomposition itself,
since it would had an influence on the incidence matrix used for computing the
blocks and their derivatives.

The problem is that, in the general case, it’s quite possible that an element
of the numerical Jacobian be zero at the evaluation point, while being quite
different from zero along the simulation path. A typical example is an
expression of the type x*y, where y is an endogenous and x is an exogenous not
present in the initval block (and hence initialized to zero).
2020-04-29 12:41:39 +02:00
Sébastien Villemot 470cb5fcb0
Block decomposition: remove mostly unused structure ModelTree::derivative_endo
It has been superseded by ModelTree::blocks_derivatives.

By the way, fix the initial number of non-zero elements in sparse Jacobian.

Also avoid computing suboptimal temporary terms.
2020-04-28 14:12:29 +02:00
Sébastien Villemot 2e136cae56
Block decomposition: simplify computation of block derivatives
— use a std::map for storing block derivatives
— remove redundant ModelTree::first_chain_rule_derivatives structure
— remove unused codepaths in StaticModel
— DynamicModel: simplify code that determines the type of derivatives in a
  block. We now use a slightly different categorization.
— by the way, fix the max lead/lag information for blocks that are obtained via
  merging. A workaround was previously implemented in
  DynamicModel::get_Derivative(), but it is no longer needed with this fix.
2020-04-28 12:26:51 +02:00
Sébastien Villemot 5ee45f5bd4
Block decomposition: remove helpers that are no longer needed since data structure are cleaner
Add a new helper for getting the number of recursive variables in a block.
2020-04-23 16:31:57 +02:00
Sébastien Villemot 118ceab85b
Block decomposition: reorganize data structures storing block information 2020-04-21 18:25:33 +02:00
Sébastien Villemot 8eafd9ab4f
Block decomposition: various refactorings 2020-04-17 19:23:52 +02:00
Sébastien Villemot a741799f90
Block decomposition: factorize various helper functions
Should have been done in 76c2e87c3b.
2020-04-17 16:04:19 +02:00
Sébastien Villemot 7327fb9f17
Block decomposition: give more explicit names to ModelTree::{inv_,}{equation,variable}_reordered 2020-04-17 15:09:22 +02:00
Sébastien Villemot 4e819f09b2
Block decomposition: various refactorings 2020-04-15 17:56:28 +02:00
Sébastien Villemot 4d30f17e0b
Block decomposition: move collectFirstOrderDerivativesEndogenous in ModelTree
By the way, use camel case for the function name.
2020-03-30 14:51:53 +02:00
Sébastien Villemot 9263a02a9b
Skip constant equations replacement only when there is an “mcp” tag
Since bef537d40a, constant equations were not
simplified as soon as they had a tag attached.

But this is too wide a restriction. In particular, this breaks the trend
component models which have a target that is set to a constant.

So we now only skip the replacement in the case where there is an “mcp” tag.

Ref. dynare#1697
2020-03-27 18:17:04 +01:00
Sébastien Villemot 50776a766a
Remove the unsignedness of many integer variables and function arguments
This was only adding unneeded complexity, for no clear reason (we’re very far
from reaching 2³¹ equations, and if we wanted to support models that large, it
would be better to use long integers to avoid being limited to 2³²).
2020-03-24 18:28:03 +01:00
Sébastien Villemot 78ff24ea14
Block decomposition: misc style and doc improvements 2020-03-24 16:57:45 +01:00
Sébastien Villemot 5d6e6338f4
Block decomposition: remove unused codepaths in computation of normalization 2020-03-20 18:49:31 +01:00
Sébastien Villemot 097bdfde03
Misc simplifications and cosmetics 2020-03-20 18:42:59 +01:00
Sébastien Villemot ce1cbb3e52
Block decomposition: turn EquationType into an enum class 2020-03-20 18:00:56 +01:00
Sébastien Villemot 8b4d046f9f
Block decomposition: turn BlockSimulationType into an enum class 2020-03-20 17:31:14 +01:00
Sébastien Villemot f1061e8df1
Block decomposition: turn BlockType into an enum class 2020-03-20 16:38:43 +01:00
Sébastien Villemot fcef9cf8b9
Block decomposition: further streamlining of function prototypes 2020-03-20 15:23:23 +01:00
Sébastien Villemot bd6eee93df
Block decomposition: refactor the prototypes of various functions
— return output arguments on the left-hand side
— do not pass class members as input/output arguments

By the way, fix a (benign) vector allocation bug in
{Static,Dynamic}Model::computeChainRuleJacobian().
2020-03-19 17:47:12 +01:00
Sébastien Villemot 76c2e87c3b
Block decomposition: factorize data structures between StaticModel and DynamicModel 2020-03-17 18:58:34 +01:00
Houtan Bastani 7371558321
rework equation tags
Create new EquationTags class to simplify use of equation tags throughout the code and avoid repeated code

issue #38
2020-02-21 17:49:38 +01:00
Sébastien Villemot bef537d40a
Do not simplify constant equations which have a tag attached
In particular, in the case of an 'mcp' tag, this could lead to a convergence
failure.

Ref. dynare#1697
2020-01-27 16:09:05 +01:00
Sébastien Villemot 23a08a3662
Ensure that dynamic model files contains all the derivatives that have been computed
Previously, there could be a discrepancy if the order mandated by
“identification” was less that the one mandated by “stoch_simul”.

Ref. #40
2020-01-20 17:22:32 +01:00
Sébastien Villemot 4e2538a2a2
Ensure original tags and line numbers are preserved with ramsey_model
In particular, introduce a method for clearing all equations, so that tags and
line numbers are also cleared.

Ref. dynare#1685
2020-01-06 18:26:35 +01:00
Sébastien Villemot e2d5a83592
Global reindentation 2019-12-20 16:59:30 +01:00
Sébastien Villemot 031c0cff5f
Various code simplifications and modernizations 2019-12-16 19:42:59 +01:00
Sébastien Villemot 2af26ee2c2
Ramsey: use information from transformed model for filling M_.nonzero_hessian_eqs
Since commit 9c9e8f816f, it’s the information
from the original model which was in this field, which is not what is expected.

By the way, do not output this field (and the related M_.hessian_eq_zero) when
the Hessian is not computed by the preprocessor (i.e. in practice for perfect
foresight), since they would otherwise contain incorrect information.

Ref. dynare#1681
2019-12-13 18:02:35 +01:00