From a3bb1810af63a353a88e8a4af18e2a751f321fc6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 15 Mar 2024 17:49:10 +0100
Subject: [PATCH] Bytecode / FBEGINBLOCK: remove duplicate information w.r.t.
 variables and equations

---
 mex/sources/bytecode/Evaluate.hh    | 14 ++++----
 mex/sources/bytecode/Interpreter.cc | 55 +++++++++++++----------------
 mex/sources/bytecode/Interpreter.hh |  2 +-
 preprocessor                        |  2 +-
 4 files changed, 34 insertions(+), 39 deletions(-)

diff --git a/mex/sources/bytecode/Evaluate.hh b/mex/sources/bytecode/Evaluate.hh
index d0291ef0a4..5822175d7f 100644
--- a/mex/sources/bytecode/Evaluate.hh
+++ b/mex/sources/bytecode/Evaluate.hh
@@ -128,9 +128,14 @@ public:
     return currentBlockTag()->get_is_linear();
   }
   [[nodiscard]] auto
-  getCurrentBlockEquationsAndVariables() const
+  getCurrentBlockVariables() const
   {
-    return currentBlockTag()->get_Block_Contain();
+    return currentBlockTag()->get_variables();
+  }
+  [[nodiscard]] auto
+  getCurrentBlockEquations() const
+  {
+    return currentBlockTag()->get_equations();
   }
   [[nodiscard]] auto
   getCurrentBlockUCount() const
@@ -143,11 +148,6 @@ public:
     return currentBlockTag()->get_exogenous();
   }
   [[nodiscard]] auto
-  getCurrentBlockEndogenous() const
-  {
-    return currentBlockTag()->get_endogenous();
-  }
-  [[nodiscard]] auto
   getCurrentBlockNbColJacob() const
   {
     return currentBlockTag()->get_nb_col_jacob();
diff --git a/mex/sources/bytecode/Interpreter.cc b/mex/sources/bytecode/Interpreter.cc
index aa7f77bdeb..068ca0ebdb 100644
--- a/mex/sources/bytecode/Interpreter.cc
+++ b/mex/sources/bytecode/Interpreter.cc
@@ -135,7 +135,7 @@ Interpreter::solve_simple_one_periods()
   while (!(cvg || iter >= maxit_))
     {
       Per_y_ = it_ * y_size;
-      ya = y[Block_Contain[0].Variable + Per_y_];
+      ya = y[variables[0] + Per_y_];
       compute_block_time(0, false, false);
       if (!isfinite(res1))
         {
@@ -149,7 +149,7 @@ Interpreter::solve_simple_one_periods()
                   if (verbosity >= 2)
                     mexPrintf("Reducing the path length in Newton step slowc=%f\n", slowc);
                   feclearexcept(FE_ALL_EXCEPT);
-                  y[Block_Contain[0].Variable + Per_y_] = ya - slowc * (r[0] / g1);
+                  y[variables[0] + Per_y_] = ya - slowc * (r[0] / g1);
                   if (fetestexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW))
                     {
                       res1 = numeric_limits<double>::quiet_NaN();
@@ -165,7 +165,7 @@ Interpreter::solve_simple_one_periods()
       if (cvg)
         continue;
       feclearexcept(FE_ALL_EXCEPT);
-      y[Block_Contain[0].Variable + Per_y_] += -slowc * (rr / g1);
+      y[variables[0] + Per_y_] += -slowc * (rr / g1);
       if (fetestexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW))
         {
           res1 = numeric_limits<double>::quiet_NaN();
@@ -251,11 +251,10 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (single_block)
             for (int j = 0; j < size; j++)
-              residual[j] = y[Block_Contain[j].Variable] - ya[Block_Contain[j].Variable];
+              residual[j] = y[variables[j]] - ya[variables[j]];
           else
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation]
-                  = y[Block_Contain[j].Variable] - ya[Block_Contain[j].Variable];
+              residual[equations[j]] = y[variables[j]] - ya[variables[j]];
         }
       else
         {
@@ -266,13 +265,11 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               if (single_block)
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j]
-                      = y[it_ * y_size + Block_Contain[j].Variable]
-                        - ya[it_ * y_size + Block_Contain[j].Variable];
+                      = y[it_ * y_size + variables[j]] - ya[it_ * y_size + variables[j]];
               else
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation]
-                      = y[it_ * y_size + Block_Contain[j].Variable]
-                        - ya[it_ * y_size + Block_Contain[j].Variable];
+                  residual[(it_ - y_kmin) * y_size + equations[j]]
+                      = y[it_ * y_size + variables[j]] - ya[it_ * y_size + variables[j]];
             }
         }
       break;
@@ -284,7 +281,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (!single_block)
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation] = r[j];
+              residual[equations[j]] = r[j];
           else
             for (int j = 0; j < size; j++)
               residual[j] = r[j];
@@ -297,7 +294,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               compute_block_time(0, true, false);
               if (!single_block)
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation] = r[j];
+                  residual[(it_ - y_kmin) * y_size + equations[j]] = r[j];
               else
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j] = r[j];
@@ -321,7 +318,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (!single_block)
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation] = r[j];
+              residual[equations[j]] = r[j];
           else
             for (int j = 0; j < size; j++)
               residual[j] = r[j];
