From 377b6f6808f9dee01802ecebc6e3e35a578c3ff2 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 28 May 2026 08:51:48 -0400 Subject: [PATCH 1/4] fixed error, need to move to C++ style --- resolve/Common.hpp | 1 + resolve/hykkt/cholesky/CholeskySolverHip.cpp | 4 ++++ tests/unit/hykkt/HykktCholeskyTests.hpp | 1 - tests/unit/hykkt/runHykktCholeskyTests.cpp | 4 ++-- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/resolve/Common.hpp b/resolve/Common.hpp index af30adadb..e59e299d7 100644 --- a/resolve/Common.hpp +++ b/resolve/Common.hpp @@ -19,6 +19,7 @@ namespace ReSolve constexpr real_type TWO = 2.0; constexpr real_type HALF = 0.5; constexpr real_type MINUS_ONE = -1.0; + constexpr index_type SEED = 12345; constexpr real_type MACHINE_EPSILON = std::numeric_limits::epsilon(); } // namespace constants diff --git a/resolve/hykkt/cholesky/CholeskySolverHip.cpp b/resolve/hykkt/cholesky/CholeskySolverHip.cpp index cc8ae4faa..682bae304 100644 --- a/resolve/hykkt/cholesky/CholeskySolverHip.cpp +++ b/resolve/hykkt/cholesky/CholeskySolverHip.cpp @@ -60,6 +60,7 @@ namespace ReSolve } A_chol_ = convertToCholmod(A); A_ = A; + mem_.deviceSynchronize(); } void CholeskySolverHip::symbolicAnalysis() @@ -70,7 +71,9 @@ namespace ReSolve { out::error() << "Cholesky symbolic analysis failed with status: " << Common_.status << "\n"; } + mem_.deviceSynchronize(); } + /** * @brief Perform numerical factorization for the Cholesky factorization @@ -156,6 +159,7 @@ namespace ReSolve out::error() << "Refactorization step failed with status: " << status << "\n"; } L_->setUpdated(memory::DEVICE); + mem_.deviceSynchronize(); } } diff --git a/tests/unit/hykkt/HykktCholeskyTests.hpp b/tests/unit/hykkt/HykktCholeskyTests.hpp index ec214381c..23b1518a8 100644 --- a/tests/unit/hykkt/HykktCholeskyTests.hpp +++ b/tests/unit/hykkt/HykktCholeskyTests.hpp @@ -127,7 +127,6 @@ namespace ReSolve solver.addMatrixInfo(A); solver.symbolicAnalysis(); solver.numericalFactorization(); - // Generate a random vector x_expected and compute b = A * x_expected vector::Vector* x_expected = randomVector(n); diff --git a/tests/unit/hykkt/runHykktCholeskyTests.cpp b/tests/unit/hykkt/runHykktCholeskyTests.cpp index d05fb5603..53ece4e47 100644 --- a/tests/unit/hykkt/runHykktCholeskyTests.cpp +++ b/tests/unit/hykkt/runHykktCholeskyTests.cpp @@ -8,7 +8,7 @@ #include #include #include - +#include "resolve/Common.hpp" #include "tests/unit/hykkt/HykktCholeskyTests.hpp" /** @@ -25,7 +25,7 @@ void runTests(const std::string& backend, ReSolve::memory::MemorySpace memspace, WorkspaceType workspace; workspace.initializeHandles(); ReSolve::MatrixHandler handler(&workspace); - + srand(ReSolve::constants::SEED); // set random seed for reproducibility ReSolve::tests::HykktCholeskyTests test(memspace, handler); result += test.minimalCorrectness(); From ecbeef9d6ab4ae9bf07218d6a8bec218ce32326a Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 28 May 2026 09:01:49 -0400 Subject: [PATCH 2/4] fixed random error due to changing seed, changed to C++ style random numbers --- tests/unit/hykkt/HykktCholeskyTests.hpp | 18 ++++++++++++------ tests/unit/hykkt/HykktSCCGTests.hpp | 12 ++++++++---- tests/unit/hykkt/runHykktCholeskyTests.cpp | 5 +++-- tests/unit/hykkt/runHykktSCCGTests.cpp | 4 +++- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/tests/unit/hykkt/HykktCholeskyTests.hpp b/tests/unit/hykkt/HykktCholeskyTests.hpp index 23b1518a8..cf1a9b8a1 100644 --- a/tests/unit/hykkt/HykktCholeskyTests.hpp +++ b/tests/unit/hykkt/HykktCholeskyTests.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -26,8 +27,8 @@ namespace ReSolve class HykktCholeskyTests : public TestBase { public: - HykktCholeskyTests(memory::MemorySpace memspace, MatrixHandler& matrixHandler) - : memspace_(memspace), matrixHandler_(matrixHandler) + HykktCholeskyTests(memory::MemorySpace memspace, MatrixHandler& matrixHandler, std::mt19937& generator) + : memspace_(memspace), matrixHandler_(matrixHandler), generator_(generator) { cholmod_start(&Common); } @@ -241,7 +242,8 @@ namespace ReSolve // reset values for (size_t j = 0; j < L->nzmax; j++) { - static_cast(L->x)[j] = 2.0 * static_cast(rand()) / RAND_MAX - 1.0; + std::uniform_real_distribution distribution(-1.0, 1.0); + static_cast(L->x)[j] = 2.0 * distribution(generator_) - 1.0; } cholmod_free_sparse(&L_tr, &Common); cholmod_free_sparse(&A_chol, &Common); @@ -265,6 +267,7 @@ namespace ReSolve private: ReSolve::memory::MemorySpace memspace_; MatrixHandler& matrixHandler_; + std::mt19937& generator_; cholmod_common Common; @@ -280,14 +283,16 @@ namespace ReSolve L_p[i + 1] = L_p[i]; for (size_t j = i; j < n; ++j) { - // with probability 'density', add a non-zero entry - if (i == j || static_cast(rand()) / RAND_MAX < density) + std::uniform_real_distribution rand_dist(0.0, 1.0); + if (i == j || rand_dist(generator_) < density) { L_i.push_back((int) j); // force diagonal entry to be non-zero double value = 2.0; if (i != j) { + std::uniform_real_distribution value_dist(-1.0, 1.0); + value = 2.0 * value_dist(generator_); value = 2.0 * static_cast(rand()) / RAND_MAX - 1.0; } L_x.push_back(value); @@ -310,9 +315,10 @@ namespace ReSolve { vector::Vector* v = new vector::Vector(n); v->allocate(memory::HOST); + std::uniform_real_distribution distribution(0.0, 1.0); for (index_type i = 0; i < n; ++i) { - v->getData(memory::HOST)[i] = static_cast(rand()) / RAND_MAX; + v->getData(memory::HOST)[i] = distribution(generator_); } v->setDataUpdated(memory::HOST); if (memspace_ == memory::DEVICE) diff --git a/tests/unit/hykkt/HykktSCCGTests.hpp b/tests/unit/hykkt/HykktSCCGTests.hpp index 2d8534742..035c82cd2 100644 --- a/tests/unit/hykkt/HykktSCCGTests.hpp +++ b/tests/unit/hykkt/HykktSCCGTests.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include #include @@ -35,9 +36,10 @@ namespace ReSolve * @param[in] memspace Memory space for the test (HOST or DEVICE). * @param[in] matrix_handler Reference to a matrix handler for the selected backend. * @param[in] vector_handler Reference to a vector handler for the selected backend. + * @param[in] generator Reference to a C++ random number generator. */ - HykktSchurComplementConjugateGradientTests(memory::MemorySpace memspace, MatrixHandler& matrix_handler, VectorHandler& vector_handler) - : memspace_(memspace), matrix_handler_(matrix_handler), vector_handler_(vector_handler) + HykktSchurComplementConjugateGradientTests(memory::MemorySpace memspace, MatrixHandler& matrix_handler, VectorHandler& vector_handler, std::mt19937& generator) + : memspace_(memspace), matrix_handler_(matrix_handler), vector_handler_(vector_handler), generator_(generator) { } @@ -130,16 +132,18 @@ namespace ReSolve memory::MemorySpace memspace_; ///< Memory space used by the test. MatrixHandler& matrix_handler_; ///< Backend-specific matrix handler. VectorHandler& vector_handler_; ///< Backend-specific vector handler. + std::mt19937& generator_; ///< C++ random number generator. /** - * @brief Generate a random vector of doubles between 0 and RAND_MAX. Copied from HykktCholeskyTests.hpp. + * @brief Generate a random vector of doubles between 0 and 1. Copied from HykktCholeskyTests.hpp. * @param[in] vec Target vector to write to. */ void randomVector(vector::Vector* vec) { + std::uniform_real_distribution distribution(0.0, 1.0); for (index_type i = 0; i < vec->getSize(); ++i) { - vec->getData(memory::HOST)[i] = static_cast(rand()) / RAND_MAX; + vec->getData(memory::HOST)[i] = distribution(generator_); } vec->setDataUpdated(memory::HOST); if (memspace_ == memory::DEVICE) diff --git a/tests/unit/hykkt/runHykktCholeskyTests.cpp b/tests/unit/hykkt/runHykktCholeskyTests.cpp index 53ece4e47..049c06c61 100644 --- a/tests/unit/hykkt/runHykktCholeskyTests.cpp +++ b/tests/unit/hykkt/runHykktCholeskyTests.cpp @@ -7,6 +7,7 @@ */ #include #include +#include #include #include "resolve/Common.hpp" #include "tests/unit/hykkt/HykktCholeskyTests.hpp" @@ -25,8 +26,8 @@ void runTests(const std::string& backend, ReSolve::memory::MemorySpace memspace, WorkspaceType workspace; workspace.initializeHandles(); ReSolve::MatrixHandler handler(&workspace); - srand(ReSolve::constants::SEED); // set random seed for reproducibility - ReSolve::tests::HykktCholeskyTests test(memspace, handler); + std::mt19937 generator(ReSolve::constants::SEED); // set random seed for reproducibility + ReSolve::tests::HykktCholeskyTests test(memspace, handler, generator); result += test.minimalCorrectness(); handler.setValuesChanged(true, memspace); diff --git a/tests/unit/hykkt/runHykktSCCGTests.cpp b/tests/unit/hykkt/runHykktSCCGTests.cpp index b3462676d..de0840a52 100644 --- a/tests/unit/hykkt/runHykktSCCGTests.cpp +++ b/tests/unit/hykkt/runHykktSCCGTests.cpp @@ -5,6 +5,7 @@ */ #include #include +#include #include #include @@ -35,7 +36,8 @@ void runTests(const std::string& backend, ReSolve::memory::MemorySpace memspace, workspace.initializeHandles(); ReSolve::MatrixHandler matrix_handler(&workspace); ReSolve::VectorHandler vector_handler(&workspace); - ReSolve::tests::HykktSchurComplementConjugateGradientTests test(memspace, matrix_handler, vector_handler); + std::mt19937 generator(ReSolve::constants::SEED); + ReSolve::tests::HykktSchurComplementConjugateGradientTests test(memspace, matrix_handler, vector_handler, generator); result += test.SCCGTest(); From 1e1d970a6619257531d559389ae8e737ccce30f9 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 28 May 2026 13:11:53 +0000 Subject: [PATCH 3/4] Apply pre-commmit fixes --- resolve/Common.hpp | 12 ++++++------ resolve/hykkt/cholesky/CholeskySolverHip.cpp | 1 - tests/unit/hykkt/runHykktCholeskyTests.cpp | 5 +++-- tests/unit/hykkt/runHykktSCCGTests.cpp | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/resolve/Common.hpp b/resolve/Common.hpp index e59e299d7..5d5ae88aa 100644 --- a/resolve/Common.hpp +++ b/resolve/Common.hpp @@ -14,12 +14,12 @@ namespace ReSolve namespace constants { - constexpr real_type ZERO = 0.0; - constexpr real_type ONE = 1.0; - constexpr real_type TWO = 2.0; - constexpr real_type HALF = 0.5; - constexpr real_type MINUS_ONE = -1.0; - constexpr index_type SEED = 12345; + constexpr real_type ZERO = 0.0; + constexpr real_type ONE = 1.0; + constexpr real_type TWO = 2.0; + constexpr real_type HALF = 0.5; + constexpr real_type MINUS_ONE = -1.0; + constexpr index_type SEED = 12345; constexpr real_type MACHINE_EPSILON = std::numeric_limits::epsilon(); } // namespace constants diff --git a/resolve/hykkt/cholesky/CholeskySolverHip.cpp b/resolve/hykkt/cholesky/CholeskySolverHip.cpp index 682bae304..358ebea9d 100644 --- a/resolve/hykkt/cholesky/CholeskySolverHip.cpp +++ b/resolve/hykkt/cholesky/CholeskySolverHip.cpp @@ -73,7 +73,6 @@ namespace ReSolve } mem_.deviceSynchronize(); } - /** * @brief Perform numerical factorization for the Cholesky factorization diff --git a/tests/unit/hykkt/runHykktCholeskyTests.cpp b/tests/unit/hykkt/runHykktCholeskyTests.cpp index 049c06c61..55ebeb7d6 100644 --- a/tests/unit/hykkt/runHykktCholeskyTests.cpp +++ b/tests/unit/hykkt/runHykktCholeskyTests.cpp @@ -9,6 +9,7 @@ #include #include #include + #include "resolve/Common.hpp" #include "tests/unit/hykkt/HykktCholeskyTests.hpp" @@ -25,8 +26,8 @@ void runTests(const std::string& backend, ReSolve::memory::MemorySpace memspace, WorkspaceType workspace; workspace.initializeHandles(); - ReSolve::MatrixHandler handler(&workspace); - std::mt19937 generator(ReSolve::constants::SEED); // set random seed for reproducibility + ReSolve::MatrixHandler handler(&workspace); + std::mt19937 generator(ReSolve::constants::SEED); // set random seed for reproducibility ReSolve::tests::HykktCholeskyTests test(memspace, handler, generator); result += test.minimalCorrectness(); diff --git a/tests/unit/hykkt/runHykktSCCGTests.cpp b/tests/unit/hykkt/runHykktSCCGTests.cpp index de0840a52..33f0e3856 100644 --- a/tests/unit/hykkt/runHykktSCCGTests.cpp +++ b/tests/unit/hykkt/runHykktSCCGTests.cpp @@ -36,7 +36,7 @@ void runTests(const std::string& backend, ReSolve::memory::MemorySpace memspace, workspace.initializeHandles(); ReSolve::MatrixHandler matrix_handler(&workspace); ReSolve::VectorHandler vector_handler(&workspace); - std::mt19937 generator(ReSolve::constants::SEED); + std::mt19937 generator(ReSolve::constants::SEED); ReSolve::tests::HykktSchurComplementConjugateGradientTests test(memspace, matrix_handler, vector_handler, generator); result += test.SCCGTest(); From 26ae71b7a168dd86383e74054f8f264576955253 Mon Sep 17 00:00:00 2001 From: shakedregev Date: Thu, 28 May 2026 09:17:36 -0400 Subject: [PATCH 4/4] changelog update --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c760b738..fdeb1ade5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ - Added classes and tests for permutation, Ruiz scaling, Cholesky factorization, Schur complement conjugate gradient and matrix multiplication and addition. +- Changed random number generation int tests to be C++ style and fixed-seed, to avoid random failures. + +- Added Schur Complement Conjugate Gradient class. + ## Changes to Re::Solve since release 0.99.2 - Added cmake-format.