Skip to content

Commit

Permalink
Pass kNumResiduals to Autodiff
Browse files Browse the repository at this point in the history
The compile-time constant kNumResiduals is now passed to the
autodiff functions as a template parameter. This will be used
by future patches to optimize autodiff performance.

Change-Id: Ia2b2cc99b88752e8f12f4ce2542b1963bda552f5
  • Loading branch information
darglein committed Mar 20, 2020
1 parent f339d71 commit e7a3035
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 187 deletions.
2 changes: 1 addition & 1 deletion include/ceres/autodiff_cost_function.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class AutoDiffCostFunction : public SizedCostFunction<kNumResiduals, Ns...> {
return internal::VariadicEvaluate<ParameterDims>(
*functor_, parameters, residuals);
}
return internal::AutoDifferentiate<ParameterDims>(
return internal::AutoDifferentiate<kNumResiduals, ParameterDims>(
*functor_,
parameters,
SizedCostFunction<kNumResiduals, Ns...>::num_residuals(),
Expand Down
1 change: 1 addition & 0 deletions include/ceres/autodiff_local_parameterization.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ class AutoDiffLocalParameterization : public LocalParameterization {
const double* parameter_ptrs[2] = {x, zero_delta};
double* jacobian_ptrs[2] = {NULL, jacobian};
return internal::AutoDifferentiate<
kGlobalSize,
internal::StaticParameterDims<kGlobalSize, kLocalSize>>(
*functor_, parameter_ptrs, kGlobalSize, x_plus_delta, jacobian_ptrs);
}
Expand Down
26 changes: 17 additions & 9 deletions include/ceres/internal/autodiff.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,14 @@ template <typename Seq, int ParameterIdx = 0, int Offset = 0>
struct Make1stOrderPerturbations;

template <int N, int... Ns, int ParameterIdx, int Offset>
struct Make1stOrderPerturbations<integer_sequence<int, N, Ns...>, ParameterIdx,
struct Make1stOrderPerturbations<integer_sequence<int, N, Ns...>,
ParameterIdx,
Offset> {
template <typename T, typename JetT>
static void Apply(T const* const* parameters, JetT* x) {
Make1stOrderPerturbation<Offset, N>(parameters[ParameterIdx], x + Offset);
Make1stOrderPerturbations<integer_sequence<int, Ns...>, ParameterIdx + 1,
Make1stOrderPerturbations<integer_sequence<int, Ns...>,
ParameterIdx + 1,
Offset + N>::Apply(parameters, x);
}
};
Expand Down Expand Up @@ -253,14 +255,16 @@ template <typename Seq, int ParameterIdx = 0, int Offset = 0>
struct Take1stOrderParts;

template <int N, int... Ns, int ParameterIdx, int Offset>
struct Take1stOrderParts<integer_sequence<int, N, Ns...>, ParameterIdx,
struct Take1stOrderParts<integer_sequence<int, N, Ns...>,
ParameterIdx,
Offset> {
template <typename JetT, typename T>
static void Apply(int num_outputs, JetT* output, T** jacobians) {
if (jacobians[ParameterIdx]) {
Take1stOrderPart<Offset, N>(num_outputs, output, jacobians[ParameterIdx]);
}
Take1stOrderParts<integer_sequence<int, Ns...>, ParameterIdx + 1,
Take1stOrderParts<integer_sequence<int, Ns...>,
ParameterIdx + 1,
Offset + N>::Apply(num_outputs, output, jacobians);
}
};
Expand All @@ -269,13 +273,17 @@ struct Take1stOrderParts<integer_sequence<int, N, Ns...>, ParameterIdx,
template <int ParameterIdx, int Offset>
struct Take1stOrderParts<integer_sequence<int>, ParameterIdx, Offset> {
template <typename T, typename JetT>
static void Apply(int /* NOT USED*/, JetT* /* NOT USED*/,
static void Apply(int /* NOT USED*/,
JetT* /* NOT USED*/,
T** /* NOT USED */) {}
};

template <typename ParameterDims, typename Functor, typename T>
template <int kNumResiduals,
typename ParameterDims,
typename Functor,
typename T>
inline bool AutoDifferentiate(const Functor& functor,
T const *const *parameters,
T const* const* parameters,
int num_outputs,
T* function_value,
T** jacobians) {
Expand All @@ -301,8 +309,8 @@ inline bool AutoDifferentiate(const Functor& functor,

Make1stOrderPerturbations<Parameters>::Apply(parameters, x.data());

if (!VariadicEvaluate<ParameterDims>(functor, unpacked_parameters.data(),
output)) {
if (!VariadicEvaluate<ParameterDims>(
functor, unpacked_parameters.data(), output)) {
return false;
}

Expand Down
Loading

0 comments on commit e7a3035

Please sign in to comment.