Loading...
Searching...
No Matches
gpr::linalg::canonical Namespace Reference

Classes

struct  TileGeometry
class  AlignedBlockCache
class  AlignedTile
class  AlignedBuffer
struct  SubstitutionCache

Typedefs

using TileMap = Eigen::Map<Eigen::MatrixXd, 0, Eigen::OuterStride<>>
using AlignedMap = Eigen::Map<Eigen::MatrixXd, Eigen::Aligned64>
 A packed tile of an AlignedBuffer, typed as AlignedTile::map().

Enumerations

enum class  Triangle { Lower , Upper }

Functions

std::atomic< int > & choleskyPanelGroup () noexcept
TileGeometry geometryOf (DistributedState &st, const int *desc)
TileMap tileAt (double *base, int lld, const TileGeometry &g, int row_tile, int col_tile, int rows, int cols)
void requireWorkspaceBudget (DistributedState &st, const char *what, std::int64_t needed, AvailableMemory available)
void trsm (DistributedState &st, Triangle triangle, const double *T, const int *desc_t, double *B, const int *desc_b, int n, int ncols, bool lower_rhs)
std::int64_t transposeWorkspaceBytes (const int *desc, int local_cols, std::int64_t batch_entries)
std::vector< double > transposeFactor (DistributedState &st, const double *L, const int *desc, int n, std::int64_t batch_entries=0)
void trsmLowerFactor (DistributedState &st, const double *L, const int *desc_l, double *B, const int *desc_b, int n, int ncols, bool transpose)
void trsmCachedLowerFactor (DistributedState &st, double *B, const int *desc_b, int n, int ncols, bool transpose)
void trsmLowerFactorCached (DistributedState &st, SubstitutionCache &cache, const double *L, const int *desc_l, double *B, const int *desc_b, int n, int ncols, bool transpose)
void solveLowerFactorCached (DistributedState &st, SubstitutionCache &cache, const double *L, const int *desc_l, double *B, const int *desc_b, int n, int ncols)
 Solves L L^T X = B in place against a cached factor.
Eigen::MatrixXd gramTiles (const Eigen::MatrixXd &x, int nb)
void solveLowerFactor (DistributedState &st, const double *L, const int *desc_l, double *B, const int *desc_b, int n, int ncols)
void choleskyInverseLower (DistributedState &st, const double *L, const int *desc, int n, std::vector< double > &inverse, std::optional< const double * > upper=std::nullopt)
void extendBorder (DistributedState &st, const DistributedState::TileCommunicators &comms, const TileGeometry &g, double *A, int lld, int n, int first_new, int tiles)
int exactOuterWidth ()
bool potrfLowerExact (DistributedState &st, const schemeii::Context &ctx, double *A, const int *desc, int n, int n_factored)
bool potrfLower (DistributedState &st, double *A, const int *desc, int n, int n_factored=0)
bool wideTileProductAvailable () noexcept
template<bool TransposeRight = false, class Target, class Left, class Right>
void subtractTileProduct (Target &&out, const Left &left, const Right &right)

Variables

constexpr int kExactMinColumns = 32
constexpr std::int64_t kTransposeBatchEntries = std::int64_t{1} << 24

Typedef Documentation

◆ AlignedMap

using gpr::linalg::canonical::AlignedMap = Eigen::Map<Eigen::MatrixXd, Eigen::Aligned64>

A packed tile of an AlignedBuffer, typed as AlignedTile::map().

Definition at line 272 of file CanonicalTriangular.h.

◆ TileMap

using gpr::linalg::canonical::TileMap = Eigen::Map<Eigen::MatrixXd, 0, Eigen::OuterStride<>>

Definition at line 128 of file CanonicalTriangular.h.

Enumeration Type Documentation

◆ Triangle

Function Documentation

◆ choleskyInverseLower()

void gpr::linalg::canonical::choleskyInverseLower ( DistributedState & st,
const double * L,
const int * desc,
int n,
std::vector< double > & inverse,
std::optional< const double * > upper = std::nullopt )
inline

Overwrites inverse with K^{-1} = L^{-T} L^{-1} on and below the tile diagonal (tiles above the diagonal hold zero), in the layout of desc. L is the lower Cholesky factor in the same layout; upper, when given, is its transpose from transposeFactor, which is otherwise built and dropped. Cache presence is independent of the local pointer: a rank with no matrix columns has an empty cached transpose and can pass an engaged null pointer.

Definition at line 1121 of file CanonicalTriangular.h.

