Loading...
Searching...
No Matches
gpr::linalg::TileCholesky Class Reference

#include <gpr/linalg/TileCholesky.h>

Public Member Functions

 TileCholesky (int n, int tile_size, int nprocs, int rank)
int owner_of (int i_tile) const noexcept
 Tile owner is a deterministic function of (i, nprocs).
size_t index (int i, int j) const noexcept
 Linear index into the tiles_ vector.
int rows_of (int i_tile) const noexcept
 Tile size at (i, j) accounting for the trailing remainder.
void load (const EigenMatrix &C)
bool factor ()
EigenMatrix gather_L () const
Eigen::VectorXd solve_lower (const Eigen::VectorXd &b) const
Eigen::VectorXd solve_upper (const Eigen::VectorXd &b) const
 Triangular solve L^T x = b.
int n () const noexcept
int tile_size () const noexcept
int n_tiles () const noexcept

Detailed Description

Definition at line 53 of file TileCholesky.h.

Constructor & Destructor Documentation

◆ TileCholesky()

gpr::linalg::TileCholesky::TileCholesky ( int n,
int tile_size,
int nprocs,
int rank )
inline

Definition at line 55 of file TileCholesky.h.

56 : n_(n),
57 mb_(tile_size > 0 ? tile_size : 64),
58 nprocs_(nprocs > 0 ? nprocs : 1),
59 rank_(rank)
60 {
61 n_tiles_ = (n_ + mb_ - 1) / mb_;
62 tiles_.assign(static_cast<size_t>(n_tiles_) * n_tiles_, EigenMatrix());
63 }
int n() const noexcept
int tile_size() const noexcept
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20

Member Function Documentation

◆ factor()

bool gpr::linalg::TileCholesky::factor ( )
inlinenodiscard

Factor the SPD matrix in place. Returns true on success, false on non-PD diagonal tile (caller can retry with extra jitter).

Definition at line 102 of file TileCholesky.h.

103 {
104 for (int j = 0; j < n_tiles_; ++j) {
105 // Step 1: owner of (j,j) factors the diagonal tile in place.
106 const int diag_owner = owner_of(j);
107 const int bj = rows_of(j);
108 EigenMatrix L_jj(bj, bj);
109 if (diag_owner == rank_) {
110 EigenMatrix& A_jj = tiles_[index(j, j)];
111 Eigen::LLT<EigenMatrix> llt(A_jj);
112 if (llt.info() == Eigen::NumericalIssue) return false;
113 A_jj = llt.matrixL();
114 L_jj = A_jj;
115 }
116#ifdef GPR_HAVE_MPI
117 if (nprocs_ > 1) {
118 MPI_Bcast(L_jj.data(), bj * bj, MPI_DOUBLE, diag_owner,
119 MPI_COMM_WORLD);
120 }
121#endif
122
123 // Step 2: panel-column solve. Owner of each (i, j) for i > j
124 // computes L_ij = A_ij @ L_jj^{-T}.
125 for (int i = j + 1; i < n_tiles_; ++i) {
126 const int panel_owner = owner_of(i);
127 if (panel_owner != rank_) continue;
128 const int bi = rows_of(i);
129 EigenMatrix& A_ij = tiles_[index(i, j)];
130 // Solve A_ij L_jj^T = L_ij <=> L_jj X^T = A_ij^T
131 A_ij = L_jj.template triangularView<Eigen::Lower>()
132 .transpose()
133 .template solve<Eigen::OnTheRight>(A_ij);
134 (void)bi;
135 }
136
137 // Step 3: trailing update. For each (i, k) with j < k <= i:
138 // A_ik -= L_ij @ L_kj^T. Each rank performs the update for the
139 // tiles it owns; the panel-column tiles needed (L_ij, L_kj) for
140 // the current j may live on other ranks, so broadcast the
141 // panel column from each row-owner.
142 std::vector<EigenMatrix> panel_col(n_tiles_);
143 for (int i = j + 1; i < n_tiles_; ++i) {
144 const int panel_owner = owner_of(i);
145 const int bi = rows_of(i);
146 panel_col[i].resize(bi, bj);
147 if (panel_owner == rank_) {
148 panel_col[i] = tiles_[index(i, j)];
149 }
150#ifdef GPR_HAVE_MPI
151 if (nprocs_ > 1) {
152 MPI_Bcast(panel_col[i].data(), bi * bj, MPI_DOUBLE,
153 panel_owner, MPI_COMM_WORLD);
154 }
155#endif
156 }
157
158 for (int i = j + 1; i < n_tiles_; ++i) {
159 const int row_owner = owner_of(i);
160 if (row_owner != rank_) continue;
161 for (int k = j + 1; k <= i; ++k) {
162 EigenMatrix& A_ik = tiles_[index(i, k)];
163 A_ik.noalias() -= panel_col[i] * panel_col[k].transpose();
164 }
165 }
166 }
167 return true;
168 }
int owner_of(int i_tile) const noexcept
Tile owner is a deterministic function of (i, nprocs).
size_t index(int i, int j) const noexcept
Linear index into the tiles_ vector.
int rows_of(int i_tile) const noexcept
Tile size at (i, j) accounting for the trailing remainder.

