32dmatrix<T>::dmatrix(
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
int bs_col__,
33 sddk::memory_t mem_type__)
34 : sddk::matrix<T>(splindex_block_cyclic<>(num_rows__,
n_blocks(blacs_grid__.num_ranks_row()),
35 block_id(blacs_grid__.rank_row()), bs_row__).local_size(),
36 splindex_block_cyclic<>(num_cols__,
n_blocks(blacs_grid__.num_ranks_col()),
37 block_id(blacs_grid__.rank_col()), bs_col__).local_size(), mem_type__)
38 , num_rows_(num_rows__)
39 , num_cols_(num_cols__)
42 , blacs_grid_(&blacs_grid__)
43 , spl_row_(num_rows_,
n_blocks(blacs_grid__.num_ranks_row()),
block_id(blacs_grid__.rank_row()), bs_row_)
44 , spl_col_(num_cols_,
n_blocks(blacs_grid__.num_ranks_col()),
block_id(blacs_grid__.rank_col()), bs_col_)
45 , spla_dist_(
spla::MatrixDistribution::create_blacs_block_cyclic_from_mapping(
46 blacs_grid__.comm().native(), blacs_grid__.rank_map().data(), blacs_grid__.num_ranks_row(),
47 blacs_grid__.num_ranks_col(), bs_row__,bs_col__))
53dmatrix<T>::dmatrix(T* ptr__,
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
55 : sddk::matrix<T>(ptr__,
56 splindex_block_cyclic<>(num_rows__,
n_blocks(blacs_grid__.num_ranks_row()),
57 block_id(blacs_grid__.rank_row()), bs_row__).local_size(),
58 splindex_block_cyclic<>(num_cols__,
n_blocks(blacs_grid__.num_ranks_col()),
59 block_id(blacs_grid__.rank_col()), bs_col__).local_size())
60 , num_rows_(num_rows__)
61 , num_cols_(num_cols__)
64 , blacs_grid_(&blacs_grid__)
65 , spl_row_(num_rows_,
n_blocks(blacs_grid__.num_ranks_row()),
block_id(blacs_grid__.rank_row()), bs_row_)
66 , spl_col_(num_cols_,
n_blocks(blacs_grid__.num_ranks_col()),
block_id(blacs_grid__.rank_col()), bs_col_)
67 , spla_dist_(
spla::MatrixDistribution::create_blacs_block_cyclic_from_mapping(
68 blacs_grid__.comm().native(), blacs_grid__.rank_map().data(), blacs_grid__.num_ranks_row(),
69 blacs_grid__.num_ranks_col(), bs_row__, bs_col__))
75dmatrix<T>::dmatrix(
int num_rows__,
int num_cols__, sddk::memory_t mem_type__)
76 : sddk::matrix<T>(num_rows__, num_cols__, mem_type__)
77 , num_rows_(num_rows__)
78 , num_cols_(num_cols__)
87dmatrix<T>::dmatrix(
int num_rows__,
int num_cols__, sddk::memory_pool& mp__, std::string
const& label__)
88 : sddk::matrix<T>(num_rows__, num_cols__, mp__, label__)
89 , num_rows_(num_rows__)
90 , num_cols_(num_cols__)
100dmatrix<T>::dmatrix(T* ptr__,
int num_rows__,
int num_cols__)
101 : sddk::matrix<T>(ptr__, num_rows__, num_cols__)
102 , num_rows_(num_rows__)
103 , num_cols_(num_cols__)
113dmatrix<T>::dmatrix(
int num_rows__,
int num_cols__, BLACS_grid
const& blacs_grid__,
int bs_row__,
int bs_col__,
114 sddk::memory_pool& mp__)
115 : sddk::matrix<T>(splindex_block_cyclic<>(num_rows__,
n_blocks(blacs_grid__.num_ranks_row()),
116 block_id(blacs_grid__.rank_row()), bs_row__).local_size(),
117 splindex_block_cyclic<>(num_cols__,
n_blocks(blacs_grid__.num_ranks_col()),
118 block_id(blacs_grid__.rank_col()), bs_col__).local_size(), mp__)
119 , num_rows_(num_rows__)
120 , num_cols_(num_cols__)
123 , blacs_grid_(&blacs_grid__)
124 , spl_row_(num_rows_,
n_blocks(blacs_grid__.num_ranks_row()),
block_id(blacs_grid__.rank_row()), bs_row_)
125 , spl_col_(num_cols_,
n_blocks(blacs_grid__.num_ranks_col()),
block_id(blacs_grid__.rank_col()), bs_col_)
126 , spla_dist_(
spla::MatrixDistribution::create_blacs_block_cyclic_from_mapping(
127 blacs_grid__.comm().native(), blacs_grid__.rank_map().data(), blacs_grid__.num_ranks_row(),
128 blacs_grid__.num_ranks_col(), bs_row__, bs_col__))
134void dmatrix<T>::set(
int ir0__,
int jc0__,
int mr__,
int nc__, T* ptr__,
int ld__)
136 splindex_block_cyclic<> spl_r0(ir0__,
n_blocks(blacs_grid().num_ranks_row()),
137 block_id(blacs_grid().rank_row()), bs_row_);
138 splindex_block_cyclic<> spl_r1(ir0__ + mr__,
n_blocks(blacs_grid().num_ranks_row()),
139 block_id(blacs_grid().rank_row()), bs_row_);
141 splindex_block_cyclic<> spl_c0(jc0__,
n_blocks(blacs_grid().num_ranks_col()),
142 block_id(blacs_grid().rank_col()), bs_col_);
143 splindex_block_cyclic<> spl_c1(jc0__ + nc__,
n_blocks(blacs_grid().num_ranks_col()),
144 block_id(blacs_grid().rank_col()), bs_col_);
146 int m0 = spl_r0.local_size();
147 int m1 = spl_r1.local_size();
148 int n0 = spl_c0.local_size();
149 int n1 = spl_c1.local_size();
150 std::vector<int> map_row(m1 - m0);
151 std::vector<int> map_col(n1 - n0);
153 for (
int i = 0; i < m1 - m0; i++) {
154 map_row[i] = spl_r1.global_index(m0 + i) - ir0__;
156 for (
int j = 0; j < n1 - n0; j++) {
157 map_col[j] = spl_c1.global_index(n0 + j) - jc0__;
161 for (
int j = 0; j < n1 - n0; j++) {
162 for (
int i = 0; i < m1 - m0; i++) {
163 (*this)(m0 + i, n0 + j) = ptr__[map_row[i] + ld__ * map_col[j]];
169void dmatrix<T>::set(
const int irow_glob,
const int icol_glob, T val)
172 auto r = spl_row_.location(irow_glob);
173 if (blacs_grid_->rank_row() == r.ib) {
174 auto c = spl_col_.location(icol_glob);
175 if (blacs_grid_->rank_col() == c.ib) {
176 (*this)(r.index_local, c.index_local) = val;
180 (*this)(irow_glob, icol_glob) = val;
185void dmatrix<T>::add(
const int irow_glob,
const int icol_glob, T val)
187 auto r = spl_row_.location(irow_glob);
188 if (blacs_grid_->rank_row() == r.ib) {
189 auto c = spl_col_.location(icol_glob);
190 if (blacs_grid_->rank_col() == c.ib) {
191 (*this)(r.index_local, c.index_local) += val;
197void dmatrix<T>::add(real_type<T> beta__,
const int irow_glob,
const int icol_glob, T val)
199 auto r = spl_row_.location(irow_glob);
200 if (blacs_grid_->rank_row() == r.ib) {
201 auto c = spl_col_.location(icol_glob);
202 if (blacs_grid_->rank_col() == c.ib) {
203 (*this)(r.index_local, c.index_local) = (*
this)(r.index_local, c.index_local) * beta__ + val;
209void dmatrix<T>::make_real_diag(
int n__)
211 for (
int i = 0; i < n__; i++) {
212 auto r = spl_row_.location(i);
213 if (blacs_grid_->rank_row() == r.ib) {
214 auto c = spl_col_.location(i);
215 if (blacs_grid_->rank_col() == c.ib) {
216 T v = (*this)(r.index_local, c.index_local);
217 (*this)(r.index_local, c.index_local) = std::real(v);
224sddk::mdarray<T, 1> dmatrix<T>::get_diag(
int n__)
226 sddk::mdarray<T, 1> d(n__);
229 for (
int i = 0; i < n__; i++) {
230 auto r = spl_row_.location(i);
231 if (blacs_grid_->rank_row() == r.ib) {
232 auto c = spl_col_.location(i);
233 if (blacs_grid_->rank_col() == c.ib) {
234 d[i] = (*this)(r.index_local, c.index_local);
238 blacs_grid_->comm().allreduce(d.template at(sddk::memory_t::host), n__);
243void dmatrix<T>::save_to_hdf5(std::string name__,
int m__,
int n__)
245 sddk::mdarray<T, 2> full_mtrx(m__, n__);
248 for (
int j = 0; j < this->num_cols_local(); j++) {
249 for (
int i = 0; i < this->num_rows_local(); i++) {
250 if (this->irow(i) < m__ && this->icol(j) < n__) {
251 full_mtrx(this->irow(i), this->icol(j)) = (*this)(i, j);
255 this->comm().allreduce(full_mtrx.template at(sddk::memory_t::host),
static_cast<int>(full_mtrx.size()));
257 if (this->blacs_grid().comm().rank() == 0) {
259 h5.write(
"nrow", m__);
260 h5.write(
"ncol", n__);
261 h5.write(
"mtrx", full_mtrx);
266template class dmatrix<double>;
267template class dmatrix<std::complex<double>>;
268#ifdef SIRIUS_USE_FP32
269template class dmatrix<float>;
270template class dmatrix<std::complex<float>>;
Interface to the HDF5 library.
Contains definition and implementation of distributed matrix class.
@ spla
SPLA library. Can take CPU and device pointers.
Namespace of the SIRIUS library.
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.