1125{
1127 const TileGeometry g = geometryOf(st, desc);
1128 const std::size_t local_size =
1129 static_cast<std::size_t>(desc[8]) * g.localColumns(n);
1130 // The inverse takes a local array; without a cached transpose it is
1131 // followed by transposeFactor's, which that call checks itself.
1132 requireWorkspaceBudget(st, "the Cholesky inverse",
1133 static_cast<std::int64_t>(local_size) *
1134 static_cast<std::int64_t>(sizeof(double)),
1136 inverse.assign(local_size, 0.0);
1137 // Identity right-hand side, tile by tile on the diagonal.
1138 for (int k = 0; k < g.tiles(n); ++k) {
1139 if (g.ownerRow(k) != g.myrow || g.ownerCol(k) != g.mycol) continue;
1140 const int bk = g.span(k, n);
1141 TileMap tile = tileAt(inverse.data(), desc[8], g, k, k, bk, bk);
1142 tile.setIdentity();
1143 }
1144 // X = L^{-1} (lower triangular), then Y = L^{-T} X.
1145 trsm(st, Triangle::Lower, L, desc, inverse.data(), desc, n, n, true);
1146 if (upper.has_value()) {
1147 trsm(st, Triangle::Upper, *upper, desc, inverse.data(), desc, n, n,
1148 true);
1149 return;
1150 }
1151 const std::vector<double> U = transposeFactor(st, L, desc, n);
1152 trsm(st, Triangle::Upper, U.data(), desc, inverse.data(), desc, n, n, true);
1153}
Eigen::Map< Eigen::MatrixXd, 0, Eigen::OuterStride<> > TileMap
TileMap tileAt(double *base, int lld, const TileGeometry &g, int row_tile, int col_tile, int rows, int cols)
TileGeometry geometryOf(DistributedState &st, const int *desc)
void requireWorkspaceBudget(DistributedState &st, const char *what, std::int64_t needed, AvailableMemory available)
void trsm(DistributedState &st, Triangle triangle, const double *T, const int *desc_t, double *B, const int *desc_b, int n, int ncols, bool lower_rhs)
std::vector< double > transposeFactor(DistributedState &st, const double *L, const int *desc, int n, std::int64_t batch_entries=0)
AvailableMemory availableHostBudget()
int span(int tile, int extent) const

◆ choleskyPanelGroup()

std::atomic< int > & gpr::linalg::canonical::choleskyPanelGroup ( )
inlinenoexcept

Panels the tile Cholesky factors before it applies them, together, to the trailing tiles. Every tile still receives the updates of the panels one at a time in ascending order, so the factor does not depend on this count; a trailing tile is loaded and stored once per group instead of once per panel. Substitution uses the same group count to amortize its tile copies while preserving the order of every update.

Definition at line 71 of file CanonicalTriangular.h.

72{
73 static std::atomic<int> group{4};
74 return group;
75}

◆ exactOuterWidth()

int gpr::linalg::canonical::exactOuterWidth ( )
inline

Width, in matrix rows, of the outer blocks of the exact factorization. The trailing update of one outer block is one Scheme II product with this inner dimension, which amortizes the residue reconstruction. The value depends on nothing but the environment, never on the grid.

Definition at line 1351 of file CanonicalTriangular.h.

1352{
1353 static const int width = [] {
1354 const char* text = std::getenv("GPR_SCHEME2_OUTER_WIDTH");
1355 const int value = text && *text ? std::atoi(text) : 512;
1356 return std::max(1, value);
1357 }();
1358 return width;
1359}

◆ extendBorder()

void gpr::linalg::canonical::extendBorder ( DistributedState & st,
const DistributedState::TileCommunicators & comms,
const TileGeometry & g,
double * A,
int lld,
int n,
int first_new,
int tiles )
inline

Brings the border tiles (tile rows from first_new on) of a partly factored matrix to the state they have in a right-looking factorization after the panel steps 0 .. first_new - 1: every border tile (i, j) with j < first_new holds L_ij, and every border tile (i1, i2) with i2 >= first_new has received the updates of those panels, in ascending panel order. The tiles of the factored block move once, to the process rows that own border tiles, in place of a broadcast of every panel to every rank. Collective.

Definition at line 1164 of file CanonicalTriangular.h.

1168{
1169 const int nb = g.nb;
1170 const int np = st.nprocs();
1171 std::vector<char> row_has_border(static_cast<std::size_t>(g.nprow), 0);
1172 std::vector<int> mine; // border tile rows of this process row
1173 for (int i = first_new; i < tiles; ++i) {
1174 row_has_border[static_cast<std::size_t>(g.ownerRow(i))] = 1;
1175 if (g.ownerRow(i) == g.myrow) mine.push_back(i);
1176 }
1177 const auto world_rank = [&](int row, int col) {
1178 return comms.rank_of[static_cast<std::size_t>(row) * g.npcol + col];
1179 };
1180 // L_ik for k < j, replicated along the process row, one block row per
1181 // border tile of this process row.
1182 std::vector<Eigen::MatrixXd> xrow;
1183 for (const int i: mine)
1184 xrow.emplace_back(g.span(i, n),
1185 static_cast<Eigen::Index>(first_new) * nb);
1186
1187 std::vector<int> send_counts(np), recv_counts(np), send_displs(np),
1188 recv_displs(np);
1189 std::vector<double> send, recv;
1190 std::vector<double> row_buffer;
1191 for (int j = 0; j < first_new; ++j) {
1192 const int jr = g.ownerRow(j), jc = g.ownerCol(j);
1193 const auto tiles_in_column = [&](int column) {
1194 int count = 0;
1195 for (int k = column; k <= j; k += g.npcol)
1196 ++count;
1197 return count;
1198 };
1199 // Tile row j of the factor goes from its owners (jr, column) to the
1200 // border-owning ranks of process column jc.
1201 std::fill(send_counts.begin(), send_counts.end(), 0);
1202 std::fill(recv_counts.begin(), recv_counts.end(), 0);
1203 for (int column = 0; column < g.npcol; ++column) {
1204 const int count = tiles_in_column(column);
1205 if (count == 0) continue;
1206 const int source = world_rank(jr, column);
1207 for (int row = 0; row < g.nprow; ++row) {
1208 if (!row_has_border[static_cast<std::size_t>(row)]) continue;
1209 const int target = world_rank(row, jc);
1210 const int size = count * nb * nb;
1211 if (source == st.myrank()) send_counts[target] = size;
1212 if (target == st.myrank()) recv_counts[source] = size;
1213 }
1214 }
1215 int send_total = 0, recv_total = 0;
1216 for (int r = 0; r < np; ++r) {
1217 send_displs[r] = 0; // every target receives the same payload
1218 recv_displs[r] = recv_total;
1219 recv_total += recv_counts[r];
1220 send_total = std::max(send_total, send_counts[r]);
1221 }
1222 send.assign(static_cast<std::size_t>(send_total), 0.0);
1223 if (send_total > 0) {
1224 std::size_t at = 0;
1225 for (int k = g.mycol; k <= j; k += g.npcol) {
1226 TileMap tile = tileAt(A, lld, g, j, k, nb, nb);
1227 Eigen::Map<Eigen::MatrixXd> out(send.data() + at, nb, nb);
1228 if (k == j)
1229 out = Eigen::MatrixXd(
1230 Eigen::MatrixXd(tile).triangularView<Eigen::Lower>());
1231 else
1232 out = tile;
1233 at += static_cast<std::size_t>(nb) * nb;
1234 }
1235 }
1236 recv.assign(static_cast<std::size_t>(recv_total), 0.0);
1237 MPI_Alltoallv(send.data(), send_counts.data(), send_displs.data(),
1238 MPI_DOUBLE, recv.data(), recv_counts.data(),
1239 recv_displs.data(), MPI_DOUBLE, st.world_comm());
1240
1241 if (!mine.empty() && g.mycol == jc) {
1242 const auto factor_tile = [&](int k) {
1243 const int source = world_rank(jr, g.ownerCol(k));
1244 const int position = (k - g.ownerCol(k)) / g.npcol;
1245 return Eigen::Map<const Eigen::MatrixXd>(
1246 recv.data() + recv_displs[source] +
1247 static_cast<std::size_t>(position) * nb * nb,
1248 nb, nb);
1249 };
1250 const Eigen::MatrixXd upper = factor_tile(j).transpose();
1251 parallelFor(static_cast<long>(mine.size()), [&](long q) {
1252 const int i = mine[static_cast<std::size_t>(q)];
1253 const int bi = g.span(i, n);
1254 TileMap tile = tileAt(A, lld, g, i, j, bi, nb);
1255 AlignedTile pending(tile);
1256 for (int k = 0; k < j; ++k) {
1257 AlignedTile left(
1258 xrow[static_cast<std::size_t>(q)].middleCols(
1259 static_cast<Eigen::Index>(k) * nb, nb)),
1260 right(factor_tile(k));
1261 portable_tiles::subtractProduct<true>(
1262 pending.map(), left.map(), right.map());
1263 }
1264 AlignedTile factor(upper);
1265 portable_tiles::solveRightUpper(factor.map().data(),
1266 pending.map().data(), bi, nb);
1267 const Eigen::MatrixXd solved = pending.map();
1268 tile = solved;
1269 xrow[static_cast<std::size_t>(q)].middleCols(
1270 static_cast<Eigen::Index>(j) * nb, nb) = solved;
1271 });
1272 }
1273 // The solved tiles of column j reach the whole process row.
1274 if (!mine.empty()) {
1275 std::size_t size = 0;
1276 for (const int i: mine)
1277 size += static_cast<std::size_t>(g.span(i, n)) * nb;
1278 row_buffer.assign(size, 0.0);
1279 if (g.mycol == jc) {
1280 std::size_t at = 0;
1281 for (std::size_t q = 0; q < mine.size(); ++q) {
1282 const int bi = g.span(mine[q], n);
1283 Eigen::Map<Eigen::MatrixXd>(row_buffer.data() + at, bi,
1284 nb) =
1285 xrow[q].middleCols(static_cast<Eigen::Index>(j) * nb,
1286 nb);
1287 at += static_cast<std::size_t>(bi) * nb;
1288 }
1289 }
1290 MPI_Bcast(row_buffer.data(), static_cast<int>(size), MPI_DOUBLE, jc,
1291 comms.row);
1292 if (g.mycol != jc) {
1293 std::size_t at = 0;
1294 for (std::size_t q = 0; q < mine.size(); ++q) {
1295 const int bi = g.span(mine[q], n);
1296 xrow[q].middleCols(static_cast<Eigen::Index>(j) * nb, nb) =
1297 Eigen::Map<const Eigen::MatrixXd>(
1298 row_buffer.data() + at, bi, nb);
1299 at += static_cast<std::size_t>(bi) * nb;
1300 }
1301 }
1302 }
1303 }
1304
1305 // Updates among the border tiles from the panels 0 .. first_new - 1, in
1306 // ascending panel order. Tile (i1, i2) lives on (ownerRow(i1),
1307 // ownerCol(i2)); it needs the block row of i2 from the process row that
1308 // owns i2.
1309 std::vector<Eigen::MatrixXd> column_rows(static_cast<std::size_t>(tiles));
1310 for (int i2 = first_new; i2 < tiles; ++i2) {
1311 if (g.ownerCol(i2) != g.mycol) continue;
1312 Eigen::MatrixXd& block = column_rows[static_cast<std::size_t>(i2)];
1313 block.resize(g.span(i2, n), static_cast<Eigen::Index>(first_new) * nb);
1314 if (g.ownerRow(i2) == g.myrow) {
1315 for (std::size_t q = 0; q < mine.size(); ++q)
1316 if (mine[q] == i2) block = xrow[q];
1317 }
1318 MPI_Bcast(block.data(), static_cast<int>(block.size()), MPI_DOUBLE,
1319 g.ownerRow(i2), comms.col);
1320 }
1321 std::vector<std::pair<int, int>> pairs;
1322 for (std::size_t q = 0; q < mine.size(); ++q)
1323 for (int i2 = first_new; i2 <= mine[q]; ++i2)
1324 if (g.ownerCol(i2) == g.mycol)
1325 pairs.emplace_back(static_cast<int>(q), i2);
1326 parallelFor(static_cast<long>(pairs.size()), [&](long u) {
1327 const std::size_t q =
1328 static_cast<std::size_t>(pairs[static_cast<std::size_t>(u)].first);
1329 const int i1 = mine[q];
1330 const int i2 = pairs[static_cast<std::size_t>(u)].second;
1331 const int b1 = g.span(i1, n), b2 = g.span(i2, n);
1332 TileMap target = tileAt(A, lld, g, i1, i2, b1, b2);
1333 const Eigen::MatrixXd& other =
1334 column_rows[static_cast<std::size_t>(i2)];
1335 AlignedTile sum(target);
1336 for (int j = 0; j < first_new; ++j) {
1337 AlignedTile left(
1338 xrow[q].middleCols(static_cast<Eigen::Index>(j) * nb, nb)),
1339 right(other.middleCols(static_cast<Eigen::Index>(j) * nb, nb));
1340 portable_tiles::subtractProduct<true>(sum.map(), left.map(),
1341 right.map());
1342 }
1343 target = sum.map();
1344 });
1345}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
void solveRightUpper(const double *factor, double *rhs, std::ptrdiff_t rows, std::ptrdiff_t order)
void parallelFor(long count, F &&fn)
parallelFor(count, fn): see TilePool::run.
const std::vector< int > & rank_of
indexed by row * npcol + col

◆ geometryOf()

TileGeometry gpr::linalg::canonical::geometryOf ( DistributedState & st,
const int * desc )
inline

Definition at line 104 of file CanonicalTriangular.h.

105{
106 if (desc[4] != desc[5] || desc[6] != 0 || desc[7] != 0)
107 throw std::logic_error(
108 "canonical triangular kernels need square blocks anchored at "
109 "process (0, 0)");
110 TileGeometry g;
111 g.nb = desc[4];
112 if (desc[1] == st.lin_context()) {
113 g.nprow = st.nprocs();
114 g.npcol = 1;
115 g.myrow = st.lin_myrow();
116 g.mycol = st.lin_mycol();
117 } else if (desc[1] == st.context()) {
118 g.nprow = st.nprow();
119 g.npcol = st.npcol();
120 g.myrow = st.myrow();
121 g.mycol = st.mycol();
122 } else {
123 throw std::invalid_argument("canonical tiles need an active grid");
124 }
125 return g;
126}

◆ gramTiles()

Eigen::MatrixXd gpr::linalg::canonical::gramTiles ( const Eigen::MatrixXd & x,
int nb )
inline

Gram matrix X^T X of a replicated matrix, accumulated over row tiles in ascending order with one dense product per (column tile, column tile, row tile). Every rank evaluates it from identical data in the same order, so the bits do not depend on the process grid that produced X.

Definition at line 1060 of file CanonicalTriangular.h.

1061{
1062 const int rows = static_cast<int>(x.rows());
1063 const int cols = static_cast<int>(x.cols());
1064 Eigen::MatrixXd gram = Eigen::MatrixXd::Zero(cols, cols);
1065 if (const auto exact = schemeIIContext(); exact && rows > 0 && cols > 0) {
1066 // One exact product: every entry is a function of two columns of x.
1067 exact->gemm(true, false, cols, cols, rows, 1.0, x.data(), rows,
1068 x.data(), rows, 0.0, gram.data(), cols);
1069 for (int j = 0; j < cols; ++j)
1070 for (int i = 0; i < j; ++i)
1071 gram(i, j) = gram(j, i);
1072 return gram;
1073 }
1074 const int row_tiles = (rows + nb - 1) / nb;
1075 const int col_tiles = (cols + nb - 1) / nb;
1076 for (int k = 0; k < row_tiles; ++k) {
1077 const int bk = std::min(nb, rows - k * nb);
1078 const Eigen::MatrixXd slab = x.middleRows(k * nb, bk);
1079 parallelFor(static_cast<long>(col_tiles) * (col_tiles + 1) / 2,
1080 [&](long pair) {
1081 int i = 0;
1082 while ((i + 1) * (i + 2) / 2 <= pair)
1083 ++i;
1084 const int j = pair - i * (i + 1) / 2;
1085 const int bi = std::min(nb, cols - i * nb);
1086 const int bj = std::min(nb, cols - j * nb);
1087 AlignedTile left(slab.middleCols(i * nb, bi)),
1088 right(slab.middleCols(j * nb, bj)),
1089 sum(gram.block(i * nb, j * nb, bi, bj));
1091 sum.map().data(), left.map().data(),
1092 right.map().data(), bi, bj, bk);
1093 gram.block(i * nb, j * nb, bi, bj) = sum.map();
1094 });
1095 }
1096 for (int j = 0; j < cols; ++j)
1097 for (int i = 0; i < j; ++i)
1098 gram(i, j) = gram(j, i);
1099 return gram;
1100}
@ x
Definition Enums.h:28
void addTransposeProduct(double *out, const double *left, const double *right, std::ptrdiff_t rows, std::ptrdiff_t columns, std::ptrdiff_t depth)
std::shared_ptr< const schemeii::Context > schemeIIContext()

◆ potrfLower()

bool gpr::linalg::canonical::potrfLower ( DistributedState & st,
double * A,
const int * desc,
int n,
int n_factored = 0 )
inline

Cholesky factorization of the lower triangle of the tiled symmetric positive definite matrix A, in place: on success the lower triangle holds L with A = L L^T. Right-looking tile algorithm: the diagonal tile is factored by one rank, the panel is solved tile by tile, and each trailing tile receives its updates in ascending panel order from whole-tile products. Collective. Returns false, on every rank, when a diagonal tile is not positive definite.

