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

#include <gpr/linalg/DistributedState.h>

Classes

struct  CholeskyFactorSnapshot
struct  TileCommunicators

Public Member Functions

 DistributedState (std::shared_ptr< const MpiCommunicator > communication)
 ~DistributedState ()
const std::shared_ptr< const MpiCommunicator > & communication () const noexcept
std::uint64_t grid_generation () const noexcept
DistributedFactorization factorization () const noexcept
bool reproducible () const noexcept
bool exceeds_lp64 (const int *desc) const
bool tile_kernels (const int *desc, const char *routine, const int *other=nullptr) const
void require_lp64 (const int *desc, const char *routine) const
void set_factorization (DistributedFactorization mode)
DistributedBackendKind backend_kind () const noexcept
const char * backend_name () const noexcept
void configure (int block_size, int nprow, bool computation_tile=false) noexcept
int configured_block_size () const noexcept
int effective_configured_block_size () const noexcept
int configured_nprow () const noexcept
void initialize (int block_size=0, int nprow=0)
int node_ranks () const noexcept
 Ranks of this process group on this node (one before initialize).
int cpu_share () const noexcept
void make_lin_desc (int m, int n, int *desc) const
int lin_local_rows (int m) const
int col_block_nb (int n) const
void col_block_range (int n, int &start, int &count) const
void make_col_desc (int m, int n, int *desc) const
int compute_ictxt () const
int lin_local_cols (int n) const
std::vector< double > scatter_lin (const EigenMatrix &global, int m, int n, int *desc) const
std::vector< double > scatter_lin_colvector_root (const Eigen::VectorXd &global, int m, int *desc) const
Eigen::VectorXd gather_lin_colvector_root (const double *local, const int *desc, int m) const
void make_desc (int m, int n, int *desc) const
int local_rows (int m) const
int local_cols (int n) const
int local_index_for_global (int global_index, int block_size, int proc_coord, int nprocs_dim) const
int global_index_for_local (int local_index, int block_size, int proc_coord, int nprocs_dim) const
std::vector< double > scatter (const EigenMatrix &global, int m, int n, int *desc) const
void scatter_by_panels (const double *global, int m, int n, const int *desc, double *local, int row, int col, int nprow, int npcol) const
std::vector< double > alloc_local (int m, int n, int *desc) const
std::vector< double > scatter_colvector_root (const Eigen::VectorXd &global, int m, int *desc) const
EigenMatrix gather (const double *local, const int *desc, int m, int n, bool broadcast_result=true) const
EigenMatrix gather_block (const double *local, const int *desc, int row0, int col0, int m, int n, bool broadcast_result=true) const
Eigen::VectorXd gather_colvector_root (const double *local, const int *desc, int m, bool broadcast_result=true) const
void cache_L (std::vector< double > L_dist, const int *desc, int n)
void claim_cached_L (const void *owner) noexcept
bool cached_L_owned_by (const void *owner) const noexcept
bool cached_L_is_unowned () const noexcept
CholeskyFactorSnapshot take_cached_L (const void *owner)
bool restore_cached_L (CholeskyFactorSnapshot snapshot, const void *owner)
bool has_cached_L () const
const double * cached_L () const
double * cached_L_mut ()
bool has_cached_U () const noexcept
void cache_U (std::vector< double > U)
const double * cached_U () const
const int * cached_L_desc () const
int cached_n () const
void invalidate_L ()
void cache_L_lin (std::vector< double > L_dist, const int *desc, int n)
bool has_cached_L_lin () const
const double * cached_L_lin () const
double * cached_L_lin_mut ()
const int * cached_L_lin_desc () const
int cached_lin_n () const
void cache_inv (std::vector< double > inv_dist, const int *desc, int n)
std::size_t inverse_builds () const noexcept
bool has_cached_inv () const
const double * cached_inv () const
const int * cached_inv_desc () const
int cached_inv_n () const
void invalidate_inv ()
void invalidate_derived_cholesky ()
bool owns_global_entry (int global_row, int global_col) const
bool owns_lin_entry (int global_row, int global_col) const
const double * cached_inv_block (int global_row, int global_col, int &leading_dimension) const
double cached_inv_local_entry (int global_row, int global_col) const
int context () const
int lin_context () const
MPI_Comm world_comm () const
int lin_myrow () const
int lin_mycol () const
int nprocs () const
int myrank () const
int myrow () const
int mycol () const
int nprow () const
int npcol () const
int block_size () const
bool is_root () const
TileCommunicators tile_communicators (int context=-1)

Static Public Member Functions

static DistributedState & instance ()
static std::uint64_t factorization_generation () noexcept
static constexpr DistributedBackendKind compiled_backend () noexcept
static int recommended_block_size (int requested_block, int nprocs)
static int recommended_nprow (int nprocs, int block_size)
static void release (std::vector< double > &buffer) noexcept
 Returns the storage of buffer to the allocator.

Detailed Description

Definition at line 72 of file DistributedState.h.

Constructor & Destructor Documentation

◆ DistributedState()

gpr::linalg::DistributedState::DistributedState ( std::shared_ptr< const MpiCommunicator > communication)
inlineexplicit

Construct independent factor storage on a shared process group. Grid initialization and destruction are collective on that group.

Definition at line 102 of file DistributedState.h.

104 : communication_(std::move(communication))
105 {
106 if (!communication_)
107 throw std::invalid_argument(
108 "distributed state requires communication");
109 }
const std::shared_ptr< const MpiCommunicator > & communication() const noexcept

◆ ~DistributedState()

gpr::linalg::DistributedState::~DistributedState ( )
inline

Definition at line 111 of file DistributedState.h.

112 {
113 int mpi_up = 0;
114 int mpi_fin = 0;
115 MPI_Initialized(&mpi_up);
116 MPI_Finalized(&mpi_fin);
117 if (!mpi_up || mpi_fin) return;
118 release_grids();
119 communication_.reset();
120 if (owns_mpi_) MPI_Finalize();
121 }

Member Function Documentation

◆ alloc_local()

std::vector< double > gpr::linalg::DistributedState::alloc_local ( int m,
int n,
int * desc ) const
inline

Definition at line 775 of file DistributedState.h.

776 {
777 make_desc(m, n, desc);
778 return std::vector<double>(
779 static_cast<size_t>(local_rows(m)) * local_cols(n), 0.0);
780 }
void make_desc(int m, int n, int *desc) const

◆ backend_kind()

DistributedBackendKind gpr::linalg::DistributedState::backend_kind ( ) const
inlinenodiscardnoexcept

Definition at line 225 of file DistributedState.h.

226 {
227 return compiled_backend();
228 }
static constexpr DistributedBackendKind compiled_backend() noexcept

◆ backend_name()

const char * gpr::linalg::DistributedState::backend_name ( ) const
inlinenodiscardnoexcept

Definition at line 230 of file DistributedState.h.

231 {
233 }
DistributedBackendKind backend_kind() const noexcept
constexpr const char * distributed_backend_name(DistributedBackendKind backend) noexcept

◆ block_size()

int gpr::linalg::DistributedState::block_size ( ) const
inline

Definition at line 1326 of file DistributedState.h.

1327 {
1328 return mb_;
1329 }

◆ cache_inv()

void gpr::linalg::DistributedState::cache_inv ( std::vector< double > inv_dist,
const int * desc,
int n )
inline

Definition at line 1182 of file DistributedState.h.

1183 {
1184 ++inverse_builds_;
1185 Inv_cache_ = std::move(inv_dist);
1186 std::copy_n(desc, 9, Inv_desc_);
1187 Inv_n_ = n;
1188 has_inv_ = true;
1189 Inv_generation_ = factorization_generation();
1190 }
static std::uint64_t factorization_generation() noexcept

◆ cache_L()

void gpr::linalg::DistributedState::cache_L ( std::vector< double > L_dist,
const int * desc,
int n )
inline

Definition at line 1046 of file DistributedState.h.

