From c9c50fae2aa9a554c77bb3d21d29c571d788f768 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Mon, 30 Oct 2023 19:51:17 -0400 Subject: [PATCH] Use enums instead of strings in copyData function. --- resolve/LinSolverDirectCuSolverGLU.cpp | 18 +-- resolve/LinSolverDirectCuSolverRf.cpp | 24 ++-- resolve/LinSolverDirectKLU.cpp | 30 ++--- resolve/MemoryUtils.hpp | 9 ++ resolve/matrix/Coo.cpp | 132 +++++++++++----------- resolve/matrix/Coo.hpp | 8 +- resolve/matrix/Csc.cpp | 132 +++++++++++----------- resolve/matrix/Csc.hpp | 8 +- resolve/matrix/Csr.cpp | 134 ++++++++++++----------- resolve/matrix/Csr.hpp | 8 +- resolve/matrix/MatrixHandler.cpp | 6 +- resolve/matrix/MatrixHandlerCpu.cpp | 18 +-- resolve/matrix/MatrixHandlerCuda.cpp | 30 ++--- resolve/matrix/MatrixHandlerHip.cpp | 28 ++--- resolve/matrix/Sparse.hpp | 8 +- resolve/matrix/io.cpp | 6 +- tests/unit/matrix/MatrixHandlerTests.hpp | 8 +- tests/unit/matrix/MatrixIoTests.hpp | 6 +- 18 files changed, 312 insertions(+), 301 deletions(-) diff --git a/resolve/LinSolverDirectCuSolverGLU.cpp b/resolve/LinSolverDirectCuSolverGLU.cpp index 75039ff4..7601d1e8 100644 --- a/resolve/LinSolverDirectCuSolverGLU.cpp +++ b/resolve/LinSolverDirectCuSolverGLU.cpp @@ -77,9 +77,9 @@ namespace ReSolve /* A is original matrix */ nnz, descr_A_, - A_->getValues("cuda"), //da_, - A_->getRowData("cuda"), //kRowPtr_, - A_->getColData("cuda"), //jCol_, + A_->getValues( memory::DEVICE), //da_, + A_->getRowData(memory::DEVICE), //kRowPtr_, + A_->getColData(memory::DEVICE), //jCol_, info_M_); error_sum += status_cusolver_; @@ -153,9 +153,9 @@ namespace ReSolve /* A is original matrix */ A_->getNnzExpanded(), descr_A_, - A_->getValues("cuda"), //da_, - A_->getRowData("cuda"), //kRowPtr_, - A_->getColData("cuda"), //jCol_, + A_->getValues( memory::DEVICE), //da_, + A_->getRowData(memory::DEVICE), //kRowPtr_, + A_->getColData(memory::DEVICE), //jCol_, info_M_); error_sum += status_cusolver_; @@ -173,9 +173,9 @@ namespace ReSolve /* A is original matrix */ A_->getNnz(), descr_A_, - A_->getValues("cuda"), //da_, - A_->getRowData("cuda"), //kRowPtr_, - A_->getColData("cuda"), //jCol_, + A_->getValues( memory::DEVICE), //da_, + A_->getRowData(memory::DEVICE), //kRowPtr_, + A_->getColData(memory::DEVICE), //jCol_, rhs->getData("cuda"),/* right hand side */ x->getData("cuda"),/* left hand side */ &ite_refine_succ_, diff --git a/resolve/LinSolverDirectCuSolverRf.cpp b/resolve/LinSolverDirectCuSolverRf.cpp index d51218cc..d16da690 100644 --- a/resolve/LinSolverDirectCuSolverRf.cpp +++ b/resolve/LinSolverDirectCuSolverRf.cpp @@ -35,17 +35,17 @@ namespace ReSolve error_sum += status_cusolverrf_; status_cusolverrf_ = cusolverRfSetupDevice(n, A_->getNnzExpanded(), - A_->getRowData("cuda"), //dia_, - A_->getColData("cuda"), //dja_, - A_->getValues("cuda"), //da_, + A_->getRowData(memory::DEVICE), //dia_, + A_->getColData(memory::DEVICE), //dja_, + A_->getValues( memory::DEVICE), //da_, L->getNnz(), - L->getRowData("cuda"), - L->getColData("cuda"), - L->getValues("cuda"), + L->getRowData(memory::DEVICE), + L->getColData(memory::DEVICE), + L->getValues( memory::DEVICE), U->getNnz(), - U->getRowData("cuda"), - U->getColData("cuda"), - U->getValues("cuda"), + U->getRowData(memory::DEVICE), + U->getColData(memory::DEVICE), + U->getValues( memory::DEVICE), d_P_, d_Q_, handle_cusolverrf_); @@ -76,9 +76,9 @@ namespace ReSolve int error_sum = 0; status_cusolverrf_ = cusolverRfResetValues(A_->getNumRows(), A_->getNnzExpanded(), - A_->getRowData("cuda"), //dia_, - A_->getColData("cuda"), //dja_, - A_->getValues("cuda"), //da_, + A_->getRowData(memory::DEVICE), //dia_, + A_->getColData(memory::DEVICE), //dja_, + A_->getValues( memory::DEVICE), //da_, d_P_, d_Q_, handle_cusolverrf_); diff --git a/resolve/LinSolverDirectKLU.cpp b/resolve/LinSolverDirectKLU.cpp index 43b612b3..69cd91a2 100644 --- a/resolve/LinSolverDirectKLU.cpp +++ b/resolve/LinSolverDirectKLU.cpp @@ -35,7 +35,7 @@ namespace ReSolve int LinSolverDirectKLU::analyze() { - Symbolic_ = klu_analyze(A_->getNumRows(), A_->getRowData("cpu"), A_->getColData("cpu"), &Common_) ; + Symbolic_ = klu_analyze(A_->getNumRows(), A_->getRowData(memory::HOST), A_->getColData(memory::HOST), &Common_) ; if (Symbolic_ == nullptr){ printf("Symbolic_ factorization crashed withCommon_.status = %d \n", Common_.status); @@ -46,7 +46,7 @@ namespace ReSolve int LinSolverDirectKLU::factorize() { - Numeric_ = klu_factor(A_->getRowData("cpu"), A_->getColData("cpu"),A_->getValues("cpu"), Symbolic_, &Common_); + Numeric_ = klu_factor(A_->getRowData(memory::HOST), A_->getColData(memory::HOST), A_->getValues(memory::HOST), Symbolic_, &Common_); if (Numeric_ == nullptr){ return 1; @@ -56,7 +56,7 @@ namespace ReSolve int LinSolverDirectKLU::refactorize() { - int kluStatus = klu_refactor (A_->getRowData("cpu"), A_->getColData("cpu"), A_->getValues("cpu"), Symbolic_, Numeric_, &Common_); + int kluStatus = klu_refactor (A_->getRowData(memory::HOST), A_->getColData(memory::HOST), A_->getValues(memory::HOST), Symbolic_, Numeric_, &Common_); if (!kluStatus){ //display error @@ -94,12 +94,12 @@ namespace ReSolve U_->allocateMatrixData("cpu"); int ok = klu_extract(Numeric_, Symbolic_, - L_->getColData("cpu"), - L_->getRowData("cpu"), - L_->getValues("cpu"), - U_->getColData("cpu"), - U_->getRowData("cpu"), - U_->getValues("cpu"), + L_->getColData(memory::HOST), + L_->getRowData(memory::HOST), + L_->getValues( memory::HOST), + U_->getColData(memory::HOST), + U_->getRowData(memory::HOST), + U_->getValues( memory::HOST), nullptr, nullptr, nullptr, @@ -129,12 +129,12 @@ namespace ReSolve U_->allocateMatrixData("cpu"); int ok = klu_extract(Numeric_, Symbolic_, - L_->getColData("cpu"), - L_->getRowData("cpu"), - L_->getValues("cpu"), - U_->getColData("cpu"), - U_->getRowData("cpu"), - U_->getValues("cpu"), + L_->getColData(memory::HOST), + L_->getRowData(memory::HOST), + L_->getValues( memory::HOST), + U_->getColData(memory::HOST), + U_->getRowData(memory::HOST), + U_->getValues( memory::HOST), nullptr, nullptr, nullptr, diff --git a/resolve/MemoryUtils.hpp b/resolve/MemoryUtils.hpp index 5e2da403..4aa2ce74 100644 --- a/resolve/MemoryUtils.hpp +++ b/resolve/MemoryUtils.hpp @@ -2,6 +2,15 @@ #include + +namespace ReSolve +{ + namespace memory + { + enum MemorySpace{HOST = 0, DEVICE}; + } +} + namespace ReSolve { /** diff --git a/resolve/matrix/Coo.cpp b/resolve/matrix/Coo.cpp index eeff7b86..e7498877 100644 --- a/resolve/matrix/Coo.cpp +++ b/resolve/matrix/Coo.cpp @@ -27,48 +27,48 @@ namespace ReSolve { } - index_type* matrix::Coo::getRowData(std::string memspace) + index_type* matrix::Coo::getRowData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_row_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_row_data_; + case DEVICE: copyData(memspace); return this->d_row_data_; - } else { + default: return nullptr; - } } } - index_type* matrix::Coo::getColData(std::string memspace) + index_type* matrix::Coo::getColData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_col_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_col_data_; + case DEVICE: copyData(memspace); return this->d_col_data_; - } else { + default: return nullptr; - } } } - real_type* matrix::Coo::getValues(std::string memspace) + real_type* matrix::Coo::getValues(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_val_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_val_data_; + case DEVICE: copyData(memspace); return this->d_val_data_; - } else { + default: return nullptr; - } } } @@ -187,55 +187,55 @@ namespace ReSolve return -1; } - int matrix::Coo::copyData(std::string memspaceOut) + int matrix::Coo::copyData(memory::MemorySpace memspaceOut) { + using namespace ReSolve::memory; index_type nnz_current = nnz_; - if (is_expanded_) {nnz_current = nnz_expanded_;} - - if (memspaceOut == "cpu") { - //check if we need to copy or not - if ((d_data_updated_ == true) && (h_data_updated_ == false)) { - if (h_row_data_ == nullptr) { - h_row_data_ = new index_type[nnz_current]; - } - if (h_col_data_ == nullptr) { - h_col_data_ = new index_type[nnz_current]; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_current]; - } - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_current); - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_current); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); - h_data_updated_ = true; - owns_cpu_data_ = true; - owns_cpu_vals_ = true; - } - return 0; + if (is_expanded_) { + nnz_current = nnz_expanded_; } - if ((memspaceOut == "cuda") || (memspaceOut == "hip")) { - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if (d_row_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_row_data_, nnz_current); + switch (memspaceOut) { + case HOST: + if ((d_data_updated_ == true) && (h_data_updated_ == false)) { + if (h_row_data_ == nullptr) { + h_row_data_ = new index_type[nnz_current]; + } + if (h_col_data_ == nullptr) { + h_col_data_ = new index_type[nnz_current]; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_current]; + } + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_current); + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_current); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); + h_data_updated_ = true; + owns_cpu_data_ = true; + owns_cpu_vals_ = true; } - if (d_col_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_col_data_, nnz_current); + return 0; + case DEVICE: + if ((d_data_updated_ == false) && (h_data_updated_ == true)) { + if (d_row_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_row_data_, nnz_current); + } + if (d_col_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_col_data_, nnz_current); + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); + } + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_current); + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_current); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); + d_data_updated_ = true; + owns_gpu_data_ = true; + owns_gpu_vals_ = true; } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); - } - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_current); - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_current); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); - d_data_updated_ = true; - owns_gpu_data_ = true; - owns_gpu_vals_ = true; - } - return 0; - } - return -1; + return 0; + } // switch } void matrix::Coo::print() diff --git a/resolve/matrix/Coo.hpp b/resolve/matrix/Coo.hpp index 3ec045c3..77cd94c7 100644 --- a/resolve/matrix/Coo.hpp +++ b/resolve/matrix/Coo.hpp @@ -15,9 +15,9 @@ namespace ReSolve { namespace matrix { bool expanded); ~Coo(); - virtual index_type* getRowData(std::string memspace); - virtual index_type* getColData(std::string memspace); - virtual real_type* getValues(std::string memspace); + virtual index_type* getRowData(memory::MemorySpace memspace); + virtual index_type* getColData(memory::MemorySpace memspace); + virtual real_type* getValues( memory::MemorySpace memspace); virtual index_type updateData(index_type* row_data, index_type* col_data, real_type* val_data, std::string memspaceIn, std::string memspaceOut); virtual index_type updateData(index_type* row_data, index_type* col_data, real_type* val_data, index_type new_nnz, std::string memspaceIn, std::string memspaceOut); @@ -26,7 +26,7 @@ namespace ReSolve { namespace matrix { virtual void print(); - virtual int copyData(std::string memspaceOut); + virtual int copyData(memory::MemorySpace memspaceOut); }; }} // namespace ReSolve::matrix diff --git a/resolve/matrix/Csc.cpp b/resolve/matrix/Csc.cpp index f6358df3..cd90c423 100644 --- a/resolve/matrix/Csc.cpp +++ b/resolve/matrix/Csc.cpp @@ -24,48 +24,48 @@ namespace ReSolve { } - index_type* matrix::Csc::getRowData(std::string memspace) + index_type* matrix::Csc::getRowData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_row_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_row_data_; + case DEVICE: copyData(memspace); return this->d_row_data_; - } else { + default: return nullptr; - } } } - index_type* matrix::Csc::getColData(std::string memspace) + index_type* matrix::Csc::getColData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_col_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_col_data_; + case DEVICE: copyData(memspace); return this->d_col_data_; - } else { + default: return nullptr; - } } } - real_type* matrix::Csc::getValues(std::string memspace) + real_type* matrix::Csc::getValues(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_val_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_val_data_; + case DEVICE: copyData(memspace); return this->d_val_data_; - } else { + default: return nullptr; - } } } @@ -184,54 +184,54 @@ namespace ReSolve return -1; } - int matrix::Csc::copyData(std::string memspaceOut) + int matrix::Csc::copyData(memory::MemorySpace memspaceOut) { + using namespace ReSolve::memory; index_type nnz_current = nnz_; - if (is_expanded_) {nnz_current = nnz_expanded_;} - - if (memspaceOut == "cpu") { - //check if we need to copy or not - if ((d_data_updated_ == true) && (h_data_updated_ == false)) { - if (h_col_data_ == nullptr) { - h_col_data_ = new index_type[n_ + 1]; - } - if (h_row_data_ == nullptr) { - h_row_data_ = new index_type[nnz_current]; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_current]; - } - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, n_ + 1); - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_current); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); - h_data_updated_ = true; - owns_cpu_data_ = true; - owns_cpu_vals_ = true; - } - return 0; + if (is_expanded_) { + nnz_current = nnz_expanded_; } - if ((memspaceOut == "cuda") || (memspaceOut == "hip")) { - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if (d_col_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_col_data_, n_ + 1); + switch(memspaceOut) { + case HOST: + if ((d_data_updated_ == true) && (h_data_updated_ == false)) { + if (h_col_data_ == nullptr) { + h_col_data_ = new index_type[n_ + 1]; + } + if (h_row_data_ == nullptr) { + h_row_data_ = new index_type[nnz_current]; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_current]; + } + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, n_ + 1); + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_current); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); + h_data_updated_ = true; + owns_cpu_data_ = true; + owns_cpu_vals_ = true; } - if (d_row_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_row_data_, nnz_current); + return 0; + case DEVICE: + if ((d_data_updated_ == false) && (h_data_updated_ == true)) { + if (d_col_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_col_data_, n_ + 1); + } + if (d_row_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_row_data_, nnz_current); + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); + } + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, n_ + 1); + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_current); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); + d_data_updated_ = true; + owns_gpu_data_ = true; + owns_gpu_vals_ = true; } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); - } - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, n_ + 1); - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_current); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); - d_data_updated_ = true; - owns_gpu_data_ = true; - owns_gpu_vals_ = true; - } - return 0; - } - return -1; + return 0; + } // switch } } diff --git a/resolve/matrix/Csc.hpp b/resolve/matrix/Csc.hpp index f0598314..99330f11 100644 --- a/resolve/matrix/Csc.hpp +++ b/resolve/matrix/Csc.hpp @@ -15,9 +15,9 @@ namespace ReSolve { namespace matrix { bool expanded); ~Csc(); - virtual index_type* getRowData(std::string memspace); - virtual index_type* getColData(std::string memspace); - virtual real_type* getValues(std::string memspace); + virtual index_type* getRowData(memory::MemorySpace memspace); + virtual index_type* getColData(memory::MemorySpace memspace); + virtual real_type* getValues( memory::MemorySpace memspace); virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, std::string memspaceIn, std::string memspaceOut); virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, index_type new_nnz, std::string memspaceIn, std::string memspaceOut); @@ -26,7 +26,7 @@ namespace ReSolve { namespace matrix { virtual void print() {return;} - virtual int copyData(std::string memspaceOut); + virtual int copyData(memory::MemorySpace memspaceOut); }; diff --git a/resolve/matrix/Csr.cpp b/resolve/matrix/Csr.cpp index 04e8dff1..ef195c95 100644 --- a/resolve/matrix/Csr.cpp +++ b/resolve/matrix/Csr.cpp @@ -24,48 +24,48 @@ namespace ReSolve { } - index_type* matrix::Csr::getRowData(std::string memspace) + index_type* matrix::Csr::getRowData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_row_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_row_data_; + case DEVICE: copyData(memspace); return this->d_row_data_; - } else { + default: return nullptr; - } } } - index_type* matrix::Csr::getColData(std::string memspace) + index_type* matrix::Csr::getColData(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_col_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_col_data_; + case DEVICE: copyData(memspace); return this->d_col_data_; - } else { + default: return nullptr; - } } } - real_type* matrix::Csr::getValues(std::string memspace) + real_type* matrix::Csr::getValues(memory::MemorySpace memspace) { - if (memspace == "cpu") { - copyData("cpu"); - return this->h_val_data_; - } else { - if ((memspace == "cuda") || (memspace == "hip")) { + using namespace ReSolve::memory; + switch (memspace) { + case HOST: + copyData(memspace); + return this->h_val_data_; + case DEVICE: copyData(memspace); return this->d_val_data_; - } else { + default: return nullptr; - } } } @@ -185,54 +185,56 @@ namespace ReSolve return -1; } - int matrix::Csr::copyData(std::string memspaceOut) + int matrix::Csr::copyData(memory::MemorySpace memspaceOut) { - index_type nnz_current = nnz_; - if (is_expanded_) {nnz_current = nnz_expanded_;} + using namespace ReSolve::memory; - if (memspaceOut == "cpu") { - //check if we need to copy or not - if ((d_data_updated_ == true) && (h_data_updated_ == false)) { - if (h_row_data_ == nullptr) { - h_row_data_ = new index_type[n_ + 1]; - } - if (h_col_data_ == nullptr) { - h_col_data_ = new index_type[nnz_current]; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_current]; - } - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, n_ + 1); - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_current); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); - h_data_updated_ = true; - owns_cpu_data_ = true; - owns_cpu_vals_ = true; - } - return 0; + index_type nnz_current = nnz_; + if (is_expanded_) { + nnz_current = nnz_expanded_; } - if ((memspaceOut == "cuda") || (memspaceOut == "hip")) { - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if (d_row_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_row_data_, n_ + 1); - } - if (d_col_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_col_data_, nnz_current); + switch (memspaceOut) { + case HOST: + //check if we need to copy or not + if ((d_data_updated_ == true) && (h_data_updated_ == false)) { + if (h_row_data_ == nullptr) { + h_row_data_ = new index_type[n_ + 1]; + } + if (h_col_data_ == nullptr) { + h_col_data_ = new index_type[nnz_current]; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_current]; + } + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, n_ + 1); + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_current); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_current); + h_data_updated_ = true; + owns_cpu_data_ = true; + owns_cpu_vals_ = true; } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); + return 0; + case DEVICE: + if ((d_data_updated_ == false) && (h_data_updated_ == true)) { + if (d_row_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_row_data_, n_ + 1); + } + if (d_col_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_col_data_, nnz_current); + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_current); + } + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, n_ + 1); + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_current); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); + d_data_updated_ = true; + owns_gpu_data_ = true; + owns_gpu_vals_ = true; } - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, n_ + 1); - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_current); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_current); - d_data_updated_ = true; - owns_gpu_data_ = true; - owns_gpu_vals_ = true; - } - return 0; - } - return -1; + return 0; + } // switch } } // namespace ReSolve diff --git a/resolve/matrix/Csr.hpp b/resolve/matrix/Csr.hpp index 43c317de..0f825023 100644 --- a/resolve/matrix/Csr.hpp +++ b/resolve/matrix/Csr.hpp @@ -18,9 +18,9 @@ namespace ReSolve { namespace matrix { ~Csr(); - virtual index_type* getRowData(std::string memspace); - virtual index_type* getColData(std::string memspace); - virtual real_type* getValues(std::string memspace); + virtual index_type* getRowData(memory::MemorySpace memspace); + virtual index_type* getColData(memory::MemorySpace memspace); + virtual real_type* getValues( memory::MemorySpace memspace); virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, std::string memspaceIn, std::string memspaceOut); virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, index_type new_nnz, std::string memspaceIn, std::string memspaceOut); @@ -29,7 +29,7 @@ namespace ReSolve { namespace matrix { virtual void print() {return;} - virtual int copyData(std::string memspaceOut); + virtual int copyData(memory::MemorySpace memspaceOut); }; }} // namespace ReSolve::matrix diff --git a/resolve/matrix/MatrixHandler.cpp b/resolve/matrix/MatrixHandler.cpp index 133a09f9..2f2fc0ad 100644 --- a/resolve/matrix/MatrixHandler.cpp +++ b/resolve/matrix/MatrixHandler.cpp @@ -124,9 +124,9 @@ namespace ReSolve { index_type* nnz_counts = new index_type[n]; std::fill_n(nnz_counts, n, 0); - index_type* coo_rows = A_coo->getRowData("cpu"); - index_type* coo_cols = A_coo->getColData("cpu"); - real_type* coo_vals = A_coo->getValues("cpu"); + index_type* coo_rows = A_coo->getRowData(memory::HOST); + index_type* coo_cols = A_coo->getColData(memory::HOST); + real_type* coo_vals = A_coo->getValues( memory::HOST); index_type* diag_control = new index_type[n]; //for DEDUPLICATION of the diagonal std::fill_n(diag_control, n, 0); diff --git a/resolve/matrix/MatrixHandlerCpu.cpp b/resolve/matrix/MatrixHandlerCpu.cpp index 2c434dcb..bf4a7fba 100644 --- a/resolve/matrix/MatrixHandlerCpu.cpp +++ b/resolve/matrix/MatrixHandlerCpu.cpp @@ -45,9 +45,9 @@ namespace ReSolve { // int error_sum = 0; if (matrixFormat == "csr") { matrix::Csr* A = (matrix::Csr*) Ageneric; - index_type* ia = A->getRowData("cpu"); - index_type* ja = A->getColData("cpu"); - real_type* a = A->getValues("cpu"); + index_type* ia = A->getRowData(memory::HOST); + index_type* ja = A->getColData(memory::HOST); + real_type* a = A->getValues( memory::HOST); real_type* x_data = vec_x->getData("cpu"); real_type* result_data = vec_result->getData("cpu"); @@ -100,13 +100,13 @@ namespace ReSolve { index_type nnz = A_csc->getNnz(); index_type n = A_csc->getNumColumns(); - index_type* rowIdxCsc = A_csc->getRowData("cpu"); - index_type* colPtrCsc = A_csc->getColData("cpu"); - real_type* valuesCsc = A_csc->getValues("cpu"); + index_type* rowIdxCsc = A_csc->getRowData(memory::HOST); + index_type* colPtrCsc = A_csc->getColData(memory::HOST); + real_type* valuesCsc = A_csc->getValues( memory::HOST); - index_type* rowPtrCsr = A_csr->getRowData("cpu"); - index_type* colIdxCsr = A_csr->getColData("cpu"); - real_type* valuesCsr = A_csr->getValues("cpu"); + index_type* rowPtrCsr = A_csr->getRowData(memory::HOST); + index_type* colIdxCsr = A_csr->getColData(memory::HOST); + real_type* valuesCsr = A_csr->getValues( memory::HOST); // Set all CSR row pointers to zero for (index_type i = 0; i <= n; ++i) { diff --git a/resolve/matrix/MatrixHandlerCuda.cpp b/resolve/matrix/MatrixHandlerCuda.cpp index 3405ba8d..11ff2972 100644 --- a/resolve/matrix/MatrixHandlerCuda.cpp +++ b/resolve/matrix/MatrixHandlerCuda.cpp @@ -57,9 +57,9 @@ namespace ReSolve { A->getNumRows(), A->getNumColumns(), A->getNnzExpanded(), - A->getRowData("cuda"), - A->getColData("cuda"), - A->getValues("cuda"), + A->getRowData(memory::DEVICE), + A->getColData(memory::DEVICE), + A->getValues( memory::DEVICE), CUSPARSE_INDEX_32I, CUSPARSE_INDEX_32I, CUSPARSE_INDEX_BASE_ZERO, @@ -137,12 +137,12 @@ namespace ReSolve { n, m, nnz, - A_csc->getValues("cuda"), - A_csc->getColData("cuda"), - A_csc->getRowData("cuda"), - A_csr->getValues("cuda"), - A_csr->getRowData("cuda"), - A_csr->getColData("cuda"), + A_csc->getValues( memory::DEVICE), + A_csc->getColData(memory::DEVICE), + A_csc->getRowData(memory::DEVICE), + A_csr->getValues( memory::DEVICE), + A_csr->getRowData(memory::DEVICE), + A_csr->getColData(memory::DEVICE), CUDA_R_64F, CUSPARSE_ACTION_NUMERIC, CUSPARSE_INDEX_BASE_ZERO, @@ -154,12 +154,12 @@ namespace ReSolve { n, m, nnz, - A_csc->getValues("cuda"), - A_csc->getColData("cuda"), - A_csc->getRowData("cuda"), - A_csr->getValues("cuda"), - A_csr->getRowData("cuda"), - A_csr->getColData("cuda"), + A_csc->getValues( memory::DEVICE), + A_csc->getColData(memory::DEVICE), + A_csc->getRowData(memory::DEVICE), + A_csr->getValues( memory::DEVICE), + A_csr->getRowData(memory::DEVICE), + A_csr->getColData(memory::DEVICE), CUDA_R_64F, CUSPARSE_ACTION_NUMERIC, CUSPARSE_INDEX_BASE_ZERO, diff --git a/resolve/matrix/MatrixHandlerHip.cpp b/resolve/matrix/MatrixHandlerHip.cpp index 370849fa..381b78f3 100644 --- a/resolve/matrix/MatrixHandlerHip.cpp +++ b/resolve/matrix/MatrixHandlerHip.cpp @@ -62,9 +62,9 @@ namespace ReSolve { A->getNumColumns(), A->getNnzExpanded(), descrA, - A->getValues("cuda"), - A->getRowData("cuda"), - A->getColData("cuda"), // cuda is used as "device" + A->getValues( memory::DEVICE), + A->getRowData(memory::DEVICE), + A->getColData(memory::DEVICE), // cuda is used as "device" infoA); error_sum += status; mem_.deviceSynchronize(); @@ -79,9 +79,9 @@ namespace ReSolve { A->getNnzExpanded(), alpha, descrA, - A->getValues("cuda"), - A->getRowData("cuda"), - A->getColData("cuda"), + A->getValues( memory::DEVICE), + A->getRowData(memory::DEVICE), + A->getColData(memory::DEVICE), infoA, vec_x->getData("cuda"), beta, @@ -125,8 +125,8 @@ namespace ReSolve { n, m, nnz, - A_csc->getColData("cuda"), - A_csc->getRowData("cuda"), + A_csc->getColData(memory::DEVICE), + A_csc->getRowData(memory::DEVICE), rocsparse_action_numeric, &bufferSize); @@ -137,12 +137,12 @@ namespace ReSolve { n, m, nnz, - A_csc->getValues("cuda"), - A_csc->getColData("cuda"), - A_csc->getRowData("cuda"), - A_csr->getValues("cuda"), - A_csr->getRowData("cuda"), - A_csr->getColData("cuda"), + A_csc->getValues( memory::DEVICE), + A_csc->getColData(memory::DEVICE), + A_csc->getRowData(memory::DEVICE), + A_csr->getValues( memory::DEVICE), + A_csr->getRowData(memory::DEVICE), + A_csr->getColData(memory::DEVICE), rocsparse_action_numeric, rocsparse_index_base_zero, d_work); diff --git a/resolve/matrix/Sparse.hpp b/resolve/matrix/Sparse.hpp index 1196c38e..77e4e9be 100644 --- a/resolve/matrix/Sparse.hpp +++ b/resolve/matrix/Sparse.hpp @@ -33,9 +33,9 @@ namespace ReSolve { namespace matrix { void setNnz(index_type nnz_new); // for resetting when removing duplicates index_type setUpdated(std::string what); - virtual index_type* getRowData(std::string memspace) = 0; - virtual index_type* getColData(std::string memspace) = 0; - virtual real_type* getValues(std::string memspace) = 0; + virtual index_type* getRowData(memory::MemorySpace memspace) = 0; + virtual index_type* getColData(memory::MemorySpace memspace) = 0; + virtual real_type* getValues( memory::MemorySpace memspace) = 0; virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, std::string memspaceIn, std::string memspaceOut) = 0; virtual int updateData(index_type* row_data, index_type* col_data, real_type* val_data, index_type new_nnz, std::string memspaceIn, std::string memspaceOut) = 0; @@ -47,7 +47,7 @@ namespace ReSolve { namespace matrix { virtual void print() = 0; - virtual int copyData(std::string memspaceOut) = 0; + virtual int copyData(memory::MemorySpace memspaceOut) = 0; //update Values just updates values; it allocates if necessary. diff --git a/resolve/matrix/io.cpp b/resolve/matrix/io.cpp index 36fb5f1b..9a35bb05 100644 --- a/resolve/matrix/io.cpp +++ b/resolve/matrix/io.cpp @@ -116,9 +116,9 @@ namespace ReSolve { namespace io { } A->setNnz(nnz); //create coo arrays - index_type* coo_rows = A->getRowData("cpu"); - index_type* coo_cols = A->getColData("cpu"); - real_type* coo_vals = A->getValues("cpu"); + index_type* coo_rows = A->getRowData(memory::HOST); + index_type* coo_cols = A->getColData(memory::HOST); + real_type* coo_vals = A->getValues( memory::HOST); i = 0; index_type a, b; real_type c; diff --git a/tests/unit/matrix/MatrixHandlerTests.hpp b/tests/unit/matrix/MatrixHandlerTests.hpp index 0bcfe544..e9e9bf7c 100644 --- a/tests/unit/matrix/MatrixHandlerTests.hpp +++ b/tests/unit/matrix/MatrixHandlerTests.hpp @@ -137,9 +137,9 @@ class MatrixHandlerTests : TestBase matrix::Csr* A = new matrix::Csr(N, N, NNZ); A->allocateMatrixData("cpu"); - index_type* rowptr = A->getRowData("cpu"); - index_type* colidx = A->getColData("cpu"); - real_type* val = A->getValues("cpu"); + index_type* rowptr = A->getRowData(memory::HOST); + index_type* colidx = A->getColData(memory::HOST); + real_type* val = A->getValues( memory::HOST); // Populate CSR matrix using same row pattern as for NNZ calculation rowptr[0] = 0; @@ -160,7 +160,7 @@ class MatrixHandlerTests : TestBase A->setUpdated("cpu"); if ((memspace == "cuda") || (memspace == "hip")) { - A->copyData(memspace); + A->copyData(memory::DEVICE); } return A; diff --git a/tests/unit/matrix/MatrixIoTests.hpp b/tests/unit/matrix/MatrixIoTests.hpp index ad14f0a7..fad0d176 100644 --- a/tests/unit/matrix/MatrixIoTests.hpp +++ b/tests/unit/matrix/MatrixIoTests.hpp @@ -176,9 +176,9 @@ class MatrixIoTests : TestBase const std::vector& val_data) { for (size_t i = 0; i < val_data.size(); ++i) { - if ((answer.getRowData("cpu")[i] != row_data[i]) || - (answer.getColData("cpu")[i] != col_data[i]) || - (!isEqual(answer.getValues("cpu")[i], val_data[i]))) + if ((answer.getRowData(memory::HOST)[i] != row_data[i]) || + (answer.getColData(memory::HOST)[i] != col_data[i]) || + (!isEqual(answer.getValues(memory::HOST)[i], val_data[i]))) { std::cout << "Incorrect matrix value at storage element " << i << ".\n"; return false;