When n_factored > 0 (a multiple of the block size) the leading n_factored x n_factored block already holds its factor, and the rows below it hold the unfactored border; the call extends the factor to order n. Every tile then receives the same updates in the same order as in a factorization of the whole matrix.

Definition at line 1638 of file CanonicalTriangular.h.

1640{
1642 if (n <= 0) return true;
1643 if (const auto exact = schemeIIContext())
1644 return potrfLowerExact(st, *exact, A, desc, n, n_factored);
1645 const TileGeometry g = geometryOf(st, desc);
1647 const int lld = desc[8];
1648 const int tiles = g.tiles(n);
1649 if (n_factored % g.nb != 0 || n_factored < 0 || n_factored > n)
1650 return false;
1651 const int first_new = n_factored / g.nb;
1652 int healthy = 1;
1653
1654 // Factored panel k: the solved tiles (i, k) of this process row, and
1655 // the tiles (j, k) that the tile columns j of this process column take
1656 // as the transposed operand, both packed aligned so the solve and the
1657 // trailing products read them in place. Two panels alternate so that
1658 // panel k + 1 is factored and sent while panel k still updates.
1659 struct Panel {
1660 AlignedBuffer row_store, col_store;
1661 double* row = nullptr;
1662 double* col = nullptr;
1663 std::vector<int> rows, cols;
1664 std::vector<std::size_t> row_offset, col_slot, chunk_offset;
1665 };
1666 const int group = std::max(1, choleskyPanelGroup().load());
1667 // Two sets of `group` panels: the set of one group is read by its
1668 // trailing update while the next group factors into the other set.
1669 std::vector<Panel> panels(2 * static_cast<std::size_t>(group));
1670 for (Panel& p: panels)
1671 p.col_slot.assign(static_cast<std::size_t>(tiles), 0);
1672 const auto panelOf = [&](int k) -> Panel& {
1673 const int offset = k - first_new;
1674 return panels[static_cast<std::size_t>((offset / group) % 2 * group +
1675 offset % group)];
1676 };
1677 std::vector<double> diagonal;
1678 Eigen::MatrixXd scratch;
1679
1680 // Diagonal factor, panel solve and the row and column broadcasts of
1681 // panel k. Expects every tile of column k to hold all updates of the
1682 // panels before k.
1683 const auto factorPanel = [&](int k, Panel& p) {
1684 const int kr = g.ownerRow(k);
1685 const int kc = g.ownerCol(k);
1686 const int bk = g.span(k, n);
1687
1688 // Diagonal tile: factor on its owner, share down its process column.
1689 if (g.mycol == kc) {
1690 diagonal.assign(static_cast<std::size_t>(bk) * bk, 0.0);
1691 if (g.myrow == kr) {
1692 TileMap tile = tileAt(A, lld, g, k, k, bk, bk);
1693 scratch = tile;
1694 AlignedTile block(scratch);
1695 if (portable_tiles::factorLower(block.map().data(), bk)) {
1696 scratch = block.map().triangularView<Eigen::Lower>();
1697 } else {
1698 healthy = 0;
1699 scratch.setIdentity(bk, bk);
1700 }
1701 tile = scratch;
1702 Eigen::Map<Eigen::MatrixXd>(diagonal.data(), bk, bk) = scratch;
1703 }
1704 MPI_Bcast(diagonal.data(), bk * bk, MPI_DOUBLE, kr, comms.col);
1705 }
1706
1707 // Panel tiles below the diagonal tile in this process row.
1708 p.rows.clear();
1709 p.row_offset.clear();
1710 std::size_t row_size = 0;
1711 for (int i =
1712 k + 1 + ((g.myrow - (k + 1) % g.nprow) + g.nprow) % g.nprow;
1713 i < tiles; i += g.nprow) {
1714 p.rows.push_back(i);
1715 p.row_offset.push_back(row_size);
1716 row_size += AlignedBuffer::padded(
1717 static_cast<std::size_t>(g.span(i, n)) * bk);
1718 }
1719 p.row = p.row_store.reserve(row_size);
1720 if (g.mycol == kc) {
1721 const Eigen::MatrixXd upper =
1722 Eigen::Map<const Eigen::MatrixXd>(diagonal.data(), bk, bk)
1723 .transpose();
1724 // One aligned copy of the factor serves every tile of the panel.
1725 AlignedTile factor(upper);
1726 parallelFor(static_cast<long>(p.rows.size()), [&](long q) {
1727 const int i = p.rows[static_cast<std::size_t>(q)];
1728 const int bi = g.span(i, n);
1729 TileMap tile = tileAt(A, lld, g, i, k, bi, bk);
1730 // X L^T = A_ik with L^T as an explicit upper factor.
1731 AlignedMap rhs(
1732 p.row + p.row_offset[static_cast<std::size_t>(q)], bi, bk);
1733 rhs = tile;
1734 portable_tiles::solveRightUpper(factor.map().data(), rhs.data(),
1735 bi, bk);
1736 tile = rhs;
1737 });
1738 }
1739 if (row_size > 0)
1740 MPI_Bcast(p.row, static_cast<int>(row_size), MPI_DOUBLE, kc,
1741 comms.row);
1742
1743 // The process column that owns tile column j needs panel tile j,
1744 // which the row broadcast delivered to process (j mod nprow, any
1745 // column). Each process row r sends its share straight into the
1746 // packed column panel.
1747 p.cols.clear();
1748 std::size_t col_size = 0;
1749 p.chunk_offset.assign(static_cast<std::size_t>(g.nprow) + 1, 0);
1750 const int first_col =
1751 k + 1 + ((g.mycol - (k + 1) % g.npcol) + g.npcol) % g.npcol;
1752 for (int r = 0; r < g.nprow; ++r) {
1753 p.chunk_offset[static_cast<std::size_t>(r)] = col_size;
1754 for (int j = first_col; j < tiles; j += g.npcol) {
1755 if (g.ownerRow(j) != r) continue;
1756 p.col_slot[static_cast<std::size_t>(j)] = col_size;
1757 col_size += AlignedBuffer::padded(
1758 static_cast<std::size_t>(g.span(j, n)) * bk);
1759 p.cols.push_back(j);
1760 }
1761 }
1762 p.chunk_offset[static_cast<std::size_t>(g.nprow)] = col_size;
1763 p.col = p.col_store.reserve(col_size);
1764 for (int r = 0; r < g.nprow; ++r) {
1765 const std::size_t begin =
1766 p.chunk_offset[static_cast<std::size_t>(r)];
1767 const std::size_t chunk =
1768 p.chunk_offset[static_cast<std::size_t>(r) + 1] - begin;
1769 if (chunk == 0) continue;
1770 if (g.myrow == r) {
1771 for (int j = first_col; j < tiles; j += g.npcol) {
1772 if (g.ownerRow(j) != r) continue;
1773 // Panel rows of this process row ascend in steps of
1774 // nprow from rows[0].
1775 const std::size_t q = static_cast<std::size_t>(
1776 (j - p.rows.front()) / g.nprow);
1777 std::copy_n(
1778 p.row + p.row_offset[q],
1779 static_cast<std::size_t>(g.span(j, n)) * bk,
1780 p.col + p.col_slot[static_cast<std::size_t>(j)]);
1781 }
1782 }
1783 MPI_Bcast(p.col + begin, static_cast<int>(chunk), MPI_DOUBLE, r,
1784 comms.col);
1785 }
1786 };
1787
1788 // Tiles (i, j) this rank owns with i >= j and j in [j_first, j_last],
1789 // listed as (position of i in the rows of p, j).
1790 const auto collect = [&](const Panel& p, int j_first, int j_last,
1791 std::vector<std::pair<int, int>>& pairs) {
1792 pairs.clear();
1793 for (std::size_t q = 0; q < p.rows.size(); ++q)
1794 for (int j: p.cols)
1795 if (j >= j_first && j <= j_last && j <= p.rows[q])
1796 pairs.emplace_back(static_cast<int>(q), j);
1797 };
1798 // A_ij -= L_ik L_jk^T for the panels k in [k_first, k_last], ascending,
1799 // one whole-tile product per panel on one aligned copy of the tile.
1800 // `rows` is the panel whose row list `pairs` indexes.
1801 const bool in_place_full_tiles =
1803 const auto update = [&](int k_first, int k_last, const Panel& rows,
1804 const std::vector<std::pair<int, int>>& pairs,
1805 long u) {
1806 const auto& pair = pairs[static_cast<std::size_t>(u)];
1807 const int i = rows.rows[static_cast<std::size_t>(pair.first)];
1808 const int j = pair.second;
1809 const int bi = g.span(i, n);
1810 const int bj = g.span(j, n);
1811 TileMap target = tileAt(A, lld, g, i, j, bi, bj);
1812 const auto apply = [&](auto&& sum) {
1813 for (int k = k_first; k <= k_last; ++k) {
1814 const Panel& p = panelOf(k);
1815 const int bk = g.span(k, n);
1816 const std::size_t q =
1817 static_cast<std::size_t>((i - p.rows.front()) / g.nprow);
1818 const AlignedMap left(p.row + p.row_offset[q], bi, bk);
1819 const AlignedMap right(
1820 p.col + p.col_slot[static_cast<std::size_t>(j)], bj, bk);
1821 portable_tiles::subtractProduct<true>(sum, left, right);
1822 }
1823 };
1824 if (in_place_full_tiles && bi == 64 && bj == 64 &&
1825 g.span(k_last, n) == 64) {
1826 apply(target);
1827 } else {
1828 AlignedTile sum(target);
1829 apply(sum.map());
1830 target = sum.map();
1831 }
1832 };
1833 // Factors the panels [k0, k_end) of a group whose columns hold every
1834 // update of the earlier groups. Each panel updates the later columns of
1835 // its own group before the next panel factors.
1836 std::vector<std::pair<int, int>> group_pairs;
1837 const auto factorGroup = [&](int k0, int k_end) {
1838 for (int k = k0; k < k_end; ++k) {
1839 factorPanel(k, panelOf(k));
1840 if (k + 1 >= k_end) break;
1841 collect(panelOf(k), k + 1, k_end - 1, group_pairs);
1842 parallelFor(static_cast<long>(group_pairs.size()), [&](long u) {
1843 update(k, k, panelOf(k), group_pairs, u);
1844 });
1845 }
1846 };
1847
1848 if (first_new > 0 && first_new < tiles)
1849 extendBorder(st, comms, g, A, lld, n, first_new, tiles);
1850 if (first_new < tiles)
1851 factorGroup(first_new, std::min(tiles, first_new + group));
1852 // Every tile receives the updates of the panels in ascending order, as in
1853 // the plain right-looking loop; only the schedule changes. The columns
1854 // of the next group take the updates of the current group first, and the
1855 // next group is factored and broadcast on the calling thread while the
1856 // workers apply the current group to the columns beyond it, which no
1857 // step of the next group reads or writes.
1858 std::vector<std::pair<int, int>> next_pairs, rest_pairs;
1859 for (int k0 = first_new;; k0 += group) {
1860 const int k_end = std::min(tiles, k0 + group);
1861 if (k_end >= tiles) break;
1862 const int next_end = std::min(tiles, k_end + group);
1863 const Panel& last = panelOf(k_end - 1);
1864 collect(last, k_end, next_end - 1, next_pairs);
1865 collect(last, next_end, tiles - 1, rest_pairs);
1866 parallelFor(static_cast<long>(next_pairs.size()), [&](long u) {
1867 update(k0, k_end - 1, last, next_pairs, u);
1868 });
1869 const auto rest = [&](long u) {
1870 update(k0, k_end - 1, last, rest_pairs, u);
1871 };
1872 if (static_cast<long>(rest_pairs.size()) <
1873 2L * tileThreadCount().load()) {
1874 // Too few tiles to cover the next group: its panel solves gain
1875 // more from the threads than from the overlap.
1876 parallelFor(static_cast<long>(rest_pairs.size()), rest);
1877 factorGroup(k_end, next_end);
1878 continue;
1879 }
1880 TileBackground background(static_cast<long>(rest_pairs.size()), rest);
1881 factorGroup(k_end, next_end);
1882 background.finish();
1883 }
1884 MPI_Allreduce(MPI_IN_PLACE, &healthy, 1, MPI_INT, MPI_MIN, st.world_comm());
1885 return healthy != 0;
1886}
TileCommunicators tile_communicators(int context=-1)
static std::size_t padded(std::size_t count) noexcept
Doubles a packed tile of count entries occupies.
std::atomic< int > & choleskyPanelGroup() noexcept
Eigen::Map< Eigen::MatrixXd, Eigen::Aligned64 > AlignedMap
A packed tile of an AlignedBuffer, typed as AlignedTile::map().
bool potrfLowerExact(DistributedState &st, const schemeii::Context &ctx, double *A, const int *desc, int n, int n_factored)
void extendBorder(DistributedState &st, const DistributedState::TileCommunicators &comms, const TileGeometry &g, double *A, int lld, int n, int first_new, int tiles)
bool factorLower(double *matrix, std::ptrdiff_t order)
void subtractProduct(Target &&out, const Left &left, const Right &right)
std::atomic< int > & tileThreadCount() noexcept
Number of threads one rank devotes to independent tile products.

