-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cuda): parameterize matrix operations tests and update build con…
…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
Showing
5 changed files
with
94 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 0 additions & 6 deletions
6
template/cuda/{{cookiecutter.project_slug}}/tests/test_main.cpp
This file was deleted.
Oops, something went wrong.
146 changes: 91 additions & 55 deletions
146
template/cuda/{{cookiecutter.project_slug}}/tests/test_matrix.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters