Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fewer copies of arrays #2114

Merged
merged 3 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions ql/math/optimization/levenbergmarquardt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ namespace QuantLib {
EndCriteria::Type LevenbergMarquardt::minimize(Problem& P,
const EndCriteria& endCriteria) {
P.reset();
Array x_ = P.currentValue();
const Array& initX = P.currentValue();
currentProblem_ = &P;
initCostValues_ = P.costFunction().values(x_);
initCostValues_ = P.costFunction().values(initX);
int m = initCostValues_.size();
int n = x_.size();
if(useCostFunctionsJacobian_) {
int n = initX.size();
if (useCostFunctionsJacobian_) {
initJacobian_ = Matrix(m,n);
P.costFunction().jacobian(initJacobian_, x_);
P.costFunction().jacobian(initJacobian_, initX);
}
std::unique_ptr<Real[]> xx(new Real[n]);
std::copy(x_.begin(), x_.end(), xx.get());
Array xx = initX;
std::unique_ptr<Real[]> fvec(new Real[m]);
std::unique_ptr<Real[]> diag(new Real[n]);
int mode = 1;
Expand Down Expand Up @@ -81,15 +80,15 @@ namespace QuantLib {
// in n variables by the Levenberg-Marquardt algorithm.
MINPACK::LmdifCostFunction lmdifCostFunction =
[this](const auto m, const auto n, const auto x, const auto fvec, const auto iflag) {
this->fcn(m, n, x, fvec, iflag);
this->fcn(m, n, x, fvec);
};
MINPACK::LmdifCostFunction lmdifJacFunction =
useCostFunctionsJacobian_
? [this](const auto m, const auto n, const auto x, const auto fjac, const auto iflag) {
this->jacFcn(m, n, x, fjac, iflag);
this->jacFcn(m, n, x, fjac);
}
: MINPACK::LmdifCostFunction();
MINPACK::lmdif(m, n, xx.get(), fvec.get(),
MINPACK::lmdif(m, n, xx.begin(), fvec.get(),
endCriteria.functionEpsilon(),
xtol_,
gtol_,
Expand Down Expand Up @@ -132,14 +131,13 @@ namespace QuantLib {
QL_FAIL("unknown MINPACK result: " << info);
}
// set problem
std::copy(xx.get(), xx.get()+n, x_.begin());
P.setCurrentValue(x_);
P.setFunctionValue(P.costFunction().value(x_));
P.setCurrentValue(std::move(xx));
P.setFunctionValue(P.costFunction().value(P.currentValue()));

return ecType;
}

void LevenbergMarquardt::fcn(int, int n, Real* x, Real* fvec, int*) {
void LevenbergMarquardt::fcn(int, int n, Real* x, Real* fvec) {
Array xt(n);
std::copy(x, x+n, xt.begin());
// constraint handling needs some improvement in the future:
Expand All @@ -152,7 +150,7 @@ namespace QuantLib {
}
}

void LevenbergMarquardt::jacFcn(int m, int n, Real* x, Real* fjac, int*) {
void LevenbergMarquardt::jacFcn(int m, int n, Real* x, Real* fjac) {
Array xt(n);
std::copy(x, x+n, xt.begin());
// constraint handling needs some improvement in the future:
Expand Down
14 changes: 3 additions & 11 deletions ql/math/optimization/levenbergmarquardt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,15 @@ namespace QuantLib {
EndCriteria::Type minimize(Problem& P,
const EndCriteria& endCriteria) override;

void fcn(int m,
int n,
Real* x,
Real* fvec,
int* iflag);
void jacFcn(int m,
int n,
Real* x,
Real* fjac,
int* iflag);
Comment on lines -58 to -67
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn't have been in the public interface but they are. I would deprecate them (and forward their calls to the private ones, of course).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


/*! \deprecated Don't use this method; inspect the result of minimize instead.
Deprecated in version 1.36.
*/
[[deprecated("Don't use this method; inspect the result of minimize instead")]]
virtual Integer getInfo() const { return info_; }
private:
void fcn(int m, int n, Real* x, Real* fvec);
void jacFcn(int m, int n, Real* x, Real* fjac);

Problem* currentProblem_;
Array initCostValues_;
Matrix initJacobian_;
Expand Down
4 changes: 2 additions & 2 deletions ql/math/optimization/problem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ namespace QuantLib {
//! Cost function
CostFunction& costFunction() const { return costFunction_; }

void setCurrentValue(const Array& currentValue) {
currentValue_=currentValue;
void setCurrentValue(Array currentValue) {
currentValue_ = std::move(currentValue);
}

//! current value of the local minimum
Expand Down
9 changes: 2 additions & 7 deletions ql/termstructures/globalbootstrap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,12 +272,6 @@ template <class Curve> void GlobalBootstrap<Curve>::calculate() const {
return std::tan((y - lowerBounds_[i]) * M_PI / (upperBounds_[i] - lowerBounds_[i]) - M_PI_2);
}

Real value(const Array& x) const override {
Array v = values(x);
std::transform(v.begin(), v.end(), v.begin(), [](Real x) -> Real { return x*x; });
return std::sqrt(std::accumulate(v.begin(), v.end(), Real(0.0)) / static_cast<Real>(v.size()));
}

Array values(const Array& x) const override {
for (Size i = 0; i < x.size(); ++i) {
Traits::updateGuess(ts_->data_, transformDirect(x[i], i), i + 1);
Expand All @@ -304,7 +298,8 @@ template <class Curve> void GlobalBootstrap<Curve>::calculate() const {
Curve *ts_;
const std::vector<Real> lowerBounds_, upperBounds_;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is local to the function, instantiated and then destroyed at function end. You can declare lowerBounds_ and upperBounds_ as const & instead and avoid all the moves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but note that constructor arguments for TargetFunction then should not be declared as const&, because doing so would allow passing temporary objects. We are then storing a reference to a temporary object, which will be destroyed at the end of the statement. I declared them as const pointers instead.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be an unnecessary precaution. This function is the only user of TargetFunction, nobody is going to pass temporary objects. But it's ok either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's safe at the moment, but we might refractor the code in the future to, say, use std ranges. I'd rather have a compile time error than debug random numbers coming out from bootstrapping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My two cents is that const reference is better syntactically and stylistically, and that any regression from future refactoring will be caught right away by the address sanitizer CI job as long as there are adequate unit tests in place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Google C++ style prohibits non-const references and requires using pointers to mutate arguments, so using pointers for retained arguments is very natural.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Too bad the interface of Problem requires this to inherit from CostFunction. Otherwise, the whole thing could be a lambda and just capture what it needs. Oh well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. We can make a templated implementation of CostFunction that wraps a functor. Then we can write this as a lambda.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How's this #2117?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks.

};
TargetFunction cost(firstHelper_, numberHelpers_, additionalErrors_, ts_, lowerBounds, upperBounds);
TargetFunction cost(firstHelper_, numberHelpers_, additionalErrors_, ts_,
std::move(lowerBounds), std::move(upperBounds));

// setup guess
Array guess(numberBounds);
Expand Down
Loading