5#include <Eigen/Cholesky>
14#ifdef GPR_LINALG_SCALAPACK
20constexpr int panel_width = 256;
26 const Eigen::Index width =
panel.cols();
29 if (
panel.rows() > width)
33 return panel.allFinite();
45 trailing.noalias() -= border * border.transpose();
48 const Eigen::Index rows = border.rows(), width = border.cols();
51 border.data(), border.outerStride(),
false, rows, width, plan);
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);
59 {&left, first, rows - first, &right, first, columns, -1.0, 1.0,
60 trailing.data() + first + first * trailing.outerStride(),
61 trailing.outerStride()});
73 trailing.noalias() -= left * right.transpose();
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;
101 const auto border = panel.bottomRows(remaining);
102 updateTrailingLower(border,
103 lower.bottomRightCorner(remaining, remaining));
106 lower.triangularView<Eigen::StrictlyUpper>().setZero();
110#ifdef GPR_LINALG_SCALAPACK
111bool trainingCholeskyDistributed(
double* lower,
const int* desc,
int order,
112 const DistributedState& state)
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())
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)
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);
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;
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");
150 Eigen::MatrixXd panel =
151 state.gather_block(values, desc, first, first, panel_rows, width,
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,
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;
172 for (
int row = row_first; row < rows; ++row)
173 local(row, column) = panel(global_rows[row] - first,
174 global_columns[column] - first);
176 if (panel_rows == width)
break;
177 const int row_start =
178 std::lower_bound(global_rows.begin(), global_rows.end(),
181 const int column_start =
182 std::lower_bound(global_columns.begin(), global_columns.end(),
184 global_columns.begin();
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);
199 local.bottomRightCorner(left.rows(), right.rows()));
201 }
catch (
const std::exception& exception) {
203 failure = exception.what();
205 MPI_Allreduce(MPI_IN_PLACE, &failed, 1, MPI_INT, MPI_MAX,
208 throw std::runtime_error(
210 ?
"training Cholesky update failed on another rank"
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.