Skip to content

Commit 7696920

Browse files
rootroot
authored andcommitted
test(mpi): UT of functions in parallel_common.cpp
1 parent 6ea92c4 commit 7696920

File tree

4 files changed

+134
-1
lines changed

4 files changed

+134
-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: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#include "mpi.h"
2+
#include "gtest/gtest.h"
3+
#include "module_base/global_variable.h"
4+
#include "src_parallel/parallel_common.h"
5+
#include <complex>
6+
#include <string>
7+
#include <cstring>
8+
9+
/************************************************
10+
* unit test of functions in parallel_common.cpp
11+
***********************************************/
12+
13+
/**
14+
* The test functions are wrappers of MPI_Bcast
15+
* in ABACUS, as defined in parallel_common.h
16+
* The source process is process 0 in all MPI_Bcast
17+
* functions
18+
*/
19+
20+
class ParaCommon : public testing::Test
21+
{
22+
protected:
23+
bool boo=true;
24+
int is=0;
25+
double fs = 0.0;
26+
std::complex<double> imgs{0.0,0.0};
27+
std::string chs ="abacus";
28+
char cha[7]="abacus";
29+
int iv[10]={0};
30+
double fv[10] = {0};
31+
std::complex<double> imgv[10]={0.0,0.0};
32+
std::string chv[10] = {""};
33+
};
34+
35+
TEST_F(ParaCommon,Bcast)
36+
{
37+
// reset data in the first process
38+
if(GlobalV::MY_RANK==0)
39+
{
40+
boo=false;
41+
is=1;
42+
fs=1.0;
43+
imgs=std::complex<double>(1.0,-1.0);
44+
chs ="ABACUS";
45+
strcpy(cha,chs.c_str());
46+
for (int i=0;i<10;i++)
47+
{
48+
double ii=static_cast<double>(i);
49+
iv[i] = i;
50+
fv[i] = ii;
51+
imgv[i]=std::complex<double>(ii,-ii);
52+
std::stringstream ss;
53+
ss<<chs<<i;
54+
chv[i] = ss.str();
55+
}
56+
}
57+
// call bcast wrappers
58+
Parallel_Common::bcast_bool(boo);
59+
Parallel_Common::bcast_int(is);
60+
Parallel_Common::bcast_double(fs);
61+
Parallel_Common::bcast_complex_double(imgs);
62+
Parallel_Common::bcast_string(chs);
63+
Parallel_Common::bcast_char(cha,7);
64+
Parallel_Common::bcast_int(iv,10);
65+
Parallel_Common::bcast_double(fv,10);
66+
Parallel_Common::bcast_complex_double(imgv,10);
67+
Parallel_Common::bcast_string(chv,10);
68+
// make comparisons
69+
EXPECT_FALSE(boo);
70+
EXPECT_EQ(is,1);
71+
EXPECT_EQ(fs,1.0);
72+
EXPECT_NEAR(imgs.real(),1.0,1E-15);
73+
EXPECT_NEAR(imgs.imag(),-1.0,1E-15);
74+
EXPECT_EQ(chs,"ABACUS");
75+
EXPECT_STREQ(cha,"ABACUS");
76+
for (int i=0;i<10;i++)
77+
{
78+
double ii=static_cast<double>(i);
79+
EXPECT_EQ(iv[i],i);
80+
EXPECT_NEAR(fv[i],ii,1E-15);
81+
EXPECT_NEAR(imgv[i].real(),ii,1E-15);
82+
EXPECT_NEAR(imgv[i].imag(),-ii,1E-15);
83+
std::stringstream ss;
84+
ss<<chs<<i;
85+
EXPECT_EQ(chv[i],ss.str());
86+
}
87+
}
88+
89+
int main(int argc, char **argv)
90+
{
91+
92+
MPI_Init(&argc, &argv);
93+
testing::InitGoogleTest(&argc, argv);
94+
95+
MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC);
96+
MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK);
97+
98+
int result = RUN_ALL_TESTS();
99+
100+
MPI_Finalize();
101+
102+
return result;
103+
}
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)