25#ifndef __BLACS_GRID_HPP__
26#define __BLACS_GRID_HPP__
34#include "dlaf_c/grid.h"
47 std::unique_ptr<mpi::Grid> mpi_grid_;
50#ifdef SIRIUS_SCALAPACK
51 int blacs_handler_{-1};
54 int blacs_context_{-1};
56 std::vector<int> rank_map_;
67 mpi_grid_ = std::make_unique<mpi::Grid>(std::vector<int>({num_ranks_row__, num_ranks_col__}), comm_);
68 rank_map_.resize(num_ranks_row__ * num_ranks_col__);
70#ifdef SIRIUS_SCALAPACK
74 for (
int j = 0; j < num_ranks_col__; j++) {
75 for (
int i = 0; i < num_ranks_row__; i++) {
76 rank_map_[i + j * num_ranks_row__] = mpi_grid_->communicator().cart_rank({i, j});
81 blacs_context_ = blacs_handler_;
82 linalg_base::gridmap(&blacs_context_, &rank_map_[0], num_ranks_row__, num_ranks_row__, num_ranks_col__);
85 int nrow1, ncol1, irow1, icol1;
86 linalg_base::gridinfo(blacs_context_, &nrow1, &ncol1, &irow1, &icol1);
88 if (rank_row() != irow1 || rank_col() != icol1 || num_ranks_row() != nrow1 || num_ranks_col() != ncol1) {
90 s <<
"wrong grid" << std::endl
91 <<
" row | col | nrow | ncol " << std::endl
92 <<
" mpi_grid " << rank_row() <<
" " << rank_col() <<
" " << num_ranks_row() <<
" " << num_ranks_col()
94 <<
" blacs " << irow1 <<
" " << icol1 <<
" " << nrow1 <<
" " << ncol1;
99 dlaf_create_grid_from_blacs(blacs_context_);
103 for (
int i = 0; i < static_cast<int>(rank_map_.size()); i++) {
107 blacs_context_ = dlaf_create_grid(comm_.
native(), num_ranks_row__, num_ranks_col__,
'R');
116 MPI_Finalized(&mpi_finalized);
117 if (mpi_finalized == 0) {
118#ifdef SIRIUS_SCALAPACK
124 dlaf_free_grid(blacs_context_);
128 inline int context()
const
130 return blacs_context_;
133 inline auto const& comm()
const
138 inline auto const& comm_row()
const
140 return mpi_grid_->communicator(1 << 0);
143 inline auto const& comm_col()
const
145 return mpi_grid_->communicator(1 << 1);
148 inline int num_ranks_row()
const
150 return comm_row().size();
153 inline int rank_row()
const
155 return comm_row().rank();
158 inline int num_ranks_col()
const
160 return comm_col().size();
163 inline int rank_col()
const
165 return comm_col().rank();
168 inline int cart_rank(
int irow__,
int icol__)
const
170 return mpi_grid_->communicator().cart_rank({irow__, icol__});
173 auto const& mpi_grid()
const
178 auto const& rank_map()
const
static void gridmap(int *blacs_context, int *map, int ld, int nrow, int ncol)
Create BLACS context for the grid of MPI ranks.
static void free_blacs_handler(int blacs_handler)
Free BLACS handler.
static int create_blacs_handler(MPI_Comm comm)
Create BLACS handler.
static void gridexit(int blacs_context)
Destroy BLACS context.
MPI communicator wrapper.
MPI_Comm native() const
Return the native raw MPI communicator handler.
Basic interface to linear algebra functions.
Contains declaration and implementation of sddk::MPI_grid class.
Namespace of the SIRIUS library.
Eror and warning handling during run-time execution.