1047 {
1048 L_cache_ = std::move(L_dist);
1049 std::copy_n(desc, 9, L_desc_);
1050 L_n_ = n;
1051 has_L_ = true;
1052 L_generation_ = factorization_generation();
1053 L_owner_ = nullptr;
1054 has_inv_ = false;
1055 has_L_lin_ = false;
1056 has_U_ = false;
1057 }

◆ cache_L_lin()

void gpr::linalg::DistributedState::cache_L_lin ( std::vector< double > L_dist,
const int * desc,
int n )
inline

Definition at line 1152 of file DistributedState.h.

1153 {
1154 L_lin_cache_ = std::move(L_dist);
1155 std::copy_n(desc, 9, L_lin_desc_);
1156 L_lin_n_ = n;
1157 has_L_lin_ = true;
1158 L_lin_generation_ = factorization_generation();
1159 }

◆ cache_U()

void gpr::linalg::DistributedState::cache_U ( std::vector< double > U)
inline

Definition at line 1121 of file DistributedState.h.

1122 {
1123 U_cache_ = std::move(U);
1124 has_U_ = true;
1125 }

◆ cached_inv()

const double * gpr::linalg::DistributedState::cached_inv ( ) const
inline

Definition at line 1201 of file DistributedState.h.

1202 {
1203 return Inv_cache_.data();
1204 }

◆ cached_inv_block()

const double * gpr::linalg::DistributedState::cached_inv_block ( int global_row,
int global_col,
int & leading_dimension ) const
inline

Local storage of the cached inverse at a global entry this rank owns, with the leading dimension of the local buffer; entries of the same block-cyclic block follow at unit row and leading-dimension column offsets.

Definition at line 1252 of file DistributedState.h.

1254 {
1255 assert(has_inv_);
1256 assert(owns_global_entry(global_row, global_col));
1257 const int local_i =
1258 local_index_for_global(global_row, mb_, myrow_, nprow_);
1259 const int local_j =
1260 local_index_for_global(global_col, mb_, mycol_, npcol_);
1261 leading_dimension = Inv_desc_[8];
1262 return Inv_cache_.data() + static_cast<size_t>(local_i) +
1263 static_cast<size_t>(local_j) * leading_dimension;
1264 }
int local_index_for_global(int global_index, int block_size, int proc_coord, int nprocs_dim) const
bool owns_global_entry(int global_row, int global_col) const

◆ cached_inv_desc()

const int * gpr::linalg::DistributedState::cached_inv_desc ( ) const
inline

Definition at line 1205 of file DistributedState.h.

1206 {
1207 return Inv_desc_;
1208 }

◆ cached_inv_local_entry()

double gpr::linalg::DistributedState::cached_inv_local_entry ( int global_row,
int global_col ) const
inline

Definition at line 1266 of file DistributedState.h.

1267 {
1268 assert(has_inv_);
1269 assert(owns_global_entry(global_row, global_col));
1270
1271 const int local_i =
1272 local_index_for_global(global_row, mb_, myrow_, nprow_);
1273 const int local_j =
1274 local_index_for_global(global_col, mb_, mycol_, npcol_);
1275 // The descriptor's leading dimension includes any storage padding.
1276 const int leading_dimension = Inv_desc_[8];
1277 return Inv_cache_[static_cast<size_t>(local_i) +
1278 static_cast<size_t>(local_j) * leading_dimension];
1279 }

◆ cached_inv_n()

int gpr::linalg::DistributedState::cached_inv_n ( ) const
inline

Definition at line 1209 of file DistributedState.h.

1210 {
1211 return Inv_n_;
1212 }

◆ cached_L()

const double * gpr::linalg::DistributedState::cached_L ( ) const
inline

Definition at line 1103 of file DistributedState.h.

1104 {
1105 return L_cache_.data();
1106 }

◆ cached_L_desc()

const int * gpr::linalg::DistributedState::cached_L_desc ( ) const
inline

Definition at line 1130 of file DistributedState.h.

1131 {
1132 return L_desc_;
1133 }

◆ cached_L_is_unowned()

bool gpr::linalg::DistributedState::cached_L_is_unowned ( ) const
inlinenodiscardnoexcept

Definition at line 1069 of file DistributedState.h.

1070 {
1071 return L_current() && L_owner_ == nullptr;
1072 }

◆ cached_L_lin()

const double * gpr::linalg::DistributedState::cached_L_lin ( ) const
inline

Definition at line 1165 of file DistributedState.h.

1166 {
1167 return L_lin_cache_.data();
1168 }

◆ cached_L_lin_desc()

const int * gpr::linalg::DistributedState::cached_L_lin_desc ( ) const
inline

Definition at line 1173 of file DistributedState.h.

1174 {
1175 return L_lin_desc_;
1176 }

◆ cached_L_lin_mut()

double * gpr::linalg::DistributedState::cached_L_lin_mut ( )
inline

Definition at line 1169 of file DistributedState.h.

1170 {
1171 return L_lin_cache_.data();
1172 }

◆ cached_L_mut()

double * gpr::linalg::DistributedState::cached_L_mut ( )
inline

Definition at line 1107 of file DistributedState.h.

1108 {
1109 has_L_lin_ = false;
1110 // The caller may write the factor, so the transpose is stale.
1111 has_U_ = false;
1112 return L_cache_.data();
1113 }

◆ cached_L_owned_by()

bool gpr::linalg::DistributedState::cached_L_owned_by ( const void * owner) const
inlinenodiscardnoexcept

Definition at line 1064 of file DistributedState.h.

1065 {
1066 return L_current() && owner != nullptr && L_owner_ == owner;
1067 }

◆ cached_lin_n()

int gpr::linalg::DistributedState::cached_lin_n ( ) const
inline

Definition at line 1177 of file DistributedState.h.

1178 {
1179 return L_lin_n_;
1180 }

◆ cached_n()

int gpr::linalg::DistributedState::cached_n ( ) const
inline

Definition at line 1134 of file DistributedState.h.

1135 {
1136 return L_n_;
1137 }

◆ cached_U()

const double * gpr::linalg::DistributedState::cached_U ( ) const
inline

Definition at line 1126 of file DistributedState.h.

1127 {
1128 return U_cache_.data();
1129 }

◆ claim_cached_L()

void gpr::linalg::DistributedState::claim_cached_L ( const void * owner)
inlinenoexcept

Definition at line 1059 of file DistributedState.h.

1060 {
1061 if (L_current()) L_owner_ = owner;
1062 }

◆ col_block_nb()

int gpr::linalg::DistributedState::col_block_nb ( int n) const
inline

Definition at line 470 of file DistributedState.h.

471 {
472 return (n + nprocs_ - 1) / nprocs_;
473 }

◆ col_block_range()

void gpr::linalg::DistributedState::col_block_range ( int n,
int & start,
int & count ) const
inline

Definition at line 477 of file DistributedState.h.

478 {
479 const int nb = col_block_nb(n);
480 start = col_mycol_ * nb;
481 if (start >= n) {
482 start = n;
483 count = 0;
484 } else {
485 count = std::min(nb, n - start);
486 }
487 }

◆ communication()

const std::shared_ptr< const MpiCommunicator > & gpr::linalg::DistributedState::communication ( ) const
inlinenodiscardnoexcept

Definition at line 123 of file DistributedState.h.

125 {
126 return communication_;
127 }

◆ compiled_backend()

constexpr DistributedBackendKind gpr::linalg::DistributedState::compiled_backend ( )
inlinestaticconstexprnoexcept

Definition at line 220 of file DistributedState.h.

221 {
223 }
constexpr DistributedBackendKind compiled_distributed_backend() noexcept

◆ compute_ictxt()

int gpr::linalg::DistributedState::compute_ictxt ( ) const
inline

Definition at line 504 of file DistributedState.h.

505 {
506 return ictxt_;
507 }

◆ configure()

void gpr::linalg::DistributedState::configure ( int block_size,
int nprow,
bool computation_tile = false )
inlinenoexcept

