Commit Graph

324 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 b31de3d9a6
Pre-allocate std::vector instances filled from loops
Automatically detected by clang-tidy using
performance-inefficient-vector-operation check.
2023-12-13 16:19:51 +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 bffd68e0bf
Add a clang-tidy configuration file
Also add some annotations to remove some false positives.

There remain some boost-related false positives, it’s unclear how to suppress
them.
2023-12-04 16:37:00 +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 e6f38e724c
Cosmetics: remove empty list of parameters for lambda functions 2023-10-19 09:55:59 -04:00
Sébastien Villemot f179ec4dac
Use __builtin_unreachable() to silent some GCC warnings
This is cleaner than using exit() or assert() for that purpose.
2023-10-18 14:28:59 -04:00
Sébastien Villemot 86b24dc9bf
Gracefully handle division by zero when attempting equation normalization
Instead of crashing the preprocessor (because DataTree::DivisionByZeroException
is not caught), just abort the normalization and mark it as failed.

Ref. #92
2023-09-28 15:32:38 +02:00
Sébastien Villemot e1e1a753d0
Do not use C++20 aggregate initialization with parentheses
Initialization with initalizer list is nicer. And, more importantly,
initialization with parentheses is not supported by Clang < 16.
2023-07-07 14:32:16 +02:00
Sébastien Villemot 3cc531315b
C++20 modernization: use new standard mathematical constants 2023-05-25 19:06:48 +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 b1e4884237
🐛 sign(0) was returning 1 instead of 0 with use_dll
The C99 copysign() function was used in the generated C output, but that
function does not correctly handle zero. Replace it by a custom sign()
function.
2023-04-07 16:38:23 +02:00
Sébastien Villemot 5a4297088d
Optimization: use std::unordered_map instead of std::map when computing temporary terms
Improve performance on very large models (⩾ 5000 equations).

Note that std::unordered_set cannot be used for the temporary_terms_t type,
because ordering is needed there (for writing the output files).
2023-04-05 19:49:16 +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 fe3f18947e
No longer replace all auxiliary variables by their definition in the static model
This is effectively a revert of commits 1b4f68f934,
32fb90d5f3 and f6f4ea70fb.

This transformation had been introduced in order to fix the computation of the
Ramsey steady state in the case where Lagrange multipliers appeared with a lead
or lag ⩾ 2 (and where thus part of the definition of an auxiliary variable).

But this transformation had introduced bugs in the handling of external
functions which were difficult to tackle.

Moreover, it seems preferable to keep the strict correspondence between the
dynamic and static model, in order to make reasoning about the preprocessor
internals easier (in particular, for this reason this transformation was not
implemented in ModFile::transformPass() but in ModFile::computingPass(), which
was a bit confusing).

A better solution for the Ramsey steady state issue will is implemented in the
descendent of the present commit.

Ref. dynare#633, dynare#1119, dynare#1133
2023-03-28 14:10:13 +02:00
Sébastien Villemot f48a698458
🐛 Suboptimal temporary terms for external functions
If a given external function was called two times with different arguments,
then the second call would always be computed in the same temporary terms
chunk (derivation order or block) as the first call, even if this was not
necessary (technically, the second call would be promoted in the temporary
terms computation in the same category as the first call).

This could possibly lead to an inefficiency.
2023-03-28 14:00:06 +02:00
Sébastien Villemot 712b11a045
🐛 Incorrect stochastic transformation with endo lead ⩾ 2 or exo lead ⩾ 1 in external functions
If an endogenous with a lead ⩾ 2 or an exogenous with a lead ⩾ 1 appeared in
the argument(s) of a call to an external function, the auxiliary variable
transformation was incorrect (the variable was replaced inside the function
call, while it is the whole function call that has to be replaced).

This could lead to incorrect results in stochastic contexts, when the external
function is nonlinear.
2023-03-28 13:56:13 +02:00
Sébastien Villemot 69a394a115
ExprNode: factorization of code that recurisively applies a transformation on a node 2023-03-23 12:39:35 +01:00
Sébastien Villemot 661f116af1
Rename the argument to ExprNode::clone() for clarity
The argument had the same name as the data member “datatree”, so this could
lead to confusion (though there was no bug, since the argument was masking
the data member).
2023-03-23 12:39:35 +01:00
Sébastien Villemot 2f111f5055
ExprNode: simplification of external function nodes clone() and toStatic() methods 2023-03-23 12:39:11 +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 f8edce01ec
Minor simplification 2023-03-02 17:28:03 +01:00
Sébastien Villemot fe83933b1d
Bugfix: undefined behaviour in AbstractExternalFunctionNode::prepareForDerivation()
Input and output ranges should not overlap when calling std::set_union(),
otherwise the behaviour is undefined.

