Skip to content

Commit

Permalink
test(cuda): parameterize matrix operations tests and update build con…
Browse files Browse the repository at this point in the history
…figuration

- 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
  • Loading branch information
pplmx committed Nov 29, 2024
1 parent bd67bee commit 23c1344
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 64 deletions.
1 change: 1 addition & 0 deletions template/cuda/{{cookiecutter.project_slug}}/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ file(GLOB_RECURSE SOURCES CONFIGURE_DEPENDS
${SRC_DIR}/*.cpp
${SRC_DIR}/*.cu
)
list(FILTER SOURCES EXCLUDE REGEX ".*main\\.cpp$")
file(GLOB_RECURSE HEADERS CONFIGURE_DEPENDS
${INCLUDE_DIR}/*.h
)
Expand Down
2 changes: 1 addition & 1 deletion template/cuda/{{cookiecutter.project_slug}}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ build:
@cmake -S . -B build
@cmake --build build --parallel
{%- else %}
@xmake -y
@XMAKE_ROOT=y xmake
{%- endif %}

# run
Expand Down

This file was deleted.

146 changes: 91 additions & 55 deletions template/cuda/{{cookiecutter.project_slug}}/tests/test_matrix.cpp
Original file line number Diff line number Diff line change
@@ -1,85 +1,121 @@
#include <gtest/gtest.h>
#include <vector>

#include "matrix_add.h"
#include "matrix_mult.h"
#include <vector>

// Template-based parameterized test for matrix operations
template <typename T>
class MatrixOperationsTest : public ::testing::Test {
class MatrixOperationsTest : public testing::Test {
protected:
static constexpr int kMatrixSize = 2;
static constexpr int kMatrixElements = kMatrixSize * kMatrixSize;

std::vector<T> matrixA;
std::vector<T> matrixB;
std::vector<T> resultMatrix;

void SetUp() override {
// Initialize matrices A and B with test data
matrixA = {1, 2, 3, 4};
matrixB = {5, 6, 7, 8};
resultMatrix.resize(kMatrixElements);
// Static constants for default matrix configuration
static constexpr int kDefaultMatrixSize = 2;
static constexpr int kDefaultMatrixElements = kDefaultMatrixSize * kDefaultMatrixSize;

// Helper function to create a matrix from initializer list
std::vector<T> createMatrix(std::initializer_list<T> values) {
return std::vector<T>(values);
}

void verifyResult(const std::vector<T>& expected) {
ASSERT_EQ(resultMatrix.size(), expected.size());
// Helper function to verify matrix calculation results
static void verifyResult(const std::vector<T>& result, const std::vector<T>& expected) {
// Check matrix size
ASSERT_EQ(result.size(), expected.size())
<< "Result matrix size does not match expected matrix size";

// Compare elements with near-equality
for (size_t i = 0; i < expected.size(); i++) {
EXPECT_NEAR(resultMatrix[i], expected[i], 1e-5)
<< "Mismatch at index " << i;
EXPECT_NEAR(result[i], expected[i], 1e-5)
<< "Mismatch at index " << i
<< ": expected " << expected[i]
<< ", got " << result[i];
}
}
};

using TestTypes = ::testing::Types<float, double>;
TYPED_TEST_SUITE(MatrixOperationsTest, TestTypes);
// Register type-parameterized test suite
TYPED_TEST_SUITE_P(MatrixOperationsTest);

// Test case 1: Square matrix addition
TYPED_TEST_P(MatrixOperationsTest, SquareMatrixAddition) {
constexpr int size = this->kDefaultMatrixSize;

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

std::vector<TypeParam> expectedSum = {6, 8, 10, 12};
this->verifyResult(expectedSum);
// Call GPU matrix addition function
addMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size);

// Verify result
auto expectedSum = this->createMatrix({6, 8, 10, 12});
this->verifyResult(resultMatrix, expectedSum);
}

TYPED_TEST(MatrixOperationsTest, MultiplicationTest) {
multiplyMatricesOnGPU(this->matrixA.data(), this->matrixB.data(), this->resultMatrix.data(),
this->kMatrixSize, this->kMatrixSize, this->kMatrixSize);
// Test case 2: Square matrix multiplication
TYPED_TEST_P(MatrixOperationsTest, SquareMatrixMultiplication) {
constexpr int size = this->kDefaultMatrixSize;

// Prepare test data
auto matrixA = this->createMatrix({1, 2, 3, 4});
auto matrixB = this->createMatrix({5, 6, 7, 8});
std::vector<TypeParam> resultMatrix(size * size);

std::vector<TypeParam> expectedProduct = {19, 22, 43, 50};
this->verifyResult(expectedProduct);
// Call GPU matrix multiplication function
multiplyMatricesOnGPU(matrixA.data(), matrixB.data(), resultMatrix.data(), size, size, size);

// Verify result
auto expectedProduct = this->createMatrix({19, 22, 43, 50});
this->verifyResult(resultMatrix, expectedProduct);
}

TYPED_TEST(MatrixOperationsTest, NonSquareAdditionTest) {
const int rows = 2;
const int cols = 3;
std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6};
std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12};
// Test case 3: Non-square matrix addition
TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixAddition) {
constexpr int rows = 2;
constexpr int cols = 3;

// Prepare test data
auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6});
auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12});
std::vector<TypeParam> nonSquareResult(rows * cols);

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

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

TYPED_TEST(MatrixOperationsTest, NonSquareMultiplicationTest) {
const int rowsA = 2;
const int colsA = 3;
const int colsB = 2;
std::vector<TypeParam> nonSquareA = {1, 2, 3, 4, 5, 6};
std::vector<TypeParam> nonSquareB = {7, 8, 9, 10, 11, 12};
// Test case 4: Non-square matrix multiplication
TYPED_TEST_P(MatrixOperationsTest, NonSquareMatrixMultiplication) {
constexpr int rowsA = 2;
constexpr int colsA = 3;
constexpr int colsB = 2;

// Prepare test data
auto nonSquareA = this->createMatrix({1, 2, 3, 4, 5, 6});
auto nonSquareB = this->createMatrix({7, 8, 9, 10, 11, 12});
std::vector<TypeParam> nonSquareResult(rowsA * colsB);

multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(),
rowsA, colsA, colsB);
// Call GPU matrix multiplication function
multiplyMatricesOnGPU(nonSquareA.data(), nonSquareB.data(), nonSquareResult.data(), rowsA, colsA, colsB);

std::vector<TypeParam> expectedProduct = {58, 64, 139, 154};
ASSERT_EQ(nonSquareResult.size(), expectedProduct.size());
for (size_t i = 0; i < expectedProduct.size(); i++) {
EXPECT_NEAR(nonSquareResult[i], expectedProduct[i], 1e-5)
<< "Mismatch at index " << i;
}
// Verify result
auto expectedProduct = this->createMatrix({58, 64, 139, 154});
this->verifyResult(nonSquareResult, expectedProduct);
}

// Register test cases
REGISTER_TYPED_TEST_SUITE_P(
MatrixOperationsTest,
SquareMatrixAddition,
SquareMatrixMultiplication,
NonSquareMatrixAddition,
NonSquareMatrixMultiplication
);

// Specify test types
using TestTypes = testing::Types<float, double>;
INSTANTIATE_TYPED_TEST_SUITE_P(MatrixOps, MatrixOperationsTest, TestTypes);
3 changes: 1 addition & 2 deletions template/cuda/{{cookiecutter.project_slug}}/tests/xmake.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-- Add Google Test package
add_requires("gtest")
add_requires("gtest", {configs = {main = true}})

-- Add test target
target("{{cookiecutter.project_slug}}-tests")
Expand All @@ -8,7 +8,6 @@ target("{{cookiecutter.project_slug}}-tests")
add_files("*.cpp")
add_deps("{{cookiecutter.package_name}}_lib")
add_packages("gtest", "cuda")
add_links("cudart", "cublas")

-- Define test run command
-- after_build(function (target)
Expand Down

0 comments on commit 23c1344

Please sign in to comment.