Skip to content

Commit 03b074a

Browse files
author
zxy.monado
committed
test(base): cover para gemm type variants
1 parent 1a7554e commit 03b074a

1 file changed

Lines changed: 62 additions & 7 deletions

File tree

source/source_base/test_parallel/test_para_gemm.cpp

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "../communication_domain.h"
12
#include "../kernels/math_kernel_op.h"
23
#include "../para_gemm.h"
34

@@ -63,6 +64,54 @@ double get_double(double& val)
6364
return val;
6465
}
6566

67+
template <typename T>
68+
void expect_near_value(const T& actual, const T& expected)
69+
{
70+
EXPECT_NEAR(std::abs(actual - expected), 0.0, 1.0e-5);
71+
}
72+
73+
template <typename T>
74+
void test_additional_type_paths()
75+
{
76+
const ModuleBase::CommunicationDomain domain = ModuleBase::world_communication_domain();
77+
MPI_Comm world = domain.communicator();
78+
const int rank = domain.rank();
79+
const int size = domain.size();
80+
const T alpha = static_cast<T>(1);
81+
const T beta = static_cast<T>(0);
82+
const T a[1] = {static_cast<T>(rank + 1)};
83+
const T b[1] = {static_cast<T>(rank + 2)};
84+
85+
ModuleBase::PGemmCN<T> single;
86+
single.set_dimension(MPI_COMM_SELF, MPI_COMM_SELF, 1, 1, 1, 1, 1, 1);
87+
T single_result[1] = {};
88+
single.multiply(alpha, a, b, beta, single_result);
89+
expect_near_value(single_result[0], a[0] * b[0]);
90+
91+
ModuleBase::PGemmCN<T> column_parallel;
92+
column_parallel.set_dimension(world, MPI_COMM_SELF, 1, 1, 1, 1, 1, size);
93+
std::vector<T> column_result(size * size);
94+
column_parallel.multiply(alpha, a, b, beta, column_result.data());
95+
for (int column = 0; column < size; ++column)
96+
{
97+
for (int row = 0; row < size; ++row)
98+
{
99+
const T expected = static_cast<T>(row + 1) * static_cast<T>(column + 2);
100+
expect_near_value(column_result[column * size + row], expected);
101+
}
102+
}
103+
104+
ModuleBase::PGemmCN<T> row_parallel;
105+
row_parallel.set_dimension(world, MPI_COMM_SELF, 1, 1, 1, 1, 1, 1, 3);
106+
std::vector<T> row_result(size);
107+
row_parallel.multiply(alpha, a, b, beta, row_result.data());
108+
for (int column = 0; column < size; ++column)
109+
{
110+
const T expected = static_cast<T>(rank + 1) * static_cast<T>(column + 2);
111+
expect_near_value(row_result[column], expected);
112+
}
113+
}
114+
66115
void scatterv_data(const double* sendbuf,
67116
const int* sendcounts,
68117
const int* displs,
@@ -434,9 +483,11 @@ TYPED_TEST(PgemmTest, divide_col)
434483
this->nrow,
435484
LDC_global,
436485
2);
437-
this->pgemm.multiply(this->alpha, this->A_local.data(), this->B_local.data(), this->beta, this->C_global.data()+ start);
438-
439-
486+
this->pgemm.multiply(this->alpha,
487+
this->A_local.data(),
488+
this->B_local.data(),
489+
this->beta,
490+
this->C_global.data() + start);
440491

441492
for (int i = 0; i < this->ncolB; i++)
442493
{
@@ -468,9 +519,9 @@ TYPED_TEST(PgemmTest, divide_row)
468519

469520
int LDC_local = this->ncolA + 2;
470521
std::vector<TypeParam> C_loc(LDC_local * ncolB_global, 0.0);
471-
for(int i = 0; i < ncolB_global; i++)
522+
for (int i = 0; i < ncolB_global; i++)
472523
{
473-
for(int j = 0; j < this->ncolA; j++)
524+
for (int j = 0; j < this->ncolA; j++)
474525
{
475526
C_loc[i * LDC_local + j] = this->C_global[i * LDC_global + start + j];
476527
}
@@ -487,8 +538,6 @@ TYPED_TEST(PgemmTest, divide_row)
487538
3);
488539
this->pgemm.multiply(this->alpha, this->A_local.data(), this->B_local.data(), this->beta, C_loc.data());
489540

490-
491-
492541
for (int i = 0; i < ncolB_global; i++)
493542
{
494543
for (int j = 0; j < this->ncolA; j++)
@@ -500,6 +549,12 @@ TYPED_TEST(PgemmTest, divide_row)
500549
}
501550
}
502551

552+
TEST(PgemmAdditionalTypes, FloatAndComplexFloat)
553+
{
554+
test_additional_type_paths<float>();
555+
test_additional_type_paths<std::complex<float>>();
556+
}
557+
503558
int main(int argc, char** argv)
504559
{
505560
::testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)