Skip to content

Commit a0ce815

Browse files
authored
Merge pull request #905 from hongriTianqi/develop
test(mpi): UT of functions in parallel_common.cpp
2 parents 6ea92c4 + 010d5bd commit a0ce815

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

source/src_parallel/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,8 @@ add_library(
1313
parallel
1414
OBJECT
1515
${objects}
16-
)
16+
)
17+
18+
IF (BUILD_TESTING)
19+
add_subdirectory(test)
20+
endif()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
AddTest(
2+
TARGET ParaCommon
3+
LIBS MPI::MPI_CXX
4+
SOURCES parallel_common_test.cpp ../../module_base/global_variable.cpp ../parallel_common.cpp
5+
)
6+
7+
install(FILES parallel_common_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
8+
9+
find_program(BASH bash)
10+
add_test(NAME parallel_common_test
11+
COMMAND ${BASH} parallel_common_test.sh
12+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
13+
)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#ifdef __MPI
2+
#include "mpi.h"
3+
#include "gtest/gtest.h"
4+
#include "module_base/global_variable.h"
5+
#include "src_parallel/parallel_common.h"
6+
#include <complex>
7+
#include <string>
8+
#include <cstring>
9+
10+
/************************************************
11+
* unit test of functions in parallel_common.cpp
12+
***********************************************/
13+
14+
/**
15+
* The tested functions are wrappers of MPI_Bcast
16+
* in ABACUS, as defined in src_parallel/parallel_common.h.
17+
* The source is process 0 in all MPI_Bcast
18+
* wrappers.
19+
*/
20+
21+
class ParaCommon : public testing::Test
22+
{
23+
protected:
24+
bool boo=true;
25+
int is=0;
26+
double fs = 0.0;
27+
std::complex<double> imgs{0.0,0.0};
28+
std::string chs ="abacus";
29+
char cha[7]="abacus";
30+
int iv[10]={0};
31+
double fv[10] = {0};
32+
std::complex<double> imgv[10]={0.0,0.0};
33+
std::string chv[10] = {""};
34+
};
35+
36+
TEST_F(ParaCommon,Bcast)
37+
{
38+
// reset data in the first process
39+
if(GlobalV::MY_RANK==0)
40+
{
41+
boo=false;
42+
is=1;
43+
fs=1.0;
44+
imgs=std::complex<double>(1.0,-1.0);
45+
chs ="ABACUS";
46+
strcpy(cha,chs.c_str());
47+
for (int i=0;i<10;i++)
48+
{
49+
double ii=static_cast<double>(i);
50+
iv[i] = i;
51+
fv[i] = ii;
52+
imgv[i]=std::complex<double>(ii,-ii);
53+
std::stringstream ss;
54+
ss<<chs<<i;
55+
chv[i] = ss.str();
56+
}
57+
}
58+
// call bcast wrappers
59+
Parallel_Common::bcast_bool(boo);
60+
Parallel_Common::bcast_int(is);
61+
Parallel_Common::bcast_double(fs);
62+
Parallel_Common::bcast_complex_double(imgs);
63+
Parallel_Common::bcast_string(chs);
64+
Parallel_Common::bcast_char(cha,7);
65+
Parallel_Common::bcast_int(iv,10);
66+
Parallel_Common::bcast_double(fv,10);
67+
Parallel_Common::bcast_complex_double(imgv,10);
68+
Parallel_Common::bcast_string(chv,10);
69+
// make comparisons
70+
EXPECT_FALSE(boo);
71+
EXPECT_EQ(is,1);
72+
EXPECT_EQ(fs,1.0);
73+
EXPECT_NEAR(imgs.real(),1.0,1E-15);
74+
EXPECT_NEAR(imgs.imag(),-1.0,1E-15);
75+
EXPECT_EQ(chs,"ABACUS");
76+
EXPECT_STREQ(cha,"ABACUS");
77+
for (int i=0;i<10;i++)
78+
{
79+
double ii=static_cast<double>(i);
80+
EXPECT_EQ(iv[i],i);
81+
EXPECT_NEAR(fv[i],ii,1E-15);
82+
EXPECT_NEAR(imgv[i].real(),ii,1E-15);
83+
EXPECT_NEAR(imgv[i].imag(),-ii,1E-15);
84+
std::stringstream ss;
85+
ss<<chs<<i;
86+
EXPECT_EQ(chv[i],ss.str());
87+
}
88+
}
89+
90+
int main(int argc, char **argv)
91+
{
92+
93+
MPI_Init(&argc, &argv);
94+
testing::InitGoogleTest(&argc, argv);
95+
96+
MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC);
97+
MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK);
98+
99+
int result = RUN_ALL_TESTS();
100+
101+
MPI_Finalize();
102+
103+
return result;
104+
}
105+
#endif
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
np=`cat /proc/cpuinfo | grep "cpu cores" | uniq| awk '{print $NF}'`
4+
echo "nprocs in this machine is $np"
5+
6+
for i in 3;do
7+
if [[ $i -gt $np ]];then
8+
continue
9+
fi
10+
echo "TEST in parallel, nprocs=$i"
11+
mpirun -np $i ./ParaCommon
12+
break
13+
done

0 commit comments

Comments
 (0)