Loading...
Searching...
No Matches
CostModel.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "CostModel.h"
6
7#include <cmath>
8#include <limits>
9
10#include "Krylov.h"
11#include "Preconditioner.h"
12
14
15double denseObjectiveFlops(Eigen::Index order, Eigen::Index hyperparameters)
16{
17 const double n = static_cast<double>(order);
18 const double factor = n * n * n / 3.0;
19 const double inverse = n * n * n + 2.0 * n * n * n;
20 const double solves = 4.0 * n * n;
21 const double contractions =
22 2.0 * n * n * static_cast<double>(hyperparameters);
23 return factor + inverse + solves + contractions;
24}
25
26double krylovObjectiveFlops(const KrylovObjectiveInputs& in, Eigen::Index rank)
27{
28 const auto r = static_cast<std::size_t>(rank);
29 if (r >= in.residual_trace.size() || !(in.noise_floor > 0.0))
30 return std::numeric_limits<double>::infinity();
31 const double n = static_cast<double>(in.order);
32 const double m = static_cast<double>(in.probes);
33 const double rr = static_cast<double>(rank);
34 // Sum of pivotFlops over the first `rank` pivots.
35 const double pivots = rr * in.column_flops + n * rr * (rr + 1.0);
36 const double kappa =
37 1.0 + std::max(0.0, in.residual_trace[r]) / in.noise_floor;
38 const double iterations =
40 const double rhs = 1.0 + m;
41 const double per_iteration =
42 rhs * (in.matvec_flops + 4.0 * n * rr + 2.0 * rr * rr + 12.0 * n);
43 const double contractions = static_cast<double>(in.hyperparameters) *
44 (2.0 * n * n * m + 2.0 * n * m);
45 const double quadrature = m * iterations * iterations * iterations;
46 return pivots + iterations * per_iteration + contractions + quadrature;
47}
48
50{
51 KrylovPlan best;
52 best.flops = std::numeric_limits<double>::infinity();
53 const double n = static_cast<double>(in.order);
54 for (std::size_t r = 0; r < in.residual_trace.size(); ++r) {
55 const auto rank = static_cast<Eigen::Index>(r);
56 const double rr = static_cast<double>(rank);
57 // Past the operations of a full Cholesky the pivots buy nothing a
58 // dense factor would not give exactly.
59 if (rr * in.column_flops + n * rr * (rr + 1.0) > n * n * n / 3.0) break;
60 const double flops = krylovObjectiveFlops(in, rank);
61 if (flops < best.flops) {
62 best.flops = flops;
63 best.rank = rank;
65 1.0 + std::max(0.0, in.residual_trace[r]) / in.noise_floor,
67 }
68 }
69 return best;
70}
71
72} // namespace gpr::linalg::linop
KrylovPlan planKrylovObjective(const KrylovObjectiveInputs &in)
Definition CostModel.cpp:49
double conjugateGradientIterations(double kappa, double tolerance)
Definition Krylov.cpp:190
double krylovObjectiveFlops(const KrylovObjectiveInputs &in, Eigen::Index rank)
Definition CostModel.cpp:26
double denseObjectiveFlops(Eigen::Index order, Eigen::Index hyperparameters)
Definition CostModel.cpp:15
double column_flops
one column read while pivoting
Definition CostModel.h:29
double noise_floor
smallest added diagonal entry
Definition CostModel.h:30
std::vector< double > residual_trace
Bound on tr(A0 - F_r F_r^T) by rank r = 0, 1, ...
Definition CostModel.h:34
double matvec_flops
one operator action on one column
Definition CostModel.h:28