◆ potrfLowerExact()

bool gpr::linalg::canonical::potrfLowerExact ( DistributedState & st,
const schemeii::Context & ctx,
double * A,
const int * desc,
int n,
int n_factored )
inline

potrfLower with exact Scheme II trailing products and fixed-order panel kernels. Outer blocks of exactOuterWidth() / nb tiles: inside an outer block the panel is factored right-looking tile by tile (fixed-order diagonal Cholesky, panel solve and inner updates); the rest of the trailing matrix receives one exact product A_ij -= L_iB L_jB^T per outer block B. Every tile receives the same operations in the same order on every grid that shares nb, and with n_factored > 0 the border rows receive exactly the operations of a factorization of the whole matrix.

Definition at line 1371 of file CanonicalTriangular.h.

1373{
1374 const TileGeometry g = geometryOf(st, desc);
1376 const int lld = desc[8];
1377 const int nb = g.nb;
1378 const int tiles = g.tiles(n);
1379 if (n_factored % nb != 0 || n_factored < 0 || n_factored > n) return false;
1380 const int first_new = n_factored / nb;
1381 if (first_new >= tiles) return true;
1382 const int outer = std::max(1, exactOuterWidth() / nb);
1383 int healthy = 1;
1384 const auto local_row = [&](int i) {
1385 return static_cast<std::size_t>(i / g.nprow) * nb;
1386 };
1387 const auto local_col = [&](int j) {
1388 return static_cast<std::size_t>(j / g.npcol) * nb;
1389 };
1390 // First tile at or after `from` in my process row (column).
1391 const auto first_row_tile = [&](int from) {
1392 return from + ((g.myrow - from % g.nprow) + g.nprow) % g.nprow;
1393 };
1394 const auto first_col_tile = [&](int from) {
1395 return from + ((g.mycol - from % g.npcol) + g.npcol) % g.npcol;
1396 };
1397 // Rows of the tiles of my process row from tile `from` on.
1398 const auto rows_from = [&](int from) {
1399 std::size_t rows = 0;
1400 for (int i = first_row_tile(from); i < tiles; i += g.nprow)
1401 rows += static_cast<std::size_t>(g.span(i, n));
1402 return rows;
1403 };
1404 const auto cols_from = [&](int from) {
1405 std::size_t cols = 0;
1406 for (int j = first_col_tile(from); j < tiles; j += g.npcol)
1407 cols += static_cast<std::size_t>(g.span(j, n));
1408 return cols;
1409 };
1410
1411 std::vector<double> diagonal, row_panel, col_panel, chunk, outer_rows,
1412 outer_cols;
1413 for (int t0 = 0; t0 < tiles; t0 += outer) {
1414 const int t1 = std::min(tiles, t0 + outer);
1415 const int width = (t1 - 1) * nb + g.span(t1 - 1, n) - t0 * nb;
1416 // Outer panel rows of my row tiles >= t1 and my column tiles >= t1.
1417 const std::size_t after_rows = rows_from(t1),
1418 after_cols = cols_from(t1);
1419 outer_rows.assign(after_rows * width, 0.0);
1420 outer_cols.assign(after_cols * width, 0.0);
1421
1422 for (int k = t0; k < t1; ++k) {
1423 const int kr = g.ownerRow(k), kc = g.ownerCol(k);
1424 const int bk = g.span(k, n);
1425 const bool final_diagonal = k < first_new;
1426
1427 // Diagonal tile: factored by its owner unless already final;
1428 // shared down its process column for the panel solve.
1429 if (g.mycol == kc) {
1430 diagonal.assign(static_cast<std::size_t>(bk) * bk, 0.0);
1431 if (g.myrow == kr) {
1432 double* tile = A + local_row(k) + local_col(k) * lld;
1433 if (!final_diagonal) {
1434 if (!fixed::choleskyLower(tile, lld, bk)) {
1435 healthy = 0;
1436 for (int c = 0; c < bk; ++c)
1437 for (int r = 0; r < bk; ++r)
1438 tile[r + static_cast<std::size_t>(c) *
1439 lld] = r == c ? 1.0 : 0.0;
1440 }
1441 // The stored factor tile is lower triangular.
1442 for (int c = 1; c < bk; ++c)
1443 for (int r = 0; r < c; ++r)
1444 tile[r + static_cast<std::size_t>(c) * lld] =
1445 0.0;
1446 }
1447 for (int c = 0; c < bk; ++c)
1448 for (int r = c; r < bk; ++r)
1449 diagonal[r + static_cast<std::size_t>(c) * bk] =
1450 tile[r + static_cast<std::size_t>(c) * lld];
1451 }
1452 MPI_Bcast(diagonal.data(), bk * bk, MPI_DOUBLE, kr, comms.col);
1453 }
1454
1455 // Panel tiles below the diagonal in my process row: rows from
1456 // first_new on are solved (X L^T = A_ik); earlier rows are final.
1457 const int panel_first = first_row_tile(k + 1);
1458 const std::size_t panel_rows = rows_from(k + 1);
1459 row_panel.assign(panel_rows * bk, 0.0);
1460 if (g.mycol == kc && panel_rows > 0) {
1461 double* column =
1462 A + local_row(panel_first) + local_col(k) * lld;
1463 const int solve_first =
1464 first_row_tile(std::max(k + 1, first_new));
1465 if (solve_first < tiles) {
1466 const std::size_t skip =
1467 static_cast<std::size_t>((solve_first - panel_first) /
1468 g.nprow) *
1469 nb;
1470 const std::size_t solve_rows = panel_rows - skip;
1471 const long pieces =
1472 static_cast<long>((solve_rows + 255) / 256);
1473 parallelFor(pieces, [&](long p) {
1474 const std::size_t r0 =
1475 static_cast<std::size_t>(p) * 256;
1476 const std::size_t count =
1477 std::min<std::size_t>(256, solve_rows - r0);
1479 diagonal.data(), bk, bk, column + skip + r0, lld,
1480 static_cast<std::int64_t>(count));
1481 });
1482 }
1483 for (int c = 0; c < bk; ++c)
1484 std::copy_n(column + static_cast<std::size_t>(c) * lld,
1485 panel_rows,
1486 row_panel.data() +
1487 static_cast<std::size_t>(c) * panel_rows);
1488 }
1489 if (panel_rows > 0)
1490 MPI_Bcast(row_panel.data(), static_cast<int>(panel_rows * bk),
1491 MPI_DOUBLE, kc, comms.row);
1492
1493 // Column panel: L_jk for my column tiles j > k, stacked by
1494 // ascending j. Process row r holds the tiles it owns in its row
1495 // panel and sends them down this process column.
1496 const int col_first = first_col_tile(k + 1);
1497 const std::size_t panel_cols = cols_from(k + 1);
1498 col_panel.assign(panel_cols * bk, 0.0);
1499 for (int r = 0; r < g.nprow; ++r) {
1500 std::vector<int> mine;
1501 std::size_t rows = 0;
1502 for (int j = col_first; j < tiles; j += g.npcol)
1503 if (g.ownerRow(j) == r) {
1504 mine.push_back(j);
1505 rows += static_cast<std::size_t>(g.span(j, n));
1506 }
1507 if (rows == 0) continue;
1508 chunk.assign(rows * bk, 0.0);
1509 if (g.myrow == r) {
1510 std::size_t at = 0;
1511 for (const int j: mine) {
1512 const std::size_t offset =
1513 static_cast<std::size_t>((j - panel_first) /
1514 g.nprow) *
1515 nb;
1516 const int bj = g.span(j, n);
1517 for (int c = 0; c < bk; ++c)
1518 std::copy_n(
1519 row_panel.data() + offset +
1520 static_cast<std::size_t>(c) * panel_rows,
1521 bj,
1522 chunk.data() + at +
1523 static_cast<std::size_t>(c) * rows);
1524 at += static_cast<std::size_t>(bj);
1525 }
1526 }
1527 MPI_Bcast(chunk.data(), static_cast<int>(rows * bk), MPI_DOUBLE,
1528 r, comms.col);
1529 std::size_t at = 0;
1530 for (const int j: mine) {
1531 const std::size_t offset =
1532 static_cast<std::size_t>((j - col_first) / g.npcol) *
1533 nb;
1534 const int bj = g.span(j, n);
1535 for (int c = 0; c < bk; ++c)
1536 std::copy_n(
1537 chunk.data() + at +
1538 static_cast<std::size_t>(c) * rows,
1539 bj,
1540 col_panel.data() + offset +
1541 static_cast<std::size_t>(c) * panel_cols);
1542 at += static_cast<std::size_t>(bj);
1543 }
1544 }
1545
1546 // Inner updates inside the outer panel, fixed order:
1547 // A_ij -= L_ik L_jk^T for my tiles k < j < t1, i >= j, i >=
1548 // first_new. Work items of at most 512 rows of one column tile.
1549 std::vector<std::pair<int, std::size_t>> inner;
1550 for (int j = col_first; j < t1; j += g.npcol) {
1551 const int i0 = first_row_tile(std::max(j, first_new));
1552 if (i0 >= tiles) continue;
1553 const std::size_t row_offset =
1554 static_cast<std::size_t>((i0 - panel_first) / g.nprow) * nb;
1555 for (std::size_t r = row_offset; r < panel_rows; r += 512)
1556 inner.emplace_back(j, r);
1557 }
1558 parallelFor(static_cast<long>(inner.size()), [&](long q) {
1559 const auto [j, r] = inner[static_cast<std::size_t>(q)];
1560 const std::size_t col_offset =
1561 static_cast<std::size_t>((j - col_first) / g.npcol) * nb;
1562 const std::size_t rows =
1563 std::min<std::size_t>(512, panel_rows - r);
1564 // Row r of the panel is local row local_row(panel_first) + r.
1565 fixed::subtractProductTransposed(
1566 row_panel.data() + r, static_cast<std::int64_t>(panel_rows),
1567 col_panel.data() + col_offset,
1568 static_cast<std::int64_t>(panel_cols),
1569 A + local_row(panel_first) + r + local_col(j) * lld, lld,
1570 static_cast<std::int64_t>(rows), g.span(j, n), bk);
1571 });
1572
1573 // Keep the panel rows and columns past the outer panel.
1574 if (t1 < tiles) {
1575 const std::size_t column =
1576 static_cast<std::size_t>(k - t0) * nb;
1577 const std::size_t row_skip = panel_rows - after_rows;
1578 const std::size_t col_skip = panel_cols - after_cols;
1579 for (int c = 0; c < bk; ++c) {
1580 std::copy_n(row_panel.data() + row_skip +
1581 static_cast<std::size_t>(c) * panel_rows,
1582 after_rows,
1583 outer_rows.data() + (column + c) * after_rows);
1584 std::copy_n(col_panel.data() + col_skip +
1585 static_cast<std::size_t>(c) * panel_cols,
1586 after_cols,
1587 outer_cols.data() + (column + c) * after_cols);
1588 }
1589 }
1590 }
1591
1592 // Exact trailing update of the outer block.
1593 if (t1 >= tiles || after_rows == 0 || after_cols == 0) continue;
1594 const int row_first = first_row_tile(t1),
1595 col_first = first_col_tile(t1);
1596 const schemeii::Plan plan = ctx.plan(width);
1597 const schemeii::Operand left = ctx.prepareRows(
1598 outer_rows.data(), static_cast<std::int64_t>(after_rows), false,
1599 static_cast<std::int64_t>(after_rows), width, plan);
1600 const schemeii::Operand right = ctx.prepareColumns(
1601 outer_cols.data(), static_cast<std::int64_t>(after_cols), true,
1602 width, static_cast<std::int64_t>(after_cols), plan);
1603 std::vector<schemeii::Task> tasks;
1604 for (int j = col_first; j < tiles; j += g.npcol) {
1605 const int i0 = first_row_tile(std::max({j, first_new, t1}));
1606 if (i0 >= tiles) continue;
1607 const std::size_t row_offset =
1608 static_cast<std::size_t>((i0 - row_first) / g.nprow) * nb;
1609 const std::size_t col_offset =
1610 static_cast<std::size_t>((j - col_first) / g.npcol) * nb;
1611 tasks.push_back({&left, static_cast<std::int64_t>(row_offset),
1612 static_cast<std::int64_t>(after_rows - row_offset),
1613 &right, static_cast<std::int64_t>(col_offset),
1614 g.span(j, n), -1.0, 1.0,
1615 A + local_row(i0) + local_col(j) * lld, lld});
1616 }
1617 ctx.run(tasks);
1618 }
1619 MPI_Allreduce(MPI_IN_PLACE, &healthy, 1, MPI_INT, MPI_MIN, st.world_comm());
1620 return healthy != 0;
1621}
Plan plan(std::int64_t k) const
Operand prepareRows(const double *a, std::int64_t lda, bool transpose, std::int64_t m, std::int64_t k, const Plan &plan, const int *exponents=nullptr) const
Row operand from op(A) (m x k). Exponents override the row maxima.
void run(std::span< const Task > tasks) const
Operand prepareColumns(const double *b, std::int64_t ldb, bool transpose, std::int64_t k, std::int64_t n, const Plan &plan, const int *exponents=nullptr) const
Column operand from op(B) (k x n).
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)

