|
| 1 | + |
| 2 | +#include "../DirectSystemSolve.hpp" |
| 3 | +#include "common/pmpl.hpp" |
| 4 | + |
| 5 | +#include <gtest/gtest.h> |
| 6 | + |
| 7 | +using namespace hpcReact; |
| 8 | +// TEST( testDirectSystemSolve, test3x3 ) |
| 9 | +// { |
| 10 | +// // **Define a Sample NxN Linear System** |
| 11 | +// double A_host[9] = |
| 12 | +// { |
| 13 | +// 1.0, 2.0, 3.0, |
| 14 | +// 2.0, -1.0, 1.0, |
| 15 | +// 3.0, 4.0, 5.0 |
| 16 | +// }; |
| 17 | +// double b_host[3] = { 14.0, 3.0, 24.0 }; // Right-hand side |
| 18 | +// double x_host[3]; // Solution |
| 19 | + |
| 20 | +// // **Allocate Memory on the GPU** |
| 21 | +// double *d_A, *d_b, *d_x; |
| 22 | +// cudaMalloc(&d_A, sizeof(A_host)); |
| 23 | +// cudaMalloc(&d_b, sizeof(b_host)); |
| 24 | +// cudaMalloc(&d_x, sizeof(x_host)); |
| 25 | + |
| 26 | +// // **Copy Data to the GPU** |
| 27 | +// cudaMemcpy(d_A, A_host, sizeof(A_host), cudaMemcpyHostToDevice); |
| 28 | +// cudaMemcpy(d_b, b_host, sizeof(b_host), cudaMemcpyHostToDevice); |
| 29 | + |
| 30 | +// // **Launch Kernel (1 block, 1 thread per system)** |
| 31 | +// kernel_solveNxN_pivoted<double,3><<<1, 1>>>(d_A, d_b, d_x, 1); |
| 32 | + |
| 33 | +// // **Copy Result Back to Host** |
| 34 | +// cudaMemcpy(x_host, d_x, sizeof(x_host), cudaMemcpyDeviceToHost); |
| 35 | + |
| 36 | +// // **Print the Solution** |
| 37 | +// std::cout << "Solution: x = [" << x_host[0] << ", " << x_host[1] << ", " << x_host[2] << "]" << std::endl; |
| 38 | + |
| 39 | +// // **Free GPU Memory** |
| 40 | +// cudaFree(d_A); |
| 41 | +// cudaFree(d_b); |
| 42 | +// cudaFree(d_x); |
| 43 | + |
| 44 | +// } |
| 45 | + |
| 46 | +template< typename REAL_TYPE, int N > |
| 47 | +struct LinearSystem |
| 48 | +{ |
| 49 | + REAL_TYPE A[N][N]; |
| 50 | + REAL_TYPE b[N]; |
| 51 | + REAL_TYPE x[N]; |
| 52 | +}; |
| 53 | + |
| 54 | +TEST( testDirectSystemSolve, test3x3 ) |
| 55 | +{ |
| 56 | + // **Define a Sample NxN Linear System** |
| 57 | + |
| 58 | + LinearSystem< double, 3 > linearSystem |
| 59 | + { |
| 60 | + { { 1.0, 2.0, 3.0 }, |
| 61 | + { 2.0, -1.0, 1.0 }, |
| 62 | + { 3.0, 4.0, 5.0 } |
| 63 | + }, |
| 64 | + { 14.0, 3.0, 24.0 }, // Right-hand side |
| 65 | + { 0.0, 0.0, 0.0 } // Solution |
| 66 | + }; |
| 67 | + |
| 68 | + pmpl::genericKernelWrapper( 1, &linearSystem, [&]( auto * copyOfLinearSystem ) |
| 69 | + { |
| 70 | + solveNxN_pivoted<double,3>( copyOfLinearSystem->A, copyOfLinearSystem->b, copyOfLinearSystem->x ); |
| 71 | + } ); |
| 72 | + |
| 73 | + EXPECT_NEAR( linearSystem.x[0], 0.0, std::numeric_limits< double >::epsilon()*100 ); |
| 74 | + EXPECT_NEAR( linearSystem.x[1], 1.0, std::numeric_limits< double >::epsilon()*100 ); |
| 75 | + EXPECT_NEAR( linearSystem.x[2], 4.0, std::numeric_limits< double >::epsilon()*100 ); |
| 76 | +// std::cout << "Solution: x = [" << linearSystem.x[0] << ", " << linearSystem.x[1] << ", " << linearSystem.x[2] << "]" << std::endl; |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +#if 0 |
| 82 | +TEST( testDirectSystemSolve, test3x3_CUDA ) |
| 83 | +{ |
| 84 | + // **Define a Sample NxN Linear System** |
| 85 | + double A_host[9] = |
| 86 | + { |
| 87 | + 1.0, 2.0, 3.0, |
| 88 | + 2.0, -1.0, 1.0, |
| 89 | + 3.0, 4.0, 5.0 |
| 90 | + }; |
| 91 | + double b_host[3] = { 14.0, 3.0, 24.0 }; // Right-hand side |
| 92 | + double x_host[3]; // Solution |
| 93 | + |
| 94 | + // **Allocate Memory on the GPU** |
| 95 | + double *d_A, *d_b, *d_x; |
| 96 | + cudaMalloc(&d_A, sizeof(A_host)); |
| 97 | + cudaMalloc(&d_b, sizeof(b_host)); |
| 98 | + cudaMalloc(&d_x, sizeof(x_host)); |
| 99 | + |
| 100 | + // **Copy Data to the GPU** |
| 101 | + cudaMemcpy(d_A, A_host, sizeof(A_host), cudaMemcpyHostToDevice); |
| 102 | + cudaMemcpy(d_b, b_host, sizeof(b_host), cudaMemcpyHostToDevice); |
| 103 | + |
| 104 | + // **Launch Kernel (1 block, 1 thread per system)** |
| 105 | + kernel_solveNxN_pivoted<double,3><<<1, 1>>>(d_A, d_b, d_x, 1); |
| 106 | + |
| 107 | + // **Copy Result Back to Host** |
| 108 | + cudaMemcpy(x_host, d_x, sizeof(x_host), cudaMemcpyDeviceToHost); |
| 109 | + |
| 110 | + // **Print the Solution** |
| 111 | + std::cout << "Solution: x = [" << x_host[0] << ", " << x_host[1] << ", " << x_host[2] << "]" << std::endl; |
| 112 | + |
| 113 | + // **Free GPU Memory** |
| 114 | + cudaFree(d_A); |
| 115 | + cudaFree(d_b); |
| 116 | + cudaFree(d_x); |
| 117 | + |
| 118 | +} |
| 119 | +#endif |
| 120 | + |
| 121 | + |
| 122 | +int main( int argc, char * * argv ) |
| 123 | +{ |
| 124 | + ::testing::InitGoogleTest( &argc, argv ); |
| 125 | + int const result = RUN_ALL_TESTS(); |
| 126 | + return result; |
| 127 | +} |
0 commit comments