From 9b525713d9c9adae5d231f94a420acbd8286c716 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 2 Sep 2015 15:29:52 +0200 Subject: [PATCH] Revert "preprocessor: julia: print only those temporary vars needed for the computation of (residuals, g1, g2, g3) in the respective dynamic! and static! functions" This reverts commit eb2890d1f58f2285306821c85fa631b865fb80b6. --- preprocessor/DynamicModel.cc | 37 ++++++++---------- preprocessor/ExprNode.cc | 75 ------------------------------------ preprocessor/ExprNode.hh | 11 ------ preprocessor/ModelTree.cc | 34 ++-------------- preprocessor/ModelTree.hh | 4 -- 5 files changed, 20 insertions(+), 141 deletions(-) diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc index 6a1a5546c..0481ec73a 100644 --- a/preprocessor/DynamicModel.cc +++ b/preprocessor/DynamicModel.cc @@ -2112,27 +2112,26 @@ DynamicModel::writeSparseDynamicMFile(const string &dynamic_basename, const stri void DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia) const { - ostringstream model_local_vars_output; // Used for storing model local vars - ostringstream model_output; // Used for storing model temp vars and equations - ostringstream jacobian_output; // Used for storing jacobian equations - ostringstream hessian_output; // Used for storing Hessian equations + ostringstream model_output; // Used for storing model + ostringstream model_eq_output; // Used for storing model equations + ostringstream jacobian_output; // Used for storing jacobian equations + ostringstream hessian_output; // Used for storing Hessian equations ostringstream third_derivatives_output; ExprNodeOutputType output_type = (use_dll ? oCDynamicModel : julia ? oJuliaDynamicModel : oMatlabDynamicModel); deriv_node_temp_terms_t tef_terms; - writeModelLocalVariables(model_local_vars_output, output_type, tef_terms); + writeModelLocalVariables(model_output, output_type, tef_terms); - writeTemporaryTerms(temporary_terms_res, model_output, output_type, tef_terms); + writeTemporaryTerms(temporary_terms, model_output, output_type, tef_terms); - writeModelEquations(model_output, output_type); + writeModelEquations(model_eq_output, output_type); int nrows = equations.size(); int hessianColsNbr = dynJacobianColsNbr * dynJacobianColsNbr; // Writing Jacobian - writeTemporaryTerms(temporary_terms_g1, jacobian_output, output_type, tef_terms); for (first_derivatives_t::const_iterator it = first_derivatives.begin(); it != first_derivatives.end(); it++) { @@ -2142,13 +2141,11 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia jacobianHelper(jacobian_output, eq, getDynJacobianCol(var), output_type); jacobian_output << "="; - d1->writeOutput(jacobian_output, output_type, temporary_terms_g1, tef_terms); + d1->writeOutput(jacobian_output, output_type, temporary_terms, tef_terms); jacobian_output << ";" << endl; } // Writing Hessian - if (second_derivatives.size() > 0) - writeTemporaryTerms(temporary_terms_g2, hessian_output, output_type, tef_terms); int k = 0; // Keep the line of a 2nd derivative in v2 for (second_derivatives_t::const_iterator it = second_derivatives.begin(); it != second_derivatives.end(); it++) @@ -2169,7 +2166,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia { for_sym << "g2[" << eq + 1 << "," << col_nb + 1 << "]"; hessian_output << " @inbounds " << for_sym.str() << " = "; - d2->writeOutput(hessian_output, output_type, temporary_terms_g2, tef_terms); + d2->writeOutput(hessian_output, output_type, temporary_terms, tef_terms); hessian_output << endl; } else @@ -2211,8 +2208,6 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia } // Writing third derivatives - if (third_derivatives.size() > 0) - writeTemporaryTerms(temporary_terms_g3, third_derivatives_output, output_type, tef_terms); k = 0; // Keep the line of a 3rd derivative in v3 for (third_derivatives_t::const_iterator it = third_derivatives.begin(); it != third_derivatives.end(); it++) @@ -2235,7 +2230,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia { for_sym << "g3[" << eq + 1 << "," << ref_col + 1 << "]"; third_derivatives_output << " @inbounds " << for_sym.str() << " = "; - d3->writeOutput(third_derivatives_output, output_type, temporary_terms_g3, tef_terms); + d3->writeOutput(third_derivatives_output, output_type, temporary_terms, tef_terms); third_derivatives_output << endl; } else @@ -2292,8 +2287,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << "%" << endl << endl << "residual = zeros(" << nrows << ", 1);" << endl - << model_local_vars_output.str() << model_output.str() + << model_eq_output.str() // Writing initialization instruction for matrix g1 << "if nargout >= 2," << endl << " g1 = zeros(" << nrows << ", " << dynJacobianColsNbr << ");" << endl @@ -2342,8 +2337,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " double lhs, rhs;" << endl << endl << " /* Residual equations */" << endl - << model_local_vars_output.str() << model_output.str() + << model_eq_output.str() << " /* Jacobian */" << endl << " if (g1 == NULL)" << endl << " return;" << endl @@ -2401,8 +2396,8 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " #" << endl << " # Model equations" << endl << " #" << endl - << model_local_vars_output.str() << model_output.str() + << model_eq_output.str() << "end" << endl << endl << "function dynamic!(y::Vector{Float64}, x::Matrix{Float64}, " << "params::Vector{Float64}," << endl @@ -2418,7 +2413,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " @assert size(g1) == (" << nrows << ", " << dynJacobianColsNbr << ")" << endl << " fill!(g1, 0.0)" << endl << " dynamic!(y, x, params, steady_state, it_, residual)" << endl - << model_local_vars_output.str() + << model_output.str() << " #" << endl << " # Jacobian matrix" << endl << " #" << endl @@ -2438,7 +2433,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " @assert size(g2) == (" << nrows << ", " << hessianColsNbr << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1)" << endl; if (second_derivatives.size()) - DynamicOutput << model_local_vars_output.str() + DynamicOutput << model_output.str() << " #" << endl << " # Hessian matrix" << endl << " #" << endl @@ -2461,7 +2456,7 @@ DynamicModel::writeDynamicModel(ostream &DynamicOutput, bool use_dll, bool julia << " @assert size(g3) == (" << nrows << ", " << ncols << ")" << endl << " dynamic!(y, x, params, steady_state, it_, residual, g1, g2)" << endl; if (third_derivatives.size()) - DynamicOutput << model_local_vars_output.str() + DynamicOutput << model_output.str() << " #" << endl << " # Third order derivatives" << endl << " #" << endl diff --git a/preprocessor/ExprNode.cc b/preprocessor/ExprNode.cc index 8145115ae..7b52a0bda 100644 --- a/preprocessor/ExprNode.cc +++ b/preprocessor/ExprNode.cc @@ -128,14 +128,6 @@ ExprNode::computeTemporaryTerms(map &reference_count, // Nothing to do for a terminal node } -void -ExprNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - // Nothing to do for a terminal node -} - pair ExprNode::normalizeEquation(int var_endo, vector > > &List_of_Op_RHS) const { @@ -1756,19 +1748,6 @@ UnaryOpNode::collectTemporary_terms(const temporary_terms_t &temporary_terms, te arg->collectTemporary_terms(temporary_terms, temporary_terms_inuse, Curr_Block); } -void -UnaryOpNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - expr_t this2 = const_cast(this); - - arg->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - - if (reference_count[this2] * cost(temporary_terms, is_matlab) > MIN_COST(is_matlab)) - temporary_terms.insert(this2); -} - bool UnaryOpNode::containsExternalFunction() const { @@ -2836,21 +2815,6 @@ BinaryOpNode::computeTemporaryTerms(map &reference_count, } } -void -BinaryOpNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - expr_t this2 = const_cast(this); - - arg1->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - arg2->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - - if (reference_count[this2] * cost(temporary_terms, is_matlab) > MIN_COST(is_matlab) - && op_code != oEqual) - temporary_terms.insert(this2); -} - double BinaryOpNode::eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivOrder) throw (EvalException, EvalExternalFunctionException) { @@ -3983,21 +3947,6 @@ TrinaryOpNode::computeTemporaryTerms(map &reference_count, } } -void -TrinaryOpNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - expr_t this2 = const_cast(this); - - arg1->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - arg2->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - arg3->computeSplitTemporaryTerms(reference_count, temporary_terms, is_matlab); - - if (reference_count[this2] * cost(temporary_terms, is_matlab) > MIN_COST(is_matlab)) - temporary_terms.insert(this2); -} - double TrinaryOpNode::eval_opcode(double v1, TrinaryOpcode op_code, double v2, double v3) throw (EvalException, EvalExternalFunctionException) { @@ -4789,14 +4738,6 @@ ExternalFunctionNode::computeTemporaryTerms(map &reference_count, v_temporary_terms[Curr_block][equation].insert(this2); } -void -ExternalFunctionNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - temporary_terms.insert(const_cast(this)); -} - void ExternalFunctionNode::compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, @@ -5052,14 +4993,6 @@ FirstDerivExternalFunctionNode::computeTemporaryTerms(map &referenc v_temporary_terms[Curr_block][equation].insert(this2); } -void -FirstDerivExternalFunctionNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - temporary_terms.insert(const_cast(this)); -} - expr_t FirstDerivExternalFunctionNode::composeDerivatives(const vector &dargs) { @@ -5368,14 +5301,6 @@ SecondDerivExternalFunctionNode::computeTemporaryTerms(map &referen v_temporary_terms[Curr_block][equation].insert(this2); } -void -SecondDerivExternalFunctionNode::computeSplitTemporaryTerms(map &reference_count, - temporary_terms_t &temporary_terms, - bool is_matlab) const -{ - temporary_terms.insert(const_cast(this)); -} - expr_t SecondDerivExternalFunctionNode::composeDerivatives(const vector &dargs) diff --git a/preprocessor/ExprNode.hh b/preprocessor/ExprNode.hh index 6584cb87e..a8f48e306 100644 --- a/preprocessor/ExprNode.hh +++ b/preprocessor/ExprNode.hh @@ -188,10 +188,6 @@ public: /*! A node will be marked as a temporary term if it is referenced at least two times (i.e. has at least two parents), and has a computing cost (multiplied by reference count) greater to datatree.min_cost */ virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - //! Splits temporary terms into those used for each of residuals, jacobian, hessian, 3rd derivs - //! based on the reference counts computed by computeTemporaryTerms - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - //! Writes output of node, using a Txxx notation for nodes in temporary_terms, and specifiying the set of already written external functions /*! \param[in] output the output stream @@ -570,7 +566,6 @@ public: UnaryOpNode(DataTree &datatree_arg, UnaryOpcode op_code_arg, const expr_t arg_arg, int expectation_information_set_arg, int param1_symb_id_arg, int param2_symb_id_arg); virtual void prepareForDerivation(); virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual bool containsExternalFunction() const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, @@ -650,7 +645,6 @@ public: virtual void prepareForDerivation(); virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual bool containsExternalFunction() const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, @@ -746,7 +740,6 @@ public: virtual void prepareForDerivation(); virtual int precedence(ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms) const; virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual bool containsExternalFunction() const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, @@ -819,7 +812,6 @@ public: const vector &arguments_arg); virtual void prepareForDerivation(); virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const = 0; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const = 0; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const = 0; virtual bool containsExternalFunction() const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, @@ -880,7 +872,6 @@ public: ExternalFunctionNode(DataTree &datatree_arg, int symb_id_arg, const vector &arguments_arg); virtual void computeTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void writeExternalFunctionOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, @@ -918,7 +909,6 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, @@ -955,7 +945,6 @@ public: int Curr_block, vector< vector > &v_temporary_terms, int equation) const; - virtual void computeSplitTemporaryTerms(map &reference_count, temporary_terms_t &temporary_terms, bool is_matlab) const; virtual void writeOutput(ostream &output, ExprNodeOutputType output_type, const temporary_terms_t &temporary_terms, deriv_node_temp_terms_t &tef_terms) const; virtual void compile(ostream &CompileCode, unsigned int &instruction_number, bool lhs_rhs, const temporary_terms_t &temporary_terms, diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc index e6b2dab68..10fb02f7a 100644 --- a/preprocessor/ModelTree.cc +++ b/preprocessor/ModelTree.cc @@ -1110,27 +1110,6 @@ ModelTree::computeTemporaryTerms(bool is_matlab) for (third_derivatives_t::iterator it = third_derivatives.begin(); it != third_derivatives.end(); it++) it->second->computeTemporaryTerms(reference_count, temporary_terms, is_matlab); - - // Now split up temporary terms - temporary_terms_res.clear(); - for (vector::iterator it = equations.begin(); - it != equations.end(); it++) - (*it)->computeSplitTemporaryTerms(reference_count, temporary_terms_res, is_matlab); - - temporary_terms_g1 = temporary_terms_res; - for (first_derivatives_t::iterator it = first_derivatives.begin(); - it != first_derivatives.end(); it++) - it->second->computeSplitTemporaryTerms(reference_count, temporary_terms_g1, is_matlab); - - temporary_terms_g2 = temporary_terms_g1; - for (second_derivatives_t::iterator it = second_derivatives.begin(); - it != second_derivatives.end(); it++) - it->second->computeSplitTemporaryTerms(reference_count, temporary_terms_g2, is_matlab); - - temporary_terms_g3 = temporary_terms_g2; - for (third_derivatives_t::iterator it = third_derivatives.begin(); - it != third_derivatives.end(); it++) - it->second->computeSplitTemporaryTerms(reference_count, temporary_terms_g3, is_matlab); } void @@ -1139,6 +1118,7 @@ ModelTree::writeTemporaryTerms(const temporary_terms_t &tt, ostream &output, { // Local var used to keep track of temp nodes already written temporary_terms_t tt2; + for (temporary_terms_t::const_iterator it = tt.begin(); it != tt.end(); it++) { @@ -1233,12 +1213,6 @@ ModelTree::writeModelLocalVariables(ostream &output, ExprNodeOutputType output_t void ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) const { - temporary_terms_t temp_terms; - if (IS_JULIA(output_type)) - temp_terms = temporary_terms_res; - else - temp_terms = temporary_terms; - for (int eq = 0; eq < (int) equations.size(); eq++) { BinaryOpNode *eq_node = equations[eq]; @@ -1260,13 +1234,13 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) if (IS_JULIA(output_type)) output << " @inbounds "; output << "lhs ="; - lhs->writeOutput(output, output_type, temp_terms); + lhs->writeOutput(output, output_type, temporary_terms); output << ";" << endl; if (IS_JULIA(output_type)) output << " @inbounds "; output << "rhs ="; - rhs->writeOutput(output, output_type, temp_terms); + rhs->writeOutput(output, output_type, temporary_terms); output << ";" << endl; if (IS_JULIA(output_type)) @@ -1284,7 +1258,7 @@ ModelTree::writeModelEquations(ostream &output, ExprNodeOutputType output_type) << eq + ARRAY_SUBSCRIPT_OFFSET(output_type) << RIGHT_ARRAY_SUBSCRIPT(output_type) << " = "; - lhs->writeOutput(output, output_type, temp_terms); + lhs->writeOutput(output, output_type, temporary_terms); output << ";" << endl; } } diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh index 09132552b..ea799585c 100644 --- a/preprocessor/ModelTree.hh +++ b/preprocessor/ModelTree.hh @@ -130,10 +130,6 @@ protected: //! Temporary terms for the static/dynamic file (those which will be noted Txxxx) temporary_terms_t temporary_terms; - temporary_terms_t temporary_terms_res; - temporary_terms_t temporary_terms_g1; - temporary_terms_t temporary_terms_g2; - temporary_terms_t temporary_terms_g3; //! Temporary terms for the file containing parameters derivatives temporary_terms_t params_derivs_temporary_terms;