Fix the grid that initialize builds. A positive block_size or nprow overrides what callers request. With computation_tile, a non-positive block_size selects the automatic computation tile (recommended_block_size) wherever the block also sets the arithmetic: in SLATE builds and under the reproducible factorization. Block sizes callers pass to initialize then describe a data layout, such as an observation stride, and do not choose the tile. The choice follows the factorization selected when initialize runs.

Definition at line 262 of file DistributedState.h.

264 {
265 configured_block_size_ = block_size > 0 ? block_size : 0;
266 configured_nprow_ = nprow > 0 ? nprow : 0;
267 computation_tile_ = computation_tile;
268 }

◆ configured_block_size()

int gpr::linalg::DistributedState::configured_block_size ( ) const
inlinenodiscardnoexcept

Definition at line 270 of file DistributedState.h.

271 {
272 return configured_block_size_;
273 }

◆ configured_nprow()

int gpr::linalg::DistributedState::configured_nprow ( ) const
inlinenodiscardnoexcept

Definition at line 288 of file DistributedState.h.

289 {
290 return configured_nprow_;
291 }

◆ context()

int gpr::linalg::DistributedState::context ( ) const
inline

Definition at line 1282 of file DistributedState.h.

1283 {
1284 return ictxt_;
1285 }

◆ cpu_share()

int gpr::linalg::DistributedState::cpu_share ( ) const
inlinenodiscardnoexcept

CPUs of this process's affinity mask divided among the ranks of the group on this node whose masks overlap it, at least one; zero before initialize or where affinity is unknown. Unbound ranks launched on one node all see the whole mask and share it; ranks bound to disjoint CPUs keep their own.

Definition at line 442 of file DistributedState.h.

443 {
444 return cpu_share_;
445 }

◆ effective_configured_block_size()

int gpr::linalg::DistributedState::effective_configured_block_size ( ) const
inlinenodiscardnoexcept

Block size initialize takes over the caller's request; zero when the caller's request stands.

Definition at line 277 of file DistributedState.h.

278 {
279 if (configured_block_size_ > 0) return configured_block_size_;
280 const bool tile_sets_arithmetic =
282 reproducible();
283 if (computation_tile_ && tile_sets_arithmetic)
284 return recommended_block_size(0, nprocs_);
285 return 0;
286 }
static int recommended_block_size(int requested_block, int nprocs)
bool reproducible() const noexcept

◆ exceeds_lp64()

bool gpr::linalg::DistributedState::exceeds_lp64 ( const int * desc) const
inlinenodiscard

True when an LP64 ScaLAPACK routine cannot index the local arrays of desc on its process grid (Lp64Limits.h). The answer depends on the descriptor fields only, so every process of the grid agrees.

Definition at line 149 of file DistributedState.h.

150 {
151 int rows = 0, cols = 0;
152 descriptor_grid(desc, rows, cols);
153 return exceedsLp64(desc, rows, cols);
154 }
bool exceedsLp64(const int *desc, int nprow, int npcol) noexcept
Definition Lp64Limits.h:151

◆ factorization()

DistributedFactorization gpr::linalg::DistributedState::factorization ( ) const
inlinenodiscardnoexcept

Distributed factorization the solves of this state use. The selection is process-wide; see configureDistributedFactorization.

Definition at line 136 of file DistributedState.h.

137 {
139 }
DistributedFactorization distributedFactorization() noexcept
Process-wide selection; Reproducible unless configured otherwise.

◆ factorization_generation()

std::uint64_t gpr::linalg::DistributedState::factorization_generation ( )
inlinestaticnodiscardnoexcept

Generation of the factorization selection; a factor held outside this state records it and is not reused under another selection.

Definition at line 215 of file DistributedState.h.

216 {
218 }
std::uint64_t distributedFactorizationGeneration() noexcept

◆ gather()

EigenMatrix gpr::linalg::DistributedState::gather ( const double * local,
const int * desc,
int m,
int n,
bool broadcast_result = true ) const
inline

Definition at line 821 of file DistributedState.h.

823 {
824 return gather_block(local, desc, 0, 0, m, n, broadcast_result);
825 }
EigenMatrix gather_block(const double *local, const int *desc, int row0, int col0, int m, int n, bool broadcast_result=true) const

◆ gather_block()

EigenMatrix gpr::linalg::DistributedState::gather_block ( const double * local,
const int * desc,
int row0,
int col0,
int m,
int n,
bool broadcast_result = true ) const
inline

Definition at line 831 of file DistributedState.h.

834 {
835 EigenMatrix global;
836
837 if (nprocs_ == 1) {
838 global.resize(m, n);
839 for (int j = 0; j < n; ++j)
840 std::copy_n(
841 local + static_cast<std::size_t>(col0 + j) * desc[8] + row0,
842 m, global.data() + static_cast<std::size_t>(j) * m);
843 return global;
844 }
845
846 if (broadcast_result || is_root()) {
847 global.resize(m, n);
848 } else {
849 global = EigenMatrix::Zero(1, 1);
850 }
851
852 // Pack owned entries into bounded panels. Redistribution through a
853 // block descriptor can reserve an entire source block even when the
854 // requested panel contains only a few columns of that block.
855 int grid_rows = 0, grid_columns = 0, row = 0, column = 0;
856 Cblacs_gridinfo(desc[1], &grid_rows, &grid_columns, &row, &column);
857 if (n == 1) {
858 // A vector has one owner for each bit pattern. Integer OR moves
859 // those patterns in one collective, preserving signed zeros and
860 // non-finite payloads without floating-point arithmetic.
861 std::vector<std::uint64_t> bits(static_cast<std::size_t>(m), 0);
862 if ((col0 / desc[5] + desc[7]) % grid_columns == column) {
863 const std::size_t local_column =
864 (col0 / desc[5] / grid_columns) * desc[5] + col0 % desc[5];
865 for (int i = 0; i < m; ++i) {
866 const int global_row = row0 + i;
867 if ((global_row / desc[4] + desc[6]) % grid_rows != row)
868 continue;
869 const std::size_t local_row =
870 (global_row / desc[4] / grid_rows) * desc[4] +
871 global_row % desc[4];
872 bits[static_cast<std::size_t>(i)] =
873 std::bit_cast<std::uint64_t>(
874 local[local_column * desc[8] + local_row]);
875 }
876 }
877 if (broadcast_result) {
878 MPI_Allreduce(MPI_IN_PLACE, bits.data(), m, MPI_UINT64_T,
879 MPI_BOR, gpr_comm_);
880 } else if (is_root()) {
881 MPI_Reduce(MPI_IN_PLACE, bits.data(), m, MPI_UINT64_T, MPI_BOR,
882 0, gpr_comm_);
883 } else {
884 MPI_Reduce(bits.data(), nullptr, m, MPI_UINT64_T, MPI_BOR, 0,
885 gpr_comm_);
886 return EigenMatrix();
887 }
888 for (int i = 0; i < m; ++i)
889 global(i, 0) =
890 std::bit_cast<double>(bits[static_cast<std::size_t>(i)]);
891 return global;
892 }
893 const int coordinates[2] = {row, column};
894 std::vector<int> owners(2 * nprocs_);
895 MPI_Gather(coordinates, 2, MPI_INT, owners.data(), 2, MPI_INT, 0,
896 gpr_comm_);
897 // Local rows ascend with global rows, so the owned rows of the block
898 // are the contiguous local range [local_first, local_first + count).
899 const int row_end = row0 + m;
900 const int local_first =
901 numroc_(&row0, &desc[4], &row, &desc[6], &grid_rows);
902 const int local_count =
903 numroc_(&row_end, &desc[4], &row, &desc[6], &grid_rows) -
904 local_first;
905 std::vector<std::vector<int>> global_rows;
906 if (is_root()) {
907 global_rows.resize(grid_rows);
908 for (int i = row0; i < row_end; ++i)
909 global_rows[(i / desc[4] + desc[6]) % grid_rows].push_back(
910 i - row0);
911 }
912 constexpr int transfer_elements = 1024 * 1024;
913 const int panel_columns =
914 std::max(1, transfer_elements / std::max(1, m));
915 std::vector<double> send, receive;
916 std::vector<int> counts(nprocs_), offsets(nprocs_);
917 std::vector<int> column_counts(grid_columns);
918 double dummy = 0.0;
919 for (int first = 0; first < n;) {
920 const int columns = std::min(panel_columns, n - first);
921 std::fill(column_counts.begin(), column_counts.end(), 0);
922 for (int j = col0 + first; j < col0 + first + columns; ++j)
923 ++column_counts[(j / desc[5] + desc[7]) % grid_columns];
924 const int send_count = local_count * column_counts[column];
925 send.resize(send_count);
926 int at = 0;
927 for (int j = col0 + first; j < col0 + first + columns; ++j) {
928 if ((j / desc[5] + desc[7]) % grid_columns != column) continue;
929 const int local_column =
930 (j / desc[5] / grid_columns) * desc[5] + j % desc[5];
931 if (local_count > 0) {
932 std::copy_n(
933 local +
934 static_cast<std::size_t>(local_column) * desc[8] +
935 local_first,
936 local_count, send.data() + at);
937 at += local_count;
938 }
939 }
940 if (is_root()) {
941 int offset = 0;
942 for (int rank = 0; rank < nprocs_; ++rank) {
943 counts[rank] =
944 static_cast<int>(global_rows[owners[2 * rank]].size()) *
945 column_counts[owners[2 * rank + 1]];
946 offsets[rank] = offset;
947 offset += counts[rank];
948 }
949 assert(offset == m * columns);
950 receive.resize(offset);
951 }
952 MPI_Gatherv(send.empty() ? &dummy : send.data(), send_count,
953 MPI_DOUBLE, receive.empty() ? &dummy : receive.data(),
954 counts.data(), offsets.data(), MPI_DOUBLE, 0,
955 gpr_comm_);
956 if (is_root()) {
957 for (int rank = 0; rank < nprocs_; ++rank) {
958 int cursor = offsets[rank];
959 const auto& rows = global_rows[owners[2 * rank]];
960 const int owner_column = owners[2 * rank + 1];
961 for (int j = first; j < first + columns; ++j) {
962 if (((col0 + j) / desc[5] + desc[7]) % grid_columns !=
963 owner_column)
964 continue;
965 for (const int i: rows)
966 global(i, j) = receive[cursor++];
967 }
968 }
969 }
970 if (broadcast_result)
971 MPI_Bcast(global.data() + static_cast<std::size_t>(first) * m,
972 m * columns, MPI_DOUBLE, 0, gpr_comm_);
973 first += columns;
974 }
975 if (!broadcast_result && !is_root()) {
976 global.resize(0, 0);
977 }
978 return global;
979 }
void Cblacs_gridinfo(int a, int *b, int *c, int *d, int *e)
int numroc_(const int *n, const int *nb, const int *iproc, const int *isrcproc, const int *nprocs)
Eigen::Vector3d at(const FieldMatrixd &mat, Index_t i)
Eigen::Matrix< double, EigenDynamic, EigenDynamic, EigenMatrixStorage > EigenMatrix
Definition Macros.h:20