It seems that in this precise case the computation would still be
correct (though inefficient), because of the properties of std::set or because
of the specific implementation in libstdc++. But it’s better to be on the safe
side.
2023-03-02 15:55:51 +01:00
Sébastien Villemot 41052ccb74
Optimization for derivation of STEADY_STATE(…) operator in a dynamic context
In a dynamic context, the only potentially non-null derivatives of
STEADY_STATE(…) are the parameters. We know that the derivatives w.r.t. other
variables are zero, so store that information in non_null_derivatives.
2023-03-02 15:11:49 +01:00
Sébastien Villemot d64317a64f
Minor simplification 2023-03-02 15:11:47 +01:00
Sébastien Villemot 62c455ff56
Misc simplifications using STL algorithms 2023-02-28 16:27:29 +01:00
Sébastien Villemot dc966014a3
Remove some incorrect normalization rules for the case mfs=3
More precisely, incorrect equation normalization could occur in the presence of
cos, sin, tan, cosh and x^n (where n is an even integer).

Also add some comments explaining why some other rules are (hopefully) correct.
2023-01-25 17:14:01 +01:00
Sébastien Villemot 0ddcf81ac0
Fix typo 2023-01-25 16:50:56 +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 0278c8577c
Provisions for new sparse representation of dynamic/static files
The new representation is only supported for MATLAB/Octave, C and Julia output
for the time being. Bytecode and JSON are unsupported.

This commit adds new fields in M_.

This is a preliminary step for dynare#1859.
2022-11-02 15:49:56 +01:00
Sébastien Villemot a31ef6069c
Correctly handle diff operator in equation renormalization
Should have no impact though, since diff nodes are already substituted out at
that point. But it’s better to implement it properly, in case we change the
substitution rules later.

By the way, make the computeSubExprContainingVariable method protected.
2022-10-18 17:39:14 +02:00
Sébastien Villemot 8cac64c1cb
use_dll: fix bug where operator abs() within a .mod file would return an incorrect result
It would compute the *integer* part of the absolute value (calling “abs”
instead of “fabs” from the C source).
2022-10-07 18:04:00 +02:00
Sébastien Villemot fc4c9a73c9
Fix comment
[skip ci]
2022-07-27 16:50:08 +02:00
Sébastien Villemot 50d5b916e2
Remove constructor and accessors for AuxVarInfo
Rather make all data members public and const, and use aggregate-initialization.
2022-07-20 14:48:30 +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 92047f547e
Minor simplification 2022-07-11 14:05:29 +02:00
Sébastien Villemot 4b30342dc2
Bytecode: various simplifications
Also improve the naming of the enum class used for identifying the type of
external function call.
2022-07-08 16:02:49 +02:00
Sébastien Villemot a7dc96516b
Minor cleanup 2022-07-08 15:13:21 +02:00
Sébastien Villemot 69354e0aec
More C++20 modernization 2022-07-08 14:52:21 +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 c27342cfeb
Fix interaction of temporary terms with steady_state operator
When the same complex expression appears outside and inside a steady_state()
operator, the same temporary term would be used for both cases, which was
obviously wrong.

The fix consists in never substituting temporary terms for expressions inside
the steady_state operator().

Incidentally, this implies that external functions can no longer be used inside
steady_state operators (since their computed values are stored inside temporary
terms).
2022-07-05 17:08:27 +02:00
Sébastien Villemot db0d9290b5
Bytecode: remove hack for exogenous deterministic index
The support in the bytecode MEX was broken anyways.
2022-07-05 14:10:34 +02:00
Sébastien Villemot c27354ace0
Bytecode: change name of 2nd argument of ExprNode::writeBytecodeOutput(), for clarity 2022-07-04 17:10:44 +02:00
Sébastien Villemot c3fc5b1dd7
Enable -Wunused-parameter
As a consequence, add many [[maybe_unused]] attributes for legitimate
cases (mainly in virtual functions).
2022-06-24 17:11:48 +02:00
Sébastien Villemot a6b5d40d4b
Pac{Expectation,TargetNonstationary}Node::writeOutput(): error out for non-LaTeX, as for VarExpectationNode 2022-06-24 17:11:48 +02:00
Sébastien Villemot 297f86a90e
NumConstNode::write{Bytecode,Json}Output(): check for temporary terms, as is done for MATLAB output 2022-06-24 17:11:48 +02:00
Sébastien Villemot 3b99ac5df0
Remove unused parameters to various methods 2022-06-24 17:11:38 +02:00
Sébastien Villemot 7e01d01d80
VariableNode::writeBytecodeOutput(): check for temporary terms, as done for other types of output 2022-06-24 13:10:23 +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