Skip to content

Rip out make_coherent, add CoherentPadOp.

Rip out make_coherent, add CoherentPadOp.

The make_coherent approach really irks me, since it modifies const inputs, and ends up requiring re-sizing expressions like CwiseNullaryOp to get working.

Here we replace with a CoherentPadOp, that artificially pads an input with zeros if the derivative sizes do not match. This only affects cases where the derivative vectors are dynamic-sized, or if both fixed sized and not equal.

Ran benchmarks, and this seems to be about 20% faster on average than the existing make_coherent approach.

Benchmark source:

#include <benchmark/benchmark.h>
#include <unsupported/Eigen/AutoDiff>

template<typename DerivativeType>
void BM_AutoDiffScalar(benchmark::State& state) {
  int nder = state.range(0);
  using AD = Eigen::AutoDiffScalar<DerivativeType>;
  AD a(1, nder, 1);
  AD b(1, nder, 2);
  AD c(1, nder, 3);
  AD d(1, nder, nder);
  AD e = 1.0;  // No initialized derivatives.
  e.derivatives().resize(nder);
  e.derivatives().setZero();
  for (auto s : state) {
    AD r;
    benchmark::DoNotOptimize(r = sin(2 * a + AD(3) * b + AD(4) * cos(c)));
    // #2235.
    benchmark::DoNotOptimize(r = 2.0 * e - d);
  }
}

BENCHMARK_TEMPLATE(BM_AutoDiffScalar, Eigen::Vector4d)->Arg(4);
BENCHMARK_TEMPLATE(BM_AutoDiffScalar, Eigen::VectorXd)->Arg(4)->Arg(8)->Arg(16)->Arg(32)->Arg(64)->Arg(128);
Comparing ./autodiff_master to ./autodiff_pad
Benchmark                                                Time             CPU      Time Old      Time New       CPU Old       CPU New
-------------------------------------------------------------------------------------------------------------------------------------
BM_AutoDiffScalar<Eigen::Vector4d>/4                  -0.0022         -0.0023             1             1             1             1
BM_AutoDiffScalar<Eigen::VectorXd>/4                  -0.2929         -0.2929            95            67            95            67
BM_AutoDiffScalar<Eigen::VectorXd>/8                  -0.2811         -0.2811            97            69            97            69
BM_AutoDiffScalar<Eigen::VectorXd>/16                 -0.3013         -0.3014           101            70           101            70
BM_AutoDiffScalar<Eigen::VectorXd>/32                 -0.2763         -0.2763           111            80           111            80
BM_AutoDiffScalar<Eigen::VectorXd>/64                 -0.1948         -0.1949           131           105           131           105
BM_AutoDiffScalar<Eigen::VectorXd>/128                -0.2250         -0.2250           212           165           212           165
OVERALL_GEOMEAN                                       -0.2303         -0.2303             0             0             0             0
Edited by Antonio Sánchez

Merge request reports

Loading