◆ gather_colvector_root()

Eigen::VectorXd gpr::linalg::DistributedState::gather_colvector_root ( const double * local,
const int * desc,
int m,
bool broadcast_result = true ) const
inline

Definition at line 985 of file DistributedState.h.

988 {
989 (void)desc;
990 if (m < 0)
991 throw std::invalid_argument(
992 "distributed column-vector gather requires a nonnegative size");
993 if (m == 0) return Eigen::VectorXd();
994 if (nprocs_ == 1) {
995 if (!local)
996 throw std::invalid_argument(
997 "distributed column-vector gather requires a source "
998 "buffer");
999 Eigen::VectorXd global(m);
1000 for (int i = 0; i < m; ++i)
1001 global(i) = local[i];
1002 return global;
1003 }
1004
1005 const bool owns_entries =
1006 mycol_ == 0 && local_cols(1) > 0 && local_rows(m) > 0;
1007 int missing_source = owns_entries && !local ? 1 : 0;
1008 int any_missing_source = 0;
1009 MPI_Allreduce(&missing_source, &any_missing_source, 1, MPI_INT, MPI_MAX,
1010 gpr_comm_);
1011 if (any_missing_source)
1012 throw std::invalid_argument(
1013 "distributed column-vector gather requires source buffers on "
1014 "ranks that own vector entries");
1015
1016 Eigen::VectorXd local_full = Eigen::VectorXd::Zero(m);
1017 if (mycol_ == 0 && local_cols(1) > 0) {
1018 for (int i = 0; i < m; ++i) {
1019 const int owner_row = (i / mb_) % nprow_;
1020 if (owner_row != myrow_) continue;
1021 const int local_i =
1022 local_index_for_global(i, mb_, myrow_, nprow_);
1023 local_full(i) = local[static_cast<size_t>(local_i)];
1024 }
1025 }
1026
1027 // Every entry has one owner; all other contributions are positive
1028 // zero. The reduction preserves each finite value independently of
1029 // its tree and needs no root buffer proportional to the rank count.
1030 if (broadcast_result) {
1031 MPI_Allreduce(MPI_IN_PLACE, local_full.data(), m, MPI_DOUBLE,
1032 MPI_SUM, gpr_comm_);
1033 return local_full;
1034 }
1035 if (is_root()) {
1036 MPI_Reduce(MPI_IN_PLACE, local_full.data(), m, MPI_DOUBLE, MPI_SUM,
1037 0, gpr_comm_);
1038 return local_full;
1039 }
1040 MPI_Reduce(local_full.data(), nullptr, m, MPI_DOUBLE, MPI_SUM, 0,
1041 gpr_comm_);
1042 return Eigen::VectorXd();
1043 }

◆ gather_lin_colvector_root()

Eigen::VectorXd gpr::linalg::DistributedState::gather_lin_colvector_root ( const double * local,
const int * desc,
int m ) const
inline

Definition at line 587 of file DistributedState.h.

589 {
590 (void)desc;
591 Eigen::VectorXd result = Eigen::VectorXd::Zero(m);
592 for (int i = 0; i < m; ++i) {
593 const int owner_row = (i / mb_) % nprocs_;
594 if (owner_row != lin_myrow_) continue;
595 const int local_i =
596 local_index_for_global(i, mb_, lin_myrow_, nprocs_);
597 result(i) = local[static_cast<size_t>(local_i)];
598 }
599 if (nprocs_ > 1) {
600 MPI_Allreduce(MPI_IN_PLACE, result.data(), m, MPI_DOUBLE, MPI_SUM,
601 gpr_comm_);
602 }
603 return result;
604 }

◆ global_index_for_local()

int gpr::linalg::DistributedState::global_index_for_local ( int local_index,
int block_size,
int proc_coord,
int nprocs_dim ) const
inline

Definition at line 674 of file DistributedState.h.

676 {
677 const int local_block = local_index / block_size;
678 const int intra = local_index % block_size;
679 const int global_block = local_block * nprocs_dim + proc_coord;
680 return global_block * block_size + intra;
681 }

◆ grid_generation()

std::uint64_t gpr::linalg::DistributedState::grid_generation ( ) const
inlinenodiscardnoexcept

Definition at line 129 of file DistributedState.h.

130 {
131 return grid_generation_;
132 }

◆ has_cached_inv()

bool gpr::linalg::DistributedState::has_cached_inv ( ) const
inline

Definition at line 1197 of file DistributedState.h.

1198 {
1199 return has_inv_ && Inv_generation_ == factorization_generation();
1200 }

◆ has_cached_L()

bool gpr::linalg::DistributedState::has_cached_L ( ) const
inline

Definition at line 1099 of file DistributedState.h.

1100 {
1101 return L_current();
1102 }

◆ has_cached_L_lin()

