Add CRS Jacobian and gradient eval benchmarks

Factor out RunEvaluatorBenchmark in evaluation_benchmark.cc and add
ResidualsAndJacobianCRS and ResidualsGradientAndJacobian benchmarks.
Remove unused BAL problem construction helpers from
benchmark_cost_functions.h.

Change-Id: Iebd0bedc071fb0bd779a0cbe3876191bf1e4915a
diff --git a/internal/ceres/benchmark_cost_functions.h b/internal/ceres/benchmark_cost_functions.h
index 464ab14..06986ff 100644
--- a/internal/ceres/benchmark_cost_functions.h
+++ b/internal/ceres/benchmark_cost_functions.h
@@ -114,8 +114,7 @@
 };
 
 // Base class declaring the residual count and parameter block dimensions of a
-// benchmark cost functor, along with CostFunction, Manifold, and LossFunction
-// factories.
+// benchmark cost functor, along with CostFunction factories.
 template <int kResiduals, int... Ns>
 struct BenchmarkCostFunctor {
   static constexpr int kNumResiduals = kResiduals;
@@ -171,12 +170,6 @@
     cost_function->SetNumResiduals(kNumResiduals);
     return cost_function;
   }
-
-  static std::unique_ptr<Manifold> MakeManifold(int /*block_index*/) {
-    return nullptr;
-  }
-
-  static std::unique_ptr<LossFunction> MakeLossFunction() { return nullptr; }
 };
 
 inline std::array<double, 3> NormalizedVector3(double x, double y, double z) {
@@ -206,21 +199,6 @@
   return pose;
 }
 
-// Converts the angle-axis rotation and translation prefix of a 9-parameter BAL
-// camera `[rx, ry, rz, tx, ty, tz, ...]` into a 7-parameter pose `[qx, qy, qz,
-// qw, tx, ty, tz]` with an Eigen-convention quaternion.
-inline std::array<double, 7> PoseFromBALCamera(const double* bal_camera) {
-  double q_wxyz[4];
-  AngleAxisToQuaternion(bal_camera, q_wxyz);
-  return {q_wxyz[1],
-          q_wxyz[2],
-          q_wxyz[3],
-          q_wxyz[0],
-          bal_camera[3],
-          bal_camera[4],
-          bal_camera[5]};
-}
-
 template <int kParameterBlockSize>
 struct ConstantCostFunction : BenchmarkCostFunctor<1, kParameterBlockSize> {
   using
@@ -293,15 +271,6 @@
     return ParameterBlocks({1., 2., 3., 4., 5., 6., 7., 8., 9.}, {1., 2., 3.});
   }
 
-  static ParameterBlocks MakeParametersFromBAL(const double* bal_camera,
-                                               const double* bal_point) {
-    std::array<double, 9> camera{};
-    std::array<double, 3> point{};
-    std::copy_n(bal_camera, 9, camera.data());
-    std::copy_n(bal_point, 3, point.data());
-    return ParameterBlocks(camera, point);
-  }
-
   template <typename T>
   inline bool operator()(const T* const camera,
                          const T* const point,
@@ -870,36 +839,6 @@
         {1000.0, 1000.0, 640.0, 360.0, -0.05, 0.01, 1e-4, -1e-4});
   }
 
-  static ParameterBlocks MakeParametersFromBAL(const double* bal_camera,
-                                               const double* bal_point) {
-    // BAL cameras look down -Z with focal length bal_camera[6] and radial
-    // distortion (bal_camera[7], bal_camera[8]); negate focal length so
-    // projection matches COLMAP's +Z camera convention.
-    std::array<double, 8> intrinsics = {-bal_camera[6],
-                                        -bal_camera[6],
-                                        0.0,
-                                        0.0,
-                                        bal_camera[7],
-                                        bal_camera[8],
-                                        1e-4,
-                                        1e-4};
-    return ParameterBlocks({bal_point[0], bal_point[1], bal_point[2]},
-                           PoseFromBALCamera(bal_camera),
-                           intrinsics);
-  }
-
-  static std::unique_ptr<Manifold> MakeManifold(int block_index) {
-    if (block_index == 1) {
-      return std::make_unique<
-          ProductManifold<EigenQuaternionManifold, EuclideanManifold<3>>>();
-    }
-    return nullptr;
-  }
-
-  static std::unique_ptr<LossFunction> MakeLossFunction() {
-    return std::make_unique<HuberLoss>(1.0);
-  }
-
   template <typename T>
   inline bool operator()(const T* const point3D_in_world,
                          const T* const cam_from_world,
@@ -1227,30 +1166,6 @@
         {0.5, -0.3, 4.2});
   }
 