◆ requireWorkspaceBudget()

void gpr::linalg::canonical::requireWorkspaceBudget ( DistributedState & st,
const char * what,
std::int64_t needed,
AvailableMemory available )
inline

All ranks refuse a workspace when any rank lacks its local share. No peer can enter the following data exchange after another rank throws.

Definition at line 796 of file CanonicalTriangular.h.

799{
800 const int local_shortfall =
801 !fitsRankShare(needed, available, st.node_ranks());
802 int any_shortfall = local_shortfall;
803 if (st.nprocs() > 1 &&
804 MPI_Allreduce(&local_shortfall, &any_shortfall, 1, MPI_INT, MPI_MAX,
805 st.world_comm()) != MPI_SUCCESS)
806 throw std::runtime_error("cannot collect workspace memory limits");
807 if (!any_shortfall) return;
808 requireRankShare(what, needed, available, st.node_ranks());
809 throw std::runtime_error(std::string(what) +
810 " exceeds another rank's memory share");
811}
int node_ranks() const noexcept
Ranks of this process group on this node (one before initialize).
void requireRankShare(const char *what, std::int64_t needed, AvailableMemory available, int node_ranks)
constexpr bool fitsRankShare(std::int64_t needed, AvailableMemory available, int node_ranks) noexcept

◆ solveLowerFactor()

void gpr::linalg::canonical::solveLowerFactor ( DistributedState & st,
const double * L,
const int * desc_l,
double * B,
const int * desc_b,
int n,
int ncols )
inline

Solves L L^T X = B in place: forward substitution with L, then back substitution with the transposed factor, which is built once.

Definition at line 1104 of file CanonicalTriangular.h.

1107{
1108 trsm(st, Triangle::Lower, L, desc_l, B, desc_b, n, ncols, false);
1109 const std::vector<double> U = transposeFactor(st, L, desc_l, n);
1110 trsm(st, Triangle::Upper, U.data(), desc_l, B, desc_b, n, ncols, false);
1111}

◆ solveLowerFactorCached()

void gpr::linalg::canonical::solveLowerFactorCached ( DistributedState & st,
SubstitutionCache & cache,
const double * L,
const int * desc_l,
double * B,
const int * desc_b,
int n,
int ncols )
inline

Solves L L^T X = B in place against a cached factor.

Definition at line 1047 of file CanonicalTriangular.h.

1051{
1052 trsmLowerFactorCached(st, cache, L, desc_l, B, desc_b, n, ncols, false);
1053 trsmLowerFactorCached(st, cache, L, desc_l, B, desc_b, n, ncols, true);
1054}
void trsmLowerFactorCached(DistributedState &st, SubstitutionCache &cache, const double *L, const int *desc_l, double *B, const int *desc_b, int n, int ncols, bool transpose)

◆ subtractTileProduct()

template<bool TransposeRight = false, class Target, class Left, class Right>
void gpr::linalg::canonical::subtractTileProduct ( Target && out,
const Left & left,
const Right & right )
inline

Subtract a dense product from a column-major tile. Full packed operands support a strided result; partial or strided operands use Eigen.

Definition at line 121 of file TileProduct.h.

123{
124#if defined(__x86_64__) && defined(__GNUC__) && \
125 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
126 !defined(EIGEN_USE_BLAS)
127 if constexpr (!std::decay_t<Target>::IsRowMajor && !Left::IsRowMajor &&
128 !Right::IsRowMajor) {
129 if (out.rows() == 64 && out.cols() == 64 && left.rows() == 64 &&
130 left.cols() == 64 && right.rows() == 64 && right.cols() == 64 &&
131 out.innerStride() == 1 && out.outerStride() >= 64 &&
132 left.innerStride() == 1 && left.outerStride() == 64 &&
133 right.innerStride() == 1 && right.outerStride() == 64 &&
135 subtractFullTileWide<TransposeRight>(out.data(), out.outerStride(),
136 left.data(), right.data());
137 return;
138 }
139 }
140#endif
141 if constexpr (TransposeRight)
142 out.noalias() -= left * right.transpose();
143 else
144 out.noalias() -= left * right;
145}
bool wideTileProductAvailable() noexcept
Definition TileProduct.h:17

◆ tileAt()

TileMap gpr::linalg::canonical::tileAt ( double * base,
int lld,
const TileGeometry & g,
int row_tile,
int col_tile,
int rows,
int cols )
inline

Definition at line 274 of file CanonicalTriangular.h.

276{
277 return TileMap(base + static_cast<std::size_t>(row_tile / g.nprow) * g.nb +
278 static_cast<std::size_t>(col_tile / g.npcol) * g.nb *
279 static_cast<std::size_t>(lld),
280 rows, cols, Eigen::OuterStride<>(lld));
281}

◆ transposeFactor()

std::vector< double > gpr::linalg::canonical::transposeFactor ( DistributedState & st,
const double * L,
const int * desc,
int n,
std::int64_t batch_entries = 0 )
inline

Returns U = L^T in the tile layout of L: tile (i, k) of U is the transpose of tile (k, i) of L. Only the lower triangle of L is read. Collective.

Tiles move between each pair of ranks in batches of at most batch_entries (kTransposeBatchEntries when zero), so the workspace beyond U is two batches rather than a copy of every tile sent and received. Before U is allocated, U and the batches are compared with this rank's share of the node's available memory; a shortfall throws std::runtime_error naming both, in place of an allocation the OOM killer ends mid-run.

Definition at line 838 of file CanonicalTriangular.h.