bool gpr::linalg::DistributedState::has_cached_L_lin ( ) const
inline

Definition at line 1161 of file DistributedState.h.

1162 {
1163 return has_L_lin_ && L_lin_generation_ == factorization_generation();
1164 }

◆ has_cached_U()

bool gpr::linalg::DistributedState::has_cached_U ( ) const
inlinenodiscardnoexcept

Definition at line 1117 of file DistributedState.h.

1118 {
1119 return L_current() && has_U_;
1120 }

◆ initialize()

void gpr::linalg::DistributedState::initialize ( int block_size = 0,
int nprow = 0 )
inline

Definition at line 294 of file DistributedState.h.

295 {
296 const int configured_block = effective_configured_block_size();
297 const int requested_block_size =
298 configured_block > 0 ? configured_block : block_size;
299 const int requested_nprow =
300 configured_nprow_ > 0 ? configured_nprow_ : nprow;
301 // Hot-path fast exit: when callers pass block_size=0 (the no-override
302 // default used by every per-iter site such as
303 // applyCrossCovarianceFunction and
304 // accumulateCrossCovarianceMatVecLocal), and we are already
305 // initialized, the requested grid cannot change. Skip all of
306 // MPI_Initialized + MPI_Comm_size + MPI_Comm_rank + the
307 // recommended-block / recommended-nprow math.
308 if (initialized_ &&
309 (requested_block_size <= 0 || requested_block_size == mb_) &&
310 (requested_nprow <= 0 || requested_nprow == nprow_)) {
311 return;
312 }
313
314 // Ensure MPI is up (BLACS piggybacks on MPI)
315 int mpi_up = 0;
316 MPI_Initialized(&mpi_up);
317 int provided = MPI_THREAD_SINGLE;
318 if (!mpi_up) {
319 if (MPI_Init_thread(nullptr, nullptr, requiredMpiThreadSupport(),
320 &provided) != MPI_SUCCESS)
321 throw std::runtime_error("could not initialize MPI");
322 owns_mpi_ = true;
323 } else if (MPI_Query_thread(&provided) != MPI_SUCCESS) {
324 throw std::runtime_error("could not inspect MPI thread support");
325 }
326 if (provided < requiredMpiThreadSupport()) {
327 if (owns_mpi_) {
328 MPI_Finalize();
329 owns_mpi_ = false;
330 }
331 throw std::runtime_error(mpiThreadSupportError());
332 }
333
334 if (!communication_)
335 communication_ = std::make_shared<MpiCommunicator>(MPI_COMM_WORLD);
336 const MpiContext process_group = communication_->view();
337 const int next_nprocs = process_group.nprocs;
338 const int next_myrank = process_group.rank;
339
340 if (requested_block_size > 0) {
341 block_size =
342 recommended_block_size(requested_block_size, next_nprocs);
343 } else if (block_size <= 0) {
344 block_size =
345 initialized_ ? mb_ : recommended_block_size(64, next_nprocs);
346 } else {
348 }
349
350 // Create BLACS process grid (as close to square as possible)
351 int next_nprow = recommended_nprow(next_nprocs, block_size);
352 if (requested_nprow > 0 && next_nprocs % requested_nprow == 0) {
353 next_nprow = requested_nprow;
354 }
355 const int next_npcol = next_nprocs / next_nprow;
356
357 if (initialized_) {
358 const bool same_grid = mb_ == block_size &&
359 nprocs_ == next_nprocs &&
360 myrank_ == next_myrank &&
361 nprow_ == next_nprow && npcol_ == next_npcol;
362 if (same_grid) return;
363
364 release_grids();
365 has_L_ = false;
366 L_owner_ = nullptr;
367 has_inv_ = false;
368 L_cache_.clear();
369 U_cache_.clear();
370 has_U_ = false;
371 Inv_cache_.clear();
372 L_n_ = 0;
373 Inv_n_ = 0;
374 L_lin_cache_.clear();
375 L_lin_n_ = 0;
376 has_L_lin_ = false;
377 desc_cache_.clear();
378 initialized_ = false;
379 }
380
381 nprocs_ = next_nprocs;
382 myrank_ = next_myrank;
383 mb_ = block_size;
384 nprow_ = next_nprow;
385 npcol_ = next_npcol;
386
387 gpr_comm_ = process_group.comm;
388 // BLACS interns system handles by MPI handle identity. A private
389 // duplicate gives this grid sole ownership of its system-table entry.
390 blacs_communication_ = std::make_unique<MpiCommunicator>(gpr_comm_);
391 system_handle_ = Csys2blacs_handle(blacs_communication_->view().comm);
392
393 ictxt_ = system_handle_;
394 Cblacs_gridinit(&ictxt_, "Row-major", nprow_, npcol_);
395 Cblacs_gridinfo(ictxt_, &nprow_, &npcol_, &myrow_, &mycol_);
396
397 // Linear (nprocs x 1) sub-context for deterministic distributed
398 // factorisations. With a single process column, pdpotrf / pdtrsm
399 // partition matrix rows across ranks in numerical order; column-wise
400 // sums stay local to one rank, and cross-rank reductions traverse
401 // ranks 0..nprocs-1 in fixed order. The dense factor / solve output
402 // is bit-stable across primary-grid topology choices, satisfying
403 // the ELPA-style invariant.
404 lin_ictxt_ = system_handle_;
405 Cblacs_gridinit(&lin_ictxt_, "Row-major", nprocs_, 1);
406 int lin_nprow = 0, lin_npcol = 0;
407 Cblacs_gridinfo(lin_ictxt_, &lin_nprow, &lin_npcol, &lin_myrow_,
408 &lin_mycol_);
409
410 // Column-block (1 x nprocs) grid: process j owns the j-th contiguous
411 // block of mode columns for the mode-distributed HS-SVD design build.
412 col_ictxt_ = system_handle_;
413 Cblacs_gridinit(&col_ictxt_, "Row-major", 1, nprocs_);
414 int col_nprow = 0, col_npcol = 0;
415 Cblacs_gridinfo(col_ictxt_, &col_nprow, &col_npcol, &col_myrow_,
416 &col_mycol_);
417
418 // Ranks sharing this node's memory, for the per-rank memory budget
419 // of large workspaces (MemoryBudget.h).
420 MPI_Comm node = MPI_COMM_NULL;
421 MPI_Comm_split_type(gpr_comm_, MPI_COMM_TYPE_SHARED, myrank_,
422 MPI_INFO_NULL, &node);
423 MPI_Comm_size(node, &node_ranks_);
424 cpu_share_ = node_cpu_share(node);
425 MPI_Comm_free(&node);
426
427 initialized_ = true;
428 ++grid_generation_;
429 }
int Csys2blacs_handle(MPI_Comm communicator)
void Cblacs_gridinit(int *a, const char *b, int c, int d)
int effective_configured_block_size() const noexcept
static int recommended_nprow(int nprocs, int block_size)

◆ instance()

DistributedState & gpr::linalg::DistributedState::instance ( )
inlinestatic

Definition at line 94 of file DistributedState.h.

95 {
96 static DistributedState inst;
97 return inst;
98 }
DistributedState(std::shared_ptr< const MpiCommunicator > communication)

◆ invalidate_derived_cholesky()

void gpr::linalg::DistributedState::invalidate_derived_cholesky ( )
inline

Definition at line 1219 of file DistributedState.h.

1220 {
1221 has_inv_ = false;
1222 has_L_lin_ = false;
1223 release(Inv_cache_);
1224 release(L_lin_cache_);
1225 }
static void release(std::vector< double > &buffer) noexcept
Returns the storage of buffer to the allocator.

◆ invalidate_inv()

void gpr::linalg::DistributedState::invalidate_inv ( )
inline

Definition at line 1213 of file DistributedState.h.

1214 {
1215 has_inv_ = false;
1216 release(Inv_cache_);
1217 }

◆ invalidate_L()

void gpr::linalg::DistributedState::invalidate_L ( )
inline

