Skip to content
Open
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
32 changes: 32 additions & 0 deletions experimental/algorithm/LAGraph_CFL_reachability_advanced.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,32 @@ typedef struct {
int32_t count;
} CFL_Symbol;

// Constraint 1 of LAGraph_rule_EWCNF: a symbol indexed in one rule must be indexed in every
// rule where it appears. Finds a symbol that is a member of an indexed family
// (base .. base+count-1) and is also used alone; *mixed = -1 if there is none.
static GrB_Info find_mixed_symbol(const LAGraph_rule_EWCNF *rules, size_t rules_count,
size_t symbols_amount, int64_t *mixed, char *msg) {
const int bit[3] = {LAGraph_EWNCF_INDEX_NONTERM, LAGraph_EWNCF_INDEX_PROD_A,
LAGraph_EWNCF_INDEX_PROD_B};
uint8_t *use = NULL; // 1: used alone, 2: in an indexed family
*mixed = -1;
GrB_Info info = LAGraph_Calloc((void **)&use, symbols_amount, sizeof(uint8_t), msg);
if (info != GrB_SUCCESS) return info;
for (size_t i = 0; i < rules_count; i++) {
const int32_t s[3] = {rules[i].nonterm, rules[i].prod_A, rules[i].prod_B};
for (int j = 0; j < 3; j++) {
if (s[j] < 0) continue;
bool fam = rules[i].indexed_count > 0 && (rules[i].indexed & bit[j]);
size_t end = (size_t)s[j] + (fam ? rules[i].indexed_count : 1);
for (size_t k = s[j]; k < end && k < symbols_amount; k++) use[k] |= fam ? 2 : 1;
}
}
for (size_t k = 0; k < symbols_amount && *mixed < 0; k++) {
if (use[k] == 3) *mixed = (int64_t)k;
}
return LAGraph_Free((void **)&use, msg);
}

// When using the OPT_BLOCK optimization, indexed symbols must be grouped together.
// This produces a mapping: [old_index -> (new_index, base_index, indexed_count)]
// - new_index: the index of the symbol in the new numeration
Expand Down Expand Up @@ -761,6 +787,12 @@ GrB_Info LAGraph_CFL_reachability_adv(
}
}

int64_t mixed;
TRY(find_mixed_symbol(rules, rules_count, symbols_amount, &mixed, msg));
LG_ASSERT_MSGF(mixed < 0, GrB_INVALID_VALUE,
"Symbol %lld is used both in an indexed family and alone.",
(long long)mixed);

GrB_Index n;
TRY(GrB_Matrix_ncols(&n, adj_matrices[0]));

Expand Down
56 changes: 56 additions & 0 deletions experimental/test/test_CFL_reachability_adv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,61 @@ void test_CFL_reachability_null_pointers(void) {
#endif
}

// A symbol of an indexed family that a rule also uses alone breaks constraint 1 of
// LAGraph_rule_EWCNF. OPT_BLOCK used to lose the pair (0, 2) here silently; it must be an error
// for every mask. Without the rule X -> [_0 the grammar is valid and gives (0, 2).
//
// Graph: 0 -[_1-> 1 -]_1-> 2
// Nonterms: [0 S] [1 C_0] [2 C_1] [3 D_0] [4 D_1] [5 X]; terms: [6 [_0] [7 [_1] [8 ]_0] [9 ]_1]
// X -> [_0, S -> C_i D_i, C_i -> [_i, D_i -> ]_i (i = 0..1)
void test_CFL_reachability_mixed_indexed_symbol(void) {
#if LAGRAPH_SUITESPARSE
setup();

for (size_t with_plain = 0; with_plain < 2; with_plain++) {
for (size_t mask = 0; mask < 16; mask++) {
GrB_Info retval;

LAGraph_rule_EWCNF *rules = NULL;
LAGraph_Calloc((void **)&rules, 4, sizeof(LAGraph_rule_EWCNF), msg);
size_t r = 0;
if (with_plain) rules[r++] = (LAGraph_rule_EWCNF){5, 6, -1, 0, 0};
rules[r++] = (LAGraph_rule_EWCNF){
0, 1, 3, 2, LAGraph_EWNCF_INDEX_PROD_A | LAGraph_EWNCF_INDEX_PROD_B};
rules[r++] = (LAGraph_rule_EWCNF){
1, 6, -1, 2, LAGraph_EWNCF_INDEX_NONTERM | LAGraph_EWNCF_INDEX_PROD_A};
rules[r++] = (LAGraph_rule_EWCNF){
3, 8, -1, 2, LAGraph_EWNCF_INDEX_NONTERM | LAGraph_EWNCF_INDEX_PROD_A};
grammar = (grammar_t){
.nonterms_count = 6, .terms_count = 4, .rules_count = r, .rules = rules};

n_adj_matrices = 10;
LAGraph_Calloc((void **)&adj_matrices, n_adj_matrices, sizeof(GrB_Matrix), msg);
for (size_t i = 0; i < n_adj_matrices; i++) {
OK(GrB_Matrix_new(&adj_matrices[i], GrB_BOOL, 3, 3));
}
OK(GrB_Matrix_setElement(adj_matrices[7], true, 0, 1));
OK(GrB_Matrix_setElement(adj_matrices[9], true, 1, 2));
init_outputs();

if (with_plain) {
check_error(GrB_INVALID_VALUE, mask);
TEST_CHECK(strstr(msg, "Symbol 6 is used both in an indexed family") != NULL);
TEST_MSG("Mask: %zu, msg: %s", mask, msg);
} else {
OK(run_algorithm(mask));
TEST_MSG("Mask: %zx, error: %s", mask, msg);
check_result("(0, 2)");
}

free_workspace();
}
}

teardown();
#endif
}

TEST_LIST = {
{"CFG_reachability_indexed", test_CFL_indexed},
{"CFG_reachability_indexed_2", test_CFL_indexed_2},
Expand All @@ -1273,6 +1328,7 @@ TEST_LIST = {
{"CFL_reachability_line", test_CFL_reachability_line},
{"CFL_reachability_two_nodes_cycle", test_CFL_reachability_two_nodes_cycle},
{"CFG_reach_basic_invalid_rules", test_CFL_reachability_invalid_rules},
{"CFL_reachability_mixed_indexed_symbol", test_CFL_reachability_mixed_indexed_symbol},
{"test_CFL_reachability_with_empty_adj_matrix",
test_CFL_reachability_with_empty_adj_matrix},
#if !defined(GRAPHBLAS_HAS_CUDA)
Expand Down