@@ -334,7 +331,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               compute_block_time(0, true, false);
               if (!single_block)
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation] = r[j];
+                  residual[(it_ - y_kmin) * y_size + equations[j]] = r[j];
               else
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j] = r[j];
@@ -348,11 +345,10 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (single_block)
             for (int j = 0; j < size; j++)
-              residual[j] = y[Block_Contain[j].Variable] - ya[Block_Contain[j].Variable];
+              residual[j] = y[variables[j]] - ya[variables[j]];
           else
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation]
-                  = y[Block_Contain[j].Variable] - ya[Block_Contain[j].Variable];
+              residual[equations[j]] = y[variables[j]] - ya[variables[j]];
         }
       else
         {
@@ -363,13 +359,11 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               if (single_block)
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j]
-                      = y[it_ * y_size + Block_Contain[j].Variable]
-                        - ya[it_ * y_size + Block_Contain[j].Variable];
+                      = y[it_ * y_size + variables[j]] - ya[it_ * y_size + variables[j]];
               else
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation]
-                      = y[it_ * y_size + Block_Contain[j].Variable]
-                        - ya[it_ * y_size + Block_Contain[j].Variable];
+                  residual[(it_ - y_kmin) * y_size + equations[j]]
+                      = y[it_ * y_size + variables[j]] - ya[it_ * y_size + variables[j]];
             }
         }
       break;
@@ -381,7 +375,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (!single_block)
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation] = r[j];
+              residual[equations[j]] = r[j];
           else
             for (int j = 0; j < size; j++)
               residual[j] = r[j];
@@ -394,7 +388,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               compute_block_time(0, true, false);
               if (!single_block)
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation] = r[j];
+                  residual[(it_ - y_kmin) * y_size + equations[j]] = r[j];
               else
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j] = r[j];
@@ -415,7 +409,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(0, true, false);
           if (!single_block)
             for (int j = 0; j < size; j++)
-              residual[Block_Contain[j].Equation] = r[j];
+              residual[equations[j]] = r[j];
           else
             for (int j = 0; j < size; j++)
               residual[j] = r[j];
@@ -428,7 +422,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
               compute_block_time(0, true, false);
               if (!single_block)
                 for (int j = 0; j < size; j++)
-                  residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation] = r[j];
+                  residual[(it_ - y_kmin) * y_size + equations[j]] = r[j];
               else
                 for (int j = 0; j < size; j++)
                   residual[(it_ - y_kmin) * size + j] = r[j];
@@ -453,7 +447,7 @@ Interpreter::evaluate_a_block(bool initialization, bool single_block, const stri
           compute_block_time(Per_u_, true, false);
           if (!single_block)
             for (int j = 0; j < size; j++)
-              residual[(it_ - y_kmin) * y_size + Block_Contain[j].Equation] = r[j];
+              residual[(it_ - y_kmin) * y_size + equations[j]] = r[j];
           else
             for (int j = 0; j < size; j++)
               residual[(it_ - y_kmin) * size + j] = r[j];
@@ -663,7 +657,7 @@ void
 Interpreter::check_for_controlled_exo_validity(const vector<s_plan>& sconstrained_extended_path)
 {
   vector<int> exogenous {evaluator.getCurrentBlockExogenous()};
-  vector<int> endogenous {evaluator.getCurrentBlockEndogenous()};
+  vector<int> endogenous {evaluator.getCurrentBlockVariables()};
   for (auto& it : sconstrained_extended_path)
     {
       if (find(endogenous.begin(), endogenous.end(), it.exo_num) != endogenous.end()
@@ -738,7 +732,8 @@ Interpreter::MainLoop(const string& bin_basename, bool evaluate, int block, bool
       size = evaluator.getCurrentBlockSize();
       type = evaluator.getCurrentBlockType();
       is_linear = evaluator.isCurrentBlockLinear();
-      Block_Contain = evaluator.getCurrentBlockEquationsAndVariables();
+      variables = evaluator.getCurrentBlockVariables();
+      equations = evaluator.getCurrentBlockEquations();
       u_count_int = evaluator.getCurrentBlockUCount();
 
       if (constrained)
diff --git a/mex/sources/bytecode/Interpreter.hh b/mex/sources/bytecode/Interpreter.hh
index 19141ce76d..4a2723c782 100644
--- a/mex/sources/bytecode/Interpreter.hh
+++ b/mex/sources/bytecode/Interpreter.hh
@@ -149,7 +149,7 @@ private:
   BlockSimulationType type;
   bool is_linear;
   int u_count_int;
-  vector<Bytecode::Block_contain_type> Block_Contain;
+  vector<int> variables, equations;
 
   int verbosity; // Corresponds to options_.verbosity
 
diff --git a/preprocessor b/preprocessor
index cbd1e8d885..bdb8ef9961 160000
--- a/preprocessor
+++ b/preprocessor
@@ -1 +1 @@
-Subproject commit cbd1e8d8850ea4832cad32b5a00be39d78e47ad5
+Subproject commit bdb8ef996192b15cf8398d7d58d59954a9bb248b
-- 
GitLab