ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
diago_elpa_utils.h
Go to the documentation of this file.
1#include "mpi.h"
4
5#include <complex>
6#include <fstream>
7#include <iomanip>
8#include <iostream>
9#include <math.h>
10#include <random>
11#include <type_traits>
12#include <vector>
13
14#ifdef __ELPA
16#endif
17
19{
21 int &nprows,
22 int &npcols,
23 int &myprow,
24 int &mypcol,
25 int &icontxt) // map the processes to 2D matrix
26{
27 int nprocs, mypnum;
28 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
29 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
30
31 for (npcols = int(sqrt(double(nprocs))); npcols >= 1; --npcols)
32 {
33 if (nprocs % npcols == 0)
34 {
35 break;
36 }
37 }
38 nprows = nprocs / npcols;
39
40 int *usermap = new int[nprocs];
41 for (int i = 0; i < nprows; i++)
42 {
43 for (int j = 0; j < npcols; j++)
44 {
45 usermap[j * nprows + i] = i * npcols + j;
46 }
47 }
48
49 int what = 0, ic = 0;
50 BlacsConnector::get(ic, what, icontxt);
51 BlacsConnector::pinfo(mypnum, nprocs);
53 icontxt,
54 usermap,
55 nprows,
56 nprows,
57 npcols);
59 icontxt,
60 nprows,
61 npcols,
62 myprow,
63 mypcol);
64 delete[] usermap;
65}
66
67inline int na_rc(
68 int n,
69 int nblk,
70 int nprows,
71 int myprow) // calculate the row/column number in each process
72{
73 int i, na = 0;
74 for (i = 0; i < n; i++)
75 {
76 if ((i % (nblk * nprows)) / nblk == myprow)
77 {
78 na++;
79 }
80 }
81 return na;
82}
83
84// distribute hmatrix to each process
85// hmatrix is row-first, and new_matrix is column-first
86template <class T>
88 T *hmatrix,
89 T *new_matrix,
90 int &nFull,
91 int &nblk,
92 int &na_rows,
93 int &na_cols,
94 int &icontxt)
95{
96 int mypnum;
97 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
98 int nprows, npcols, myprow, mypcol;
100 icontxt,
101 nprows,
102 npcols,
103 myprow,
104 mypcol);
105
106 int SENDPROW = 0, SENDPCOL = 0, tag = 0;
107 T *tmp = new T[nblk];
108
109 // do iteration for matrix, distribute old_matrix to each process, pass a block each time
110 for (int row = 0; row < nFull; row++)
111 {
112 int recv_prow = (row / nblk) % nprows; // the row number of recive process
113 int nm_row = ((row / nblk) / nprows) * nblk + row % nblk; // row number of block in new_matrix
114 for (int col = 0; col < nFull; col += nblk)
115 {
116 int recv_pcol = (col / nblk) % npcols; // the column number of recive process
117 int nm_col = ((col / nblk) / npcols) * nblk + col % nblk;
118 int pass_length = std::min(nFull - col, nblk); // nFull may not be devided by nblk;
119
120 if (myprow == SENDPROW && mypcol == SENDPCOL)
121 {
122 if (myprow == recv_prow && mypcol == recv_pcol)
123 {
124 for (int i = 0; i < pass_length; i++)
125 {
126 new_matrix[(nm_col + i) * na_rows + nm_row] = hmatrix[nFull * row + col + i];
127 }
128 }
129 else
130 {
131 if (std::is_same<T, double>::value)
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 }
141 else
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 }
151 }
152 }
153 else if (myprow == recv_prow && mypcol == recv_pcol)
154 {
155 if (std::is_same<T, double>::value)
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 }
166 else
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 }
177 for (int i = 0; i < pass_length; i++)
178 {
179 new_matrix[(nm_col + i) * na_rows + nm_row] = tmp[i];
180 }
181 }
182 }
183 }
184 delete[] tmp;
185}
186
187template <class T>
188inline void print_matrix(
189 std::ofstream &fp,
190 T *matrix,
191 int &nrow,
192 int &ncol,
193 bool row_first)
194{
195 int coef_row = row_first ? ncol : 1;
196 int coef_col = row_first ? 1 : nrow;
197 for (int i = 0; i < nrow; i++)
198 {
199 for (int j = 0; j < ncol; j++)
200 {
201 fp << std::setw(15) << matrix[i * coef_row + j * coef_col] << " ";
202 }
203 fp << std::endl;
204 }
205}
206
207template <class T> bool read_hs(std::string fname, T &matrix)
208{
209 int ndim;
210 std::ifstream inf(fname);
211 if (!inf.is_open())
212 {
213 std::cout << "Error: open file " << fname << " failed, skip!" << std::endl;
214 return false;
215 }
216 inf >> ndim;
217 matrix.resize(ndim * ndim);
218 for (int i = 0; i < ndim; i++)
219 {
220 for (int j = i; j < ndim; j++)
221 {
222 inf >> matrix[i * ndim + j];
223 if (i != j)
224 {
225 matrix[j * ndim + i] = matrix[i * ndim + j];
226 }
227 }
228 }
229 inf.close();
230 return true;
231}
232
234 double *hmatrix,
235 double *smatrix,
236 double *e,
237 int &nFull)
238{
239 const int itype = 1; // solve A*X=(lambda)*B*X
240 const char jobz = 'V'; // 'N':only calc eigenvalue, 'V': eigenvalues and eigenvectors
241 const char uplo = 'U'; // Upper triangles
242 int lwork = (nFull + 2) * nFull, info = 0;
243 double *ev = new double[lwork];
244
245 double *a = new double[nFull * nFull];
246 double *b = new double[nFull * nFull];
247 for (int i = 0; i < nFull * nFull; i++)
248 {
249 a[i] = hmatrix[i];
250 b[i] = smatrix[i];
251 }
252
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);
266 if (info != 0)
267 {
268 std::cout << "ERROR: solvered by LAPACK error, info=" << info << std::endl;
269 exit(1);
270 }
271
272 delete[] a;
273 delete[] b;
274 delete[] ev;
275}
276
278 std::complex<double> *hmatrix,
279 std::complex<double> *smatrix,
280 double *e,
281 int &nFull)
282{
283 const int itype = 1; // solve A*X=(lambda)*B*X
284 const char jobz = 'V'; // 'N':only calc eigenvalue, 'V': eigenvalues and eigenvectors
285 const char uplo = 'U'; // Upper triangles
286 int lwork = (nFull + 1) * nFull, info = 0;
287 double *rwork = new double[3 * nFull - 2];
288 std::complex<double> *ev = new std::complex<double>[lwork];
289
290 std::complex<double> *a = new std::complex<double>[nFull * nFull];
291 std::complex<double> *b = new std::complex<double>[nFull * nFull];
292 for (int i = 0; i < nFull * nFull; i++)
293 {
294 a[i] = hmatrix[i];
295 b[i] = smatrix[i];
296 }
297
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);
312 if (info != 0)
313 {
314 std::cout << "ERROR: solvered by LAPACK error, info=" << info << std::endl;
315 exit(1);
316 }
317
318 delete[] a;
319 delete[] b;
320 delete[] ev;
321 delete[] rwork;
322}
323
324} // namespace LCAO_DIAGO_TEST
This is a direct wrapper of some LAPACK routines. Column-Major version. Direct wrapping of standard L...
static void gridmap(int &context, int *usermap, int ldumap, int nprow, int npcol)
Definition blacs_connector.h:81
static void pinfo(int &rank, int &size)
Definition blacs_connector.h:75
static void get(int context, int what, int &value)
Definition blacs_connector.h:70
static void gridinfo(int context, int &nprow, int &npcol, int &myprow, int &mypcol)
Definition blacs_connector.h:86
#define T
Definition exp.cpp:237
Definition diago_elpa_utils.h:19
int na_rc(int n, int nblk, int nprows, int myprow)
Definition diago_elpa_utils.h:67
void process_2d(int &nprows, int &npcols, int &myprow, int &mypcol, int &icontxt)
Definition diago_elpa_utils.h:20
void print_matrix(std::ofstream &fp, T *matrix, int &nrow, int &ncol, bool row_first)
Definition diago_elpa_utils.h:188
void distribute_data(T *hmatrix, T *new_matrix, int &nFull, int &nblk, int &na_rows, int &na_cols, int &icontxt)
Definition diago_elpa_utils.h:87
void lapack_diago(double *hmatrix, double *smatrix, double *e, int &nFull)
Definition diago_elpa_utils.h:233
bool read_hs(std::string fname, T &matrix)
Definition diago_elpa_utils.h:207
int mypcol
Definition tddft_test.cpp:14
int myprow
Definition tddft_test.cpp:14