Skip to content

Commit 5b0de46

Browse files
committed
add unittest on to_qo_mpi
1 parent aac33ad commit 5b0de46

File tree

4 files changed

+63
-11
lines changed

4 files changed

+63
-11
lines changed

source/module_io/test/to_qo_test.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,16 @@ class toQOTest : public testing::Test
110110
protected:
111111
void SetUp() override
112112
{
113+
#ifdef __MPI
114+
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
115+
#endif
113116
}
114117

115118
void TearDown() override
116119
{
117120
}
118121
UnitCell ucell;
122+
int myrank = 0;
119123
};
120124

121125
TEST_F(toQOTest, Constructor)
@@ -1500,6 +1504,50 @@ TEST_F(toQOTest, CalculateHydrogenlike)
15001504
}
15011505
}
15021506

1507+
TEST_F(toQOTest, BcastStdvectorOfVector3Int)
1508+
{
1509+
#ifdef __MPI
1510+
std::vector<ModuleBase::Vector3<int>> vec;
1511+
if (this->myrank == 0)
1512+
{
1513+
vec.push_back(ModuleBase::Vector3<int>(1, 2, 3));
1514+
vec.push_back(ModuleBase::Vector3<int>(4, 5, 6));
1515+
vec.push_back(ModuleBase::Vector3<int>(7, 8, 9));
1516+
}
1517+
toQO::bcast_stdvector_ofvector3int(vec, myrank);
1518+
if (this->myrank != 0)
1519+
{
1520+
EXPECT_EQ(vec[0], ModuleBase::Vector3<int>(1, 2, 3));
1521+
EXPECT_EQ(vec[1], ModuleBase::Vector3<int>(4, 5, 6));
1522+
EXPECT_EQ(vec[2], ModuleBase::Vector3<int>(7, 8, 9));
1523+
}
1524+
#else
1525+
GTEST_SKIP();
1526+
#endif
1527+
}
1528+
1529+
TEST_F(toQOTest, BcastStdvectorOfVector3Double)
1530+
{
1531+
#ifdef __MPI
1532+
std::vector<ModuleBase::Vector3<double>> vec;
1533+
if (this->myrank == 0)
1534+
{
1535+
vec.push_back(ModuleBase::Vector3<double>(1.0, 2.0, 3.0));
1536+
vec.push_back(ModuleBase::Vector3<double>(4.0, 5.0, 6.0));
1537+
vec.push_back(ModuleBase::Vector3<double>(7.0, 8.0, 9.0));
1538+
}
1539+
toQO::bcast_stdvector_ofvector3double(vec, myrank);
1540+
if (this->myrank != 0)
1541+
{
1542+
EXPECT_EQ(vec[0], ModuleBase::Vector3<double>(1.0, 2.0, 3.0));
1543+
EXPECT_EQ(vec[1], ModuleBase::Vector3<double>(4.0, 5.0, 6.0));
1544+
EXPECT_EQ(vec[2], ModuleBase::Vector3<double>(7.0, 8.0, 9.0));
1545+
}
1546+
#else
1547+
GTEST_SKIP();
1548+
#endif
1549+
}
1550+
15031551
/**/
15041552
int main(int argc, char** argv)
15051553
{

source/module_io/to_qo.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,10 @@ class toQO
145145
void append_ovlpR_eiRk(int ik, int iR); //< append S(R) to S(k), memory saving
146146

147147
// MPI related
148-
void bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& vec);
149-
void bcast_stdvector_ofvector3double(std::vector<ModuleBase::Vector3<double>>& vec);
148+
static void bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& vec,
149+
const int rank);
150+
static void bcast_stdvector_ofvector3double(std::vector<ModuleBase::Vector3<double>>& vec,
151+
const int rank);
150152

151153
// Neighboring list
152154
/// @brief get all possible (n1n2n3) defining supercell and scatter if MPI enabled

source/module_io/to_qo_mpi.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
#include "../module_base/parallel_common.h"
44
#endif
55

6-
void toQO::bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& vec)
6+
void toQO::bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& vec,
7+
const int rank)
78
{
89
#ifdef __MPI
910
int dim;
1011
std::vector<int> vec_1d;
11-
if(iproc_ == 0)
12+
if(rank == 0)
1213
{
1314
dim = vec.size();
1415
for(int i = 0; i < dim; i++)
@@ -19,9 +20,9 @@ void toQO::bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& v
1920
}
2021
}
2122
Parallel_Common::bcast_int(dim);
22-
if(iproc_ != 0) vec_1d.resize(dim * 3);
23+
if(rank != 0) { vec_1d.resize(dim * 3); }
2324
Parallel_Common::bcast_int(vec_1d.data(), dim * 3);
24-
if(iproc_ != 0)
25+
if(rank != 0)
2526
{
2627
vec.clear(); vec.resize(dim);
2728
for(int i = 0; i < dim; i++)
@@ -32,12 +33,13 @@ void toQO::bcast_stdvector_ofvector3int(std::vector<ModuleBase::Vector3<int>>& v
3233
#endif
3334
}
3435

35-
void toQO::bcast_stdvector_ofvector3double(std::vector<ModuleBase::Vector3<double>>& vec)
36+
void toQO::bcast_stdvector_ofvector3double(std::vector<ModuleBase::Vector3<double>>& vec,
37+
const int rank)
3638
{
3739
#ifdef __MPI
3840
int dim;
3941
std::vector<double> vec_1d;
40-
if(iproc_ == 0)
42+
if(rank == 0)
4143
{
4244
dim = vec.size();
4345
for(int i = 0; i < dim; i++)
@@ -48,9 +50,9 @@ void toQO::bcast_stdvector_ofvector3double(std::vector<ModuleBase::Vector3<doubl
4850
}
4951
}
5052
Parallel_Common::bcast_int(dim);
51-
if(iproc_ != 0) vec_1d.resize(dim * 3);
53+
if(rank != 0) { vec_1d.resize(dim * 3); }
5254
Parallel_Common::bcast_double(vec_1d.data(), dim * 3);
53-
if(iproc_ != 0)
55+
if(rank != 0)
5456
{
5557
vec.clear(); vec.resize(dim);
5658
for(int i = 0; i < dim; i++)

source/module_io/to_qo_structures.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ void toQO::scan_supercell(const int& rank, const int& nranks)
219219
#ifdef __MPI // scatter supercells_ to all ranks
220220
Parallel_Common::bcast_int(nR_);
221221
Parallel_Common::bcast_int(nR_tot_);
222-
bcast_stdvector_ofvector3int(supercells_);
222+
bcast_stdvector_ofvector3int(supercells_, iproc_);
223223
// scatter
224224
std::vector<std::vector<int>> nR_divided(nranks); // indiced by iproc, then list of indices of supercells_
225225
if(rank == 0)

0 commit comments

Comments
 (0)