Skip to content

Commit 23c1344

Browse files
committed
test(cuda): parameterize matrix operations tests and update build configuration
- Parameterize matrix operations tests for float and double types - Add tests for square and non-square matrix addition and multiplication - Update CMakeLists.txt to exclude main.cpp from sources - Modify Makefile to set XMAKE_ROOT environment variable - Remove test_main.cpp and update test_matrix.cpp for parameterized testing - Update xmake.lua to include gtest main configuration
1 parent bd67bee commit 23c1344

File tree

5 files changed

+94
-64
lines changed

5 files changed

+94
-64
lines changed

template/cuda/{{cookiecutter.project_slug}}/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
4747
${SRC_DIR}/*.cpp
4848
${SRC_DIR}/*.cu
4949
)
50+
list(FILTER SOURCES EXCLUDE REGEX ".*main\\.cpp$")
5051
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS
5152
${INCLUDE_DIR}/*.h
5253
)

template/cuda/{{cookiecutter.project_slug}}/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ build:
1313
@cmake -S . -B build
1414
@cmake --build build --parallel
1515
{%- else %}
16-
@xmake -y
16+
@XMAKE_ROOT=y xmake
1717
{%- endif %}
1818

1919
# run

template/cuda/{{cookiecutter.project_slug}}/tests/test_main.cpp

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,85 +1,121 @@
11
#include <gtest/gtest.h>
2+
#include <vector>
3+
24
#include "matrix_add.h"
35
#include "matrix_mult.h"
4-
#include <vector>
56

7+
// Template-based parameterized test for matrix operations
68
template <typename T>
7-
class MatrixOperationsTest : public ::testing::Test {
9+
class MatrixOperationsTest : public testing::Test {
810
protected:
9-
static constexpr int kMatrixSize = 2;
10-
static constexpr int kMatrixElements = kMatrixSize * kMatrixSize;
11-
12-
std::vector<T> matrixA;
13-
std::vector<T> matrixB;
14-
std::vector<T> resultMatrix;
15-
16-
void SetUp() override {
17-
// Initialize matrices A and B with test data
18-
matrixA = {1, 2, 3, 4};
19-
matrixB = {5, 6, 7, 8};
20-
resultMatrix.resize(kMatrixElements);
11+
// Static constants for default matrix configuration
12+
static constexpr int kDefaultMatrixSize = 2;
13+
static constexpr int kDefaultMatrixElements = kDefaultMatrixSize * kDefaultMatrixSize;
14+
15+
// Helper function to create a matrix from initializer list
16+
std::vector<T> createMatrix(std::initializer_list<T> values) {
17+
return std::vector<T>(values);
2118
}
2219

23-
void verifyResult(const std::vector<T>& expected) {
24-
ASSERT_EQ(resultMatrix.size(), expected.size());
20+
// Helper function to verify matrix calculation results
21+
static void verifyResult(const std::vector<T>& result, const std::vector<T>& expected) {
22+
// Check matrix size
23+
ASSERT_EQ(result.size(), expected.size())
24+
<< "Result matrix size does not match expected matrix size";
25+
26+
// Compare elements with near-equality
2527
for (size_t i = 0; i < expected.size(); i++) {
26-
EXPECT_NEAR(resultMatrix[i], expected[i], 1e-5)
27-
<< "Mismatch at index " << i;
28+
EXPECT_NEAR(result[i], expected[i], 1e-5)
29+
<< "Mismatch at index " << i
30+
<< ": expected " << expected[i]
31+
<< ", got " << result[i];
2832
}
2933
}
3034
};
3135

32-
using TestTypes = ::testing::Types<float, double>;
33-
TYPED_TEST_SUITE(MatrixOperationsTest, TestTypes);
36+
// Register type-parameterized test suite
37+
TYPED_TEST_SUITE_P(MatrixOperationsTest);
38+
39+
// Test case 1: Square matrix addition
40+
TYPED_TEST_P(MatrixOperationsTest, SquareMatrixAddition) {
41+
constexpr int size = this->kDefaultMatrixSize;
3442

35-
TYPED_TEST(MatrixOperationsTest, AdditionTest) {
36-
addMatricesOnGPU(this->matrixA.data(), this->matrixB.data(), this->resultMatrix.data(),
37-
this->kMatrixSize, this->kMatrixSize);
43+
// Prepare test data
44+
auto matrixA = this->createMatrix({1, 2, 3, 4});
45+
auto matrixB = this->createMatrix({5, 6, 7, 8});
46+
std::vector<TypeParam> resultMatrix(size * size);
3847

39-
std::vector<TypeParam> expectedSum = {6, 8, 10, 12};
40-
this->verifyResult(expectedSum);
48+
// Call GPU matrix addition function
49+
addMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size);
50+
51+
// Verify result
52+
auto expectedSum = this->createMatrix({6, 8, 10, 12});
53+
this->verifyResult(resultMatrix, expectedSum);
4154
}
4255

43-
TYPED_TEST(MatrixOperationsTest, MultiplicationTest) {
44-
multiplyMatricesOnGPU(this->matrixA.data(), this->matrixB.data(), this->resultMatrix.data(),
45-
this->kMatrixSize, this->kMatrixSize, this->kMatrixSize);
56+
// Test case 2: Square matrix multiplication
57+
TYPED_TEST_P(MatrixOperationsTest, SquareMatrixMultiplication) {
58+
constexpr int size = this->kDefaultMatrixSize;
59+
60+
// Prepare test data
61+
auto matrixA = this->createMatrix({1, 2, 3, 4});
62+
auto matrixB = this->createMatrix({5, 6, 7, 8});
63+
std::vector<TypeParam> resultMatrix(size * size);
4664

47-
std::vector<TypeParam> expectedProduct = {19, 22, 43, 50};
48-
this->verifyResult(expectedProduct);
65+
// Call GPU matrix multiplication function
66+
multiplyMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size, size);
67+
68+
// Verify result
69+
auto expectedProduct = this->createMatrix({19, 22, 43, 50});
70+
this->verifyResult(resultMatrix, expectedProduct);
4971
}
5072

51-
TYPED_TEST(MatrixOperationsTest, NonSquareAdditionTest) {
52-
const int rows = 2;
53-
const int cols = 3;
54-
std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6};
55-
std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12};
73+
// Test case 3: Non-square matrix addition
74+
TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixAddition) {
75+
constexpr int rows = 2;
76+
constexpr int cols = 3;
77+
78+
// Prepare test data
79+
auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6});
80+
auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12});
5681
std::vector<TypeParam> nonSquareResult(rows * cols);
5782

83+
// Call GPU matrix addition function
5884
addMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), rows, cols);
5985

60-
std::vector<TypeParam> expectedSum = {8, 10, 12, 14, 16, 18};
61-
ASSERT_EQ(nonSquareResult.size(), expectedSum.size());
62-
for (size_t i = 0; i < expectedSum.size(); i++) {
63-
EXPECT_NEAR(nonSquareResult[i], expectedSum[i], 1e-5)
64-
<< "Mismatch at index " << i;
65-
}
86+
// Verify result
87+
auto expectedSum = this->createMatrix({8, 10, 12, 14, 16, 18});
88+
this->verifyResult(nonSquareResult, expectedSum);
6689
}
6790

68-
TYPED_TEST(MatrixOperationsTest, NonSquareMultiplicationTest) {
69-
const int rowsA = 2;
70-
const int colsA = 3;
71-
const int colsB = 2;
72-
std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6};
73-
std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12};
91+
// Test case 4: Non-square matrix multiplication
92+
TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixMultiplication) {
93+
constexpr int rowsA = 2;
94+
constexpr int colsA = 3;
95+
constexpr int colsB = 2;
96+
97+
// Prepare test data
98+
auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6});
99+
auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12});
74100
std::vector<TypeParam> nonSquareResult(rowsA * colsB);
75101

76-
multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(),
77-
rowsA, colsA, colsB);
102+
// Call GPU matrix multiplication function
103+
multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), rowsA, colsA, colsB);
78104

79-
std::vector<TypeParam> expectedProduct = {58, 64, 139, 154};
80-
ASSERT_EQ(nonSquareResult.size(), expectedProduct.size());
81-
for (size_t i = 0; i < expectedProduct.size(); i++) {
82-
EXPECT_NEAR(nonSquareResult[i], expectedProduct[i], 1e-5)
83-
<< "Mismatch at index " << i;
84-
}
105+
// Verify result
106+
auto expectedProduct = this->createMatrix({58, 64, 139, 154});
107+
this->verifyResult(nonSquareResult, expectedProduct);
85108
}
109+
110+
// Register test cases
111+
REGISTER_TYPED_TEST_SUITE_P(
112+
MatrixOperationsTest,
113+
SquareMatrixAddition,
114+
SquareMatrixMultiplication,
115+
NonSquareMatrixAddition,
116+
NonSquareMatrixMultiplication
117+
);
118+
119+
// Specify test types
120+
using TestTypes = testing::Types<float, double>;
121+
INSTANTIATE_TYPED_TEST_SUITE_P(MatrixOps, MatrixOperationsTest, TestTypes);

template/cuda/{{cookiecutter.project_slug}}/tests/xmake.lua

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Add Google Test package
2-
add_requires("gtest")
2+
add_requires("gtest", {configs = {main = true}})
33

44
-- Add test target
55
target("{{cookiecutter.project_slug}}-tests")
@@ -8,7 +8,6 @@ target("{{cookiecutter.project_slug}}-tests")
88
add_files("*.cpp")
99
add_deps("{{cookiecutter.package_name}}_lib")
1010
add_packages("gtest", "cuda")
11-
add_links("cudart", "cublas")
1211

1312
-- Define test run command
1413
-- after_build(function (target)

0 commit comments

Comments
 (0)