Definition at line 1138 of file DistributedState.h.

1139 {
1140 has_L_ = false;
1141 has_U_ = false;
1142 has_L_lin_ = false;
1143 L_owner_ = nullptr;
1144 // Each buffer is a share of an n x n matrix; held after invalidation
1145 // it sits beside the next factorization's covariance and factor.
1146 release(L_cache_);
1147 release(U_cache_);
1148 release(L_lin_cache_);
1149 }

◆ inverse_builds()

std::size_t gpr::linalg::DistributedState::inverse_builds ( ) const
inlinenodiscardnoexcept

Definition at line 1192 of file DistributedState.h.

1193 {
1194 return inverse_builds_;
1195 }

◆ is_root()

bool gpr::linalg::DistributedState::is_root ( ) const
inline

Definition at line 1330 of file DistributedState.h.

1331 {
1332 return myrank_ == 0;
1333 }

◆ lin_context()

int gpr::linalg::DistributedState::lin_context ( ) const
inline

Definition at line 1286 of file DistributedState.h.

1287 {
1288 return lin_ictxt_;
1289 }

◆ lin_local_cols()

int gpr::linalg::DistributedState::lin_local_cols ( int n) const
inline

Definition at line 509 of file DistributedState.h.

510 {
511 // Single column on the linear grid: every rank owns all n columns.
512 (void)n;
513 return n;
514 }

◆ lin_local_rows()

int gpr::linalg::DistributedState::lin_local_rows ( int m) const
inline

Definition at line 459 of file DistributedState.h.

460 {
461 int zero = 0;
462 return numroc_(&m, &mb_, &lin_myrow_, &zero, &nprocs_);
463 }

◆ lin_mycol()

int gpr::linalg::DistributedState::lin_mycol ( ) const
inline

Definition at line 1298 of file DistributedState.h.

1299 {
1300 return lin_mycol_;
1301 }

◆ lin_myrow()

int gpr::linalg::DistributedState::lin_myrow ( ) const
inline

Definition at line 1294 of file DistributedState.h.

1295 {
1296 return lin_myrow_;
1297 }

◆ local_cols()

int gpr::linalg::DistributedState::local_cols ( int n) const
inline

Definition at line 653 of file DistributedState.h.

654 {
655 int zero = 0;
656 return numroc_(&n, &mb_, &mycol_, &zero, &npcol_);
657 }

◆ local_index_for_global()

int gpr::linalg::DistributedState::local_index_for_global ( int global_index,
int block_size,
int proc_coord,
int nprocs_dim ) const
inline

Definition at line 659 of file DistributedState.h.

661 {
662 const int block = global_index / block_size;
663 const int intra = global_index % block_size;
664#ifndef NDEBUG
665 const int owner = block % nprocs_dim;
666 assert(owner == proc_coord);
667#else
668 (void)proc_coord;
669#endif
670 const int local_block = block / nprocs_dim;
671 return local_block * block_size + intra;
672 }

◆ local_rows()

int gpr::linalg::DistributedState::local_rows ( int m) const
inline

Definition at line 646 of file DistributedState.h.

647 {
648 int zero = 0;
649 return numroc_(&m, &mb_, &myrow_, &zero, &nprow_);
650 }

◆ make_col_desc()

void gpr::linalg::DistributedState::make_col_desc ( int m,
int n,
int * desc ) const
inline

Definition at line 493 of file DistributedState.h.

494 {
495 const int nb = col_block_nb(n);
496 int lld = std::max(1, m);
497 int info = 0;
498 int zero = 0;
499 descinit_(desc, &m, &n, &m, &nb, &zero, &zero, &col_ictxt_, &lld,
500 &info);
501 assert(info == 0);
502 }
void descinit_(int *desc, const int *m, const int *n, const int *mb, const int *nb, const int *irsrc, const int *icsrc, const int *ictxt, const int *lld, int *info)

◆ make_desc()

void gpr::linalg::DistributedState::make_desc ( int m,
int n,
int * desc ) const
inline

Definition at line 614 of file DistributedState.h.

615 {
616 // desc_cache_ is mutable and shared across threads on this
617 // singleton state. Today's call sites are all outside any
618 // OpenMP region, but since the kernel-assembly hot path could
619 // legitimately be wrapped in #pragma omp parallel for in the
620 // future (per the day-1 par_unseq target), guard the cache
621 // read/write with a mutex so the latent hazard cannot
622 // silently corrupt the descriptor vector. The lock is
623 // uncontended on the serial path (one acquire per descinit_
624 // call, ~10 ns); under multi-threaded callers it serialises
625 // cache access without affecting the cached descinit_ output.
626 std::lock_guard<std::mutex> guard(desc_cache_mutex_);
627 for (const auto& entry: desc_cache_) {
628 if (entry.m == m && entry.n == n) {
629 std::copy_n(entry.desc, 9, desc);
630 return;
631 }
632 }
633 int lld = std::max(1, local_rows(m));
634 int info = 0;
635 int zero = 0;
636 descinit_(desc, &m, &n, &mb_, &mb_, &zero, &zero, &ictxt_, &lld, &info);
637 assert(info == 0);
638 DescCacheEntry entry;
639 entry.m = m;
640 entry.n = n;
641 std::copy_n(desc, 9, entry.desc);
642 desc_cache_.push_back(entry);
643 }

◆ make_lin_desc()

void gpr::linalg::DistributedState::make_lin_desc ( int m,
int n,
int * desc ) const
inline

Definition at line 449 of file DistributedState.h.

450 {
451 int lld = std::max(1, lin_local_rows(m));
452 int info = 0;
453 int zero = 0;
454 descinit_(desc, &m, &n, &mb_, &mb_, &zero, &zero, &lin_ictxt_, &lld,
455 &info);
456 assert(info == 0);
457 }

◆ mycol()

int gpr::linalg::DistributedState::mycol ( ) const
inline

Definition at line 1314 of file DistributedState.h.

1315 {
1316 return mycol_;
1317 }

◆ myrank()

int gpr::linalg::DistributedState::myrank ( ) const
inline

Definition at line 1306 of file DistributedState.h.

1307 {
1308 return myrank_;
1309 }

◆ myrow()

int gpr::linalg::DistributedState::myrow ( ) const
inline

Definition at line 1310 of file DistributedState.h.

1311 {
1312 return myrow_;
1313 }

◆ node_ranks()

int gpr::linalg::DistributedState::node_ranks ( ) const
inlinenodiscardnoexcept

Ranks of this process group on this node (one before initialize).

Definition at line 432 of file DistributedState.h.

433 {
434 return node_ranks_;
435 }

◆ npcol()

int gpr::linalg::DistributedState::npcol ( ) const
inline

Definition at line 1322 of file DistributedState.h.

1323 {
1324 return npcol_;
1325 }

◆ nprocs()

int gpr::linalg::DistributedState::nprocs ( ) const
inline

Definition at line 1302 of file DistributedState.h.

1303 {
1304 return nprocs_;
1305 }

◆ nprow()

int gpr::linalg::DistributedState::nprow ( ) const
inline

Definition at line 1318 of file DistributedState.h.

1319 {
1320 return nprow_;
1321 }

◆ owns_global_entry()

bool gpr::linalg::DistributedState::owns_global_entry ( int global_row,
int global_col ) const
inline

Definition at line 1233 of file DistributedState.h.

1234 {
1235 const int owner_row = (global_row / mb_) % nprow_;
1236 const int owner_col = (global_col / mb_) % npcol_;
1237 return owner_row == myrow_ && owner_col == mycol_;
1238 }

◆ owns_lin_entry()

bool gpr::linalg::DistributedState::owns_lin_entry ( int global_row,
int global_col ) const
inline

Definition at line 1242 of file DistributedState.h.

1243 {
1244 (void)global_col;
1245 return (global_row / mb_) % nprocs_ == lin_myrow_;
1246 }

◆ recommended_block_size()

int gpr::linalg::DistributedState::recommended_block_size ( int requested_block,
int nprocs )
inlinestatic

