Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/core/Numerics.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

#define FEAS_TOL 1e-6
#define CANCEL_TOL_REL 1e-12
/* 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) */
#define P_DIAG_CANCEL_TOL_REL 1e-9
Expand Down
26 changes: 14 additions & 12 deletions src/explorers/Parallel_rows.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]));
}
}
}
Expand Down Expand Up @@ -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;
}
Expand Down
58 changes: 58 additions & 0 deletions tests/test_Parallel_rows.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down
Loading