diff --git a/resolve/workspace/LinAlgWorkspaceCUDA.cpp b/resolve/workspace/LinAlgWorkspaceCUDA.cpp index fd9a7d9d..9dc953a1 100644 --- a/resolve/workspace/LinAlgWorkspaceCUDA.cpp +++ b/resolve/workspace/LinAlgWorkspaceCUDA.cpp @@ -23,7 +23,9 @@ namespace ReSolve cusparseDestroy(handle_cusparse_); cusolverSpDestroy(handle_cusolversp_); cublasDestroy(handle_cublas_); - cusparseDestroySpMat(mat_A_); + if (matvec_setup_done_) { + cusparseDestroySpMat(mat_A_); + } } void* LinAlgWorkspaceCUDA::getSpmvBuffer() @@ -53,7 +55,7 @@ namespace ReSolve void LinAlgWorkspaceCUDA::setNormBufferState(bool r) { - norm_buffer_ready_ = r;; + norm_buffer_ready_ = r; } cusparseHandle_t LinAlgWorkspaceCUDA::getCusparseHandle() diff --git a/resolve/workspace/LinAlgWorkspaceHIP.cpp b/resolve/workspace/LinAlgWorkspaceHIP.cpp index ccfa38c0..68f6dae1 100644 --- a/resolve/workspace/LinAlgWorkspaceHIP.cpp +++ b/resolve/workspace/LinAlgWorkspaceHIP.cpp @@ -18,7 +18,9 @@ namespace ReSolve { rocsparse_destroy_handle(handle_rocsparse_); rocblas_destroy_handle(handle_rocblas_); - rocsparse_destroy_mat_descr(mat_A_); + if (matvec_setup_done_) { + rocsparse_destroy_mat_descr(mat_A_); + } if (d_r_size_ != 0) mem_.deleteOnDevice(d_r_); if (norm_buffer_ready_ == true) mem_.deleteOnDevice(norm_buffer_); } diff --git a/tests/unit/matrix/MatrixHandlerTests.hpp b/tests/unit/matrix/MatrixHandlerTests.hpp index e107927c..f2aa54fc 100644 --- a/tests/unit/matrix/MatrixHandlerTests.hpp +++ b/tests/unit/matrix/MatrixHandlerTests.hpp @@ -18,8 +18,15 @@ namespace ReSolve { namespace tests { class MatrixHandlerTests : TestBase { public: - MatrixHandlerTests(std::string memspace) : memspace_(memspace) - {} + MatrixHandlerTests(ReSolve::MatrixHandler& handler) : handler_(handler) + { + if (handler_.getIsCudaEnabled() || handler_.getIsHipEnabled()) { + memspace_ = memory::DEVICE; + } else { + memspace_ = memory::HOST; + } + } + virtual ~MatrixHandlerTests() {} @@ -34,88 +41,52 @@ class MatrixHandlerTests : TestBase TestOutcome matrixInfNorm(index_type N) { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::MatrixHandler* handler = createMatrixHandler(); - matrix::Csr* A = createCsrMatrix(N, memspace_); + matrix::Csr* A = createCsrMatrix(N); real_type norm; - handler->matrixInfNorm(A, &norm, ms); + handler_.matrixInfNorm(A, &norm, memspace_); status *= (norm == 30.0); - delete handler; delete A; + return status.report(__func__); } TestOutcome matVec(index_type N) { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::MatrixHandler* handler = createMatrixHandler(); - matrix::Csr* A = createCsrMatrix(N, memspace_); + matrix::Csr* A = createCsrMatrix(N); vector::Vector x(N); vector::Vector y(N); - x.allocate(ms); - if (x.getData(ms) == NULL) printf("oups we have an issue \n"); - y.allocate(ms); + x.allocate(memspace_); + if (x.getData(memspace_) == NULL) + std::cout << "Oups we have an issue \n"; + y.allocate(memspace_); - x.setToConst(1.0, ms); - y.setToConst(1.0, ms); + x.setToConst(1.0, memspace_); + y.setToConst(1.0, memspace_); real_type alpha = 2.0/30.0; real_type beta = 2.0; - handler->setValuesChanged(true, ms); - handler->matvec(A, &x, &y, &alpha, &beta, "csr", ms); + handler_.setValuesChanged(true, memspace_); + handler_.matvec(A, &x, &y, &alpha, &beta, "csr", memspace_); - status *= verifyAnswer(y, 4.0, memspace_); + status *= verifyAnswer(y, 4.0); - delete handler; delete A; return status.report(__func__); } private: - std::string memspace_{"cpu"}; - - ReSolve::MatrixHandler* createMatrixHandler() - { - if (memspace_ == "cpu") { - LinAlgWorkspaceCpu* workspace = new LinAlgWorkspaceCpu(); - return new MatrixHandler(workspace); -#ifdef RESOLVE_USE_CUDA - } else if (memspace_ == "cuda") { - LinAlgWorkspaceCUDA* workspace = new LinAlgWorkspaceCUDA(); - workspace->initializeHandles(); - return new MatrixHandler(workspace); -#endif -#ifdef RESOLVE_USE_HIP - } else if (memspace_ == "hip") { - LinAlgWorkspaceHIP* workspace = new LinAlgWorkspaceHIP(); - workspace->initializeHandles(); - return new MatrixHandler(workspace); -#endif - } else { - std::cout << "ReSolve not built with support for memory space " << memspace_ << "\n"; - } - return nullptr; - } + ReSolve::MatrixHandler& handler_; + memory::MemorySpace memspace_{memory::HOST}; - bool verifyAnswer(vector::Vector& x, real_type answer, std::string memspace) + bool verifyAnswer(vector::Vector& x, real_type answer) { bool status = true; - if (memspace != "cpu") { + if (memspace_ == memory::DEVICE) { x.copyData(memory::DEVICE, memory::HOST); } @@ -131,7 +102,7 @@ class MatrixHandlerTests : TestBase return status; } - matrix::Csr* createCsrMatrix(const index_type N, std::string memspace) + matrix::Csr* createCsrMatrix(const index_type N) { std::vector r1 = {1., 5., 7., 8., 3., 2., 4.}; // sum 30 std::vector r2 = {1., 3., 2., 2., 1., 6., 7., 3., 2., 3.}; // sum 30 @@ -177,8 +148,8 @@ class MatrixHandlerTests : TestBase } A->setUpdated(memory::HOST); - if ((memspace == "cuda") || (memspace == "hip")) { - A->copyData(memory::DEVICE); + if (memspace_ == memory::DEVICE) { + A->copyData(memspace_); } return A; diff --git a/tests/unit/matrix/runMatrixHandlerTests.cpp b/tests/unit/matrix/runMatrixHandlerTests.cpp index 0837346e..9ed25247 100644 --- a/tests/unit/matrix/runMatrixHandlerTests.cpp +++ b/tests/unit/matrix/runMatrixHandlerTests.cpp @@ -11,8 +11,12 @@ int main(int, char**) { std::cout << "Running tests on CPU:\n"; - ReSolve::tests::MatrixHandlerTests test("cpu"); - + + ReSolve::LinAlgWorkspaceCpu workspace; + workspace.initializeHandles(); + ReSolve::MatrixHandler handler(&workspace); + + ReSolve::tests::MatrixHandlerTests test(handler); result += test.matrixHandlerConstructor(); result += test.matrixInfNorm(10000); result += test.matVec(50); @@ -23,8 +27,11 @@ int main(int, char**) #ifdef RESOLVE_USE_CUDA { std::cout << "Running tests with CUDA backend:\n"; - ReSolve::tests::MatrixHandlerTests test("cuda"); + ReSolve::LinAlgWorkspaceCUDA workspace; + workspace.initializeHandles(); + ReSolve::MatrixHandler handler(&workspace); + ReSolve::tests::MatrixHandlerTests test(handler); result += test.matrixHandlerConstructor(); result += test.matrixInfNorm(1000000); result += test.matVec(50); @@ -36,8 +43,11 @@ int main(int, char**) #ifdef RESOLVE_USE_HIP { std::cout << "Running tests with HIP backend:\n"; - ReSolve::tests::MatrixHandlerTests test("hip"); + ReSolve::LinAlgWorkspaceHIP workspace; + workspace.initializeHandles(); + ReSolve::MatrixHandler handler(&workspace); + ReSolve::tests::MatrixHandlerTests test(handler); result += test.matrixHandlerConstructor(); result += test.matrixInfNorm(1000000); result += test.matVec(50); diff --git a/tests/unit/vector/GramSchmidtTests.hpp b/tests/unit/vector/GramSchmidtTests.hpp index 177fec09..5f7f020c 100644 --- a/tests/unit/vector/GramSchmidtTests.hpp +++ b/tests/unit/vector/GramSchmidtTests.hpp @@ -18,8 +18,12 @@ namespace ReSolve class GramSchmidtTests : TestBase { public: - GramSchmidtTests(ReSolve::VectorHandler* handler) : handler_(handler) + GramSchmidtTests(ReSolve::VectorHandler& handler) : handler_(handler) { + if (handler_.getIsCudaEnabled() || handler_.getIsHipEnabled()) + memspace_ = memory::DEVICE; + else + memspace_ = memory::HOST; } virtual ~GramSchmidtTests() @@ -62,24 +66,20 @@ namespace ReSolve break; } - ReSolve::memory::MemorySpace ms; - if (handler_->getIsCudaEnabled() || handler_->getIsHipEnabled()) - ms = memory::DEVICE; - else - ms = memory::HOST; - - vector::Vector* V = new vector::Vector(N, 3); // we will be using a space of 3 vectors + vector::Vector V(N, 3); // we will be using a space of 3 vectors real_type* H = new real_type[9]; // In this case, Hessenberg matrix is NOT 3 x 2 ??? real_type* aux_data = nullptr; // needed for setup - V->allocate(memory::DEVICE); - V->allocate(memory::HOST); + V.allocate(memspace_); + if (memspace_ == memory::DEVICE) { + V.allocate(memory::HOST); + } - ReSolve::GramSchmidt* GS = new ReSolve::GramSchmidt(handler_, var); - GS->setup(N, 3); + ReSolve::GramSchmidt GS(&handler_, var); + GS.setup(N, 3); //fill 2nd and 3rd vector with values - aux_data = V->getVectorData(1, memory::HOST); + aux_data = V.getVectorData(1, memory::HOST); for (int i = 0; i < N; ++i) { if ( i % 2 == 0) { aux_data[i] = constants::ONE; @@ -87,7 +87,7 @@ namespace ReSolve aux_data[i] = var1; } } - aux_data = V->getVectorData(2, memory::HOST); + aux_data = V.getVectorData(2, memory::HOST); for (int i = 0; i < N; ++i) { if ( i % 3 > 0) { aux_data[i] = constants::ZERO; @@ -95,66 +95,59 @@ namespace ReSolve aux_data[i] = var2; } } - V->setDataUpdated(memory::HOST); - V->copyData(memory::HOST, ms); + V.setDataUpdated(memory::HOST); + V.copyData(memory::HOST, memspace_); //set the first vector to all 1s, normalize - V->setToConst(0, 1.0, ms); - real_type nrm = handler_->dot(V, V, ms); + V.setToConst(0, 1.0, memspace_); + real_type nrm = handler_.dot(&V, &V, memspace_); nrm = sqrt(nrm); nrm = 1.0 / nrm; - handler_->scal(&nrm, V, ms); + handler_.scal(&nrm, &V, memspace_); - GS->orthogonalize(N, V, H, 0); - GS->orthogonalize(N, V, H, 1); + GS.orthogonalize(N, &V, H, 0); + GS.orthogonalize(N, &V, H, 1); status *= verifyAnswer(V, 3); delete [] H; - delete V; - delete GS; return status.report(testname.c_str()); } private: - ReSolve::VectorHandler* handler_{nullptr}; + ReSolve::VectorHandler& handler_; + ReSolve::memory::MemorySpace memspace_; // x is a multivector containing K vectors - bool verifyAnswer(vector::Vector* x, index_type K) + bool verifyAnswer(vector::Vector& x, index_type K) { - ReSolve::memory::MemorySpace ms; - if (handler_->getIsCudaEnabled() || handler_->getIsHipEnabled()) - ms = memory::DEVICE; - else - ms = memory::HOST; - - vector::Vector* a = new vector::Vector(x->getSize()); - vector::Vector* b = new vector::Vector(x->getSize()); + vector::Vector a(x.getSize()); + vector::Vector b(x.getSize()); real_type ip; bool status = true; for (index_type i = 0; i < K; ++i) { for (index_type j = 0; j < K; ++j) { - a->update(x->getVectorData(i, ms), ms, memory::HOST); - b->update(x->getVectorData(j, ms), ms, memory::HOST); - ip = handler_->dot(a, b, memory::HOST); + a.update(x.getVectorData(i, memspace_), memspace_, memory::HOST); + b.update(x.getVectorData(j, memspace_), memspace_, memory::HOST); + ip = handler_.dot(&a, &b, memory::HOST); if ( (i != j) && !isEqual(ip, 0.0)) { status = false; std::cout << "Vectors " << i << " and " << j << " are not orthogonal!" - << " Inner product computed: " << ip << ", expected: " << 0.0 << "\n"; + << " Inner product computed: " << ip << ", expected: " << 0.0 << "\n"; break; } if ( (i == j) && !isEqual(sqrt(ip), 1.0)) { status = false; std::cout << std::setprecision(16); - std::cout << "Vector " << i << " has norm: " << sqrt(ip) << " expected: "<< 1.0 <<"\n"; + std::cout << "Vector " << i << " has norm: " << sqrt(ip) + << " expected: " << 1.0 << "\n"; break; } } } - delete a; - delete b; + return status; } }; // class diff --git a/tests/unit/vector/VectorHandlerTests.hpp b/tests/unit/vector/VectorHandlerTests.hpp index b1447f6d..bbaa7c9d 100644 --- a/tests/unit/vector/VectorHandlerTests.hpp +++ b/tests/unit/vector/VectorHandlerTests.hpp @@ -19,9 +19,14 @@ namespace ReSolve { class VectorHandlerTests : TestBase { public: - VectorHandlerTests(std::string memspace) : memspace_(memspace) - { - } + VectorHandlerTests(ReSolve::VectorHandler& handler) : handler_(handler) + { + if (handler_.getIsCudaEnabled() || handler_.getIsHipEnabled()) { + memspace_ = memory::DEVICE; + } else { + memspace_ = memory::HOST; + } + } virtual ~VectorHandlerTests() { @@ -38,33 +43,24 @@ namespace ReSolve { TestOutcome infNorm(index_type N) { TestStatus status; - ReSolve::memory::MemorySpace ms; + status = true; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); - - vector::Vector* x = new vector::Vector(N); + vector::Vector x(N); real_type* data = new real_type[N]; for (int i = 0; i < N; ++i) { data[i] = 0.1 * (real_type) i; } - x->update(data, memory::HOST, ms); + x.update(data, memory::HOST, memspace_); - real_type ans = handler->infNorm(x, ms); - bool st = true; - if (ans != (real_type) (N - 1) * 0.1) { - st = false; - printf("the wrong answer is %f expecting %f \n", ans, (real_type) N); - } - status *= st; + real_type result = handler_.infNorm(&x, memspace_); + real_type answer = static_cast(N - 1) * 0.1; - delete handler; - delete x; + if (!isEqual(result, answer)) { + std::cout << "The result " << result << " is incorrect. " + << "Expected answer is " << answer << "\n"; + status *= false; + } return status.report(__func__); } @@ -73,31 +69,20 @@ namespace ReSolve { { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); + vector::Vector x(N); + vector::Vector y(N); - vector::Vector* x = new vector::Vector(N); - vector::Vector* y = new vector::Vector(N); + x.allocate(memspace_); + y.allocate(memspace_); - x->allocate(ms); - y->allocate(ms); - - x->setToConst(3.0, ms); - y->setToConst(1.0, ms); + x.setToConst(3.0, memspace_); + y.setToConst(1.0, memspace_); real_type alpha = 0.5; - //the result is a vector with y[i] = 2.5; - handler->axpy(&alpha, x, y, ms); - status *= verifyAnswer(y, 2.5, memspace_); - delete handler; - delete x; - delete y; + //the result is a vector with y[i] = 2.5 forall i; + handler_.axpy(&alpha, &x, &y, memspace_); + status *= verifyAnswer(y, 2.5); return status.report(__func__); } @@ -105,37 +90,26 @@ namespace ReSolve { TestOutcome dot(index_type N) { TestStatus status; + status = true; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); + vector::Vector x(N); + vector::Vector y(N); - vector::Vector* x = new vector::Vector(N); - vector::Vector* y = new vector::Vector(N); + x.allocate(memspace_); + y.allocate(memspace_); - x->allocate(ms); - y->allocate(ms); + x.setToConst(0.25, memspace_); + y.setToConst(4.0, memspace_); - x->setToConst(0.25, ms); - y->setToConst(4.0, ms); - real_type ans; - //the result is N - ans = handler->dot(x, y, ms); + // the answer is N + real_type answer = static_cast(N); + real_type result = handler_.dot(&x, &y, memspace_); - bool st = true;; - if (ans != (real_type) N) { - st = false; - printf("the wrong answer is %f expecting %f \n", ans, (real_type) N); + if (!isEqual(result, answer)) { + std::cout << "The result " << result << " is incorrect. " + << "Expected answer is " << answer << "\n"; + status *= false; } - status *= st; - - delete handler; - delete x; - delete y; return status.report(__func__); } @@ -144,28 +118,18 @@ namespace ReSolve { { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); - - vector::Vector* x = new vector::Vector(N); + vector::Vector x(N); - x->allocate(ms); + x.allocate(memspace_); - x->setToConst(1.25, ms); + x.setToConst(1.25, memspace_); real_type alpha = 3.5; - //the answer is x[i] = 4.375; - handler->scal(&alpha, x, ms); - status *= verifyAnswer(x, 4.375, memspace_); - - delete handler; - delete x; + //the answer is x[i] = 4.375; + real_type answer = 4.375; + handler_.scal(&alpha, &x, memspace_); + status *= verifyAnswer(x, answer); return status.report(__func__); } @@ -174,24 +138,16 @@ namespace ReSolve { { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); - - vector::Vector* x = new vector::Vector(N, K); - vector::Vector* y = new vector::Vector(N); - vector::Vector* alpha = new vector::Vector(K);; + vector::Vector x(N, K); + vector::Vector y(N); + vector::Vector alpha(K);; - x->allocate(ms); - y->allocate(ms); - alpha->allocate(ms); + x.allocate(memspace_); + y.allocate(memspace_); + alpha.allocate(memspace_); - alpha->setToConst(-1.0, ms); - y->setToConst(2.0, ms); + alpha.setToConst(-1.0, memspace_); + y.setToConst(2.0, memspace_); for (int ii = 0; ii < K; ++ii) { real_type c; @@ -200,19 +156,14 @@ namespace ReSolve { } else { c = 0.5; } - x->setToConst(ii, c, ms); + x.setToConst(ii, c, memspace_); } index_type r = K % 2; real_type res = (real_type) ((floor((real_type) K / 2.0) + r) * 1.0 + floor((real_type) K / 2.0) * (-0.5)); - handler->massAxpy(N, alpha, K, x, y, ms); - status *= verifyAnswer(y, 2.0 - res, memspace_); - - delete handler; - delete x; - delete y; - delete alpha; + handler_.massAxpy(N, &alpha, K, &x, &y, memspace_); + status *= verifyAnswer(y, 2.0 - res); return status.report(__func__); } @@ -221,31 +172,19 @@ namespace ReSolve { { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; + vector::Vector x(N, K); + vector::Vector y(N, 2); + vector::Vector res(K, 2); + x.allocate(memspace_); + y.allocate(memspace_); + res.allocate(memspace_); - ReSolve::VectorHandler* handler = createVectorHandler(); + x.setToConst(1.0, memspace_); + y.setToConst(-1.0, memspace_); + handler_.massDot2Vec(N, &x, K, &y, &res, memspace_); - vector::Vector* x = new vector::Vector(N, K); - vector::Vector* y = new vector::Vector(N, 2); - vector::Vector* res = new vector::Vector(K, 2); - x->allocate(ms); - y->allocate(ms); - res->allocate(ms); + status *= verifyAnswer(res, (-1.0) * (real_type) N); - x->setToConst(1.0, ms); - y->setToConst(-1.0, ms); - handler->massDot2Vec(N, x, K, y, res, ms); - - status *= verifyAnswer(res, (-1.0) * (real_type) N, memspace_); - - delete handler; - delete x; - delete y; - delete res; return status.report(__func__); } @@ -253,83 +192,53 @@ namespace ReSolve { { TestStatus status; - ReSolve::memory::MemorySpace ms; - if (memspace_ == "cpu") - ms = memory::HOST; - else - ms = memory::DEVICE; - - ReSolve::VectorHandler* handler = createVectorHandler(); - vector::Vector* V = new vector::Vector(N, K); - // for the test with NO TRANSPOSE - vector::Vector* yN = new vector::Vector(K); - vector::Vector* xN = new vector::Vector(N); - // for the test with TRANSPOSE - vector::Vector* yT = new vector::Vector(N); - vector::Vector* xT = new vector::Vector(K); - - V->allocate(ms); - yN->allocate(ms); - xN->allocate(ms); - yT->allocate(ms); - xT->allocate(ms); - - V->setToConst(1.0, ms); - yN->setToConst(-1.0, ms); - xN->setToConst(.5, ms); - yT->setToConst(-1.0, ms); - xT->setToConst(.5, ms); + vector::Vector V(N, K); + vector::Vector yN(K); ///< For the test with NO TRANSPOSE + vector::Vector xN(N); + vector::Vector yT(N); ///< for the test with TRANSPOSE + vector::Vector xT(K); + + V.allocate(memspace_); + yN.allocate(memspace_); + xN.allocate(memspace_); + yT.allocate(memspace_); + xT.allocate(memspace_); + + V.setToConst(1.0, memspace_); + yN.setToConst(-1.0, memspace_); + xN.setToConst(.5, memspace_); + yT.setToConst(-1.0, memspace_); + xT.setToConst(.5, memspace_); real_type alpha = -1.0; real_type beta = 1.0; - handler->gemv('N', N, K, &alpha, &beta, V, yN, xN, ms); - status *= verifyAnswer(xN, (real_type) (K) + 0.5, memspace_); - handler->gemv('T', N, K, &alpha, &beta, V, yT, xT, ms); - status *= verifyAnswer(xT, (real_type) (N) + 0.5, memspace_); + handler_.gemv('N', N, K, &alpha, &beta, &V, &yN, &xN, memspace_); + status *= verifyAnswer(xN, static_cast(K) + 0.5); + handler_.gemv('T', N, K, &alpha, &beta, &V, &yT, &xT, memspace_); + status *= verifyAnswer(xT, static_cast(N) + 0.5); return status.report(__func__); } private: - std::string memspace_{"cpu"}; - - ReSolve::VectorHandler* createVectorHandler() - { - if (memspace_ == "cpu") { - LinAlgWorkspaceCpu* workpsace = new LinAlgWorkspaceCpu(); - return new VectorHandler(workpsace); -#ifdef RESOLVE_USE_CUDA - } else if (memspace_ == "cuda") { - LinAlgWorkspaceCUDA* workspace = new LinAlgWorkspaceCUDA(); - workspace->initializeHandles(); - return new VectorHandler(workspace); -#endif -#ifdef RESOLVE_USE_HIP - } else if (memspace_ == "hip") { - LinAlgWorkspaceHIP* workspace = new LinAlgWorkspaceHIP(); - workspace->initializeHandles(); - return new VectorHandler(workspace); -#endif - } else { - std::cout << "ReSolve not built with support for memory space " << memspace_ << "\n"; - } - return nullptr; - } + ReSolve::VectorHandler& handler_; + ReSolve::memory::MemorySpace memspace_{memory::HOST}; // we can verify through norm but that would defeat the purpose of testing vector handler ... - bool verifyAnswer(vector::Vector* x, real_type answer, std::string memspace) + bool verifyAnswer(vector::Vector& x, real_type answer) { bool status = true; - if (memspace != "cpu") { - x->copyData(memory::DEVICE, memory::HOST); + + if (memspace_ == memory::DEVICE) { + x.copyData(memory::DEVICE, memory::HOST); } - for (index_type i = 0; i < x->getSize(); ++i) { + for (index_type i = 0; i < x.getSize(); ++i) { // std::cout << x->getData("cpu")[i] << "\n"; - if (!isEqual(x->getData(memory::HOST)[i], answer)) { + if (!isEqual(x.getData(memory::HOST)[i], answer)) { std::cout << std::setprecision(16); status = false; - std::cout << "Solution vector element x[" << i << "] = " << x->getData(memory::HOST)[i] + std::cout << "Solution vector element x[" << i << "] = " << x.getData(memory::HOST)[i] << ", expected: " << answer << "\n"; break; } @@ -337,6 +246,6 @@ namespace ReSolve { return status; } };//class - } -} + } // namespace tests +} //namespace ReSolve diff --git a/tests/unit/vector/runGramSchmidtTests.cpp b/tests/unit/vector/runGramSchmidtTests.cpp index bac6ab1e..fff135a8 100644 --- a/tests/unit/vector/runGramSchmidtTests.cpp +++ b/tests/unit/vector/runGramSchmidtTests.cpp @@ -7,13 +7,12 @@ int main(int, char**) { ReSolve::tests::TestingResults result; -#ifdef RESOLVE_USE_CUDA { - std::cout << "Running tests with CUDA backend:\n"; + std::cout << "Running tests on the CPU:\n"; - ReSolve::LinAlgWorkspaceCUDA* workspace = new ReSolve::LinAlgWorkspaceCUDA(); - workspace->initializeHandles(); - ReSolve::VectorHandler* handler = new ReSolve::VectorHandler(workspace); + ReSolve::LinAlgWorkspaceCpu workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); ReSolve::tests::GramSchmidtTests test(handler); result += test.GramSchmidtConstructor(); @@ -23,19 +22,15 @@ int main(int, char**) result += test.orthogonalize(5000, ReSolve::GramSchmidt::mgs_pm); result += test.orthogonalize(5000, ReSolve::GramSchmidt::cgs1); std::cout << "\n"; - - delete handler; - delete workspace; } -#endif -#ifdef RESOLVE_USE_HIP +#ifdef RESOLVE_USE_CUDA { - std::cout << "Running tests with HIP backend:\n"; + std::cout << "Running tests with CUDA backend:\n"; - ReSolve::LinAlgWorkspaceHIP* workspace = new ReSolve::LinAlgWorkspaceHIP(); - workspace->initializeHandles(); - ReSolve::VectorHandler* handler = new ReSolve::VectorHandler(workspace); + ReSolve::LinAlgWorkspaceCUDA workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); ReSolve::tests::GramSchmidtTests test(handler); result += test.GramSchmidtConstructor(); @@ -45,18 +40,16 @@ int main(int, char**) result += test.orthogonalize(5000, ReSolve::GramSchmidt::mgs_pm); result += test.orthogonalize(5000, ReSolve::GramSchmidt::cgs1); std::cout << "\n"; - - delete handler; - delete workspace; } #endif +#ifdef RESOLVE_USE_HIP { - std::cout << "Running tests on the CPU:\n"; + std::cout << "Running tests with HIP backend:\n"; - ReSolve::LinAlgWorkspaceCpu* workspace = new ReSolve::LinAlgWorkspaceCpu(); - workspace->initializeHandles(); - ReSolve::VectorHandler* handler = new ReSolve::VectorHandler(workspace); + ReSolve::LinAlgWorkspaceHIP workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); ReSolve::tests::GramSchmidtTests test(handler); result += test.GramSchmidtConstructor(); @@ -66,9 +59,8 @@ int main(int, char**) result += test.orthogonalize(5000, ReSolve::GramSchmidt::mgs_pm); result += test.orthogonalize(5000, ReSolve::GramSchmidt::cgs1); std::cout << "\n"; - - delete handler; - delete workspace; } +#endif + return result.summary(); } diff --git a/tests/unit/vector/runVectorHandlerTests.cpp b/tests/unit/vector/runVectorHandlerTests.cpp index 877249e2..1315d05b 100644 --- a/tests/unit/vector/runVectorHandlerTests.cpp +++ b/tests/unit/vector/runVectorHandlerTests.cpp @@ -9,8 +9,12 @@ int main(int, char**) { std::cout << "Running tests on CPU:\n"; - ReSolve::tests::VectorHandlerTests test("cpu"); - + + ReSolve::LinAlgWorkspaceCpu workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); + + ReSolve::tests::VectorHandlerTests test(handler); result += test.vectorHandlerConstructor(); result += test.dot(50); result += test.axpy(50); @@ -26,8 +30,12 @@ int main(int, char**) #ifdef RESOLVE_USE_CUDA { std::cout << "Running tests with CUDA backend:\n"; - ReSolve::tests::VectorHandlerTests test("cuda"); + ReSolve::LinAlgWorkspaceCUDA workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); + + ReSolve::tests::VectorHandlerTests test(handler); result += test.dot(5000); result += test.axpy(5000); result += test.scal(5000); @@ -45,8 +53,12 @@ int main(int, char**) #ifdef RESOLVE_USE_HIP { std::cout << "Running tests with HIP backend:\n"; - ReSolve::tests::VectorHandlerTests test("hip"); + ReSolve::LinAlgWorkspaceHIP workspace; + workspace.initializeHandles(); + ReSolve::VectorHandler handler(&workspace); + + ReSolve::tests::VectorHandlerTests test(handler); result += test.dot(5000); result += test.axpy(5000); result += test.scal(5000); @@ -60,5 +72,6 @@ int main(int, char**) std::cout << "\n"; } #endif + return result.summary(); }