-  static ParameterBlocks MakeParametersFromBAL(const double* bal_camera,
-                                               const double* bal_point) {
-    std::array<double, 9> intrinsics = {-bal_camera[6],
-                                        0.0,
-                                        0.0,
-                                        bal_camera[7],
-                                        bal_camera[8],
-                                        0.0,
-                                        0.0,
-                                        1e-4,
-                                        1e-4};
-    std::array<double, 6> pose{};
-    std::copy_n(bal_camera, 6, pose.data());
-    return ParameterBlocks(
-        intrinsics, pose, {bal_point[0], bal_point[1], bal_point[2]});
-  }
-
-  static std::unique_ptr<Manifold> MakeManifold(int block_index) {
-    if (block_index == 0) {
-      return std::make_unique<SubsetManifold>(9, std::vector<int>{1, 2, 5, 6});
-    }
-    return nullptr;
-  }
-
   template <typename T>
   inline bool operator()(const T* const intrinsics,
                          const T* const R_t,
diff --git a/internal/ceres/evaluation_benchmark.cc b/internal/ceres/evaluation_benchmark.cc
index 13cddb3..45faf32 100644
--- a/internal/ceres/evaluation_benchmark.cc
+++ b/internal/ceres/evaluation_benchmark.cc
@@ -239,13 +239,21 @@
   std::unique_ptr<ImplicitSchurComplement> implicit_schur_complement_diag;
 };
 
-static void Residuals(benchmark::State& state,
-                      BALData* data,
-                      ContextImpl* context) {
+static void RunEvaluatorBenchmark(benchmark::State& state,
+                                  BALData* data,
+                                  ContextImpl* context,
+                                  bool evaluate_gradient,
+                                  bool evaluate_jacobian,
+                                  bool crs_jacobian) {
   const int num_threads = static_cast<int>(state.range(0));
 
   Evaluator::Options options;
-  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
+  if (crs_jacobian) {
+    options.linear_solver_type = CGNR;
+    options.sparse_linear_algebra_library_type = CUDA_SPARSE;
+  } else {
+    options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
+  }
   options.num_threads = num_threads;
   options.context = context;
   options.num_eliminate_blocks = 0;
@@ -255,10 +263,12 @@
   auto program = data->preprocessed_problem->reduced_program.get();
   CHECK(program != nullptr);
   auto evaluator = Evaluator::Create(options, program, &error);
-  CHECK(evaluator != nullptr);
+  CHECK(evaluator != nullptr) << error;
 
   double cost = 0.;
   Vector residuals = Vector::Zero(program->NumResiduals());
+  Vector gradient = Vector::Zero(program->NumEffectiveParameters());
+  auto jacobian = evaluate_jacobian ? evaluator->CreateJacobian() : nullptr;
 
   Evaluator::EvaluateOptions eval_options;
   for (auto _ : state) {
@@ -266,42 +276,53 @@
                               data->parameters.data(),
                               &cost,
                               residuals.data(),
-                              nullptr,
-                              nullptr));
+                              evaluate_gradient ? gradient.data() : nullptr,
+                              jacobian.get()));
   }
 }
 