842{
843 const TileGeometry g = geometryOf(st, desc);
845 const int lld = desc[8];
846 const int tiles = g.tiles(n);
847 const int np = st.nprocs();
848 const int local_cols = g.localColumns(n);
849 const std::size_t local_size = static_cast<std::size_t>(lld) * local_cols;
850 if (batch_entries <= 0) batch_entries = kTransposeBatchEntries;
851 batch_entries = std::max<std::int64_t>(
852 batch_entries, static_cast<std::int64_t>(g.nb) * g.nb);
854 st, "the transposed Cholesky factor",
855 transposeWorkspaceBytes(desc, local_cols, np > 1 ? batch_entries : 0),
857 std::vector<double> U(local_size, 0.0);
858 // Tile (k, i) of L, i <= k, transposed into tile (i, k) of U; only the
859 // lower triangle of a diagonal tile is read.
860 const auto transposeTile = [&](int i, int k, double* out, int ld) {
861 const int bi = g.span(i, n), bk = g.span(k, n);
862 TileMap src = tileAt(const_cast<double*>(L), lld, g, k, i, bk, bi);
863 for (int c = 0; c < bk; ++c)
864 for (int r = 0; r < bi; ++r)
865 out[r + static_cast<std::size_t>(c) * ld] =
866 (i == k && c < r) ? 0.0 : src(c, r);
867 };
868 if (np == 1) {
869 parallelFor(static_cast<long>(tiles), [&](long kk) {
870 const int k = static_cast<int>(kk);
871 for (int i = 0; i <= k; ++i) {
872 TileMap dst =
873 tileAt(U.data(), lld, g, i, k, g.span(i, n), g.span(k, n));
874 transposeTile(i, k, dst.data(), lld);
875 }
876 });
877 return U;
878 }
879
880 // The tiles of U each pair of ranks exchanges, in the (row tile i,
881 // column tile k >= i) order that sender and receiver both enumerate, so
882 // both cut the list into the same batches. Tiles this rank both owns in
883 // L and in U are transposed in place.
884 const auto world_rank = [&](int row, int col) {
885 return comms.rank_of[static_cast<std::size_t>(row) * g.npcol + col];
886 };
887 const int me = st.myrank();
888 std::vector<std::vector<std::pair<int, int>>> to(np), from(np);
889 for (int i = 0; i < tiles; ++i)
890 for (int k = i; k < tiles; ++k) {
891 const int src = world_rank(g.ownerRow(k), g.ownerCol(i));
892 const int dst = world_rank(g.ownerRow(i), g.ownerCol(k));
893 if (src == me) to[static_cast<std::size_t>(dst)].emplace_back(i, k);
894 if (dst == me && src != me)
895 from[static_cast<std::size_t>(src)].emplace_back(i, k);
896 }
897 const auto entries = [&](const std::pair<int, int>& tile) {
898 return static_cast<std::int64_t>(g.span(tile.first, n)) *
899 g.span(tile.second, n);
900 };
901 const auto& local = to[static_cast<std::size_t>(me)];
902 parallelFor(static_cast<long>(local.size()), [&](long q) {
903 const auto [i, k] = local[static_cast<std::size_t>(q)];
904 TileMap dst =
905 tileAt(U.data(), lld, g, i, k, g.span(i, n), g.span(k, n));
906 transposeTile(i, k, dst.data(), lld);
907 });
908 // Batch boundaries of a tile list: [first, end) spans of at most
909 // batch_entries entries (or one tile), with their buffer offsets.
910 struct Batch {
911 std::size_t first = 0, end = 0;
912 std::vector<std::int64_t> offset;
913 std::int64_t size = 0;
914 };
915 const auto batches = [&](const std::vector<std::pair<int, int>>& list) {
916 std::vector<Batch> out;
917 for (std::size_t t = 0; t < list.size();) {
918 Batch b;
919 b.first = t;
920 while (
921 t < list.size() &&
922 (t == b.first || b.size + entries(list[t]) <= batch_entries)) {
923 b.offset.push_back(b.size);
924 b.size += entries(list[t++]);
925 }
926 b.end = t;
927 out.push_back(std::move(b));
928 }
929 return out;
930 };
931 // Tags apart from the tile kernels' point-to-point messages (tag 0);
932 // the round and batch index keep consecutive messages of a pair apart.
933 constexpr int tag_base = 0x4000;
934 std::vector<double> send_buffer, recv_buffer;
935 for (int round = 1; round < np; ++round) {
936 const int dst = (me + round) % np;
937 const int src = (me - round + np) % np;
938 const auto& out_list = to[static_cast<std::size_t>(dst)];
939 const auto& in_list = from[static_cast<std::size_t>(src)];
940 const std::vector<Batch> out_batches = batches(out_list);
941 const std::vector<Batch> in_batches = batches(in_list);
942 const std::size_t steps =
943 std::max(out_batches.size(), in_batches.size());
944 for (std::size_t s = 0; s < steps; ++s) {
945 MPI_Request requests[2];
946 int pending = 0;
947 const int tag = tag_base + static_cast<int>(s % 0x3fff);
948 if (s < in_batches.size()) {
949 recv_buffer.resize(
950 static_cast<std::size_t>(in_batches[s].size));
951 MPI_Irecv(recv_buffer.data(),
952 static_cast<int>(in_batches[s].size), MPI_DOUBLE, src,
953 tag, st.world_comm(), &requests[pending++]);
954 }
955 if (s < out_batches.size()) {
956 const Batch& b = out_batches[s];
957 send_buffer.resize(static_cast<std::size_t>(b.size));
958 parallelFor(static_cast<long>(b.end - b.first), [&](long q) {
959 const auto [i, k] =
960 out_list[b.first + static_cast<std::size_t>(q)];
961 transposeTile(i, k,
962 send_buffer.data() +
963 b.offset[static_cast<std::size_t>(q)],
964 g.span(i, n));
965 });
966 MPI_Isend(send_buffer.data(), static_cast<int>(b.size),
967 MPI_DOUBLE, dst, tag, st.world_comm(),
968 &requests[pending++]);
969 }
970 MPI_Waitall(pending, requests, MPI_STATUSES_IGNORE);
971 if (s < in_batches.size()) {
972 const Batch& b = in_batches[s];
973 parallelFor(static_cast<long>(b.end - b.first), [&](long q) {
974 const auto [i, k] =
975 in_list[b.first + static_cast<std::size_t>(q)];
976 const int bi = g.span(i, n), bk = g.span(k, n);
977 tileAt(U.data(), lld, g, i, k, bi, bk) =
978 Eigen::Map<const Eigen::MatrixXd>(
979 recv_buffer.data() +
980 b.offset[static_cast<std::size_t>(q)],
981 bi, bk);
982 });
983 }
984 }
985 }
986 return U;
987}
std::int64_t transposeWorkspaceBytes(const int *desc, int local_cols, std::int64_t batch_entries)
constexpr std::int64_t kTransposeBatchEntries

◆ transposeWorkspaceBytes()

std::int64_t gpr::linalg::canonical::transposeWorkspaceBytes ( const int * desc,
int local_cols,
std::int64_t batch_entries )
inline

Bytes transposeFactor claims beyond the factor: the transposed factor (the local array of desc) and one send and one receive batch.

Definition at line 819 of file CanonicalTriangular.h.

821{
822 return (static_cast<std::int64_t>(desc[8]) * local_cols +
823 2 * batch_entries) *
824 static_cast<std::int64_t>(sizeof(double));
825}

◆ trsm()

void gpr::linalg::canonical::trsm ( DistributedState & st,
Triangle triangle,
const double * T,
const int * desc_t,
double * B,
const int * desc_b,
int n,
int ncols,
bool lower_rhs )
inline

Definition at line 307 of file CanonicalTriangular.h.

