diff --git a/main.cpp b/main.cpp index b9090bb5171e1fa819c2c7493105d826672147d3..4b6db4434a91a7c16b4abcab3d45c2dc7745cc47 100644 --- a/main.cpp +++ b/main.cpp @@ -94,9 +94,59 @@ int main() { std::cout << elapsed_us << " s " << std::endl; + } + // 2. --- Basic Matrix Operations Test --- + std::cout << "\n--- Basic Matrix Operations Test ---" << std::endl; + + std::vector<float> dataA = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f}; // 2x3 + std::vector<float> dataB = {7.0f, 8.0f, 9.0f, 10.0f, 11.0f, 12.0f}; // 2x3 + std::vector<float> dataC = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f}; // 3x3 + + MatrixCL matA(2, 3, context, queue, &dataA); + MatrixCL matB(2, 3, context, queue, &dataB); + MatrixCL matC(3, 3, context, queue, &dataC); + MatrixCL matD(2, 3, context, queue); // Initialized to 0 by constructor + + printMatrix("Matrix A (original)", matA); + printMatrix("Matrix B", matB); + printMatrix("Matrix C (3x3)", matC); + printMatrix("Matrix D (initially zero)", matD); + + // Test fill + matD.fill(5.5f); + printMatrix("Matrix D after fill(5.5)", matD); + assert(verifyMatrix("Matrix D fill", matD, {5.5f, 5.5f, 5.5f, 5.5f, 5.5f, 5.5f})); + + // Test Copy Constructor + MatrixCL matA_copy(matA); + printMatrix("Matrix A Copy (via copy constructor)", matA_copy); + assert(verifyMatrix("Matrix A Copy Ctor", matA_copy, dataA)); + + // Test Copy Assignment Operator + MatrixCL matD_assigned(1, 1, context, queue); // Create with different dimensions + matD_assigned = matD; + printMatrix("Matrix D Assigned (via assignment operator)", matD_assigned); + assert(verifyMatrix("Matrix D Assignment Op", matD_assigned, {5.5f, 5.5f, 5.5f, 5.5f, 5.5f, 5.5f})); + + + // Test Addition + MatrixCL matAdd = matA + matB; + printMatrix("Matrix A + B", matAdd); + assert(verifyMatrix("Matrix A + B", matAdd, {8.0f, 10.0f, 12.0f, 14.0f, 16.0f, 18.0f})); + + // Test Transpose + MatrixCL matATrans = matA.transpose(); + printMatrix("Matrix A Transposed", matATrans); // Should be 3x2 + assert(verifyMatrix("Matrix A Transposed", matATrans, {1.0f, 4.0f, 2.0f, 5.0f, 3.0f, 6.0f})); + + // Test Matrix Multiplication: A(2x3) * C(3x3) -> Result(2x3) + // Expected: [ (1*1+2*4+3*7) (1*2+2*5+3*8) (1*3+2*6+3*9) ] = [ 30 36 42 ] + // [ (4*1+5*4+6*7) (4*2+5*5+6*8) (4*3+5*6+6*9) ] = [ 66 81 96 ] + MatrixCL matMul = matA * matC; + printMatrix("Matrix A * C", matMul); + assert(verifyMatrix("Matrix A * C", matMul, {30.0f, 36.0f, 42.0f, 66.0f, 81.0f, 96.0f})); - } } catch (const cl::BuildError& err) { std::cerr << "OpenCL Build Error: " << err.what() << " (" << err.err() << ")" << std::endl;