Skip to content

Commit

Permalink
All the optimization I care to do
Browse files Browse the repository at this point in the history
  • Loading branch information
reverendbedford committed Sep 14, 2022
1 parent 5cf18e2 commit 5376c6f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 89 deletions.
12 changes: 12 additions & 0 deletions include/history.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions include/models.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
};
Expand Down
17 changes: 2 additions & 15 deletions include/walker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <class T>
History blank_derivative_() const
{
return blank_hist_().derivative<T>();
return gather_blank_history_().derivative<T>();
}

protected:
Expand All @@ -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 <class T>
History gather_derivative_(double * const h) const
Expand All @@ -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_<History>(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;
}
Expand Down
30 changes: 29 additions & 1 deletion src/history.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,16 @@ std::vector<std::string> 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();
Expand Down Expand Up @@ -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<double*>(data));
return h;
}

History HistoryNEMLObject::gather_blank_history_() const
{
return stored_hist_;
}

} // namespace neml
21 changes: 1 addition & 20 deletions src/models.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<double*>(data));
return h;
}

History NEMLModel::gather_blank_history_() const
{
return stored_hist_;
}

std::vector<std::string> NEMLModel::report_internal_variable_names() const
{
return gather_blank_history_().formatted_names();
Expand Down
45 changes: 9 additions & 36 deletions src/walker.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<double*>(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
Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}

Expand Down Expand Up @@ -1699,7 +1672,7 @@ TestFlowRule::TestFlowRule(ParameterSet & params)
s0_(params.get_parameter<double>("s0")),
K_(params.get_parameter<double>("K"))
{
populate_hist(stored_hist_);
cache_history_();
}

std::string TestFlowRule::type()
Expand Down Expand Up @@ -1838,7 +1811,7 @@ WalkerFlowRule::WalkerFlowRule(ParameterSet & params) :
X->set_scaling(scaling_);
}

populate_hist(stored_hist_);
cache_history_();
}

std::string WalkerFlowRule::type()
Expand Down
22 changes: 11 additions & 11 deletions src/walker_wrap.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -315,73 +315,73 @@ PYBIND11_MODULE(walker, m) {
.def("dg_da_wrap",
[](WrappedViscoPlasticFlowRule & m, State & state) -> History
{
History res = m.create_blank_hist_().derivative<Symmetric>();
History res = m.gather_blank_history_().derivative<Symmetric>();
m.dg_da(state, res);
return res;
}, "The derivative of the flow direction wrt history")

.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<Symmetric>();
History res = m.gather_blank_history_().derivative<Symmetric>();
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")

.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<Symmetric>();
History res = m.gather_blank_history_().derivative<Symmetric>();
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")

.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<Symmetric>();
History res = m.gather_blank_history_().derivative<Symmetric>();
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")
Expand Down

0 comments on commit 5376c6f

Please sign in to comment.