Skip to content

Commit 289e5d0

Browse files
mohanchenabacus_fixer
andauthored
Refactor Parallel Codes (#7478)
* add getter function in parallel_2d.h * refactor(parallel): Add MPI_Type type mapping template to eliminate duplicate specializations - Add MPI_Type<T> template struct to map C++ types to MPI_Datatype - Replace 30+ duplicate template specializations with 8 generic templates - Add explicit instantiations for all required types - Maintain full backward compatibility * fix is_serial * fix pv->coord[0] and pv->coord[1] * update parallel_orbital, delete useless variable, use vector instead of new * update parallel_orbital.h, change get_col_size(int iat) to get_ncol_atom(int iat), same for row * add a new function paraV->is_invalid_atom_pair(iat1, iat2) * add the function * update * delete useless run_symmetry.cpp * delete useless functions and variables * update * fix bugs * update * update --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 9d99dce commit 289e5d0

65 files changed

Lines changed: 363 additions & 664 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

source/source_base/parallel_2d.h

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,51 @@ class Parallel_2D
7474
return nb;
7575
};
7676

77+
/// number of processes in row dimension of the MPI Cartesian grid
78+
int get_dim0() const
79+
{
80+
return dim0;
81+
};
82+
83+
/// number of processes in column dimension of the MPI Cartesian grid
84+
int get_dim1() const
85+
{
86+
return dim1;
87+
};
88+
89+
/// row coordinate in the BLACS grid
90+
int get_coord_row() const
91+
{
92+
return coord[0];
93+
};
94+
95+
/// column coordinate in the BLACS grid
96+
int get_coord_col() const
97+
{
98+
return coord[1];
99+
};
100+
101+
/// check whether a given BLACS grid coordinate is this process
102+
bool blacs_in_this_processor(const int iprow, const int ipcol) const
103+
{
104+
return iprow == coord[0] && ipcol == coord[1];
105+
};
106+
107+
/// set process coordinate in the BLACS grid (intended for testing)
108+
void set_coord(const int row, const int col)
109+
{
110+
coord[0] = row;
111+
coord[1] = col;
112+
};
113+
77114
#ifdef __MPI
115+
116+
/// ScaLAPACK descriptor
117+
const int* get_desc() const
118+
{
119+
return desc;
120+
};
121+
78122
/**
79123
* @brief Initialize a BLACS grid with the given MPI communicator
80124
* and set up the info of a block-cyclic distribution.
@@ -126,13 +170,14 @@ class Parallel_2D
126170
int dim0 = 0;
127171
int dim1 = 0;
128172

173+
private:
129174
/// process coordinate in the BLACS grid
130175
int coord[2] = {-1, -1};
131-
132176
/// whether to use the serial mode
133177
bool is_serial = false;
134178

135179
protected:
180+
136181
/// map from global index to local index
137182
std::vector<int> global2local_row_;
138183
std::vector<int> global2local_col_;

source/source_base/parallel_comm.h

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,32 @@ extern MPI_Comm DIAG_WORLD; // mohan add 2012-01-13
1414
class MPICommGroup
1515
{
1616

17-
public:
17+
public:
1818

19-
MPICommGroup(MPI_Comm parent_comm);
20-
~MPICommGroup();
19+
MPICommGroup(MPI_Comm parent_comm);
20+
~MPICommGroup();
2121

22-
void divide_group_comm(const int& ngroup, const bool assert_even = true);
22+
void divide_group_comm(const int& ngroup, const bool assert_even = true);
2323

24-
bool is_even = false; ///< whether the group is even
24+
bool is_even = false; ///< whether the group is even
2525

26-
MPI_Comm parent_comm = MPI_COMM_NULL; ///< parent communicator
27-
int gsize = 0; ///< size of parent communicator
28-
int grank = 0; ///< rank of parent communicator
26+
MPI_Comm parent_comm = MPI_COMM_NULL; ///< parent communicator
27+
int gsize = 0; ///< size of parent communicator
28+
int grank = 0; ///< rank of parent communicator
2929

30-
MPI_Comm group_comm = MPI_COMM_NULL; ///< group communicator
31-
int ngroups = 0; ///< number of groups
32-
int nprocs_in_group = 0; ///< number of processes in the group
33-
int my_group = 0; ///< the group index
34-
int rank_in_group = 0; ///< the rank in the group
30+
MPI_Comm group_comm = MPI_COMM_NULL; ///< group communicator
31+
int ngroups = 0; ///< number of groups
32+
int nprocs_in_group = 0; ///< number of processes in the group
33+
int my_group = 0; ///< the group index
34+
int rank_in_group = 0; ///< the rank in the group
3535

36-
MPI_Comm inter_comm = MPI_COMM_NULL; ///< inter communicator
37-
bool has_inter_comm = false; ///< whether has inter communicator
38-
int& nprocs_in_inter = ngroups; ///< number of processes in the inter communicator
39-
int& my_inter = rank_in_group; ///< the rank in the inter communicator
40-
int& rank_in_inter = my_group; ///< the inter group index
36+
MPI_Comm inter_comm = MPI_COMM_NULL; ///< inter communicator
37+
bool has_inter_comm = false; ///< whether has inter communicator
38+
int& nprocs_in_inter = ngroups; ///< number of processes in the inter communicator
39+
int& my_inter = rank_in_group; ///< the rank in the inter communicator
40+
int& rank_in_inter = my_group; ///< the inter group index
4141
};
4242

4343
#endif
4444

45-
#endif // PARALLEL_COMM_H
45+
#endif // PARALLEL_COMM_H

source/source_base/parallel_common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_PARALLEL_COMMON_H
2-
#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_PARALLEL_COMMON_H
1+
#ifndef PARALLEL_COMMON_H
2+
#define PARALLEL_COMMON_H
33

44
#ifdef __MPI
55
#include "mpi.h"
66
#endif
7+
78
#include <complex>
89
#include <string>
910

1011
namespace Parallel_Common
1112
{
13+
1214
//(1) bcast array
1315
void bcast_complex_double(std::complex<double>* object, const int n);
1416
void bcast_string(std::string* object, const int n);

source/source_base/parallel_global.cpp

Lines changed: 3 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,6 @@ namespace Parallel_Global
2525
int mpi_number = 0;
2626
int omp_number = 0;
2727
} // namespace Parallel_Global
28-
29-
void Parallel_Global::myProd(std::complex<double>* in, std::complex<double>* inout, int* len, MPI_Datatype* dptr)
30-
{
31-
for (int i = 0; i < *len; i++)
32-
{
33-
// (*inout).real()=(*inout).real()+(*in).real();
34-
// (*inout).imag()=(*inout).imag()+(*in).imag();
35-
36-
// mohan updat 2011-09-21
37-
(*inout) = std::complex<double>((*inout).real() + (*in).real(), (*inout).imag() + (*in).imag());
38-
39-
in++;
40-
inout++;
41-
}
42-
return;
43-
}
4428
#endif
4529

4630
void Parallel_Global::split_diag_world(const int& diag_np,
@@ -108,8 +92,6 @@ void Parallel_Global::read_pal_param(int argc,
10892
MPI_Init(&argc, &argv); // Peize Lin change 2018-07-12
10993
#endif //_OPENMP
11094

111-
// KPAR = atoi(argv[1]); // mohan abandon 2010-06-09
112-
11395
// get world size --> NPROC
11496
// get global rank --> MY_RANK
11597
MPI_Comm_size(MPI_COMM_WORLD, &NPROC);
@@ -174,26 +156,10 @@ void Parallel_Global::read_pal_param(int argc,
174156

175157
NTHREAD_PER_PROC = current_thread_num;
176158

177-
// for test
178-
/*
179-
for (int i=0; i<NPROC; i++)
180-
{
181-
if (MY_RANK == i)
182-
{
183-
std::cout << " PROCESSOR " << std::setw(4) << MY_RANK+1 << " IS READY." << std::endl;
184-
}
185-
MPI_Barrier(MPI_COMM_WORLD);
186-
}
187-
*/
188-
189-
// This section can be chosen !!
190-
// mohan 2011-03-15
191159
if (MY_RANK != 0)
192160
{
193-
// std::cout.rdbuf(NULL);
194-
std::cout.setstate(std::ios::failbit); // qianrui modify 2020-10-14
161+
std::cout.setstate(std::ios::failbit);
195162
}
196-
// end test
197163
#endif //__MPI
198164
return;
199165
}
@@ -229,6 +195,7 @@ void Parallel_Global::finalize_mpi()
229195
}
230196
#endif
231197

