Loading...
Searching...
No Matches
TrainingCholesky.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3#include "TrainingCholesky.h"
4
5#include <Eigen/Cholesky>
6#include <algorithm>
7#include <limits>
8#include <stdexcept>
9#include <vector>
10
11#include "FixedOrderKernels.h"
12#include "schemeii/SchemeII.h"
13
14#ifdef GPR_LINALG_SCALAPACK
15#include "DistributedState.h"
16#endif
17
18namespace gpr::linalg {
19namespace {
20constexpr int panel_width = 256;
21
22// Fixed-order panel factor: the Cholesky factor of the leading block, then
23// the rows below by X L^-T. The rounding depends on the panel values only.
24bool factorPanel(ProductMatrix panel)
25{
26 const Eigen::Index width = panel.cols();
27 if (!fixed::choleskyLower(panel.data(), panel.outerStride(), width))
28 return false;
29 if (panel.rows() > width)
31 panel.data(), panel.outerStride(), width, panel.data() + width,
32 panel.outerStride(), panel.rows() - width);
33 return panel.allFinite();
34}
35
36// trailing -= left right^T on and below the diagonal of a square trailing
37// block (left and right are the same border). The in-process Scheme II
38// engine computes only the lower block columns from prepared residues.
39void updateTrailingLower(ConstProductMatrix border, ProductMatrix trailing)
40{
41 const auto scheme = schemeIIContext();
42 if (!scheme) {
43 if (!tryTrainingProduct(border, border, trailing, false, true, -1.0,
44 1.0, true))
45 trailing.noalias() -= border * border.transpose();
46 return;
47 }
48 const Eigen::Index rows = border.rows(), width = border.cols();
49 const schemeii::Plan plan = scheme->plan(width);
50 const schemeii::Operand left = scheme->prepareRows(
51 border.data(), border.outerStride(), false, rows, width, plan);
52 const schemeii::Operand right = scheme->prepareColumns(
53 border.data(), border.outerStride(), true, width, rows, plan);
54 std::vector<schemeii::Task> tasks;
55 for (Eigen::Index first = 0; first < rows; first += panel_width) {
56 const Eigen::Index columns =
57 std::min<Eigen::Index>(panel_width, rows - first);
58 tasks.push_back(
59 {&left, first, rows - first, &right, first, columns, -1.0, 1.0,
60 trailing.data() + first + first * trailing.outerStride(),
61 trailing.outerStride()});
62 }
63 scheme->run(tasks);
64}
65
66void updateTrailing(ConstProductMatrix left, ConstProductMatrix right,
67 ProductMatrix trailing)
68{
69 // Partitioned: the arithmetic of a local block cannot depend on its
70 // size, which follows the process grid.
71 if (!tryTrainingProduct(left, right, trailing, false, true, -1.0, 1.0,
72 true))
73 trailing.noalias() -= left * right.transpose();
74}
75} // namespace
76
77bool trainingCholeskySelected(Eigen::Index order)
78{
79 return order >= 2 * panel_width && trainingProductConfigured();
80}
81
83{
84 // The in-process Scheme II engine serves the reproducible tile
85 // factorization instead, which keeps the block-cyclic layout and gathers
86 // no panel on one rank.
87 return order >= 2 * panel_width && trainingProductLibraryLoaded();
88}
89
91{
92 if (lower.rows() != lower.cols()) return false;
93 lower.triangularView<Eigen::StrictlyUpper>().setZero();
94 for (Eigen::Index first = 0; first < lower.rows(); first += panel_width) {
95 const Eigen::Index rows = lower.rows() - first;
96 const Eigen::Index width = std::min<Eigen::Index>(panel_width, rows);
97 auto panel = lower.block(first, first, rows, width);
98 if (!factorPanel(panel)) return false;
99 const Eigen::Index remaining = rows - width;
100 if (remaining > 0) {
101 const auto border = panel.bottomRows(remaining);
102 updateTrailingLower(border,
103 lower.bottomRightCorner(remaining, remaining));
104 }
105 }
106 lower.triangularView<Eigen::StrictlyUpper>().setZero();
107 return true;
108}
109
110#ifdef GPR_LINALG_SCALAPACK
111bool trainingCholeskyDistributed(double* lower, const int* desc, int order,
112 const DistributedState& state)
113{
114 if (!desc || order <= 0 || desc[1] != state.context() || desc[2] != order ||
115 desc[3] != order || desc[4] <= 0 || desc[5] <= 0 || desc[6] < 0 ||
116 desc[6] >= state.nprow() || desc[7] < 0 || desc[7] >= state.npcol())
117 return false;
118 const int nprow = state.nprow(), npcol = state.npcol();
119 const int myrow = state.myrow(), mycol = state.mycol();
120 const int rows = numroc_(&order, &desc[4], &myrow, &desc[6], &nprow);
121 const int columns = numroc_(&order, &desc[5], &mycol, &desc[7], &npcol);
122 if (desc[8] < std::max(1, rows)) return false;
123 const int row_coordinate = (myrow - desc[6] + nprow) % nprow;
124 const int column_coordinate = (mycol - desc[7] + npcol) % npcol;
125 std::vector<int> global_rows(rows), global_columns(columns);
126 for (int row = 0; row < rows; ++row)
127 global_rows[row] =
128 state.global_index_for_local(row, desc[4], row_coordinate, nprow);
129 for (int column = 0; column < columns; ++column)
130 global_columns[column] = state.global_index_for_local(
131 column, desc[5], column_coordinate, npcol);
132 double dummy = 0.0;
133 double* values = rows && columns ? lower : &dummy;
134 Eigen::Map<Eigen::MatrixXd, 0, Eigen::OuterStride<>> local(
135 values, rows, columns, Eigen::OuterStride<>(desc[8]));
136 for (int column = 0; column < columns; ++column)
137 for (int row = 0; row < rows; ++row)
138 if (global_rows[row] < global_columns[column])
139 local(row, column) = 0.0;
140
141 for (int first = 0; first < order; first += panel_width) {
142 const int panel_rows = order - first;
143 const int width = std::min(panel_width, panel_rows);
144 if (panel_rows > std::numeric_limits<int>::max() / width)
145 throw std::length_error(
146 "training Cholesky panel exceeds MPI count");
147 // The panel moves by a hand-packed gather and owner-side writes with
148 // local offsets in std::size_t; Cpdgemr2d forms LP64 offsets and
149 // cannot reach past 2^31 - 1 local entries.
150 Eigen::MatrixXd panel =
151 state.gather_block(values, desc, first, first, panel_rows, width,
152 /*broadcast_result=*/false);
153 int valid = 1;
154 if (state.is_root()) valid = factorPanel(panel);
155 MPI_Bcast(&valid, 1, MPI_INT, 0, state.world_comm());
156 if (!valid) return false;
157 if (!state.is_root()) panel.resize(panel_rows, width);
158 MPI_Bcast(panel.data(), panel_rows * width, MPI_DOUBLE, 0,
159 state.world_comm());
160 {
161 const auto row_first =
162 static_cast<int>(std::lower_bound(global_rows.begin(),
163 global_rows.end(), first) -
164 global_rows.begin());
165 const auto column_first =
166 static_cast<int>(std::lower_bound(global_columns.begin(),
167 global_columns.end(), first) -
168 global_columns.begin());
169 for (int column = column_first;
170 column < columns && global_columns[column] < first + width;
171 ++column)
172 for (int row = row_first; row < rows; ++row)
173 local(row, column) = panel(global_rows[row] - first,
174 global_columns[column] - first);
175 }
176 if (panel_rows == width) break;
177 const int row_start =
178 std::lower_bound(global_rows.begin(), global_rows.end(),
179 first + width) -
180 global_rows.begin();
181 const int column_start =
182 std::lower_bound(global_columns.begin(), global_columns.end(),
183 first + width) -
184 global_columns.begin();
185 int failed = 0;
186 std::string failure;
187 try {
188 if (rows > row_start && columns > column_start) {
189 Eigen::MatrixXd left(rows - row_start, width);
190 Eigen::MatrixXd right(columns - column_start, width);
191 for (int row = row_start; row < rows; ++row)
192 left.row(row - row_start) =
193 panel.row(global_rows[row] - first);
194 for (int column = column_start; column < columns; ++column)
195 right.row(column - column_start) =
196 panel.row(global_columns[column] - first);
197 updateTrailing(
198 left, right,
199 local.bottomRightCorner(left.rows(), right.rows()));
200 }
201 } catch (const std::exception& exception) {
202 failed = 1;
203 failure = exception.what();
204 }
205 MPI_Allreduce(MPI_IN_PLACE, &failed, 1, MPI_INT, MPI_MAX,
206 state.world_comm());
207 if (failed)
208 throw std::runtime_error(
209 failure.empty()
210 ? "training Cholesky update failed on another rank"
211 : failure);
212 }
213 return true;
214}
215#endif
216} // namespace gpr::linalg
int numroc_(const int *n, const int *nb, const int *iproc, const int *isrcproc, const int *nprocs)
void solveRightLowerTranspose(const double *l, std::int64_t ldl, std::int64_t n, double *x, std::int64_t ldx, std::int64_t rows)
bool choleskyLower(double *a, std::int64_t lda, std::int64_t n)
Dense and structured linear algebra behind the GP: Cholesky and eigen solvers, linear operators,...
Eigen::Ref< const Eigen::MatrixXd, 0, Eigen::OuterStride<> > ConstProductMatrix
Eigen::Ref< Eigen::MatrixXd, 0, Eigen::OuterStride<> > ProductMatrix
bool trainingCholeskyLower(ProductMatrix lower)
bool trainingProductLibraryLoaded()
A device library (not an in-process engine) is configured.
bool tryTrainingProduct(ConstProductMatrix a, ConstProductMatrix b, ProductMatrix c, bool transpose_a, bool transpose_b, double alpha, double beta, bool partitioned)
bool trainingCholeskySelected(Eigen::Index order)
std::shared_ptr< const schemeii::Context > schemeIIContext()
bool trainingProductConfigured()
A device library or an in-process engine is configured.
bool trainingCholeskyDistributedSelected(Eigen::Index order)
Panel panel(Real left, Real right, const Function &function, std::size_t &evaluations)
Moduli for one inner dimension k.
Definition SchemeII.h:101