Loading...
Searching...
No Matches
CholeskyUpdate.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4#pragma once
5
6#include <Eigen/Core>
7#include <algorithm>
8#include <cmath>
9
11#ifdef GPR_LINALG_SCALAPACK
12#include "DistributedState.h"
13#endif
14
15namespace gpr::linalg {
16
20 const Eigen::VectorXd& vector)
21{
22 const Eigen::Index n = lower.rows();
23 if (n <= 0 || lower.cols() != n || vector.size() != n ||
24 !vector.allFinite())
25 return false;
26 if (vector.isZero(0.0)) return true;
27 Eigen::VectorXd update = vector;
28 for (Eigen::Index j = 0; j < n; ++j) {
29 const double diagonal = lower(j, j);
30 const double radius = std::hypot(diagonal, update(j));
31 if (!(diagonal > 0.0) || !std::isfinite(radius)) return false;
32 const double cosine = diagonal / radius;
33 const double sine = update(j) / radius;
34 lower(j, j) = radius;
35 for (Eigen::Index i = j + 1; i < n; ++i) {
36 const double value = lower(i, j);
37 const double tail = update(i);
38 lower(i, j) = std::fma(sine, tail, cosine * value);
39 update(i) = std::fma(-sine, value, cosine * tail);
40 }
41 }
42 return true;
43}
44
47 EigenMatrix& lower, const Eigen::VectorXd& vector
49 ,
51#endif
52)
53{
54#ifdef GPR_LINALG_SCALAPACK
55 if (lower.size() != 0) return rank_one_update_lower(lower, vector);
56 const int n = state.cached_n();
57 if (!state.has_cached_L() || n <= 0 || vector.size() != n ||
58 !vector.allFinite())
59 return false;
60 if (vector.isZero(0.0)) return true;
61 state.invalidate_derived_cholesky();
62 const int block = state.block_size();
63 const bool linear = state.cached_L_desc()[1] == state.lin_context();
64 const int grid_rows = linear ? state.nprocs() : state.nprow();
65 const int grid_cols = linear ? 1 : state.npcol();
66 const int proc_row = linear ? state.lin_myrow() : state.myrow();
67 const int proc_col = linear ? state.lin_mycol() : state.mycol();
68 const int rows = linear ? state.lin_local_rows(n) : state.local_rows(n);
69 std::vector<int> global_rows;
70 global_rows.reserve(rows);
71 for (int i = 0; i < n; ++i)
72 if ((i / block) % grid_rows == proc_row) global_rows.push_back(i);
73 Eigen::VectorXd update = vector;
74 std::vector<double> row_update(static_cast<std::size_t>(rows));
75 std::vector<double> rotations(
76 1 + 2 * static_cast<std::size_t>(std::min(block, n)));
77 // Rotations reuse the grid's communication contexts across fits; a
78 // rank update changes the factor values and leaves its layout intact.
79 const auto comms = state.tile_communicators(state.cached_L_desc()[1]);
80 double* factor = state.cached_L_mut();
81 bool valid = true;
82 for (int first = 0; first < n;) {
83 const int width = std::min(block, n - first);
84 const int end = first + width;
85 const int owner_row = (first / block) % grid_rows;
86 const int owner_col = (first / block) % grid_cols;
87 const int local_first_col =
88 state.local_index_for_global(first, block, owner_col, grid_cols);
89 rotations[0] = 1.0;
90 // Every pivot in this block depends only on its lower triangle and
91 // the update entries in these rows. One owner forms the rotations;
92 // rows below the block consume them in the same increasing order.
93 if (proc_row == owner_row && proc_col == owner_col) {
94 const int local_first_row = state.local_index_for_global(
95 first, block, owner_row, grid_rows);
96 for (int offset = 0; offset < width; ++offset) {
97 const int j = first + offset;
98 double* column =
99 factor +
100 static_cast<std::size_t>(local_first_col + offset) * rows;
101 const double diagonal = column[local_first_row + offset];
102 const double radius = std::hypot(diagonal, update(j));
103 if (!(diagonal > 0.0) || !std::isfinite(radius)) {
104 rotations[0] = 0.0;
105 break;
106 }
107 const double cosine = diagonal / radius;
108 const double sine = update(j) / radius;
109 rotations[1 + 2 * offset] = cosine;
110 rotations[2 + 2 * offset] = sine;
111 column[local_first_row + offset] = radius;
112 for (int i = offset + 1; i < width; ++i) {
113 const double value = column[local_first_row + i];
114 const double tail = update(first + i);
115 column[local_first_row + i] =
116 std::fma(sine, tail, cosine * value);
117 update(first + i) = std::fma(-sine, value, cosine * tail);
118 }
119 }
120 }
121 MPI_Bcast(
122 rotations.data(), 1 + 2 * width, MPI_DOUBLE,
123 comms.rank_of[static_cast<std::size_t>(owner_row) * grid_cols +
124 owner_col],
125 state.world_comm());
126 if (rotations[0] == 0.0) {
127 valid = false;
128 break;
129 }
130 if (proc_col == owner_col && rows > 0) {
131 for (int offset = 0; offset < width; ++offset) {
132 const double cosine = rotations[1 + 2 * offset];
133 const double sine = rotations[2 + 2 * offset];
134 double* column =
135 factor +
136 static_cast<std::size_t>(local_first_col + offset) * rows;
137 for (int local_row = 0; local_row < rows; ++local_row) {
138 const int i = global_rows[local_row];
139 if (i < end) continue;
140 const double value = column[local_row];
141 const double tail = update(i);
142 column[local_row] = std::fma(sine, tail, cosine * value);
143 update(i) = std::fma(-sine, value, cosine * tail);
144 }
145 }
146 for (int local_row = 0; local_row < rows; ++local_row)
147 row_update[local_row] = update(global_rows[local_row]);
148 }
149 // Replicate the completed row updates across process columns before
150 // the next diagonal block selects its owner.
151 MPI_Bcast(row_update.data(), rows, MPI_DOUBLE, owner_col, comms.row);
152 for (int local_row = 0; local_row < rows; ++local_row)
153 update(global_rows[local_row]) = row_update[local_row];
154 first = end;
155 }
156 if (!valid) state.invalidate_L();
157 return valid;
158#else
159 return rank_one_update_lower(lower, vector);
160#endif
161}
162
163} // namespace gpr::linalg
#define GPR_LINALG_SCALAPACK
Definition SlateBackend.h:9
static DistributedState & instance()
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
bool cholesky_rank_update(EigenMatrix &lower, const Eigen::VectorXd &vector)
Update the selected backend's live Cholesky factor in its existing layout.
bool rank_one_update_lower(EigenMatrix &lower, const Eigen::VectorXd &vector)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20