◆ gather_L()

EigenMatrix gpr::linalg::TileCholesky::gather_L ( ) const
inlinenodiscard

Gather the factor L (lower triangle) into a replicated dense matrix on every rank. Used by callers that downstream consume L densely; avoid in tight inner loops.

Definition at line 173 of file TileCholesky.h.

174 {
175 EigenMatrix L = EigenMatrix::Zero(n_, n_);
176 for (int i = 0; i < n_tiles_; ++i) {
177 for (int j = 0; j <= i; ++j) {
178 const int owner = owner_of(i);
179 const int br = rows_of(i);
180 const int bc = rows_of(j);
181 EigenMatrix tile(br, bc);
182 if (owner == rank_) {
183 tile = tiles_[index(i, j)];
184 }
185#ifdef GPR_HAVE_MPI
186 if (nprocs_ > 1) {
187 MPI_Bcast(tile.data(), br * bc, MPI_DOUBLE, owner,
188 MPI_COMM_WORLD);
189 }
190#endif
191 L.block(i * mb_, j * mb_, br, bc) = tile;
192 }
193 }
194 return L;
195 }

◆ index()

size_t gpr::linalg::TileCholesky::index ( int i,
int j ) const
inlinenodiscardnoexcept

Linear index into the tiles_ vector.

Definition at line 72 of file TileCholesky.h.

73 {
74 return static_cast<size_t>(i) * n_tiles_ + j;
75 }

◆ load()

void gpr::linalg::TileCholesky::load ( const EigenMatrix & C)
inline

Load the input symmetric matrix C (replicated on every rank) into the tile array. Each rank stores only the tiles it owns; remaining tiles stay empty. The caller passes C in dense ColMajor form.

Definition at line 86 of file TileCholesky.h.

87 {
88 assert(C.rows() == n_ && C.cols() == n_);
89 for (int i = 0; i < n_tiles_; ++i) {
90 const int owner = owner_of(i);
91 if (owner != rank_) continue;
92 for (int j = 0; j <= i; ++j) {
93 const int br = rows_of(i);
94 const int bc = rows_of(j);
95 tiles_[index(i, j)] = C.block(i * mb_, j * mb_, br, bc);
96 }
97 }
98 }

◆ n()

int gpr::linalg::TileCholesky::n ( ) const
inlinenodiscardnoexcept

Definition at line 273 of file TileCholesky.h.

274 {
275 return n_;
276 }

◆ n_tiles()

int gpr::linalg::TileCholesky::n_tiles ( ) const
inlinenodiscardnoexcept

Definition at line 281 of file TileCholesky.h.

282 {
283 return n_tiles_;
284 }

◆ owner_of()

int gpr::linalg::TileCholesky::owner_of ( int i_tile) const
inlinenodiscardnoexcept

Tile owner is a deterministic function of (i, nprocs).

Definition at line 66 of file TileCholesky.h.

