1- #include " source_base/module_external/blacs_connector.h"
2- #include " source_base/module_container/base/third_party/lapack.h"
31#include " mpi.h"
2+ #include " source_base/module_container/base/third_party/lapack.h"
3+ #include " source_base/module_external/blacs_connector.h"
44
55#include < complex>
66#include < fstream>
1717
1818namespace LCAO_DIAGO_TEST
1919{
20- void process_2d (int &nprows, int &npcols, int &myprow, int &mypcol, int &icontxt) // map the processes to 2D matrix
20+ void process_2d (
21+ int &nprows,
22+ int &npcols,
23+ int &myprow,
24+ int &mypcol,
25+ int &icontxt) // map the processes to 2D matrix
2126{
2227 int nprocs, mypnum;
2328 MPI_Comm_size (MPI_COMM_WORLD , &nprocs);
@@ -26,51 +31,85 @@ void process_2d(int &nprows, int &npcols, int &myprow, int &mypcol, int &icontxt
2631 for (npcols = int (sqrt (double (nprocs))); npcols >= 1 ; --npcols)
2732 {
2833 if (nprocs % npcols == 0 )
34+ {
2935 break ;
36+ }
3037 }
3138 nprows = nprocs / npcols;
3239
3340 int *usermap = new int [nprocs];
3441 for (int i = 0 ; i < nprows; i++)
3542 {
3643 for (int j = 0 ; j < npcols; j++)
44+ {
3745 usermap[j * nprows + i] = i * npcols + j;
46+ }
3847 }
3948
4049 int what = 0 , ic = 0 ;
4150 BlacsConnector::get (ic, what, icontxt);
4251 BlacsConnector::pinfo (mypnum, nprocs);
43- BlacsConnector::gridmap (icontxt, usermap, nprows, nprows, npcols);
44- BlacsConnector::gridinfo (icontxt, nprows, npcols, myprow, mypcol);
52+ BlacsConnector::gridmap (
53+ icontxt,
54+ usermap,
55+ nprows,
56+ nprows,
57+ npcols);
58+ BlacsConnector::gridinfo (
59+ icontxt,
60+ nprows,
61+ npcols,
62+ myprow,
63+ mypcol);
4564 delete[] usermap;
4665}
4766
48- inline int na_rc (int n, int nblk, int nprows, int myprow) // calculate the row/column number in each process
67+ inline int na_rc (
68+ int n,
69+ int nblk,
70+ int nprows,
71+ int myprow) // calculate the row/column number in each process
4972{
5073 int i, na = 0 ;
5174 for (i = 0 ; i < n; i++)
75+ {
5276 if ((i % (nblk * nprows)) / nblk == myprow)
77+ {
5378 na++;
79+ }
80+ }
5481 return na;
5582}
5683
5784// distribute hmatrix to each process
5885// hmatrix is row-first, and new_matrix is column-first
5986template <class T >
60- void distribute_data (T *hmatrix, T *new_matrix, int &nFull, int &nblk, int &na_rows, int &na_cols, int &icontxt)
87+ void distribute_data (
88+ T *hmatrix,
89+ T *new_matrix,
90+ int &nFull,
91+ int &nblk,
92+ int &na_rows,
93+ int &na_cols,
94+ int &icontxt)
6195{
6296 int mypnum;
6397 MPI_Comm_rank (MPI_COMM_WORLD , &mypnum);
6498 int nprows, npcols, myprow, mypcol;
65- BlacsConnector::gridinfo (icontxt, nprows, npcols, myprow, mypcol);
99+ BlacsConnector::gridinfo (
100+ icontxt,
101+ nprows,
102+ npcols,
103+ myprow,
104+ mypcol);
66105
67106 int SENDPROW = 0 , SENDPCOL = 0 , tag = 0 ;
68107 T *tmp = new T[nblk];
69108
70109 // do iteration for matrix, distribute old_matrix to each process, pass a block each time
71110 for (int row = 0 ; row < nFull; row++)
72111 {
73- int recv_prow = (row / nblk) % nprows; // the row number of recive process
112+ int recv_prow = (row / nblk) % nprows; // the row number of recive process
74113 int nm_row = ((row / nblk) / nprows) * nblk + row % nblk; // row number of block in new_matrix
75114 for (int col = 0 ; col < nFull; col += nblk)
76115 {
@@ -90,27 +129,51 @@ void distribute_data(T *hmatrix, T *new_matrix, int &nFull, int &nblk, int &na_r
90129 else
91130 {
92131 if (std::is_same<T, double >::value)
93- MPI_Send (&(hmatrix[nFull * row + col]),
94- pass_length,
95- MPI_DOUBLE ,
96- recv_prow * npcols + recv_pcol,
97- tag,
98- MPI_COMM_WORLD );
132+ {
133+ MPI_Send (
134+ &(hmatrix[nFull * row + col]),
135+ pass_length,
136+ MPI_DOUBLE ,
137+ recv_prow * npcols + recv_pcol,
138+ tag,
139+ MPI_COMM_WORLD );
140+ }
99141 else
100- MPI_Send (&(hmatrix[nFull * row + col]),
101- pass_length,
102- MPI_DOUBLE_COMPLEX ,
103- recv_prow * npcols + recv_pcol,
104- tag,
105- MPI_COMM_WORLD );
142+ {
143+ MPI_Send (
144+ &(hmatrix[nFull * row + col]),
145+ pass_length,
146+ MPI_DOUBLE_COMPLEX ,
147+ recv_prow * npcols + recv_pcol,
148+ tag,
149+ MPI_COMM_WORLD );
150+ }
106151 }
107152 }
108153 else if (myprow == recv_prow && mypcol == recv_pcol)
109154 {
110155 if (std::is_same<T, double >::value)
111- MPI_Recv (tmp, pass_length, MPI_DOUBLE , 0 , tag, MPI_COMM_WORLD , MPI_STATUS_IGNORE );
156+ {
157+ MPI_Recv (
158+ tmp,
159+ pass_length,
160+ MPI_DOUBLE ,
161+ 0 ,
162+ tag,
163+ MPI_COMM_WORLD ,
164+ MPI_STATUS_IGNORE );
165+ }
112166 else
113- MPI_Recv (tmp, pass_length, MPI_DOUBLE_COMPLEX , 0 , tag, MPI_COMM_WORLD , MPI_STATUS_IGNORE );
167+ {
168+ MPI_Recv (
169+ tmp,
170+ pass_length,
171+ MPI_DOUBLE_COMPLEX ,
172+ 0 ,
173+ tag,
174+ MPI_COMM_WORLD ,
175+ MPI_STATUS_IGNORE );
176+ }
114177 for (int i = 0 ; i < pass_length; i++)
115178 {
116179 new_matrix[(nm_col + i) * na_rows + nm_row] = tmp[i];
@@ -121,7 +184,13 @@ void distribute_data(T *hmatrix, T *new_matrix, int &nFull, int &nblk, int &na_r
121184 delete[] tmp;
122185}
123186
124- template <class T > inline void print_matrix (std::ofstream &fp, T *matrix, int &nrow, int &ncol, bool row_first)
187+ template <class T >
188+ inline void print_matrix (
189+ std::ofstream &fp,
190+ T *matrix,
191+ int &nrow,
192+ int &ncol,
193+ bool row_first)
125194{
126195 int coef_row = row_first ? ncol : 1 ;
127196 int coef_col = row_first ? 1 : nrow;
@@ -139,13 +208,13 @@ template <class T> bool read_hs(std::string fname, T &matrix)
139208{
140209 int ndim;
141210 std::ifstream inf (fname);
142- if (! inf.is_open ())
211+ if (! inf.is_open ())
143212 {
144213 std::cout << " Error: open file " << fname << " failed, skip!" << std::endl;
145214 return false ;
146215 }
147216 inf >> ndim;
148- matrix.resize (ndim* ndim);
217+ matrix.resize (ndim * ndim);
149218 for (int i = 0 ; i < ndim; i++)
150219 {
151220 for (int j = i; j < ndim; j++)
@@ -161,9 +230,13 @@ template <class T> bool read_hs(std::string fname, T &matrix)
161230 return true ;
162231}
163232
164- void lapack_diago (double *hmatrix, double *smatrix, double *e, int &nFull)
233+ void lapack_diago (
234+ double *hmatrix,
235+ double *smatrix,
236+ double *e,
237+ int &nFull)
165238{
166- const int itype = 1 ; // solve A*X=(lambda)*B*X
239+ const int itype = 1 ; // solve A*X=(lambda)*B*X
167240 const char jobz = ' V' ; // 'N':only calc eigenvalue, 'V': eigenvalues and eigenvectors
168241 const char uplo = ' U' ; // Upper triangles
169242 int lwork = (nFull + 2 ) * nFull, info = 0 ;
@@ -177,7 +250,19 @@ void lapack_diago(double *hmatrix, double *smatrix, double *e, int &nFull)
177250 b[i] = smatrix[i];
178251 }
179252
180- container::lapackConnector::hegv (itype, jobz, uplo, nFull, a, nFull, b, nFull, e, ev, lwork, info);
253+ container::lapackConnector::hegv (
254+ itype,
255+ jobz,
256+ uplo,
257+ nFull,
258+ a,
259+ nFull,
260+ b,
261+ nFull,
262+ e,
263+ ev,
264+ lwork,
265+ info);
181266 if (info != 0 )
182267 {
183268 std::cout << " ERROR: solvered by LAPACK error, info=" << info << std::endl;
@@ -189,9 +274,13 @@ void lapack_diago(double *hmatrix, double *smatrix, double *e, int &nFull)
189274 delete[] ev;
190275}
191276
192- void lapack_diago (std::complex <double > *hmatrix, std::complex <double > *smatrix, double *e, int &nFull)
277+ void lapack_diago (
278+ std::complex <double > *hmatrix,
279+ std::complex <double > *smatrix,
280+ double *e,
281+ int &nFull)
193282{
194- const int itype = 1 ; // solve A*X=(lambda)*B*X
283+ const int itype = 1 ; // solve A*X=(lambda)*B*X
195284 const char jobz = ' V' ; // 'N':only calc eigenvalue, 'V': eigenvalues and eigenvectors
196285 const char uplo = ' U' ; // Upper triangles
197286 int lwork = (nFull + 1 ) * nFull, info = 0 ;
@@ -206,7 +295,20 @@ void lapack_diago(std::complex<double> *hmatrix, std::complex<double> *smatrix,
206295 b[i] = smatrix[i];
207296 }
208297
209- container::lapackConnector::hegv (itype, jobz, uplo, nFull, a, nFull, b, nFull, e, ev, lwork, rwork, info);
298+ container::lapackConnector::hegv (
299+ itype,
300+ jobz,
301+ uplo,
302+ nFull,
303+ a,
304+ nFull,
305+ b,
306+ nFull,
307+ e,
308+ ev,
309+ lwork,
310+ rwork,
311+ info);
210312 if (info != 0 )
211313 {
212314 std::cout << " ERROR: solvered by LAPACK error, info=" << info << std::endl;
0 commit comments