310{
312 if (n <= 0 || ncols <= 0) return;
313 const TileGeometry g = geometryOf(st, desc_t);
314 if (desc_b[4] != g.nb || desc_b[5] != g.nb)
315 throw std::logic_error("canonical trsm needs matching block sizes");
317 st.tile_communicators(desc_t[1]);
318 const int lld_t = desc_t[8];
319 const int lld_b = desc_b[8];
320 const int row_tiles = g.tiles(n);
321 const int col_tiles = g.tiles(ncols);
322 const bool forward = triangle == Triangle::Lower;
323 // A right-hand side of one tile column lives on process column 0 alone;
324 // the factor tiles then go from their owning column to column 0 only,
325 // in place of a broadcast to the whole process row.
326 const bool single = col_tiles == 1;
327
328 std::vector<int> my_col_tiles;
329 for (int j = g.mycol; j < col_tiles; j += g.npcol)
330 my_col_tiles.push_back(j);
331
332 // The solved block row and the broadcast panel are packed aligned, so
333 // the update products read them in place.
334 std::vector<double> diagonal;
335 std::vector<double> left_stack;
336 const auto exact = schemeIIContext();
337 // group == 1, including a single right-hand-side column and Scheme II,
338 // keeps one panel and updates every trailing row before the next step.
339 // A wider group updates the next group's rows, then applies later rows
340 // while that next group is solved into the other panel set. Each tile
341 // still receives the panels one at a time, in solution order.
342 const auto local_rows = (row_tiles + g.nprow - 1LL) / g.nprow;
343 const int requested_group =
344 std::clamp(choleskyPanelGroup().load(), 1, row_tiles);
345 const long double max_prefetch =
346 static_cast<long double>(requested_group) * (local_rows + 1) *
347 ((static_cast<std::int64_t>(g.nb) * g.nb + 7) / 8 * 8);
348 const int group =
349 exact || single || max_prefetch > std::numeric_limits<int>::max()
350 ? 1
351 : requested_group;
352 // Overlap needs a worker while the caller solves the next group. A
353 // single-threaded rank updates all trailing rows before reusing panels.
354 const bool lookahead = group > 1 && tileThreadCount().load() > 1;
355 const auto panelIndex = [group, lookahead](int update_step) {
356 const int set = lookahead ? (update_step / group) % 2 : 0;
357 return static_cast<std::size_t>(set * group + update_step % group);
358 };
359 struct Panel {
360 AlignedBuffer solved_store, panel_store;
361 double* panel = nullptr;
362 std::vector<int> active_cols, active_rows;
363 std::vector<std::size_t> col_offset, row_offset;
364 std::size_t diagonal_offset = 0;
365 int k = 0;
366 };
367 std::vector<Panel> panels(
368 static_cast<std::size_t>(lookahead ? 2 * group : group));
369 std::vector<AlignedBuffer> prefetch_store(lookahead ? 2U : 1U);
370 std::vector<int> counts(g.npcol), displacements(g.npcol);
371 std::vector<std::size_t> factor_offsets(static_cast<std::size_t>(group));
372 if (group > 1) {
373 const auto local_cols = (col_tiles + g.npcol - 1LL) / g.npcol;
374 const long double bytes =
375 (lookahead ? 2.0L : 1.0L) * static_cast<long double>(group) *
376 (local_rows + 1 + local_cols) *
377 ((static_cast<std::int64_t>(g.nb) * g.nb + 7) / 8 * 8) *
378 sizeof(double);
379 if (bytes > std::numeric_limits<std::int64_t>::max())
380 throw std::length_error(
381 "canonical substitution panels exceed addressable storage");
382 requireWorkspaceBudget(st, "canonical substitution panels",
383 static_cast<std::int64_t>(bytes),
385 }
386 // Tiles are packed at aligned offsets for the tile kernels; the Scheme
387 // II products read the solved block row as one dense matrix.
388 const auto pad = [&](std::size_t count) {
389 return exact ? count : AlignedBuffer::padded(count);
390 };
391 // A selected row takes every panel in the group whose solved row contains
392 // its column. A missing factor row or solved column is a broken schedule.
393 const char* missing_panel =
394 "canonical substitution is missing a panel for a selected row";
395 const auto panelRow = [&](const Panel& p, int i) {
396 if (p.panel == nullptr || p.active_rows.empty() ||
397 p.row_offset.size() != p.active_rows.size() || g.nprow <= 0)
398 throw std::logic_error(missing_panel);
399 const int delta = i - p.active_rows.front();
400 if (delta < 0 || delta % g.nprow != 0)
401 throw std::logic_error(missing_panel);
402 const auto row_slot = static_cast<std::size_t>(delta / g.nprow);
403 if (row_slot >= p.active_rows.size() || p.active_rows[row_slot] != i)
404 throw std::logic_error(missing_panel);
405 return row_slot;
406 };
407 const bool in_place_full_tiles =
409 const auto updateTile = [&](int i, int j, int begin_step, int last_step) {
410 if (lower_rhs && j > i) return;
411 const int bi = g.span(i, n);
412 const int bj = g.span(j, ncols);
413 TileMap target = tileAt(B, lld_b, g, i, j, bi, bj);
414 const auto apply = [&](auto&& result) {
415 for (int update_step = begin_step; update_step <= last_step;
416 ++update_step) {
417 const Panel& p = panels[panelIndex(update_step)];
418 if (lower_rhs && j > p.k) continue;
419 const auto row_slot = panelRow(p, i);
420 const auto col_slot = static_cast<std::size_t>(j / g.npcol);
421 if (col_slot >= p.col_offset.size())
422 throw std::logic_error(missing_panel);
423 const int width = g.span(p.k, n);
424 const Eigen::Map<const Eigen::MatrixXd, Eigen::Aligned64> left(
425 p.panel + p.row_offset[row_slot], bi, width);
426 const Eigen::Map<const Eigen::MatrixXd, Eigen::Aligned64> right(
427 p.solved_store.data() + p.col_offset[col_slot], width, bj);
428 portable_tiles::subtractProduct(result, left, right);
429 }
430 };
431 const int last_panel = std::max(panels[panelIndex(begin_step)].k,
432 panels[panelIndex(last_step)].k);
433 if (in_place_full_tiles && bi == 64 && bj == 64 &&
434 g.span(last_panel, n) == 64) {
435 apply(target);
436 } else {
437 AlignedTile result(target);
438 apply(result.map());
439 target = result.map();
440 }
441 };
442 const auto applyRows = [&](const std::vector<int>& rows,
443 const std::vector<int>& cols, int begin_step,
444 int last_step) {
445 if (rows.empty() || cols.empty()) return;
446 const long count =
447 static_cast<long>(rows.size()) * static_cast<long>(cols.size());
448 parallelFor(count, [&](long u) {
449 const std::size_t qi = static_cast<std::size_t>(u) / cols.size();
450 const std::size_t qj = static_cast<std::size_t>(u) % cols.size();
451 updateTile(rows[qi], cols[qj], begin_step, last_step);
452 });
453 };
454 std::unique_ptr<TileBackground> trailing;
455 const auto joinTrailing = [&] {
456 auto job = std::move(trailing);
457 if (job) job->finish();
458 };
459
460 for (int step = 0; step < row_tiles; ++step) {
461 if (group > 1 && step % group == 0) {
462 // T is immutable throughout substitution. Each process row
463 // collects its factor panels and their diagonal tiles together;
464 // only the solved right-hand sides depend on preceding steps.
465 // Rank-ordered packing makes every sender's segment contiguous.
466 const int end = std::min(row_tiles, step + group);
467 std::size_t total = 0;
468 for (int owner = 0; owner < g.npcol; ++owner) {
469 displacements[owner] = static_cast<int>(total);
470 for (int next = step; next < end; ++next) {
471 const int k = forward ? next : row_tiles - 1 - next;
472 if (g.ownerCol(k) != owner) continue;
473 const auto slot = static_cast<std::size_t>(next % group);
474 Panel& p = panels[panelIndex(next)];
475 p.k = k;
476 p.active_rows.clear();
477 p.row_offset.clear();
478 factor_offsets[slot] = total;
479 std::size_t size = 0;
480 const int first = forward ? k + 1 : 0;
481 const int last = forward ? row_tiles : k;
482 const int start =
483 first +
484 ((g.myrow - first % g.nprow) + g.nprow) % g.nprow;
485 const int bk = g.span(k, n);
486 for (int i = start; i < last; i += g.nprow) {
487 p.active_rows.push_back(i);
488 p.row_offset.push_back(size);
489 size +=
490 pad(static_cast<std::size_t>(g.span(i, n)) * bk);
491 }
492 p.diagonal_offset = size;
493 if (g.myrow == g.ownerRow(k))
494 size += pad(static_cast<std::size_t>(bk) * bk);
495 total += size;
496 }
497 counts[owner] = static_cast<int>(total) - displacements[owner];
498 }
499 const int prefetch_set = lookahead ? (step / group) % 2 : 0;
500 double* const data = prefetch_store[prefetch_set].reserve(total);
501 for (int next = step; next < end; ++next) {
502 Panel& p = panels[panelIndex(next)];
503 p.panel = total > 0
504 ? data + factor_offsets[static_cast<std::size_t>(
505 next % group)]
506 : nullptr;
507 if (g.mycol != g.ownerCol(p.k)) continue;
508 const int bk = g.span(p.k, n);
509 for (std::size_t q = 0; q < p.active_rows.size(); ++q) {
510 const int i = p.active_rows[q];
511 const int bi = g.span(i, n);
512 Eigen::Map<Eigen::MatrixXd>(p.panel + p.row_offset[q], bi,
513 bk) =
514 tileAt(const_cast<double*>(T), lld_t, g, i, p.k, bi,
515 bk);
516 }
517 if (g.myrow == g.ownerRow(p.k))
518 Eigen::Map<Eigen::MatrixXd>(p.panel + p.diagonal_offset, bk,
519 bk) =
520 tileAt(const_cast<double*>(T), lld_t, g, p.k, p.k, bk,
521 bk);
522 }
523 if (total > 0)
524 MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DOUBLE, data, counts.data(),
525 displacements.data(), MPI_DOUBLE, comms.row);
526 }
527 const int k = forward ? step : row_tiles - 1 - step;
528 Panel& current = panels[panelIndex(step)];
529 current.k = k;
530 auto& solved_store = current.solved_store;
531 auto& panel_store = current.panel_store;
532 auto& active_cols = current.active_cols;
533 auto& active_rows = current.active_rows;
534 auto& col_offset = current.col_offset;
535 auto& row_offset = current.row_offset;
536 const int kr = g.ownerRow(k);
537 const int kc = g.ownerCol(k);
538 const int bk = g.span(k, n);
539
540 // Right-hand-side tile columns of this process column that take part.
541 active_cols.clear();
542 col_offset.clear();
543 std::size_t solved_size = 0;
544 for (int j: my_col_tiles) {
545 if (lower_rhs && j > k) continue;
546 active_cols.push_back(j);
547 col_offset.push_back(solved_size);
548 solved_size += pad(static_cast<std::size_t>(bk) * g.span(j, ncols));
549 }
550 double* const solved = solved_store.reserve(solved_size);
551
552 // Row k of the solution: the diagonal tile is shared along the
553 // process row that owns the block row, each owner solves its tiles.
554 const bool sends_diagonal = single && kc != 0 && g.mycol == kc;
555 if (g.myrow == kr &&
556 (!active_cols.empty() || !single || sends_diagonal)) {
557 diagonal.assign(static_cast<std::size_t>(bk) * bk, 0.0);
558 if (group > 1) {
559 Eigen::Map<Eigen::MatrixXd>(diagonal.data(), bk, bk) =
560 Eigen::Map<const Eigen::MatrixXd>(
561 current.panel + current.diagonal_offset, bk, bk);
562 } else {
563 if (g.mycol == kc) {
564 TileMap source =
565 tileAt(const_cast<double*>(T), lld_t, g, k, k, bk, bk);
566 Eigen::Map<Eigen::MatrixXd>(diagonal.data(), bk, bk) =
567 source;
568 }
569 if (!single)
570 MPI_Bcast(diagonal.data(), bk * bk, MPI_DOUBLE, kc,
571 comms.row);
572 else if (sends_diagonal)
573 MPI_Send(diagonal.data(), bk * bk, MPI_DOUBLE, 0, 0,
574 comms.row);
575 else if (kc != 0)
576 MPI_Recv(diagonal.data(), bk * bk, MPI_DOUBLE, kc, 0,
577 comms.row, MPI_STATUS_IGNORE);
578 }
579 Eigen::Map<const Eigen::MatrixXd> d(diagonal.data(), bk, bk);
580 // One aligned copy of the diagonal tile serves every column.
581 AlignedTile factor(d);
582 // Columns are independent; each solve runs on one thread.
583 parallelFor(static_cast<long>(active_cols.size()), [&](long c) {
584 const std::size_t q = static_cast<std::size_t>(c);
585 const int j = active_cols[q];
586 const int bj = g.span(j, ncols);
587 TileMap target = tileAt(B, lld_b, g, k, j, bk, bj);
588 if (exact) {
589 fixed::solveLeftTriangular(diagonal.data(), bk, bk, forward,
590 target.data(), lld_b, bj);
591 Eigen::Map<Eigen::MatrixXd>(solved + col_offset[q], bk,
592 bj) = target;
593 return;
594 }
595 AlignedMap rhs(solved + col_offset[q], bk, bj);
596 rhs = target;
597 portable_tiles::solveLeft(factor.map().data(), rhs.data(), bk,
598 bj, forward);
599 target = rhs;
600 });
601 }
602 if (solved_size > 0)
603 MPI_Bcast(solved, static_cast<int>(solved_size), MPI_DOUBLE, kr,
604 comms.col);
605
606 // Panel of T below (forward) or above (backward) the diagonal tile,
607 // restricted to the tile rows of this process row.
608 if (group == 1) {
609 active_rows.clear();
610 row_offset.clear();
611 std::size_t panel_size = 0;
612 const int first = forward ? k + 1 : 0;
613 const int last = forward ? row_tiles : k;
614 int start =
615 first + ((g.myrow - first % g.nprow) + g.nprow) % g.nprow;
616 for (int i = start; i < last; i += g.nprow) {
617 active_rows.push_back(i);
618 row_offset.push_back(panel_size);
619 panel_size += pad(static_cast<std::size_t>(g.span(i, n)) * bk);
620 }
621 current.panel = panel_store.reserve(panel_size);
622 double* const panel = current.panel;
623 {
624 if (g.mycol == kc) {
625 for (std::size_t q = 0; q < active_rows.size(); ++q) {
626 const int i = active_rows[q];
627 const int bi = g.span(i, n);
628 TileMap source = tileAt(const_cast<double*>(T), lld_t,
629 g, i, k, bi, bk);
630 Eigen::Map<Eigen::MatrixXd>(panel + row_offset[q], bi,
631 bk) = source;
632 }
633 }
634 if (panel_size > 0) {
635 if (!single)
636 MPI_Bcast(panel, static_cast<int>(panel_size),
637 MPI_DOUBLE, kc, comms.row);
638 else if (kc != 0 && g.mycol == kc)
639 MPI_Send(panel, static_cast<int>(panel_size),
640 MPI_DOUBLE, 0, 1, comms.row);
641 else if (kc != 0 && g.mycol == 0)
642 MPI_Recv(panel, static_cast<int>(panel_size),
643 MPI_DOUBLE, kc, 1, comms.row,
644 MPI_STATUS_IGNORE);
645 }
646 }
647 }
648 const double* const panel = current.panel;
649
650 // Update: B_ij -= T_ik X_kj. With a Scheme II engine: one exact
651 // product per step for wide right-hand sides, a fixed-order kernel
652 // for narrow ones. Otherwise one dense kernel per output tile.
653 if (exact) {
654 if (active_rows.empty() || active_cols.empty()) continue;
655 std::size_t total_rows = 0;
656 std::vector<std::size_t> stacked(active_rows.size());
657 for (std::size_t q = 0; q < active_rows.size(); ++q) {
658 stacked[q] = total_rows;
659 total_rows +=
660 static_cast<std::size_t>(g.span(active_rows[q], n));
661 }
662 left_stack.assign(total_rows * bk, 0.0);
663 for (std::size_t q = 0; q < active_rows.size(); ++q) {
664 const int i = active_rows[q];
665 const int bi = g.span(i, n);
666 Eigen::Map<Eigen::MatrixXd, 0, Eigen::OuterStride<>> out(
667 left_stack.data() + stacked[q], bi, bk,
668 Eigen::OuterStride<>(
669 static_cast<Eigen::Index>(total_rows)));
670 out = Eigen::Map<const Eigen::MatrixXd>(panel + row_offset[q],
671 bi, bk);
672 }
673 // Rows of the output for active column q: all active rows, or
674 // with a lower right-hand side the rows i >= j.
675 const auto first_row = [&](int j) {
676 std::size_t q = 0;
677 while (q < active_rows.size() && lower_rhs &&
678 active_rows[q] < j)
679 ++q;
680 return q;
681 };
682 const auto target_of = [&](std::size_t qi, int j) {
683 return B +
684 static_cast<std::size_t>(active_rows[qi] / g.nprow) *
685 g.nb +
686 static_cast<std::size_t>(j / g.npcol) * g.nb *
687 static_cast<std::size_t>(lld_b);
688 };
689 if (ncols >= kExactMinColumns) {
690 const schemeii::Plan plan = exact->plan(bk);
691 const schemeii::Operand left = exact->prepareRows(
692 left_stack.data(), static_cast<std::int64_t>(total_rows),
693 false, static_cast<std::int64_t>(total_rows), bk, plan);
694 const schemeii::Operand right = exact->prepareColumns(
695 solved, bk, false, bk,
696 static_cast<std::int64_t>(solved_size / bk), plan);
697 std::vector<schemeii::Task> tasks;
698 for (std::size_t qj = 0; qj < active_cols.size(); ++qj) {
699 const int j = active_cols[qj];
700 const std::size_t qi = first_row(j);
701 if (qi == active_rows.size()) continue;
702 tasks.push_back(
703 {&left, static_cast<std::int64_t>(stacked[qi]),
704 static_cast<std::int64_t>(total_rows - stacked[qi]),
705 &right, static_cast<std::int64_t>(col_offset[qj] / bk),
706 g.span(j, ncols), -1.0, 1.0, target_of(qi, j), lld_b});
707 }
708 exact->run(tasks);
709 } else {
710 parallelFor(static_cast<long>(active_cols.size()), [&](long u) {
711 const std::size_t qj = static_cast<std::size_t>(u);
712 const int j = active_cols[qj];
713 const std::size_t qi = first_row(j);
714 if (qi == active_rows.size()) return;
715 fixed::subtractProduct(
716 left_stack.data() + stacked[qi],
717 static_cast<std::int64_t>(total_rows), false,
718 solved + col_offset[qj], bk, target_of(qi, j), lld_b,
719 static_cast<std::int64_t>(total_rows - stacked[qi]),
720 g.span(j, ncols), bk);
721 });
722 }
723 continue;
724 }
725 if (group == 1) {
726 applyRows(active_rows, active_cols, step, step);
727 continue;
728 }
729 const int group_first = step - step % group;
730 const int group_end = std::min(row_tiles, group_first + group);
731 const bool group_complete = step + 1 == group_end;
732 const auto rowStep = [&](int row) {
733 return forward ? row : row_tiles - 1 - row;
734 };
735 if (!group_complete) {
736 // Rows solved later in this group need this panel before their
737 // diagonal solve. Rows outside the group wait for the whole group.
738 const long updates = static_cast<long>(active_rows.size()) *
739 static_cast<long>(active_cols.size());
740 parallelFor(updates, [&](long u) {
741 const std::size_t qi =
742 static_cast<std::size_t>(u) / active_cols.size();
743 const std::size_t qj =
744 static_cast<std::size_t>(u) % active_cols.size();
745 const int i = active_rows[qi];
746 const int j = active_cols[qj];
747 if (rowStep(i) >= group_end) return;
748 updateTile(i, j, step, step);
749 });
750 continue;
751 }
752 if (!lookahead) {
753 applyRows(active_rows, active_cols, group_first, step);
754 continue;
755 }
756 // Far rows of the previous group must hold that group's panels before
757 // this group's products land on the same tiles.
758 joinTrailing();
759 const int next_end = std::min(row_tiles, group_end + group);
760 std::vector<int> next_rows, rest_rows;
761 for (int row: active_rows) {
762 const int row_step = rowStep(row);
763 if (row_step < next_end)
764 next_rows.push_back(row);
765 else
766 rest_rows.push_back(row);
767 }
768 const std::vector<int> columns(active_cols.begin(), active_cols.end());
769 applyRows(next_rows, columns, group_first, step);
770 if (rest_rows.empty() || columns.empty()) continue;
771 const long rest_pairs = static_cast<long>(rest_rows.size()) *
772 static_cast<long>(columns.size());
773 // Local: a rank with few trailing tiles updates them before the next
774 // collective, and a rank with many updates them while that collective
775 // runs. Both ranks post the same collectives afterwards.
776 if (rest_pairs < 2L * tileThreadCount().load()) {
777 applyRows(rest_rows, columns, group_first, step);
778 continue;
779 }
780 const int begin_step = group_first;
781 const int last_step = step;
782 trailing = std::make_unique<TileBackground>(
783 rest_pairs, [&, rest_rows, columns, begin_step, last_step](long u) {
784 const std::size_t qi =
785 static_cast<std::size_t>(u) / columns.size();
786 const std::size_t qj =
787 static_cast<std::size_t>(u) % columns.size();
788 updateTile(rest_rows[qi], columns[qj], begin_step, last_step);
789 });
790 }
791 joinTrailing();
792}
void solveLeft(const double *factor, double *rhs, std::ptrdiff_t order, std::ptrdiff_t columns, bool lower)
Panel panel(Real left, Real right, const Function &function, std::size_t &evaluations)