198+
#ifdef __MPI
232199
void Parallel_Global::init_pools(const int& NPROC,
233200
const int& MY_RANK,
234201
const int& BNDPAR,
@@ -240,10 +207,6 @@ void Parallel_Global::init_pools(const int& NPROC,
240207
int& RANK_IN_POOL,
241208
int& MY_POOL)
242209
{
243-
#ifdef __MPI
244-
//----------------------------------------------------------
245-
// CALL Function : divide_pools
246-
//----------------------------------------------------------
247210
Parallel_Global::divide_pools(NPROC,
248211
MY_RANK,
249212
BNDPAR,
@@ -254,42 +217,8 @@ void Parallel_Global::init_pools(const int& NPROC,
254217
NPROC_IN_POOL,
255218
RANK_IN_POOL,
256219
MY_POOL);
257-
258-
// for test
259-
// turn on when you want to check the index of pools.
260-
/*
261-
if (GlobalV::MY_RANK==0)
262-
{
263-
std::cout << "\n " << std::setw(8) << "MY_RANK"
264-
<< std::setw(8) << "MY_POOL"
265-
<< std::setw(13) << "RANK_IN_POOL"
266-
<< std::setw(6) << "NPROC"
267-
<< std::setw(6) << "KPAR"
268-
<< std::setw(14) << "NPROC_IN_POOL" << std::endl;
269-
}
270-
for (int i=0; i<GlobalV::NPROC; i++)
271-
{
272-
if (GlobalV::MY_RANK == i)
273-
{
274-
std::cout << " I'm " << std::setw(8) << GlobalV::MY_RANK
275-
<< std::setw(8) << GlobalV::MY_POOL
276-
<< std::setw(13) << GlobalV::RANK_IN_POOL
277-
<< std::setw(6) << GlobalV::NPROC
278-
<< std::setw(6) << GlobalV::KPAR
279-
<< std::setw(14) << GlobalV::NPROC_IN_POOL << std::endl;
280-
}
281-
MPI_Barrier(MPI_COMM_WORLD);
282-
}
283-
284-
if (GlobalV::MY_RANK != 0 )
285-
{
286-
std::cout.rdbuf(NULL);
287-
}
288-
*/
289-
290-
return;
291-
#endif
292220
}
221+
#endif
293222

294223
#ifdef __MPI
295224
void Parallel_Global::divide_pools(const int& NPROC,

source/source_base/parallel_global.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66
#ifndef PARALLEL_GLOBAL_H
77
#define PARALLEL_GLOBAL_H
88

9-
#include <complex>
109
#include "parallel_comm.h"
1110

12-
// void myProd(std::complex<double> *in,std::complex<double> *inout,int *len,MPI_Datatype *dptr);
13-
1411
namespace Parallel_Global
1512
{
1613
extern int mpi_number;
@@ -21,9 +18,6 @@ extern int omp_number;
2118

2219
// changed from read_mpi_parameters in 2024-1018
2320
void read_pal_param(int argc, char** argv, int& NPROC, int& NTHREAD_PER_PROC, int& MY_RANK);
24-
#ifdef __MPI
25-
void myProd(std::complex<double>* in, std::complex<double>* inout, int* len, MPI_Datatype* dptr);
26-
#endif
2721

2822
/**-------------------------------------------
2923
* call to split the "diago world"

0 commit comments

Comments
 (0)