ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
gather_mat.h
Go to the documentation of this file.
1#ifndef GATHER_MAT_H
2#define GATHER_MAT_H
3
7
8namespace module_rt
9{
10//------------------------ MPI gathering and distributing functions ------------------------//
11// This struct is used for collecting matrices from all processes to root process
12template <typename T>
14{
15 std::shared_ptr<T> p;
16 size_t row;
17 size_t col;
18 std::shared_ptr<int> desc;
19};
20
21#ifdef __MPI
22// Collect matrices from all processes to root process
23template <typename T>
24void gatherMatrix(const int myid, const int root_proc, const hamilt::MatrixBlock<T>& mat_l, Matrix_g<T>& mat_g)
25{
26 const int* desca = mat_l.desc; // Obtain the descriptor of the local matrix
27 int ctxt = desca[1]; // BLACS context
28 int nrows = desca[2]; // Global matrix row number
29 int ncols = desca[3]; // Global matrix column number
30
31 if (myid == root_proc)
32 {
33 mat_g.p.reset(new T[nrows * ncols]); // No need to delete[] since it is a shared_ptr
34 }
35 else
36 {
37 mat_g.p.reset(new T[nrows * ncols]); // Placeholder for non-root processes
38 }
39
40 // Set the descriptor of the global matrix
41 mat_g.desc.reset(new int[9]{1, ctxt, nrows, ncols, nrows, ncols, 0, 0, nrows});
42 mat_g.row = nrows;
43 mat_g.col = ncols;
44
45 // Call the Cpxgemr2d function in ScaLAPACK to collect the matrix data
46 Cpxgemr2d(nrows, ncols, mat_l.p, 1, 1, const_cast<int*>(desca), mat_g.p.get(), 1, 1, mat_g.desc.get(), ctxt);
47}
48
49template <typename T>
51{
52 const int* desc_local = mat_l.desc; // Obtain the descriptor of the local matrix
53 int ctxt = desc_local[1]; // BLACS context
54 int nrows = desc_local[2]; // Global matrix row number
55 int ncols = desc_local[3]; // Global matrix column number
56
57 // Check matrix size consistency
58 if (mat_g.row != static_cast<size_t>(nrows) || mat_g.col != static_cast<size_t>(ncols))
59 {
60 throw std::invalid_argument("module_rt::distributeMatrix: Global matrix size mismatch.");
61 }
62
63 // Call the Cpxgemr2d function in ScaLAPACK to distribute the matrix data
64 Cpxgemr2d(nrows, ncols, mat_g.p.get(), 1, 1, mat_g.desc.get(), mat_l.p, 1, 1, const_cast<int*>(desc_local), ctxt);
65}
66
67template <typename T>
68void gatherPsi(const int myid,
69 const int root_proc,
70 T* psi_l,
71 const Parallel_Blkcyclic& wfc_layout,
73{
74 const int* desc_psi = wfc_layout.desc(); // Obtain the descriptor from the wavefunction layout
75 int ctxt = desc_psi[1]; // BLACS context
76 int nrows = desc_psi[2]; // Global matrix row number
77 int ncols = desc_psi[3]; // Global matrix column number
78
79 if (myid == root_proc)
80 {
81 psi_g.p.reset(new T[nrows * ncols]); // No need to delete[] since it is a shared_ptr
82 }
83 else
84 {
85 psi_g.p.reset(new T[nrows * ncols]); // Placeholder for non-root processes
86 }
87
88 // Set the descriptor of the global psi
89 psi_g.desc.reset(new int[9]{1, ctxt, nrows, ncols, nrows, ncols, 0, 0, nrows});
90 psi_g.row = nrows;
91 psi_g.col = ncols;
92
93 // Call the Cpxgemr2d function in ScaLAPACK to collect the matrix data
94 Cpxgemr2d(nrows, ncols, psi_l, 1, 1, const_cast<int*>(desc_psi), psi_g.p.get(), 1, 1, psi_g.desc.get(), ctxt);
95}
96
97template <typename T>
98void distributePsi(const Parallel_Blkcyclic& wfc_layout, T* psi_l, const module_rt::Matrix_g<T>& psi_g)
99{
100 const int* desc_psi = wfc_layout.desc(); // Obtain the descriptor from the wavefunction layout
101 int ctxt = desc_psi[1]; // BLACS context
102 int nrows = desc_psi[2]; // Global matrix row number
103 int ncols = desc_psi[3]; // Global matrix column number
104
105 // Call the Cpxgemr2d function in ScaLAPACK to distribute the matrix data
106 Cpxgemr2d(nrows, ncols, psi_g.p.get(), 1, 1, psi_g.desc.get(), psi_l, 1, 1, const_cast<int*>(desc_psi), ctxt);
107}
108//------------------------ MPI gathering and distributing functions ------------------------//
109
110#endif // __MPI
111} // namespace module_rt
112#endif // GATHER_MAT_H
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
const int * desc() const
Definition parallel_blkcyc.h:125
#define T
Definition exp.cpp:237
Definition band_energy.cpp:21
void distributePsi(const Parallel_Blkcyclic &wfc_layout, T *psi_l, const module_rt::Matrix_g< T > &psi_g)
Definition gather_mat.h:98
void gatherPsi(const int myid, const int root_proc, T *psi_l, const Parallel_Blkcyclic &wfc_layout, module_rt::Matrix_g< T > &psi_g)
Definition gather_mat.h:68
void distributeMatrix(hamilt::MatrixBlock< T > &mat_l, const module_rt::Matrix_g< T > &mat_g)
Definition gather_mat.h:50
void gatherMatrix(const int myid, const int root_proc, const hamilt::MatrixBlock< T > &mat_l, Matrix_g< T > &mat_g)
Definition gather_mat.h:24
std::enable_if< block2d_data_type< T >::value, void >::type Cpxgemr2d(int M, int N, T *A, int IA, int JA, int *DESCA, T *B, int IB, int JB, int *DESCB, int ICTXT)
Definition scalapack_connector.h:229
A non-owning description of a matrix stored in memory.
Definition matrix_block.h:23
T * p
Definition matrix_block.h:25
const int * desc
Definition matrix_block.h:28
Definition gather_mat.h:14
size_t col
Definition gather_mat.h:17
size_t row
Definition gather_mat.h:16
std::shared_ptr< T > p
Definition gather_mat.h:15
std::shared_ptr< int > desc
Definition gather_mat.h:18