Definition at line 235 of file DistributedState.h.

236 {
237 (void)nprocs;
238 if (requested_block <= 0) return 64;
239 return requested_block;
240 }

◆ recommended_nprow()

int gpr::linalg::DistributedState::recommended_nprow ( int nprocs,
int block_size )
inlinestatic

Definition at line 242 of file DistributedState.h.

243 {
244 (void)block_size;
245 if (nprocs <= 1) return 1;
246 int nprow = static_cast<int>(std::sqrt(static_cast<double>(nprocs)));
247 if (nprow < 1) nprow = 1;
248 while (nprocs % nprow != 0)
249 --nprow;
250 return nprow;
251 }

◆ release()

void gpr::linalg::DistributedState::release ( std::vector< double > & buffer)
inlinestaticnoexcept

Returns the storage of buffer to the allocator.

Definition at line 1228 of file DistributedState.h.

1229 {
1230 std::vector<double>().swap(buffer);
1231 }

◆ reproducible()

bool gpr::linalg::DistributedState::reproducible ( ) const
inlinenodiscardnoexcept

Definition at line 141 of file DistributedState.h.

142 {
144 }
DistributedFactorization factorization() const noexcept

◆ require_lp64()

void gpr::linalg::DistributedState::require_lp64 ( const int * desc,
const char * routine ) const
inline

Throws std::length_error, on every process alike, when routine has no tile counterpart and its LP64 offsets on desc would overflow.

Definition at line 188 of file DistributedState.h.

189 {
190 if (!exceeds_lp64(desc)) return;
191 int rows = 0, cols = 0;
192 descriptor_grid(desc, rows, cols);
193 throw std::length_error(
194 std::string(routine) + ": a " + std::to_string(desc[2]) + " x " +
195 std::to_string(desc[3]) + " matrix on a " + std::to_string(rows) +
196 " x " + std::to_string(cols) + " grid needs " +
197 std::to_string(largestLocalEntries(desc, rows, cols)) +
198 " local entries, past the " + std::to_string(lp64IndexLimit()) +
199 " an LP64 ScaLAPACK indexes; run on more processes");
200 }
bool exceeds_lp64(const int *desc) const
std::int64_t lp64IndexLimit() noexcept
Definition Lp64Limits.h:120
constexpr std::int64_t largestLocalEntries(std::int64_t m, std::int64_t n, std::int64_t mb, std::int64_t nb, std::int64_t rsrc, std::int64_t csrc, std::int64_t nprow, std::int64_t npcol) noexcept
Definition Lp64Limits.h:84

◆ restore_cached_L()

bool gpr::linalg::DistributedState::restore_cached_L ( CholeskyFactorSnapshot snapshot,
const void * owner )
inline

Definition at line 1090 of file DistributedState.h.

1091 {
1092 if (!snapshot.valid() || owner == nullptr) return false;
1093 cache_L(std::move(snapshot.values), snapshot.descriptor.data(),
1094 snapshot.order);
1095 L_owner_ = owner;
1096 return true;
1097 }
void cache_L(std::vector< double > L_dist, const int *desc, int n)

◆ scatter()

std::vector< double > gpr::linalg::DistributedState::scatter ( const EigenMatrix & global,
int m,
int n,
int * desc ) const
inline

Definition at line 686 of file DistributedState.h.

688 {
689 make_desc(m, n, desc);
690 int lr = local_rows(m);
691 int lc = local_cols(n);
692 std::vector<double> local(static_cast<size_t>(lr) * lc, 0.0);
693
694 if (nprocs_ == 1) {
695 // Single rank: just copy (no redistribution overhead)
696 assert(lr == m && lc == n);
697 if (global.size() > 0)
698 std::copy_n(global.data(), static_cast<size_t>(m) * n,
699 local.data());
700 return local;
701 }
702
703 // Build a single-process descriptor covering the full matrix on rank 0
704 int desc_g[9];
705 int lld_g = std::max(1, m);
706 int zero = 0;
707 int info = 0;
708 descinit_(desc_g, &m, &n, &m, &n, &zero, &zero, &ictxt_, &lld_g, &info);
709 assert(info == 0);
710
711 // Redistribute: rank-0 global -> distributed block-cyclic. Non-root
712 // ranks may keep the dense matrix empty when the caller only
713 // materializes the assembled covariance on the root rank.
714 double dummy = 0.0;
715 const bool root_has_data =
716 is_root() && global.size() == static_cast<Eigen::Index>(m) * n;
717 if (exceeds_lp64(desc_g) || exceeds_lp64(desc)) {
718 scatter_by_panels(root_has_data ? global.data() : nullptr, m, n,
719 desc, local.data(), myrow_, mycol_, nprow_,
720 npcol_);
721 return local;
722 }
723 const double* global_data = root_has_data ? global.data() : &dummy;
724 Cpdgemr2d(m, n, global_data, 1, 1, desc_g, local.data(), 1, 1, desc,
725 ictxt_);
726 return local;
727 }
void Cpdgemr2d(int m, int n, const double *a, int ia, int ja, const int *desca, double *b, int ib, int jb, const int *descb, int ictxt)
void scatter_by_panels(const double *global, int m, int n, const int *desc, double *local, int row, int col, int nprow, int npcol) const

◆ scatter_by_panels()

void gpr::linalg::DistributedState::scatter_by_panels ( const double * global,
int m,
int n,
const int * desc,
double * local,
int row,
int col,
int nprow,
int npcol ) const
inline

Definition at line 735 of file DistributedState.h.

738 {
739 constexpr int transfer_elements = 1024 * 1024;
740 const int panel_columns =
741 std::max(1, transfer_elements / std::max(1, m));
742 const int local_rows_here =
743 numroc_(&m, &desc[4], &row, &desc[6], &nprow);
744 std::vector<int> owned_rows(static_cast<std::size_t>(local_rows_here));
745 for (int il = 0; il < local_rows_here; ++il)
746 owned_rows[static_cast<std::size_t>(il)] =
747 ((il / desc[4]) * nprow + (row - desc[6] + nprow) % nprow) *
748 desc[4] +
749 il % desc[4];
750 std::vector<double> panel;
751 for (int first = 0; first < n; first += panel_columns) {
752 const int columns = std::min(panel_columns, n - first);
753 panel.assign(static_cast<std::size_t>(m) * columns, 0.0);
754 if (is_root() && global != nullptr)
755 std::copy_n(global + static_cast<std::size_t>(first) * m,
756 panel.size(), panel.data());
757 MPI_Bcast(panel.data(), static_cast<int>(panel.size()), MPI_DOUBLE,
758 0, gpr_comm_);
759 for (int j = first; j < first + columns; ++j) {
760 if ((j / desc[5] + desc[7]) % npcol != col) continue;
761 const int jl = (j / desc[5] / npcol) * desc[5] + j % desc[5];
762 double* target = local + static_cast<std::size_t>(jl) * desc[8];
763 const double* source =
764 panel.data() + static_cast<std::size_t>(j - first) * m;
765 for (int il = 0; il < local_rows_here; ++il)
766 target[il] =
767 source[owned_rows[static_cast<std::size_t>(il)]];
768 }
769 }
770 }
Panel panel(Real left, Real right, const Function &function, std::size_t &evaluations)

◆ scatter_colvector_root()

std::vector< double > gpr::linalg::DistributedState::scatter_colvector_root ( const Eigen::VectorXd & global,
int m,
int * desc ) const
inline

Definition at line 785 of file DistributedState.h.

