25#ifndef __DMATRIX_HPP__
26#define __DMATRIX_HPP__
29#include <spla/spla.hpp>
30#include <costa/layout.hpp>
31#include <costa/grid2grid/transformer.hpp>
46std::ostream&
operator<<(std::ostream& out, std::complex<T> z)
48 out << z.real() <<
" + I*" << z.imag();
83 spla::MatrixDistribution
spla_dist_{spla::MatrixDistribution::create_mirror(MPI_COMM_SELF)};
85 costa::grid_layout<T> grid_layout_;
90#ifdef SIRIUS_SCALAPACK
94 grid_layout_ = costa::block_cyclic_layout<T>(this->
num_rows(), this->num_cols(), this->
bs_row(),
95 this->
bs_col(), 1, 1, this->
num_rows(), this->num_cols(), this->blacs_grid().num_ranks_row(),
96 this->blacs_grid().num_ranks_col(),
'R', 0, 0, this->at(sddk::memory_t::host), this->
ld(),
'C',
97 this->blacs_grid().comm().rank());
102 dmatrix(dmatrix<T>
const& src) =
delete;
104 dmatrix<T>& operator=(dmatrix<T>
const& src) =
delete;
112 dmatrix(
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
int bs_col__,
113 sddk::memory_t mem_type__ = sddk::memory_t::host);
115 dmatrix(
int num_rows__,
int num_cols__, sddk::memory_t mem_type__ = sddk::memory_t::host);
117 dmatrix(
int num_rows__,
int num_cols__, sddk::memory_pool& mp__, std::string
const& label__ =
"");
119 dmatrix(T* ptr__,
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
int bs_col__);
121 dmatrix(
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
int bs_col__,
122 sddk::memory_pool& mp__);
124 dmatrix(T* ptr__,
int num_rows__,
int num_cols__);
126 dmatrix(dmatrix<T>&& src) =
default;
128 dmatrix<T>& operator=(dmatrix<T>&& src) =
default;
139 inline int size_local()
const
163 inline int irow(
int irow_loc)
const
168 inline int num_cols()
const
185 inline int icol(
int icol_loc)
const
190 inline int const* descriptor()
const
195 inline spla::MatrixDistribution& spla_distribution()
223 void copy_to(sddk::memory_t mem__,
int ir0__,
int ic0__,
int nr__,
int nc__)
227 splindex_block_cyclic<> spl_r0(ir0__,
n_blocks(blacs_grid().num_ranks_row()),
229 splindex_block_cyclic<> spl_r1(ir0__ + nr__,
n_blocks(blacs_grid().num_ranks_row()),
232 splindex_block_cyclic<> spl_c0(ic0__,
n_blocks(blacs_grid().num_ranks_col()),
234 splindex_block_cyclic<> spl_c1(ic0__ + nc__,
n_blocks(blacs_grid().num_ranks_col()),
237 m0 = spl_r0.local_size();
238 m1 = spl_r1.local_size();
239 n0 = spl_c0.local_size();
240 n1 = spl_c1.local_size();
250 this->at(sddk::memory_t::device, m0, n0), this->
ld(), m1 - m0, n1 - n0);
253 acc::copyin(this->at(sddk::memory_t::device, m0, n0), this->
ld(),
254 this->at(sddk::memory_t::host, m0, n0), this->
ld(), m1 - m0, n1 - n0);
258 void set(
int ir0__,
int jc0__,
int mr__,
int nc__, T* ptr__,
int ld__);
260 void set(
const int irow_glob,
const int icol_glob, T val);
262 void add(
const int irow_glob,
const int icol_glob, T val);
264 void add(real_type<T> beta__,
const int irow_glob,
const int icol_glob, T val);
266 void make_real_diag(
int n__);
268 sddk::mdarray<T, 1> get_diag(
int n__);
270 inline auto const& spl_col()
const
275 inline auto const& spl_row()
const
280 inline int rank_row()
const
285 inline int num_ranks_row()
const
290 inline int rank_col()
const
295 inline int num_ranks_col()
const
312 inline auto const& blacs_grid()
const
318 void save_to_hdf5(std::string name__,
int m__,
int n__);
320 auto get_full_matrix()
const
327 full_mtrx(
irow(i),
icol(j)) = (*this)(i, j);
331 blacs_grid_->comm().allreduce(full_mtrx.at(sddk::memory_t::host),
static_cast<int>(full_mtrx.size()));
336 nlohmann::json serialize_to_json(
int m__,
int n__)
const
338 auto full_mtrx = get_full_matrix();
341 dict[
"mtrx_re"] = nlohmann::json::array();
342 for (
int i = 0; i <
num_rows(); i++) {
343 dict[
"mtrx_re"].push_back(nlohmann::json::array());
344 for (
int j = 0; j < num_cols(); j++) {
345 dict[
"mtrx_re"][i].push_back(std::real(full_mtrx(i, j)));
348 if (!std::is_scalar<T>::value) {
349 dict[
"mtrx_im"] = nlohmann::json::array();
350 for (
int i = 0; i <
num_rows(); i++) {
351 dict[
"mtrx_im"].push_back(nlohmann::json::array());
352 for (
int j = 0; j < num_cols(); j++) {
353 dict[
"mtrx_im"][i].push_back(std::imag(full_mtrx(i, j)));
360 std::stringstream serialize(std::string name__,
int m__,
int n__)
const
362 auto full_mtrx = get_full_matrix();
364 std::stringstream out;
366 out << std::setprecision(12) << std::setw(24) << std::fixed;
368 out <<
"matrix label : " << name__ << std::endl;
369 out <<
"{" << std::endl;
370 for (
int i = 0; i < m__; i++) {
372 for (
int j = 0; j < n__; j++) {
373 out << full_mtrx(i, j);
379 out <<
"}," << std::endl;
381 out <<
"}" << std::endl;
389 inline T checksum(
int m__,
int n__)
const
394 splindex_block_cyclic<> spl_row(m__,
n_blocks(this->blacs_grid().num_ranks_row()),
396 splindex_block_cyclic<> spl_col(n__,
n_blocks(this->blacs_grid().num_ranks_col()),
398 for (
int i = 0; i < spl_col.local_size(); i++) {
399 for (
int j = 0; j < spl_row.local_size(); j++) {
403 this->blacs_grid().comm().allreduce(&cs, 1);
405 for (
int i = 0; i < n__; i++) {
406 for (
int j = 0; j < m__; j++) {
414 inline auto const& comm()
const
417 return blacs_grid().comm();
419 return mpi::Communicator::self();
423 costa::grid_layout<T>& grid_layout()
428 costa::grid_layout<T> grid_layout(
int irow0__,
int jcol0__,
int mrow__,
int ncol__)
430 return costa::block_cyclic_layout<T>(this->
num_rows(), this->num_cols(), this->
bs_row(),
431 this->
bs_col(), irow0__ + 1, jcol0__ + 1, mrow__, ncol__, this->blacs_grid().num_ranks_row(),
432 this->blacs_grid().num_ranks_col(),
'R', 0, 0, this->at(sddk::memory_t::host), this->
ld(),
'C',
433 this->blacs_grid().comm().rank());
Contains declaration and implementation of sddk::BLACS_grid class.
int icol(int icol_loc) const
Inindex of column in global matrix.
int num_cols_
Global number of matrix columns.
int num_rows_local() const
Return local number of rows for this MPI rank.
int num_rows_
Global number of matrix rows.
int size() const
Return size of the square matrix or -1 in case of rectangular matrix.
ftn_int descriptor_[9]
ScaLAPACK matrix descriptor.
spla::MatrixDistribution spla_dist_
Matrix distribution used for SPLA library functions.
int num_rows() const
Return number of rows in the global matrix.
sirius::splindex_block_cyclic spl_col_
Split index of matrix columns.
sirius::splindex_block_cyclic spl_row_
Split index of matrix rows.
int bs_row() const
Row blocking factor.
int num_cols_local() const
Local number of columns.
int bs_col() const
Column blocking factor.
int bs_row_
Row block size.
int bs_col_
Column block size.
BLACS_grid const * blacs_grid_
BLACS grid.
int num_rows_local(int rank) const
Return local number of rows for a given MPI rank.
int irow(int irow_loc) const
Return global row index in the range [0, num_rows) by the local index in the range [0,...
void copy_to(memory_t mem__, size_t idx0__, size_t n__, acc::stream_id sid=acc::stream_id(-1))
Copy n elements starting from idx0 from one memory type to another.
uint32_t ld() const
Return leading dimension size.
Index_t::global global_index(typename Index_t::local idxloc__, block_id block_id__) const
Return global index of an element by local index and block id.
value_type local_size(block_id block_id__) const
Return local size of the split index for a given block.
Contains definition and implementation of sirius::HDF5_tree class.
Interface to nlohmann::json library and helper functions.
Memory management functions and classes.
bool is_device_memory(memory_t mem__)
Check if this is a valid device memory (memory, accessible by the device).
bool is_host_memory(memory_t mem__)
Check if this is a valid host memory (memory, accessible by the host).
void copyout(T *target__, T const *source__, size_t n__)
Copy memory from device to host.
void copyin(T *target__, T const *source__, size_t n__)
Copy memory from host to device.
Namespace of the SIRIUS library.
std::ostream & operator<<(std::ostream &out, hbar &&b)
Inject horisontal bar to ostream.
strong_type< int, struct __block_id_tag > block_id
ID of the block.
strong_type< int, struct __n_blocks_tag > n_blocks
Number of blocks to which the global index is split.
Eror and warning handling during run-time execution.
Contains definition of sddk::splindex_base and specializations of sddk::splindex class.
Contains typedefs, enums and simple descriptors.