67 {
68 return i_tile % nprocs_;
69 }

◆ rows_of()

int gpr::linalg::TileCholesky::rows_of ( int i_tile) const
inlinenodiscardnoexcept

Tile size at (i, j) accounting for the trailing remainder.

Definition at line 78 of file TileCholesky.h.

79 {
80 return (i_tile == n_tiles_ - 1) ? n_ - i_tile * mb_ : mb_;
81 }

◆ solve_lower()

Eigen::VectorXd gpr::linalg::TileCholesky::solve_lower ( const Eigen::VectorXd & b) const
inlinenodiscard

Triangular solve L x = b. b is replicated; result is replicated. Iterates tiles in row order; intra-row contributions are computed by row owner, broadcast, then applied. Bit-deterministic.

Definition at line 200 of file TileCholesky.h.

201 {
202 Eigen::VectorXd x = b;
203 for (int i = 0; i < n_tiles_; ++i) {
204 const int br = rows_of(i);
205 const int row_start = i * mb_;
206 const int owner = owner_of(i);
207 Eigen::VectorXd x_i(br);
208 if (owner == rank_) {
209 Eigen::VectorXd rhs = x.segment(row_start, br);
210 for (int j = 0; j < i; ++j) {
211 const int bc = rows_of(j);
212 const int col_start = j * mb_;
213 rhs.noalias() -=
214 tiles_[index(i, j)] * x.segment(col_start, bc);
215 }
216 x_i = tiles_[index(i, i)]
217 .template triangularView<Eigen::Lower>()
218 .solve(rhs);
219 }
220#ifdef GPR_HAVE_MPI
221 if (nprocs_ > 1) {
222 MPI_Bcast(x_i.data(), br, MPI_DOUBLE, owner, MPI_COMM_WORLD);
223 }
224#endif
225 x.segment(row_start, br) = x_i;
226 }
227 return x;
228 }
@ x
Definition Enums.h:28

◆ solve_upper()

Eigen::VectorXd gpr::linalg::TileCholesky::solve_upper ( const Eigen::VectorXd & b) const
inlinenodiscard

Triangular solve L^T x = b.

Definition at line 231 of file TileCholesky.h.

232 {
233 Eigen::VectorXd x = b;
234 for (int i = n_tiles_ - 1; i >= 0; --i) {
235 const int br = rows_of(i);
236 const int row_start = i * mb_;
237 const int owner = owner_of(i);
238 Eigen::VectorXd x_i(br);
239 if (owner == rank_) {
240 Eigen::VectorXd rhs = x.segment(row_start, br);
241 for (int j = i + 1; j < n_tiles_; ++j) {
242 const int bc = rows_of(j);
243 const int col_start = j * mb_;
244 // L^T_ij = L_ji^T (we own L_ji because owner_of(j) may
245 // differ; broadcast required if not owned)
246 EigenMatrix L_ji_T(br, bc);
247 if (owner_of(j) == rank_) {
248 L_ji_T = tiles_[index(j, i)].transpose();
249 }
250#ifdef GPR_HAVE_MPI
251 if (nprocs_ > 1) {
252 MPI_Bcast(L_ji_T.data(), br * bc, MPI_DOUBLE,
253 owner_of(j), MPI_COMM_WORLD);
254 }
255#endif
256 rhs.noalias() -= L_ji_T * x.segment(col_start, bc);
257 }
258 x_i = tiles_[index(i, i)]
259 .template triangularView<Eigen::Lower>()
260 .transpose()
261 .solve(rhs);
262 }
263#ifdef GPR_HAVE_MPI
264 if (nprocs_ > 1) {
265 MPI_Bcast(x_i.data(), br, MPI_DOUBLE, owner, MPI_COMM_WORLD);
266 }
267#endif
268 x.segment(row_start, br) = x_i;
269 }
270 return x;
271 }

◆ tile_size()

int gpr::linalg::TileCholesky::tile_size ( ) const
inlinenodiscardnoexcept

Definition at line 277 of file TileCholesky.h.

278 {
279 return mb_;
280 }

The documentation for this class was generated from the following file: