From 6cde310b6d95a4fc6c63fde2ea7fb5ffd10e77b1 Mon Sep 17 00:00:00 2001 From: dance858 Date: Mon, 16 Nov 2026 17:39:23 -0800 Subject: [PATCH 1/2] relative tolerance in parallel rows --- include/core/Numerics.h | 5 +++ src/explorers/Parallel_rows.c | 26 ++++++++-------- tests/test_Parallel_rows.h | 58 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 12 deletions(-) diff --git a/include/core/Numerics.h b/include/core/Numerics.h index 5a1da0a8..75d7d2f5 100644 --- a/include/core/Numerics.h +++ b/include/core/Numerics.h @@ -23,6 +23,11 @@ #define FEAS_TOL 1e-6 #define CANCEL_TOL_REL 1e-12 +/* relative tolerance under which two rows (or two columns) count as parallel. + A mismatch this small moves the activity of a row by less than a solver's + feasibility tolerance relative to the terms of the row, so a solver cannot + tell the two apart, and keeping both leaves it a nearly dependent pair */ +#define PARALLEL_TOL_REL 1e-6 /* relative tolerance under which an updated diagonal entry of P counts as zero (the 2x2 principal minors of an input P are only PSD up to rounding) */ #define P_DIAG_CANCEL_TOL_REL 1e-9 diff --git a/src/explorers/Parallel_rows.c b/src/explorers/Parallel_rows.c index ae511c09..635c6675 100644 --- a/src/explorers/Parallel_rows.c +++ b/src/explorers/Parallel_rows.c @@ -133,6 +133,13 @@ static inline int get_bin_size(int start, size_t n_rows, const int *rows, return i - start; } +/* True when two coefficients agree up to PARALLEL_TOL_REL relative to the + larger one. */ +static inline bool is_parallel_coeff(double a, double b) +{ + return ABS(a - b) <= PARALLEL_TOL_REL * MAX(ABS(a), ABS(b)); +} + #ifndef NDEBUG // make sure that the rows in the same bin have the same coefficient // hash value and the same sparsity ID @@ -184,18 +191,16 @@ static void VERIFY_PARALLEL_ROWS(const Matrix *A, const RowTag *rows_tags, ARRAYS_EQUAL_INT(cols1, A->i + A->p[parallel_rows[j]].start, len1); } - // check that all rows have same normalized coefficients - double *vals1 = A->x + A->p[parallel_rows[start]].start; - for (j = start + 1; j < end; ++j) + // check that all rows have the same coefficients up to a multiple. + // Every row was accepted against the reference row, stored last. + double *vals1 = A->x + A->p[parallel_rows[end - 1]].start; + for (j = start; j < end - 1; ++j) { double *vals2 = A->x + A->p[parallel_rows[j]].start; double ratio = vals1[0] / vals2[0]; for (k = 1; k < len1; ++k) { - // relative tolerance: rows with large coefficients are - // detected through hashes of normalized values - assert(ABS(vals1[k] - ratio * vals2[k]) <= - FEAS_TOL * MAX(1.0, ABS(vals1[k]))); + assert(is_parallel_coeff(vals1[k], ratio * vals2[k])); } } } @@ -244,11 +249,8 @@ static inline int find_parallel_rows_in_bin(const Matrix *A, const int *bin, double ratio = vals1[0] / vals2[0]; for (j = 0; j < len2; ++j) { - // if we run into numerical issues we might want to do - // diff = vals1[j] / vals2[j] - ratio - double diff = vals1[j] - ratio * vals2[j]; - - if (!IS_ZERO_FEAS_TOL(diff) || cols1[j] != cols2[j]) + if (cols1[j] != cols2[j] || + !is_parallel_coeff(vals1[j], ratio * vals2[j])) { break; } diff --git a/tests/test_Parallel_rows.h b/tests/test_Parallel_rows.h index 29d897f1..60a9da22 100644 --- a/tests/test_Parallel_rows.h +++ b/tests/test_Parallel_rows.h @@ -1461,6 +1461,61 @@ static char *test_21_parallel_rows_eq_eq_large_sides() return 0; } +/* The rows (1, 1e-3) and (1, 1.0003e-3) are not parallel: their second + entries differ by 0.03%, although the difference is below 1e-6 in absolute + terms. */ +static char *test_22_parallel_rows_small_entry_not_parallel() +{ + double Ax[] = {1, 1e-3, 1, 1.0003e-3}; + int Ai[] = {0, 1, 0, 1}; + int Ap[] = {0, 2, 4}; + + Matrix *A = matrix_new(Ax, Ai, Ap, 2, 2, 4); + RowTag row_tags[] = {R_TAG_NONE, R_TAG_NONE}; + + iVec *group_start = iVec_new(10); + int parallel_rows[2]; + int sparsity_IDs[2]; + int coeff_hashes[2]; + int radix_aux[2]; + + find_parallel_rows(A, row_tags, group_start, parallel_rows, sparsity_IDs, + coeff_hashes, R_TAG_INACTIVE, radix_aux, NULL); + + mu_assert("not grouped", group_start->len == 1); + + free_matrix(A); + iVec_free(group_start); + return 0; +} + +/* The rows (1e6, 2e6) and (1e6, 2e6 + 0.5) are parallel: they agree to 2.5e-7 + relative, although the difference is 0.5 in absolute terms. */ +static char *test_23_parallel_rows_large_coeffs_parallel() +{ + double Ax[] = {1e6, 2e6, 1e6, 2e6 + 0.5}; + int Ai[] = {0, 1, 0, 1}; + int Ap[] = {0, 2, 4}; + + Matrix *A = matrix_new(Ax, Ai, Ap, 2, 2, 4); + RowTag row_tags[] = {R_TAG_NONE, R_TAG_NONE}; + + iVec *group_start = iVec_new(10); + int parallel_rows[2]; + int sparsity_IDs[2]; + int coeff_hashes[2]; + int radix_aux[2]; + + find_parallel_rows(A, row_tags, group_start, parallel_rows, sparsity_IDs, + coeff_hashes, R_TAG_INACTIVE, radix_aux, NULL); + + mu_assert("grouped", group_start->len == 2 && group_start->data[1] == 2); + + free_matrix(A); + iVec_free(group_start); + return 0; +} + static const char *all_tests_parallel_rows() { mu_run_test(test_1_parallel_rows, counter_parallel_rows); @@ -1485,6 +1540,9 @@ static const char *all_tests_parallel_rows() mu_run_test(test_20_parallel_rows_eq_ineq_rounding_swapped, counter_parallel_rows); mu_run_test(test_21_parallel_rows_eq_eq_large_sides, counter_parallel_rows); + mu_run_test(test_22_parallel_rows_small_entry_not_parallel, + counter_parallel_rows); + mu_run_test(test_23_parallel_rows_large_coeffs_parallel, counter_parallel_rows); return 0; } From fc3284040d536273d4558ed623abcdac364f13f9 Mon Sep 17 00:00:00 2001 From: dance858 Date: Mon, 16 Nov 2026 18:38:24 -0800 Subject: [PATCH 2/2] comment --- include/core/Numerics.h | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/include/core/Numerics.h b/include/core/Numerics.h index 75d7d2f5..ac05afcb 100644 --- a/include/core/Numerics.h +++ b/include/core/Numerics.h @@ -23,10 +23,7 @@ #define FEAS_TOL 1e-6 #define CANCEL_TOL_REL 1e-12 -/* relative tolerance under which two rows (or two columns) count as parallel. - A mismatch this small moves the activity of a row by less than a solver's - feasibility tolerance relative to the terms of the row, so a solver cannot - tell the two apart, and keeping both leaves it a nearly dependent pair */ +/* relative tolerance under which two rows (or two columns) count as parallel */ #define PARALLEL_TOL_REL 1e-6 /* relative tolerance under which an updated diagonal entry of P counts as zero (the 2x2 principal minors of an input P are only PSD up to rounding) */