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
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ CI: $(TARGET)
run $(CI_RUN) -a CFL_multsrc --use-start-nodes --compute-results; \
run_advanced CFL_multsrc_adv --compute-results; \
run_advanced CFL_multsrc_adv --use-start-nodes --compute-results; \
run $(CI_RUN) -a CFL_Kron; \
exit $$status

$(BUILD_DIR)/test_explode_indices: tests/test_explode_indices.c $(LIB_SRCS)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ reachable pairs and saves the measurements to CSV files in `results/`. It suppor
`CFL_CFPQ_RSM` takes the grammar as a recursive state machine read from the
`.rsm` file next to the grammar (`data/grammars/c_alias.rsm` for
`data/grammars/c_alias.cnf`), in the FLPQ_Data format
- `CFL_Kron`, which finds all reachable pairs with the Kronecker product of the graph
and the same `.rsm` recursive state machine as `CFL_CFPQ_RSM`

The graphs and grammars come from the [FLPQ_Data](https://github.com/FormalLanguageConstrainedPathQuerying/FLPQ_Data)
collection.
Expand All @@ -46,7 +48,7 @@ Benchmark options:
--use-random-percent <n>
Use a random n% of the vertices as start vertices, the same
ones for every run
-a <algorithm> Algorithm to use (default: CFL_adv; options: CFL_adv, CFL, CFL_single_path, CFL_all_path, CFL_all_path_adv, CFL_CFPQ_RSM, CFL_multsrc, CFL_multsrc_adv)
-a <algorithm> Algorithm to use (default: CFL_adv; options: CFL_adv, CFL, CFL_single_path, CFL_all_path, CFL_all_path_adv, CFL_CFPQ_RSM, CFL_multsrc, CFL_multsrc_adv, CFL_Kron)

Optimization flags:
-e Enable empty optimization
Expand Down
200 changes: 200 additions & 0 deletions src/adapters/adapter_CFL_Kron.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
#include "adapter_CFL_Kron.h"
#include "GraphBLAS.h"
#include "LAGraph.h"
#include "adapter_CFL_common.h"
#include "parser.h"
#include "rsm_file.h"
#include <stdlib.h>

#define TRY(GrB_method) \
{ \
GrB_Info LG_GrB_Info = GrB_method; \
if (LG_GrB_Info < GrB_SUCCESS) { \
fprintf(stderr, "LAGraph failure (file %s, line %d): (%d, msg: %s) \n", __FILE__, __LINE__, LG_GrB_Info, \
state.msg); \
return (LG_GrB_Info); \
} \
}

// inner state of the adapter
typedef struct {
RSM rsm;
GrB_Matrix *adj_matrices;
GrB_Matrix *outputs;
GrB_Matrix M_intersect;
char msg[LAGRAPH_MSG_LEN];
} state_t;

static state_t state;

static GrB_Info adapter_CFL_setup(void) {
TRY(LAGr_Init(GrB_NONBLOCKING, malloc, NULL, NULL, free, state.msg));

return GrB_SUCCESS;
}

// prepare the adapter for use with the given parser result
// this may include converting the grammar and graph into a format suitable for the algorithm
//
// this modify ther inner state of the adapter
//
// adapter_CFL_prepare should be called just once for each config
static GrB_Info adapter_CFL_prepare(const ParserResult *parser_result, void *prepare_data) {
(void)prepare_data;
ParserResult working_copy = parser_result_copy(parser_result);
Grammar *grammar = &working_copy.grammar;
Graph *graph = &working_copy.graph;
SymbolList *list = &working_copy.symbols;

grammar_to_WCNF(grammar, list);
SymbolList terms = symbol_list_create();
SymbolList nonterms = symbol_list_create();

grammar_split_terms_nonterms(grammar, list, &terms, &nonterms);

// change term indecies in graph
for (size_t i = 0; i < graph->edge_count; i++) {
graph->edges[i].term_index = symbol_list_get_index_str(&terms, list->symbols[graph->edges[i].term_index].label);
}

explode_indices_CFL(grammar, graph, &nonterms, &terms);

// the RSM takes over "terms" even when it fails to parse
CFG_RSM *rsm = rsm_from_file(parser_result->rsm_path, parser_result->block_count, &terms);
if (rsm == NULL) {
symbol_list_free(&nonterms);
free_parser_result(&working_copy);
return GrB_INVALID_VALUE;
}

GrB_Matrix *prepared_adj_matrices = calloc(rsm->terms.count, sizeof(GrB_Matrix));
if (prepared_adj_matrices == NULL) {
fprintf(stderr, "out of memory\n");
abort();
}
for (size_t i = 0; i < rsm->terms.count; i++) {
TRY(GrB_Matrix_new(prepared_adj_matrices + i, GrB_BOOL, graph->node_count, graph->node_count));
}

GrB_Scalar true_scalar;
TRY(GrB_Scalar_new(&true_scalar, GrB_BOOL));
TRY(GrB_Scalar_setElement_BOOL(true_scalar, true));

GrB_Index *row = malloc(sizeof(GrB_Index) * graph->edge_count);
GrB_Index *col = malloc(sizeof(GrB_Index) * graph->edge_count);
if ((row == NULL || col == NULL) && graph->edge_count > 0) {
fprintf(stderr, "out of memory\n");
abort();
}
for (size_t i = 0; i < rsm->terms.count; i++) {
size_t count = 0;

for (size_t j = 0; j < graph->edge_count; j++) {
if (i == graph->edges[j].term_index) {
row[count] = graph->edges[j].u;
col[count] = graph->edges[j].v;
count++;
}
}

TRY(GxB_Matrix_build_Scalar(prepared_adj_matrices[i], row, col, true_scalar, count));
#ifdef DEBUG_parser
GxB_print(prepared_adj_matrices[i], 1);
#endif
}

state.rsm = rsm_convert_to_lagraph(rsm);
state.adj_matrices = prepared_adj_matrices;
state.outputs = NULL;
state.M_intersect = NULL;

free(row);
free(col);
GrB_free(&true_scalar);

symbol_list_free(&nonterms);

rsm_free(rsm);
free_parser_result(&working_copy);

return GrB_SUCCESS;
}

// initialize output matrices
//
// this should be called before each run of the algorithm
static GrB_Info adapter_CFL_init_outputs(void) {
// the algorithm creates the output matrices itself
TRY(adapter_CFL_init_outputs_common(&state.outputs, state.rsm.nonterminal_count, 0, state.msg));
state.M_intersect = NULL;

return GrB_SUCCESS;
}

// run the algorithm
//
// this should be called after adapter_CFL_init_outputs
static GrB_Info adapter_CFL_run(void) {
TRY(LAGraph_CFL_AllPaths_Kronecker(&state.M_intersect, state.outputs, state.adj_matrices,
(int64_t)state.rsm.terminal_count, &state.rsm, state.msg));

return GrB_SUCCESS;
}

// check if the result of the algorithm is valid
static ResultType adapter_CFL_is_result_valid(size_t valid_result) {
ResultType is_valid = RESULT_UNKNOWN;
GrB_Info info =
adapter_CFL_is_result_valid_common(state.outputs[state.rsm.start_nonterminal], valid_result, &is_valid);
if (info < GrB_SUCCESS) {
fprintf(stderr, "LAGraph failure (file %s, line %d): (%d, msg: %s) \n", __FILE__, __LINE__, info, state.msg);
return RESULT_UNKNOWN;
}
return is_valid;
}

static size_t adapter_CFL_get_result(void) {
size_t result = 0;
TRY(adapter_CFL_get_result_common(state.outputs[state.rsm.start_nonterminal], &result));
return result;
}

// free output matrices
//
// this should be called after each run of the algorithm
static GrB_Info adapter_CFL_free_outputs(void) {
TRY(GrB_free(&state.M_intersect));
TRY(adapter_CFL_free_outputs_common(&state.outputs, state.rsm.nonterminal_count, state.msg));

return GrB_SUCCESS;
}

// free all resources allocated by the adapter except outputs
//
// this should be called after all runs of the algorithm for the given config
static GrB_Info adapter_CFL_cleanup(void) {
TRY(adapter_CFL_cleanup_common(&state.adj_matrices, state.rsm.terminal_count, (void **)NULL));
rsm_lagraph_rsm_free(&state.rsm);

return GrB_SUCCESS;
}

// free LAGraph\GraphBLAS resources
static GrB_Info adapter_CFL_teardown(void) {
TRY(LAGraph_Finalize(state.msg));
return GrB_SUCCESS;
}

// get the methods of the adapter
AdapterMethods adapter_CFL_Kron_get_methods(void) {
AdapterMethods methods = {.setup = adapter_CFL_setup,
.teardown = adapter_CFL_teardown,
.init_outputs = adapter_CFL_init_outputs,
.free_outputs = adapter_CFL_free_outputs,
.run = adapter_CFL_run,
.prepare = adapter_CFL_prepare,
.cleanup = adapter_CFL_cleanup,
.is_result_valid = adapter_CFL_is_result_valid,
.get_result = adapter_CFL_get_result};
return methods;
}
7 changes: 7 additions & 0 deletions src/adapters/adapter_CFL_Kron.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include "adapter.h"
#include <GraphBLAS.h>
#include <LAGraph.h>

AdapterMethods adapter_CFL_Kron_get_methods(void);
2 changes: 2 additions & 0 deletions src/adapters/registry.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "registry.h"
#include "adapter_CFL.h"
#include "adapter_CFL_CFPQ_RSM.h"
#include "adapter_CFL_Kron.h"
#include "adapter_CFL_adv.h"
#include "adapter_CFL_all_path.h"
#include "adapter_CFL_all_path_adv.h"
Expand Down Expand Up @@ -63,6 +64,7 @@ static const AlgorithmEntry algorithms[] = {
{"CFL_CFPQ_RSM", adapter_CFL_CFPQ_RSM_get_methods, prepare_CFL_CFPQ_RSM, true},
{"CFL_multsrc", adapter_CFL_multsrc_get_methods, prepare_CFL_multsrc, true},
{"CFL_multsrc_adv", adapter_CFL_multsrc_adv_get_methods, prepare_CFL_multsrc_adv, true},
{"CFL_Kron", adapter_CFL_Kron_get_methods, prepare_no_data, false},
};

#define ALGORITHMS_COUNT (sizeof(algorithms) / sizeof(algorithms[0]))
Expand Down
1 change: 1 addition & 0 deletions tools/run_test_dataset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ for flags in "${optimizations[@]}"; do
variants+=("CFL_multsrc_adv $flags --compute-results")
variants+=("CFL_multsrc_adv $flags --use-start-nodes --compute-results")
done
variants+=("CFL_Kron")

limiter=()
if command -v systemd-run >/dev/null && systemd-run --user --scope -q true 2>/dev/null; then
Expand Down
Loading