From 7d82835367c5e48190b62fe4874ab2ed2f8e5368 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 9 Oct 2024 20:20:00 -0400 Subject: [PATCH 1/4] matrix getters don't sync. --- resolve/matrix/Csr.cpp | 6 +++--- tests/functionality/testKLU_RocSolver.cpp | 2 ++ tests/functionality/testKLU_RocSolver_FGMRES.cpp | 2 ++ tests/functionality/testSysRefactor.cpp | 2 ++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/resolve/matrix/Csr.cpp b/resolve/matrix/Csr.cpp index 63ce1f8e..83cb057e 100644 --- a/resolve/matrix/Csr.cpp +++ b/resolve/matrix/Csr.cpp @@ -151,7 +151,7 @@ namespace ReSolve index_type* matrix::Csr::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // copyData(memspace); switch (memspace) { case HOST: return this->h_row_data_; @@ -165,7 +165,7 @@ namespace ReSolve index_type* matrix::Csr::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // copyData(memspace); switch (memspace) { case HOST: return this->h_col_data_; @@ -179,7 +179,7 @@ namespace ReSolve real_type* matrix::Csr::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // copyData(memspace); switch (memspace) { case HOST: return this->h_val_data_; diff --git a/tests/functionality/testKLU_RocSolver.cpp b/tests/functionality/testKLU_RocSolver.cpp index 97759667..b60d879b 100644 --- a/tests/functionality/testKLU_RocSolver.cpp +++ b/tests/functionality/testKLU_RocSolver.cpp @@ -61,6 +61,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->copyData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -173,6 +174,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->copyData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testKLU_RocSolver_FGMRES.cpp b/tests/functionality/testKLU_RocSolver_FGMRES.cpp index 4d27a84b..e1324a92 100644 --- a/tests/functionality/testKLU_RocSolver_FGMRES.cpp +++ b/tests/functionality/testKLU_RocSolver_FGMRES.cpp @@ -77,6 +77,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->copyData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -193,6 +194,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->copyData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testSysRefactor.cpp b/tests/functionality/testSysRefactor.cpp index 74b039ce..12f5303c 100644 --- a/tests/functionality/testSysRefactor.cpp +++ b/tests/functionality/testSysRefactor.cpp @@ -92,6 +92,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1, true); + A->copyData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -224,6 +225,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->copyData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector From b34715207e2e11011cf041b63c8d57e5c57c4bd0 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 9 Oct 2024 20:27:59 -0400 Subject: [PATCH 2/4] All tests pass with manual host-data sync. --- resolve/matrix/Csr.cpp | 6 +++--- tests/functionality/testKLU_GLU.cpp | 2 ++ tests/functionality/testKLU_Rf.cpp | 2 ++ tests/functionality/testKLU_Rf_FGMRES.cpp | 2 ++ tests/functionality/testKLU_RocSolver.cpp | 4 ++-- tests/functionality/testKLU_RocSolver_FGMRES.cpp | 4 ++-- tests/functionality/testSysRefactor.cpp | 4 ++-- 7 files changed, 15 insertions(+), 9 deletions(-) diff --git a/resolve/matrix/Csr.cpp b/resolve/matrix/Csr.cpp index 83cb057e..69eca91e 100644 --- a/resolve/matrix/Csr.cpp +++ b/resolve/matrix/Csr.cpp @@ -151,7 +151,7 @@ namespace ReSolve index_type* matrix::Csr::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // copyData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_row_data_; @@ -165,7 +165,7 @@ namespace ReSolve index_type* matrix::Csr::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // copyData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_col_data_; @@ -179,7 +179,7 @@ namespace ReSolve real_type* matrix::Csr::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // copyData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_val_data_; diff --git a/tests/functionality/testKLU_GLU.cpp b/tests/functionality/testKLU_GLU.cpp index 53f0870b..9cf993d7 100644 --- a/tests/functionality/testKLU_GLU.cpp +++ b/tests/functionality/testKLU_GLU.cpp @@ -57,6 +57,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -165,6 +166,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testKLU_Rf.cpp b/tests/functionality/testKLU_Rf.cpp index 60cd4afa..aeac8618 100644 --- a/tests/functionality/testKLU_Rf.cpp +++ b/tests/functionality/testKLU_Rf.cpp @@ -57,6 +57,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -164,6 +165,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testKLU_Rf_FGMRES.cpp b/tests/functionality/testKLU_Rf_FGMRES.cpp index 46542d42..7db43630 100644 --- a/tests/functionality/testKLU_Rf_FGMRES.cpp +++ b/tests/functionality/testKLU_Rf_FGMRES.cpp @@ -77,6 +77,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -198,6 +199,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testKLU_RocSolver.cpp b/tests/functionality/testKLU_RocSolver.cpp index b60d879b..0ceba109 100644 --- a/tests/functionality/testKLU_RocSolver.cpp +++ b/tests/functionality/testKLU_RocSolver.cpp @@ -61,7 +61,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -174,7 +174,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testKLU_RocSolver_FGMRES.cpp b/tests/functionality/testKLU_RocSolver_FGMRES.cpp index e1324a92..c388140d 100644 --- a/tests/functionality/testKLU_RocSolver_FGMRES.cpp +++ b/tests/functionality/testKLU_RocSolver_FGMRES.cpp @@ -77,7 +77,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -194,7 +194,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector diff --git a/tests/functionality/testSysRefactor.cpp b/tests/functionality/testSysRefactor.cpp index 12f5303c..1ae563d7 100644 --- a/tests/functionality/testSysRefactor.cpp +++ b/tests/functionality/testSysRefactor.cpp @@ -92,7 +92,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1, true); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -225,7 +225,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); - A->copyData(ReSolve::memory::DEVICE); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector From 2ba4fd0642041da7f7dfc53ccdb04c46c47f315c Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Wed, 9 Oct 2024 21:35:59 -0400 Subject: [PATCH 3/4] Use explicit host-device syncing. --- resolve/SystemSolver.cpp | 4 +- resolve/matrix/Coo.cpp | 89 +++++++++++++---------- resolve/matrix/Csc.cpp | 87 ++++++++++++---------- resolve/matrix/Csr.cpp | 82 ++++++++++++--------- tests/functionality/testKLU_Rf.cpp | 2 + tests/functionality/testKLU_Rf_FGMRES.cpp | 2 + tests/functionality/testSysGLU.cpp | 2 + 7 files changed, 157 insertions(+), 111 deletions(-) diff --git a/resolve/SystemSolver.cpp b/resolve/SystemSolver.cpp index e57eea10..6adbd923 100644 --- a/resolve/SystemSolver.cpp +++ b/resolve/SystemSolver.cpp @@ -391,7 +391,9 @@ namespace ReSolve } if (refactorizationMethod_ == "cusolverrf") { matrix::Csc* L_csc = dynamic_cast(L_); - matrix::Csc* U_csc = dynamic_cast(U_); + matrix::Csc* U_csc = dynamic_cast(U_); + L_csc->syncData(memory::DEVICE); + U_csc->syncData(memory::DEVICE); matrix::Csr* L_csr = new matrix::Csr(L_csc->getNumRows(), L_csc->getNumColumns(), L_csc->getNnz()); matrix::Csr* U_csr = new matrix::Csr(U_csc->getNumRows(), U_csc->getNumColumns(), U_csc->getNnz()); matrixHandler_->csc2csr(L_csc, L_csr, memory::DEVICE); diff --git a/resolve/matrix/Coo.cpp b/resolve/matrix/Coo.cpp index fe6dfc19..7e7e9f4f 100644 --- a/resolve/matrix/Coo.cpp +++ b/resolve/matrix/Coo.cpp @@ -1,6 +1,7 @@ #include // <-- includes memcpy #include -#include +#include +#include #include #include "Coo.hpp" @@ -132,7 +133,7 @@ namespace ReSolve index_type* matrix::Coo::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_row_data_; @@ -146,7 +147,7 @@ namespace ReSolve index_type* matrix::Coo::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_col_data_; @@ -160,7 +161,7 @@ namespace ReSolve real_type* matrix::Coo::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_val_data_; @@ -302,44 +303,56 @@ namespace ReSolve switch (memspace) { case HOST: - if ((d_data_updated_ == true) && (h_data_updated_ == false)) { - if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { - out::error() << "In Coo::syncData one of host row or column data is null!\n"; - } - if ((h_row_data_ == nullptr) && (h_col_data_ == nullptr)) { - h_row_data_ = new index_type[nnz_]; - h_col_data_ = new index_type[nnz_]; - owns_cpu_data_ = true; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_]; - owns_cpu_vals_ = true; - } - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_); - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); - h_data_updated_ = true; + if (h_data_updated_) { + out::misc() << "In Csr::syncData trying to sync host, but host already up to date!\n"; + return 0; } + if (!d_data_updated_) { + out::error() << "In Csr::syncData trying to sync host with device, but device is out of date!\n"; + assert(d_data_updated_); + } + if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { + out::error() << "In Coo::syncData one of host row or column data is null!\n"; + } + if ((h_row_data_ == nullptr) && (h_col_data_ == nullptr)) { + h_row_data_ = new index_type[nnz_]; + h_col_data_ = new index_type[nnz_]; + owns_cpu_data_ = true; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_]; + owns_cpu_vals_ = true; + } + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_); + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); + h_data_updated_ = true; return 0; case DEVICE: - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { - out::error() << "In Coo::syncData one of device row or column data is null!\n"; - } - if ((d_row_data_ == nullptr) && (d_col_data_ == nullptr)) { - mem_.allocateArrayOnDevice(&d_row_data_, nnz_); - mem_.allocateArrayOnDevice(&d_col_data_, nnz_); - owns_gpu_data_ = true; - } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_); - owns_gpu_vals_ = true; - } - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_); - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); - d_data_updated_ = true; + if (d_data_updated_) { + out::misc() << "In Csr::syncData trying to sync device, but device already up to date!\n"; + return 0; } + if (!h_data_updated_) { + out::error() << "In Csr::syncData trying to sync device with host, but host is out of date!\n"; + assert(h_data_updated_); + } + if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { + out::error() << "In Coo::syncData one of device row or column data is null!\n"; + } + if ((d_row_data_ == nullptr) && (d_col_data_ == nullptr)) { + mem_.allocateArrayOnDevice(&d_row_data_, nnz_); + mem_.allocateArrayOnDevice(&d_col_data_, nnz_); + owns_gpu_data_ = true; + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_); + owns_gpu_vals_ = true; + } + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_); + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); + d_data_updated_ = true; return 0; default: return 1; diff --git a/resolve/matrix/Csc.cpp b/resolve/matrix/Csc.cpp index 03601be2..a3a54a1b 100644 --- a/resolve/matrix/Csc.cpp +++ b/resolve/matrix/Csc.cpp @@ -1,5 +1,6 @@ #include // <-- includes memcpy #include +#include #include #include "Csc.hpp" @@ -33,7 +34,7 @@ namespace ReSolve index_type* matrix::Csc::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_row_data_; @@ -47,7 +48,7 @@ namespace ReSolve index_type* matrix::Csc::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_col_data_; @@ -61,7 +62,7 @@ namespace ReSolve real_type* matrix::Csc::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - syncData(memspace); + // syncData(memspace); switch (memspace) { case HOST: return this->h_val_data_; @@ -209,44 +210,56 @@ namespace ReSolve switch(memspace) { case HOST: - if ((d_data_updated_ == true) && (h_data_updated_ == false)) { - if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { - out::error() << "In Csc::syncData one of host row or column data is null!\n"; - } - if ((h_col_data_ == nullptr) && (h_row_data_ == nullptr)) { - h_col_data_ = new index_type[m_ + 1]; - h_row_data_ = new index_type[nnz_]; - owns_cpu_data_ = true; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_]; - owns_cpu_vals_ = true; - } - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, m_ + 1); - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); - h_data_updated_ = true; + if (h_data_updated_) { + out::misc() << "In Csc::syncData trying to sync host, but host already up to date!\n"; + return 0; } + if (!d_data_updated_) { + out::error() << "In Csc::syncData trying to sync host with device, but device is out of date!\n"; + assert(d_data_updated_); + } + if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { + out::error() << "In Csc::syncData one of host row or column data is null!\n"; + } + if ((h_col_data_ == nullptr) && (h_row_data_ == nullptr)) { + h_col_data_ = new index_type[m_ + 1]; + h_row_data_ = new index_type[nnz_]; + owns_cpu_data_ = true; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_]; + owns_cpu_vals_ = true; + } + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, m_ + 1); + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, nnz_); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); + h_data_updated_ = true; return 0; case DEVICE: - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { - out::error() << "In Csc::syncData one of device row or column data is null!\n"; - } - if ((d_col_data_ == nullptr) && (d_row_data_ == nullptr)) { - mem_.allocateArrayOnDevice(&d_col_data_, m_ + 1); - mem_.allocateArrayOnDevice(&d_row_data_, nnz_); - owns_gpu_data_ = true; - } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_); - owns_gpu_vals_ = true; - } - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, m_ + 1); - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); - d_data_updated_ = true; + if (d_data_updated_) { + out::misc() << "In Csr::syncData trying to sync device, but device already up to date!\n"; + return 0; } + if (!h_data_updated_) { + out::error() << "In Csr::syncData trying to sync device with host, but host is out of date!\n"; + assert(h_data_updated_); + } + if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { + out::error() << "In Csc::syncData one of device row or column data is null!\n"; + } + if ((d_col_data_ == nullptr) && (d_row_data_ == nullptr)) { + mem_.allocateArrayOnDevice(&d_col_data_, m_ + 1); + mem_.allocateArrayOnDevice(&d_row_data_, nnz_); + owns_gpu_data_ = true; + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_); + owns_gpu_vals_ = true; + } + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, m_ + 1); + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, nnz_); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); + d_data_updated_ = true; return 0; default: return 1; diff --git a/resolve/matrix/Csr.cpp b/resolve/matrix/Csr.cpp index 69eca91e..0adf8433 100644 --- a/resolve/matrix/Csr.cpp +++ b/resolve/matrix/Csr.cpp @@ -319,47 +319,59 @@ namespace ReSolve switch (memspace) { 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_col_data_ == nullptr)) { - out::error() << "In Csr::syncData one of host row or column data is null!\n"; - } - if ((h_row_data_ == nullptr) && (h_col_data_ == nullptr)) { - h_row_data_ = new index_type[n_ + 1]; - h_col_data_ = new index_type[nnz_]; - owns_cpu_data_ = true; - } - if (h_val_data_ == nullptr) { - h_val_data_ = new real_type[nnz_]; - owns_cpu_vals_ = true; - } - mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, n_ + 1); - mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_); - mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); - h_data_updated_ = true; + if (h_data_updated_) { + out::misc() << "In Csr::syncData trying to sync host, but host already up to date!\n"; + return 0; } + if (!d_data_updated_) { + out::error() << "In Csr::syncData trying to sync host with device, but device is out of date!\n"; + assert(d_data_updated_); + } + if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { + out::error() << "In Csr::syncData one of host row or column data is null!\n"; + } + if ((h_row_data_ == nullptr) && (h_col_data_ == nullptr)) { + h_row_data_ = new index_type[n_ + 1]; + h_col_data_ = new index_type[nnz_]; + owns_cpu_data_ = true; + } + if (h_val_data_ == nullptr) { + h_val_data_ = new real_type[nnz_]; + owns_cpu_vals_ = true; + } + mem_.copyArrayDeviceToHost(h_row_data_, d_row_data_, n_ + 1); + mem_.copyArrayDeviceToHost(h_col_data_, d_col_data_, nnz_); + mem_.copyArrayDeviceToHost(h_val_data_, d_val_data_, nnz_); + h_data_updated_ = true; return 0; case DEVICE: - if ((d_data_updated_ == false) && (h_data_updated_ == true)) { - if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { - out::error() << "In Csr::syncData one of device row or column data is null!\n"; - } - if ((d_row_data_ == nullptr) && (d_col_data_ == nullptr)) { - mem_.allocateArrayOnDevice(&d_row_data_, n_ + 1); - mem_.allocateArrayOnDevice(&d_col_data_, nnz_); - owns_gpu_data_ = true; - } - if (d_val_data_ == nullptr) { - mem_.allocateArrayOnDevice(&d_val_data_, nnz_); - owns_gpu_vals_ = true; - } - mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, n_ + 1); - mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_); - mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); - d_data_updated_ = true; + if (d_data_updated_) { + out::misc() << "In Csr::syncData trying to sync device, but device already up to date!\n"; + return 0; + } + if (!h_data_updated_) { + out::error() << "In Csr::syncData trying to sync device with host, but host is out of date!\n"; + assert(h_data_updated_); } + if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { + out::error() << "In Csr::syncData one of device row or column data is null!\n"; + } + if ((d_row_data_ == nullptr) && (d_col_data_ == nullptr)) { + mem_.allocateArrayOnDevice(&d_row_data_, n_ + 1); + mem_.allocateArrayOnDevice(&d_col_data_, nnz_); + owns_gpu_data_ = true; + } + if (d_val_data_ == nullptr) { + mem_.allocateArrayOnDevice(&d_val_data_, nnz_); + owns_gpu_vals_ = true; + } + mem_.copyArrayHostToDevice(d_row_data_, h_row_data_, n_ + 1); + mem_.copyArrayHostToDevice(d_col_data_, h_col_data_, nnz_); + mem_.copyArrayHostToDevice(d_val_data_, h_val_data_, nnz_); + d_data_updated_ = true; return 0; default: - return -1; + return 1; } // switch } diff --git a/tests/functionality/testKLU_Rf.cpp b/tests/functionality/testKLU_Rf.cpp index aeac8618..f9975460 100644 --- a/tests/functionality/testKLU_Rf.cpp +++ b/tests/functionality/testKLU_Rf.cpp @@ -146,6 +146,8 @@ int main(int argc, char *argv[]) ReSolve::matrix::Csc* L_csc = (ReSolve::matrix::Csc*) KLU->getLFactor(); ReSolve::matrix::Csc* U_csc = (ReSolve::matrix::Csc*) KLU->getUFactor(); + L_csc->syncData(ReSolve::memory::DEVICE); + U_csc->syncData(ReSolve::memory::DEVICE); ReSolve::matrix::Csr* L = new ReSolve::matrix::Csr(L_csc->getNumRows(), L_csc->getNumColumns(), L_csc->getNnz()); ReSolve::matrix::Csr* U = new ReSolve::matrix::Csr(U_csc->getNumRows(), U_csc->getNumColumns(), U_csc->getNnz()); error_sum += matrix_handler->csc2csr(L_csc,L, ReSolve::memory::DEVICE); diff --git a/tests/functionality/testKLU_Rf_FGMRES.cpp b/tests/functionality/testKLU_Rf_FGMRES.cpp index 7db43630..8587d910 100644 --- a/tests/functionality/testKLU_Rf_FGMRES.cpp +++ b/tests/functionality/testKLU_Rf_FGMRES.cpp @@ -172,6 +172,8 @@ int main(int argc, char *argv[]) ReSolve::matrix::Csc* U_csc = (ReSolve::matrix::Csc*) KLU->getUFactor(); ReSolve::matrix::Csr* L = new ReSolve::matrix::Csr(L_csc->getNumRows(), L_csc->getNumColumns(), L_csc->getNnz()); ReSolve::matrix::Csr* U = new ReSolve::matrix::Csr(U_csc->getNumRows(), U_csc->getNumColumns(), U_csc->getNnz()); + L_csc->syncData(ReSolve::memory::DEVICE); + U_csc->syncData(ReSolve::memory::DEVICE); error_sum += matrix_handler->csc2csr(L_csc,L, ReSolve::memory::DEVICE); error_sum += matrix_handler->csc2csr(U_csc,U, ReSolve::memory::DEVICE); diff --git a/tests/functionality/testSysGLU.cpp b/tests/functionality/testSysGLU.cpp index cf2aba15..c903229f 100644 --- a/tests/functionality/testSysGLU.cpp +++ b/tests/functionality/testSysGLU.cpp @@ -66,6 +66,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::matrix::Csr* A = ReSolve::io::createCsrFromFile(mat1); + A->syncData(ReSolve::memory::DEVICE); mat1.close(); // Read first rhs vector @@ -202,6 +203,7 @@ int main(int argc, char *argv[]) return -1; } ReSolve::io::updateMatrixFromFile(mat2, A); + A->syncData(ReSolve::memory::DEVICE); mat2.close(); // Load the second rhs vector From 51569333e9330372f6c004b86031591bed67ee38 Mon Sep 17 00:00:00 2001 From: Slaven Peles Date: Thu, 10 Oct 2024 09:45:32 -0400 Subject: [PATCH 4/4] Fix output messages in matrix classes. --- resolve/matrix/Coo.cpp | 14 +++++++------- resolve/matrix/Csc.cpp | 10 +++++----- resolve/matrix/Csr.cpp | 6 +++--- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/resolve/matrix/Coo.cpp b/resolve/matrix/Coo.cpp index 7e7e9f4f..cb3316a9 100644 --- a/resolve/matrix/Coo.cpp +++ b/resolve/matrix/Coo.cpp @@ -133,7 +133,7 @@ namespace ReSolve index_type* matrix::Coo::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_row_data_; @@ -147,7 +147,7 @@ namespace ReSolve index_type* matrix::Coo::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_col_data_; @@ -161,7 +161,7 @@ namespace ReSolve real_type* matrix::Coo::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_val_data_; @@ -304,11 +304,11 @@ namespace ReSolve switch (memspace) { case HOST: if (h_data_updated_) { - out::misc() << "In Csr::syncData trying to sync host, but host already up to date!\n"; + out::misc() << "In Coo::syncData trying to sync host, but host already up to date!\n"; return 0; } if (!d_data_updated_) { - out::error() << "In Csr::syncData trying to sync host with device, but device is out of date!\n"; + out::error() << "In Coo::syncData trying to sync host with device, but device is out of date!\n"; assert(d_data_updated_); } if ((h_row_data_ == nullptr) != (h_col_data_ == nullptr)) { @@ -330,11 +330,11 @@ namespace ReSolve return 0; case DEVICE: if (d_data_updated_) { - out::misc() << "In Csr::syncData trying to sync device, but device already up to date!\n"; + out::misc() << "In Coo::syncData trying to sync device, but device already up to date!\n"; return 0; } if (!h_data_updated_) { - out::error() << "In Csr::syncData trying to sync device with host, but host is out of date!\n"; + out::error() << "In Coo::syncData trying to sync device with host, but host is out of date!\n"; assert(h_data_updated_); } if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { diff --git a/resolve/matrix/Csc.cpp b/resolve/matrix/Csc.cpp index a3a54a1b..899f37a3 100644 --- a/resolve/matrix/Csc.cpp +++ b/resolve/matrix/Csc.cpp @@ -34,7 +34,7 @@ namespace ReSolve index_type* matrix::Csc::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_row_data_; @@ -48,7 +48,7 @@ namespace ReSolve index_type* matrix::Csc::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_col_data_; @@ -62,7 +62,7 @@ namespace ReSolve real_type* matrix::Csc::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_val_data_; @@ -237,11 +237,11 @@ namespace ReSolve return 0; case DEVICE: if (d_data_updated_) { - out::misc() << "In Csr::syncData trying to sync device, but device already up to date!\n"; + out::misc() << "In Csc::syncData trying to sync device, but device already up to date!\n"; return 0; } if (!h_data_updated_) { - out::error() << "In Csr::syncData trying to sync device with host, but host is out of date!\n"; + out::error() << "In Csc::syncData trying to sync device with host, but host is out of date!\n"; assert(h_data_updated_); } if ((d_row_data_ == nullptr) != (d_col_data_ == nullptr)) { diff --git a/resolve/matrix/Csr.cpp b/resolve/matrix/Csr.cpp index 0adf8433..1e4dacb1 100644 --- a/resolve/matrix/Csr.cpp +++ b/resolve/matrix/Csr.cpp @@ -151,7 +151,7 @@ namespace ReSolve index_type* matrix::Csr::getRowData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_row_data_; @@ -165,7 +165,7 @@ namespace ReSolve index_type* matrix::Csr::getColData(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_col_data_; @@ -179,7 +179,7 @@ namespace ReSolve real_type* matrix::Csr::getValues(memory::MemorySpace memspace) { using namespace ReSolve::memory; - // syncData(memspace); + switch (memspace) { case HOST: return this->h_val_data_;