Improve finite_diff_auto with Eigen expressions#3096
Conversation
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
SteveBronder
left a comment
There was a problem hiding this comment.
Quick Q about move semantics but rest looks good!
| decltype(auto) x_temp | ||
| = EigT::NullaryExpr(x.size(), [&x, &i, &h, &j](Eigen::Index k) { | ||
| return k == i ? x[i] + h * h_scale[j] : x[k]; | ||
| }); | ||
| delta_f += f(std::forward<decltype(x_temp)>(x_temp)) * mults[j]; |
There was a problem hiding this comment.
Why is this decltype(auto) instead of auto?
Also if it is being made here then it can be done using std::move
There was a problem hiding this comment.
Given that it was taking a universal/forwarding reference, I wasn't sure if decltype(auto) was needed on the return to maintain any reference/const/etc status - but happy to drop if unnecessary!
Jenkins Console Log Machine informationNo LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 20.04.3 LTS Release: 20.04 Codename: focalCPU: G++: Clang: |
Summary
finite_diff_gradient_autocurrently requires copying the input parameters and iteratively replacing values in the vector, requiring serial evaluation of the loop.Rather than replacing values, we can instead use an Eigen expression to 'mask' a value at a specified index - avoiding the initial copy
Tests
N/A - current tests should still pass
Side Effects
N/A
Release notes
Improved efficiency of
finite_diff_gradient_autoChecklist
Copyright holder: Andrew Johnson
The copyright holder is typically you or your assignee, such as a university or company. By submitting this pull request, the copyright holder is agreeing to the license the submitted work under the following licenses:
- Code: BSD 3-clause (https://opensource.org/licenses/BSD-3-Clause)
- Documentation: CC-BY 4.0 (https://creativecommons.org/licenses/by/4.0/)
the basic tests are passing
./runTests.py test/unit)make test-headers)make test-math-dependencies)make doxygen)make cpplint)the code is written in idiomatic C++ and changes are documented in the doxygen
the new changes are tested