787 {
788 make_desc(m, 1, desc);
789 const int lr = local_rows(m);
790 const int lc = local_cols(1);
791 std::vector<double> local(static_cast<size_t>(lr) * lc, 0.0);
792
793 if (nprocs_ == 1) {
794 assert(lr == m && lc == 1);
795 if (global.size() > 0)
796 std::copy_n(global.data(), static_cast<size_t>(m),
797 local.data());
798 return local;
799 }
800
801 Eigen::VectorXd replicated =
802 is_root() ? global : Eigen::VectorXd::Zero(m);
803 if (replicated.size() != m) replicated.resize(m);
804 MPI_Bcast(replicated.data(), m, MPI_DOUBLE, 0, gpr_comm_);
805
806 if (lc == 0 || mycol_ != 0) return local;
807
808 for (int i = 0; i < m; ++i) {
809 const int owner_row = (i / mb_) % nprow_;
810 if (owner_row != myrow_) continue;
811 const int local_i = local_index_for_global(i, mb_, myrow_, nprow_);
812 local[static_cast<size_t>(local_i)] = replicated(i);
813 }
814 return local;
815 }

◆ scatter_lin()

std::vector< double > gpr::linalg::DistributedState::scatter_lin ( const EigenMatrix & global,
int m,
int n,
int * desc ) const
inline

Definition at line 520 of file DistributedState.h.

522 {
523 make_lin_desc(m, n, desc);
524 const int lr = lin_local_rows(m);
525 const int lc = lin_local_cols(n);
526 std::vector<double> local(static_cast<size_t>(lr) * lc, 0.0);
527
528 int desc_g[9];
529 int lld_g = std::max(1, m);
530 int zero = 0;
531 int info = 0;
532 descinit_(desc_g, &m, &n, &m, &n, &zero, &zero, &lin_ictxt_, &lld_g,
533 &info);
534 assert(info == 0);
535
536 double dummy = 0.0;
537 const bool root_has_data =
538 lin_myrow_ == 0 && lin_mycol_ == 0 &&
539 global.size() == static_cast<Eigen::Index>(m) * n;
540 if (exceeds_lp64(desc_g) || exceeds_lp64(desc)) {
541 // Grid process (0, 0) of the linear grid is world rank 0.
542 scatter_by_panels(root_has_data ? global.data() : nullptr, m, n,
543 desc, local.data(), lin_myrow_, 0, nprocs_, 1);
544 return local;
545 }
546 const double* global_data = root_has_data ? global.data() : &dummy;
547 Cpdgemr2d(m, n, global_data, 1, 1, desc_g, local.data(), 1, 1, desc,
548 lin_ictxt_);
549 return local;
550 }
void make_lin_desc(int m, int n, int *desc) const

◆ scatter_lin_colvector_root()

std::vector< double > gpr::linalg::DistributedState::scatter_lin_colvector_root ( const Eigen::VectorXd & global,
int m,
int * desc ) const
inline

Definition at line 557 of file DistributedState.h.

559 {
560 make_lin_desc(m, 1, desc);
561 const int lr = lin_local_rows(m);
562 std::vector<double> local(static_cast<size_t>(lr), 0.0);
563
564 Eigen::VectorXd replicated =
565 is_root() ? global : Eigen::VectorXd::Zero(m);
566 if (replicated.size() != m) replicated.resize(m);
567 if (nprocs_ > 1) {
568 MPI_Bcast(replicated.data(), m, MPI_DOUBLE, 0, gpr_comm_);
569 }
570
571 for (int i = 0; i < m; ++i) {
572 const int owner_row = (i / mb_) % nprocs_;
573 if (owner_row != lin_myrow_) continue;
574 const int local_i =
575 local_index_for_global(i, mb_, lin_myrow_, nprocs_);
576 local[static_cast<size_t>(local_i)] = replicated(i);
577 }
578 return local;
579 }

◆ set_factorization()

void gpr::linalg::DistributedState::set_factorization ( DistributedFactorization mode)
inline

Select the factorization and drop every cached factor, transpose, replica and inverse. Caches of other states compare the generation they were computed under and read as absent after the change.

Definition at line 205 of file DistributedState.h.

206 {
207 if (mode == factorization()) return;
209 invalidate_L();
211 }
void configureDistributedFactorization(DistributedFactorization mode)

◆ take_cached_L()

CholeskyFactorSnapshot gpr::linalg::DistributedState::take_cached_L ( const void * owner)
inlinenodiscard

Definition at line 1074 of file DistributedState.h.

1075 {
1076 CholeskyFactorSnapshot snapshot;
1077 if (!cached_L_owned_by(owner)) return snapshot;
1078 snapshot.values = std::move(L_cache_);
1079 std::copy_n(L_desc_, 9, snapshot.descriptor.begin());
1080 snapshot.order = L_n_;
1081 has_L_ = false;
1082 L_owner_ = nullptr;
1083 L_n_ = 0;
1084 has_inv_ = false;
1085 has_L_lin_ = false;
1086 has_U_ = false;
1087 return snapshot;
1088 }
bool cached_L_owned_by(const void *owner) const noexcept

◆ tile_communicators()

TileCommunicators gpr::linalg::DistributedState::tile_communicators ( int context = -1)
inline

Definition at line 1343 of file DistributedState.h.

1344 {
1345 const bool linear = context == lin_ictxt_;
1346 if (context >= 0 && context != ictxt_ && !linear)
1347 throw std::invalid_argument(
1348 "tile communicator needs an active grid");
1349 auto& row_comm = linear ? lin_tile_row_comm_ : tile_row_comm_;
1350 auto& col_comm = linear ? lin_tile_col_comm_ : tile_col_comm_;
1351 auto& rank_of = linear ? lin_tile_rank_of_ : tile_rank_of_;
1352 const int row = linear ? lin_myrow_ : myrow_;
1353 const int col = linear ? lin_mycol_ : mycol_;
1354 const int columns = linear ? 1 : npcol_;
1355 if (row_comm == MPI_COMM_NULL) {
1356 MPI_Comm_split(gpr_comm_, row, col, &row_comm);
1357 MPI_Comm_split(gpr_comm_, col, row, &col_comm);
1358 std::vector<int> coordinates(2 * static_cast<std::size_t>(nprocs_));
1359 const int mine[2] = {row, col};
1360 MPI_Allgather(mine, 2, MPI_INT, coordinates.data(), 2, MPI_INT,
1361 gpr_comm_);
1362 rank_of.assign(static_cast<std::size_t>(nprocs_), -1);
1363 for (int r = 0; r < nprocs_; ++r)
1364 rank_of[static_cast<std::size_t>(coordinates[2 * r]) * columns +
1365 coordinates[2 * r + 1]] = r;
1366 }
1367 return {row_comm, col_comm, rank_of};
1368 }

◆ tile_kernels()

bool gpr::linalg::DistributedState::tile_kernels ( const int * desc,
const char * routine,
const int * other = nullptr ) const
inlinenodiscard

True when a factorization, substitution or inverse on desc (and on the second operand other, when given) runs on the tile kernels: always under the reproducible selection, and under the library selection when the LP64 ScaLAPACK routine would overflow its element offsets on either operand. The tile kernels index in 64 bits. The first rerouted call of a process prints one line on the root rank.

Definition at line 162 of file DistributedState.h.

164 {
165 if (reproducible()) return true;
166 if (other != nullptr && !exceeds_lp64(desc) && exceeds_lp64(other))
167 desc = other;
168 if (!exceeds_lp64(desc)) return false;
169 static std::atomic<bool> reported{false};
170 if (is_root() && !reported.exchange(true)) {
171 int rows = 0, cols = 0;
172 descriptor_grid(desc, rows, cols);
173 std::fprintf(
174 stderr,
175 "[gpr::linalg] library factorization: %s on order %d needs "
176 "%lld local entries on a %d x %d grid, past the %lld an "
177 "LP64 ScaLAPACK indexes; operations on this order run on "
178 "the tile kernels\n",
179 routine, desc[2],
180 static_cast<long long>(largestLocalEntries(desc, rows, cols)),
181 rows, cols, static_cast<long long>(lp64IndexLimit()));
182 }
183 return true;
184 }

◆ world_comm()

MPI_Comm gpr::linalg::DistributedState::world_comm ( ) const
inline

Definition at line 1290 of file DistributedState.h.

1291 {
1292 return gpr_comm_;
1293 }

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