◆ trsmCachedLowerFactor()

void gpr::linalg::canonical::trsmCachedLowerFactor ( DistributedState & st,
double * B,
const int * desc_b,
int n,
int ncols,
bool transpose )
inline

Same as trsmLowerFactor for the factor cached in st: the transposed factor is built once per factorization and reused by every back substitution.

Definition at line 1006 of file CanonicalTriangular.h.

1009{
1010 if (transpose && !st.has_cached_U())
1011 st.cache_U(transposeFactor(st, st.cached_L(), st.cached_L_desc(), n));
1012 const double* factor = transpose ? st.cached_U() : st.cached_L();
1013 trsm(st, transpose ? Triangle::Upper : Triangle::Lower, factor,
1014 st.cached_L_desc(), B, desc_b, n, ncols, false);
1015}
void cache_U(std::vector< double > U)
const double * cached_U() const
bool has_cached_U() const noexcept
const double * cached_L() const

◆ trsmLowerFactor()

void gpr::linalg::canonical::trsmLowerFactor ( DistributedState & st,
const double * L,
const int * desc_l,
double * B,
const int * desc_b,
int n,
int ncols,
bool transpose )
inline

Solves L X = B (transpose == false) or L^T X = B (transpose == true) in place for the lower Cholesky factor L in the block-cyclic layout.

Definition at line 991 of file CanonicalTriangular.h.

994{
995 if (!transpose) {
996 trsm(st, Triangle::Lower, L, desc_l, B, desc_b, n, ncols, false);
997 return;
998 }
999 const std::vector<double> U = transposeFactor(st, L, desc_l, n);
1000 trsm(st, Triangle::Upper, U.data(), desc_l, B, desc_b, n, ncols, false);
1001}

◆ trsmLowerFactorCached()

void gpr::linalg::canonical::trsmLowerFactorCached ( DistributedState & st,
SubstitutionCache & cache,
const double * L,
const int * desc_l,
double * B,
const int * desc_b,
int n,
int ncols,
bool transpose )
inline

Solves L X = B (transpose == false) or L^T X = B in place for a factor that outlives the call, reusing the transposed factor kept in cache.

Definition at line 1031 of file CanonicalTriangular.h.

1036{
1037 if (transpose && !cache.has_upper) {
1038 cache.upper = transposeFactor(st, L, desc_l, n);
1039 cache.has_upper = true;
1040 }
1041 const double* factor = transpose ? cache.upper.data() : L;
1042 trsm(st, transpose ? Triangle::Upper : Triangle::Lower, factor, desc_l, B,
1043 desc_b, n, ncols, false);
1044}

◆ wideTileProductAvailable()

bool gpr::linalg::canonical::wideTileProductAvailable ( )
inlinenoexcept

Definition at line 17 of file TileProduct.h.

18{
19#if defined(__x86_64__) && defined(__GNUC__) && \
20 defined(EIGEN_VECTORIZE_FMA) && !defined(EIGEN_VECTORIZE_AVX512) && \
21 !defined(EIGEN_USE_BLAS)
22 static const bool available =
23 __builtin_cpu_supports("avx512f") && __builtin_cpu_supports("fma");
24 return available;
25#else
26 return false;
27#endif
28}

Variable Documentation

◆ kExactMinColumns

int gpr::linalg::canonical::kExactMinColumns = 32
inlineconstexpr

Solves T X = B in place (X overwrites B) for a tiled triangular T.

T is read on its stored triangle only. For Triangle::Lower the tile rows are visited in ascending order (forward substitution) and for Triangle::Upper in descending order (back substitution). When lower_rhs is true, B holds a matrix whose tiles above the tile diagonal are zero and only tiles on or below the diagonal of X are computed; this halves the work for the inverse.

Collective over the grid. T and B share the block size of their descriptors; B has n rows and ncols columns. Right-hand sides from which a substitution step takes its update from one Scheme II product; narrower solves use the fixed-order kernel. Compared with the global column count, so the choice does not follow the grid.

Definition at line 301 of file CanonicalTriangular.h.

◆ kTransposeBatchEntries

std::int64_t gpr::linalg::canonical::kTransposeBatchEntries = std::int64_t{1} << 24
inlineconstexpr

Entries of one message batch of the factor transpose: 2^24 doubles (128 MiB), at least one tile.

Definition at line 815 of file CanonicalTriangular.h.

815{1} << 24;