23 MPI_Comm_size(MPI_COMM_WORLD, &nprocs);
24 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
26 for (npcols =
int(sqrt(
double(nprocs))); npcols >= 1; --npcols)
28 if (nprocs % npcols == 0)
31 nprows = nprocs / npcols;
33 int *usermap =
new int[nprocs];
34 for (
int i = 0; i < nprows; i++)
36 for (
int j = 0; j < npcols; j++)
37 usermap[j * nprows + i] = i * npcols + j;
51 for (i = 0; i < n; i++)
52 if ((i % (nblk * nprows)) / nblk ==
myprow)
60void distribute_data(
T *hmatrix,
T *new_matrix,
int &nFull,
int &nblk,
int &na_rows,
int &na_cols,
int &icontxt)
63 MPI_Comm_rank(MPI_COMM_WORLD, &mypnum);
67 int SENDPROW = 0, SENDPCOL = 0, tag = 0;
71 for (
int row = 0; row < nFull; row++)
73 int recv_prow = (row / nblk) % nprows;
74 int nm_row = ((row / nblk) / nprows) * nblk + row % nblk;
75 for (
int col = 0; col < nFull; col += nblk)
77 int recv_pcol = (col / nblk) % npcols;
78 int nm_col = ((col / nblk) / npcols) * nblk + col % nblk;
79 int pass_length = std::min(nFull - col, nblk);
85 for (
int i = 0; i < pass_length; i++)
87 new_matrix[(nm_col + i) * na_rows + nm_row] = hmatrix[nFull * row + col + i];
92 if (std::is_same<T, double>::value)
93 MPI_Send(&(hmatrix[nFull * row + col]),
96 recv_prow * npcols + recv_pcol,
100 MPI_Send(&(hmatrix[nFull * row + col]),
103 recv_prow * npcols + recv_pcol,
110 if (std::is_same<T, double>::value)
111 MPI_Recv(tmp, pass_length, MPI_DOUBLE, 0, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
113 MPI_Recv(tmp, pass_length, MPI_DOUBLE_COMPLEX, 0, tag, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
114 for (
int i = 0; i < pass_length; i++)
116 new_matrix[(nm_col + i) * na_rows + nm_row] = tmp[i];
124template <
class T>
inline void print_matrix(std::ofstream &fp,
T *matrix,
int &nrow,
int &ncol,
bool row_first)
126 int coef_row = row_first ? ncol : 1;
127 int coef_col = row_first ? 1 : nrow;
128 for (
int i = 0; i < nrow; i++)
130 for (
int j = 0; j < ncol; j++)
132 fp << std::setw(15) << matrix[i * coef_row + j * coef_col] <<
" ";
138template <
class T>
bool read_hs(std::string fname,
T &matrix)
141 std::ifstream inf(fname);
144 std::cout <<
"Error: open file " << fname <<
" failed, skip!" << std::endl;
148 matrix.resize(ndim*ndim);
149 for (
int i = 0; i < ndim; i++)
151 for (
int j = i; j < ndim; j++)
153 inf >> matrix[i * ndim + j];
156 matrix[j * ndim + i] = matrix[i * ndim + j];
164void lapack_diago(
double *hmatrix,
double *smatrix,
double *e,
int &nFull)
167 const char jobz =
'V';
168 const char uplo =
'U';
169 int lwork = (nFull + 2) * nFull, info = 0;
170 double *ev =
new double[nFull * nFull];
172 double *a =
new double[nFull * nFull];
173 double *b =
new double[nFull * nFull];
174 for (
int i = 0; i < nFull * nFull; i++)
180 dsygv_(&itype, &jobz, &uplo, &nFull, a, &nFull, b, &nFull, e, ev, &lwork, &info);
183 std::cout <<
"ERROR: solvered by LAPACK error, info=" << info << std::endl;
192void lapack_diago(std::complex<double> *hmatrix, std::complex<double> *smatrix,
double *e,
int &nFull)
195 const char jobz =
'V';
196 const char uplo =
'U';
197 int lwork = (nFull + 1) * nFull, info = 0;
198 double *rwork =
new double[3 * nFull - 2];
199 std::complex<double> *ev =
new std::complex<double>[nFull * nFull];
201 std::complex<double> *a =
new std::complex<double>[nFull * nFull];
202 std::complex<double> *b =
new std::complex<double>[nFull * nFull];
203 for (
int i = 0; i < nFull * nFull; i++)
209 zhegv_(&itype, &jobz, &uplo, &nFull, a, &nFull, b, &nFull, e, ev, &lwork, rwork, &info);
212 std::cout <<
"ERROR: solvered by LAPACK error, info=" << info << std::endl;
void Cblacs_get(int icontxt, int what, int *val)
void Cblacs_pinfo(int *myid, int *nprocs)
void Cblacs_gridmap(int *icontxt, int *usermap, int ldumap, int nprow, int npcol)
void Cblacs_gridinfo(int icontxt, int *nprow, int *npcol, int *myprow, int *mypcol)
#define T
Definition exp.cpp:237
void zhegv_(const int *itype, const char *jobz, const char *uplo, const int *n, std::complex< double > *a, const int *lda, std::complex< double > *b, const int *ldb, double *w, std::complex< double > *work, int *lwork, double *rwork, int *info)
void dsygv_(const int *itype, const char *jobz, const char *uplo, const int *n, double *a, const int *lda, double *b, const int *ldb, double *w, double *work, int *lwork, int *info)
Definition diago_elpa_utils.h:19
int na_rc(int n, int nblk, int nprows, int myprow)
Definition diago_elpa_utils.h:48
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:124
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:60
void lapack_diago(double *hmatrix, double *smatrix, double *e, int &nFull)
Definition diago_elpa_utils.h:164
bool read_hs(std::string fname, T &matrix)
Definition diago_elpa_utils.h:138
int mypcol
Definition tddft_test.cpp:14
int myprow
Definition tddft_test.cpp:14