From 53c5266df32ff17770b1d88dccd8474056cae087 Mon Sep 17 00:00:00 2001 From: Mark Messner Date: Tue, 30 Aug 2022 21:23:23 -0500 Subject: [PATCH] All the optimization I care to do --- include/history.h | 12 ++++++++++++ include/models.h | 6 ------ include/walker.h | 17 ++--------------- src/history.cxx | 30 +++++++++++++++++++++++++++++- src/models.cxx | 21 +-------------------- src/walker.cxx | 45 +++++++++------------------------------------ src/walker_wrap.cxx | 22 +++++++++++----------- 7 files changed, 64 insertions(+), 89 deletions(-) diff --git a/include/history.h b/include/history.h index 23f54793..7b26ca33 100644 --- a/include/history.h +++ b/include/history.h @@ -296,8 +296,20 @@ class NEML_EXPORT HistoryNEMLObject: public NEMLObject { /// Assemble a complete derivative name std::string dprefix(std::string a, std::string b) const; + /// Cache the history to avoid recreating it every time + void cache_history_(); + + /// Quickly setup history + History gather_history_(double * data) const; + History gather_history_(const double * data) const; + History gather_blank_history_() const; + protected: std::string prefix_; + History stored_hist_; + + private: + bool cached_; }; } // namespace neml diff --git a/include/models.h b/include/models.h index da602f14..3392df62 100644 --- a/include/models.h +++ b/include/models.h @@ -97,13 +97,7 @@ class NEML_EXPORT NEMLModel: public HistoryNEMLObject { /// Cache history objects with a view to increasing performance void cache_history_(); - /// Quickly setup history - History gather_history_(double * data) const; - History gather_history_(const double * data) const; - History gather_blank_history_() const; - protected: - History stored_hist_; History stored_state_; History stored_static_; }; diff --git a/include/walker.h b/include/walker.h index 044c541a..52834557 100644 --- a/include/walker.h +++ b/include/walker.h @@ -590,17 +590,11 @@ class NEML_EXPORT WrappedViscoPlasticFlowRule : public ViscoPlasticFlowRule { /// Wrapped derivative of h_temp wrt history virtual void dh_da_temp(const State & state, History & res) const; - /// Blank history - History blank_hist_() const; - - /// Special function for wrappers... - History create_blank_hist_() const; - /// Blank derivative of a history template History blank_derivative_() const { - return blank_hist_().derivative(); + return gather_blank_history_().derivative(); } protected: @@ -609,10 +603,6 @@ class NEML_EXPORT WrappedViscoPlasticFlowRule : public ViscoPlasticFlowRule { T) const; private: - /// Make a history object - History gather_hist_(double * const h) const; - History gather_hist_(const double * const h) const; - /// Initialized derivative template History gather_derivative_(double * const h) const @@ -621,16 +611,13 @@ class NEML_EXPORT WrappedViscoPlasticFlowRule : public ViscoPlasticFlowRule { hv.set_data(h); return hv; } - - protected: - History stored_hist_; }; /// Initialized derivative wrt history template <> History WrappedViscoPlasticFlowRule::gather_derivative_(double * const h) const { - History hv = blank_hist_().history_derivative(blank_hist_()); + History hv = gather_blank_history_().history_derivative(gather_blank_history_()); hv.set_data(h); return hv; } diff --git a/src/history.cxx b/src/history.cxx index aeda244a..a7aa7349 100644 --- a/src/history.cxx +++ b/src/history.cxx @@ -430,13 +430,16 @@ std::vector History::formatted_names() const } HistoryNEMLObject::HistoryNEMLObject(ParameterSet & params) : - NEMLObject(params), prefix_("") + NEMLObject(params), prefix_(""), cached_(false) { } size_t HistoryNEMLObject::nhist() const { + if (cached_) + return stored_hist_.size(); + History h; populate_hist(h); return h.size(); @@ -474,4 +477,29 @@ std::string HistoryNEMLObject::dprefix(std::string a, std::string b) const return prefix(a) + "_" + prefix(b); } +void HistoryNEMLObject::cache_history_() +{ + populate_hist(stored_hist_); + cached_ = true; +} + +History HistoryNEMLObject::gather_history_(double * data) const +{ + History h = gather_blank_history_(); + h.set_data(data); + return h; +} + +History HistoryNEMLObject::gather_history_(const double * data) const +{ + History h = gather_blank_history_(); + h.set_data(const_cast(data)); + return h; +} + +History HistoryNEMLObject::gather_blank_history_() const +{ + return stored_hist_; +} + } // namespace neml diff --git a/src/models.cxx b/src/models.cxx index adfcc6ad..e1f54869 100644 --- a/src/models.cxx +++ b/src/models.cxx @@ -83,30 +83,11 @@ size_t NEMLModel::nstatic() const void NEMLModel::cache_history_() { - populate_hist(stored_hist_); + HistoryNEMLObject::cache_history_(); populate_state(stored_state_); populate_static(stored_static_); } -History NEMLModel::gather_history_(double * data) const -{ - History h = gather_blank_history_(); - h.set_data(data); - return h; -} - -History NEMLModel::gather_history_(const double * data) const -{ - History h = gather_blank_history_(); - h.set_data(const_cast(data)); - return h; -} - -History NEMLModel::gather_blank_history_() const -{ - return stored_hist_; -} - std::vector NEMLModel::report_internal_variable_names() const { return gather_blank_history_().formatted_names(); diff --git a/src/walker.cxx b/src/walker.cxx index fec33688..db5aa39b 100644 --- a/src/walker.cxx +++ b/src/walker.cxx @@ -1492,42 +1492,15 @@ SymSymR4 WalkerKinematicHardening::dN_(VariableState & state) } WrappedViscoPlasticFlowRule::WrappedViscoPlasticFlowRule(ParameterSet & params) : - ViscoPlasticFlowRule(params), - stored_hist_(false) + ViscoPlasticFlowRule(params) { } -History WrappedViscoPlasticFlowRule::blank_hist_() const -{ - return stored_hist_; -} - -History WrappedViscoPlasticFlowRule::create_blank_hist_() const -{ - History h; - populate_hist(h); - return h; -} - -History WrappedViscoPlasticFlowRule::gather_hist_(double * const h) const -{ - History hv = blank_hist_(); - hv.set_data(h); - return hv; -} - -History WrappedViscoPlasticFlowRule::gather_hist_(const double * const h) const -{ - History hv = blank_hist_(); - hv.set_data(const_cast(h)); - return hv; -} - State WrappedViscoPlasticFlowRule::make_state_(const double * const s, const double * const alpha, double T) const { - return State(Symmetric(s), gather_hist_(alpha), T); + return State(Symmetric(s), gather_history_(alpha), T); } // Rate rule @@ -1585,7 +1558,7 @@ void WrappedViscoPlasticFlowRule::dg_da(const double * const s, const double * c void WrappedViscoPlasticFlowRule::h(const double * const s, const double * const alpha, double T, double * const hv) const { - History res = gather_hist_(hv); + History res = gather_history_(hv); h(make_state_(s, alpha, T), res); } @@ -1605,7 +1578,7 @@ void WrappedViscoPlasticFlowRule::dh_da(const double * const s, const double * c dh_da(make_state_(s, alpha, T), res); - res.unravel_hh(blank_hist_(), dhv); + res.unravel_hh(gather_blank_history_(), dhv); delete [] temp; } @@ -1614,7 +1587,7 @@ void WrappedViscoPlasticFlowRule::dh_da(const double * const s, const double * c void WrappedViscoPlasticFlowRule::h_time(const double * const s, const double * const alpha, double T, double * const hv) const { - History res = gather_hist_(hv); + History res = gather_history_(hv); h_time(make_state_(s, alpha, T), res); } @@ -1644,7 +1617,7 @@ void WrappedViscoPlasticFlowRule::dh_da_time(const double * const s, const doubl dh_da_time(make_state_(s, alpha, T), res); - res.unravel_hh(blank_hist_(), dhv); + res.unravel_hh(gather_blank_history_(), dhv); delete [] temp; } @@ -1658,7 +1631,7 @@ void WrappedViscoPlasticFlowRule::dh_da_time(const State & state, History & res) void WrappedViscoPlasticFlowRule::h_temp(const double * const s, const double * const alpha, double T, double * const hv) const { - History res = gather_hist_(hv); + History res = gather_history_(hv); h_temp(make_state_(s, alpha, T), res); } @@ -1699,7 +1672,7 @@ TestFlowRule::TestFlowRule(ParameterSet & params) s0_(params.get_parameter("s0")), K_(params.get_parameter("K")) { - populate_hist(stored_hist_); + cache_history_(); } std::string TestFlowRule::type() @@ -1838,7 +1811,7 @@ WalkerFlowRule::WalkerFlowRule(ParameterSet & params) : X->set_scaling(scaling_); } - populate_hist(stored_hist_); + cache_history_(); } std::string WalkerFlowRule::type() diff --git a/src/walker_wrap.cxx b/src/walker_wrap.cxx index b6f1304c..0b4d46d0 100644 --- a/src/walker_wrap.cxx +++ b/src/walker_wrap.cxx @@ -293,7 +293,7 @@ PYBIND11_MODULE(walker, m) { .def("dy_da_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History h = m.create_blank_hist_(); + History h = m.gather_blank_history_(); m.dy_da(state, h); return h; }, "The derivative of the scalar inelastic strain rate wrt history") @@ -315,7 +315,7 @@ PYBIND11_MODULE(walker, m) { .def("dg_da_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().derivative(); + History res = m.gather_blank_history_().derivative(); m.dg_da(state, res); return res; }, "The derivative of the flow direction wrt history") @@ -323,21 +323,21 @@ PYBIND11_MODULE(walker, m) { .def("h_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_(); + History res = m.gather_blank_history_(); m.h(state, res); return res; }, "The history proportional to the scalar inelastic strain rate") .def("dh_ds_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().derivative(); + History res = m.gather_blank_history_().derivative(); m.dh_ds(state, res); return res; }, "The derivative of the history ptt inelastic strain rate wrt stress") .def("dh_da_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().history_derivative(m.create_blank_hist_()); + History res = m.gather_blank_history_().history_derivative(m.gather_blank_history_()); m.dh_da(state, res); return res; }, "The derivative of the history ptt inelastic strain rate wrt history") @@ -345,21 +345,21 @@ PYBIND11_MODULE(walker, m) { .def("h_time_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_(); + History res = m.gather_blank_history_(); m.h_time(state, res); return res; }, "The history proportional to the time rate") .def("dh_ds_time_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().derivative(); + History res = m.gather_blank_history_().derivative(); m.dh_ds_time(state, res); return res; }, "The derivative of the history ptt time rate wrt stress") .def("dh_da_time_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().history_derivative(m.create_blank_hist_()); + History res = m.gather_blank_history_().history_derivative(m.gather_blank_history_()); m.dh_da_time(state, res); return res; }, "The derivative of the history ptt time rate wrt history") @@ -367,21 +367,21 @@ PYBIND11_MODULE(walker, m) { .def("h_temp_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_(); + History res = m.gather_blank_history_(); m.h_temp(state, res); return res; }, "The history proportional to the temperature rate") .def("dh_ds_temp_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().derivative(); + History res = m.gather_blank_history_().derivative(); m.dh_ds_temp(state, res); return res; }, "The derivative of the history ptt temperature rate wrt stress") .def("dh_da_temp_wrap", [](WrappedViscoPlasticFlowRule & m, State & state) -> History { - History res = m.create_blank_hist_().history_derivative(m.create_blank_hist_()); + History res = m.gather_blank_history_().history_derivative(m.gather_blank_history_()); m.dh_da_temp(state, res); return res; }, "The derivative of the history ptt temperature rate wrt history")