+static void Residuals(benchmark::State& state,
+                      BALData* data,
+                      ContextImpl* context) {
+  RunEvaluatorBenchmark(state,
+                        data,
+                        context,
+                        /*evaluate_gradient=*/false,
+                        /*evaluate_jacobian=*/false,
+                        /*crs_jacobian=*/false);
+}
+
 static void ResidualsAndJacobian(benchmark::State& state,
                                  BALData* data,
                                  ContextImpl* context) {
-  const int num_threads = static_cast<int>(state.range(0));
+  RunEvaluatorBenchmark(state,
+                        data,
+                        context,
+                        /*evaluate_gradient=*/false,
+                        /*evaluate_jacobian=*/true,
+                        /*crs_jacobian=*/false);
+}
 
-  Evaluator::Options options;
-  options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
-  options.num_threads = num_threads;
-  options.context = context;
-  options.num_eliminate_blocks = 0;
+static void ResidualsAndJacobianCRS(benchmark::State& state,
+                                    BALData* data,
+                                    ContextImpl* context) {
+  RunEvaluatorBenchmark(state,
+                        data,
+                        context,
+                        /*evaluate_gradient=*/false,
+                        /*evaluate_jacobian=*/true,
+                        /*crs_jacobian=*/true);
+}
 
-  std::string error;
-  CHECK(data->preprocessed_problem != nullptr);
-  auto program = data->preprocessed_problem->reduced_program.get();
-  CHECK(program != nullptr);
-  auto evaluator = Evaluator::Create(options, program, &error);
-  CHECK(evaluator != nullptr);
-
-  double cost = 0.;
-  Vector residuals = Vector::Zero(program->NumResiduals());
-  auto jacobian = evaluator->CreateJacobian();
-
-  Evaluator::EvaluateOptions eval_options;
-  for (auto _ : state) {
-    CHECK(evaluator->Evaluate(eval_options,
-                              data->parameters.data(),
-                              &cost,
-                              residuals.data(),
-                              nullptr,
-                              jacobian.get()));
-  }
+static void ResidualsGradientAndJacobian(benchmark::State& state,
+                                         BALData* data,
+                                         ContextImpl* context) {
+  RunEvaluatorBenchmark(state,
+                        data,
+                        context,
+                        /*evaluate_gradient=*/true,
+                        /*evaluate_jacobian=*/true,
+                        /*crs_jacobian=*/false);
 }
 
 static void Plus(benchmark::State& state, BALData* data, ContextImpl* context) {
@@ -807,10 +828,11 @@
 
   for (int i = 1; i < argc; ++i) {
     const std::string path(argv[i]);
-    const std::string name_residuals = "Residuals<" + path + ">";
     benchmark_data.emplace_back(
         std::make_unique<ceres::internal::BALData>(path));
     auto data = benchmark_data.back().get();
+
+    const std::string name_residuals = "Residuals<" + path + ">";
     ::benchmark::RegisterBenchmark(
         name_residuals.c_str(), ceres::internal::Residuals, data, &context)
         ->Arg(1)
@@ -830,6 +852,31 @@
         ->Arg(8)
         ->Arg(16);
 
+    const std::string name_jacobians_crs =
+        "ResidualsAndJacobianCRS<" + path + ">";
+    ::benchmark::RegisterBenchmark(name_jacobians_crs.c_str(),
+                                   ceres::internal::ResidualsAndJacobianCRS,
+                                   data,
+                                   &context)
+        ->Arg(1)
+        ->Arg(2)
+        ->Arg(4)
+        ->Arg(8)
+        ->Arg(16);
+
+    const std::string name_gradient_and_jacobians =
+        "ResidualsGradientAndJacobian<" + path + ">";
+    ::benchmark::RegisterBenchmark(
+        name_gradient_and_jacobians.c_str(),
+        ceres::internal::ResidualsGradientAndJacobian,
+        data,
+        &context)
+        ->Arg(1)
+        ->Arg(2)
+        ->Arg(4)
+        ->Arg(8)
+        ->Arg(16);
+
     const std::string name_plus = "Plus<" + path + ">";
     ::benchmark::RegisterBenchmark(
         name_plus.c_str(), ceres::internal::Plus, data, &context)