forked from GraphBLAS/LAGraph
-
Notifications
You must be signed in to change notification settings - Fork 2
Implement CFL all-paths path-finding #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DanyaLitva
wants to merge
44
commits into
SparseLinearAlgebra:stable
Choose a base branch
from
DanyaLitva:stable
base: stable
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,509
−294
Open
Changes from 27 commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
b6fc74f
init commit
DanyaLitva 15c7f77
init commit
DanyaLitva bc75ac2
Merge pull request #1 from DanyaLitva/AllPaths
DanyaLitva 4d4eb8d
demo demo version
DanyaLitva b5cf0c3
change struct and remove LG_ function
DanyaLitva ae86682
bottom scalar?
DanyaLitva 9cf1ccc
Update LAGraph_CFL_all_paths.c
DanyaLitva 79bc5e2
Update LAGraph_CFL_all_paths.c
DanyaLitva b5e8036
Update LAGraph_CFL_all_paths.c
DanyaLitva 627c6c4
fix
DanyaLitva bb7f6a6
fix
DanyaLitva caefef0
fix
DanyaLitva 9d5c08d
Update LAGraph_CFL_AllPaths.c
DanyaLitva 3fdc0fc
Merge pull request #2 from DanyaLitva/AllPaths
DanyaLitva bf057d5
Update LAGraph_CFL_AllPaths.c
DanyaLitva 5a506c5
Merge pull request #3 from DanyaLitva/AllPaths
DanyaLitva fea16a9
clear
DanyaLitva d2f3d5d
Merge branch 'stable' of https://github.com/DanyaLitva/LAGraph into s…
DanyaLitva cf6b131
Update LAGraph_CFL_reachability.c
DanyaLitva ab63c57
Update LAGraphX.h
DanyaLitva d366dbd
fix
DanyaLitva 72e0b44
tests to check that something is running
DanyaLitva 28fdd2b
Update LAGraph_CFL_AllPaths.c
DanyaLitva 571bf7b
index op fix
DanyaLitva c612d92
Update LAGraph_CFL_AllPaths.c
DanyaLitva 3e6add0
replace GrB_Matrix_nvals in CFPQ_core
DanyaLitva 5ca41bb
cosmetic
DanyaLitva ad8bedc
fix naming, comment, and replace get_nvals
DanyaLitva 2252e74
Sanitizer fix
DanyaLitva 2cbf64e
delete add without free mem
DanyaLitva dba1f05
delete add_eps from LAGraphX.h
DanyaLitva 239f822
cosmetic
DanyaLitva ab746fb
shared tests across different algorithms
DanyaLitva 14d69d9
fix naming and remove print from tests
DanyaLitva 719250e
change test name
DanyaLitva fb81351
free output matrix with iterator and return print for debug
DanyaLitva 3d89e8c
to the previous commit
DanyaLitva f9f7047
get_nvals v1.1 and first comment
DanyaLitva d4fe2d5
fix free type in algorithm
DanyaLitva 3128f69
add check GxB_VERSION and add comment
DanyaLitva 4b050ee
valgrind fix
DanyaLitva 3f49636
switch get_nvals
DanyaLitva 2caeade
вынесение первой итерации в merge
DanyaLitva 0c0931c
Update LAGraph_CFL_AllPaths.c
DanyaLitva File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,257 @@ | ||
| #define LG_FREE_WORK \ | ||
| { \ | ||
| GrB_free(&AllPaths_semiring_free); \ | ||
| GrB_free(&AllPaths_monoid_free); \ | ||
| GrB_free(&bottom_scalar); \ | ||
| GrB_free(&IAllPaths_mult); \ | ||
| GrB_free(&AllPaths_set); \ | ||
| GrB_free(&AllPaths_mult); \ | ||
| GrB_free(&AllPaths_add); \ | ||
| GrB_free(&AllPaths_add_free); \ | ||
| GrB_free(&Theta); \ | ||
| } | ||
|
|
||
| #include "LG_internal.h" | ||
| #include <LAGraphX.h> | ||
|
|
||
| static GrB_Index* merge_all_paths(GrB_Index* n, const void* left, const GrB_Index na, const void* right, const GrB_Index nb){ | ||
| GrB_Index* a = (GrB_Index*) left; | ||
| GrB_Index* b = (GrB_Index*) right; | ||
|
|
||
| if (na == 0 && nb == 0) { | ||
| *n = 0; | ||
| return NULL; | ||
| } | ||
|
|
||
| GrB_Index *tmp = malloc((na + nb) * sizeof(GrB_Index)); | ||
| // LG_TRY(LAGraph_Malloc((void**)&tmp, na+nb, sizeof(GrB_Index), msg)); | ||
|
|
||
| GrB_Index ia = 0, ib = 0, outn = 0; | ||
| while (ia < na && ib < nb) { | ||
| GrB_Index va = a[ia]; | ||
| GrB_Index vb = b[ib]; | ||
| if (va < vb) { | ||
| if (outn == 0 || tmp[outn-1] != va) tmp[outn++] = va; | ||
| ia++; | ||
| } else if (vb < va) { | ||
| if (outn == 0 || tmp[outn-1] != vb) tmp[outn++] = vb; | ||
| ib++; | ||
| } else { | ||
| if (outn == 0 || tmp[outn-1] != va) tmp[outn++] = va; | ||
| ia++; ib++; | ||
| } | ||
| } | ||
| while (ia < na) { | ||
| GrB_Index va = a[ia++]; | ||
| if (outn == 0 || tmp[outn-1] != va) tmp[outn++] = va; | ||
| } | ||
| while (ib < nb) { | ||
| GrB_Index vb = b[ib++]; | ||
| if (outn == 0 || tmp[outn-1] != vb) tmp[outn++] = vb; | ||
| } | ||
|
|
||
| // LG_TRY(LAGraph_Realloc((void**)&tmp, outn, na+nb, sizeof(GrB_Index), msg)); | ||
| GrB_Index *sh = realloc(tmp, outn * sizeof(GrB_Index)); | ||
| if (sh) tmp = sh; | ||
|
|
||
| *n = outn; | ||
| return tmp; | ||
| } | ||
|
|
||
| void clear_all_paths_vex(AllPathsVex *z){ | ||
| // if(z->middle) LG_TRY(LAGraph_Free((void**) z->middle, msg)); | ||
| if(z->middle){ | ||
| free(z->middle); | ||
| z->middle=NULL; | ||
| } | ||
| z->n = 0; | ||
| } | ||
|
|
||
| void add_all_paths(AllPathsVex *z, const AllPathsVex *x, const AllPathsVex *y) | ||
DanyaLitva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| z->middle = merge_all_paths(&z->n, x->middle, x->n, y->middle, y->n); | ||
| } | ||
|
|
||
| void add_all_paths_free(AllPathsVex *z, AllPathsVex *x, AllPathsVex *y) | ||
| { | ||
| AllPathsVex v_temp; | ||
| AllPathsVex* temp = &v_temp; | ||
| temp->middle = merge_all_paths(&temp->n, x->middle, x->n, y->middle, y->n); | ||
| clear_all_paths_vex(x); | ||
| clear_all_paths_vex(y); | ||
| // clear_all_paths_vex(z); | ||
DanyaLitva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| z->middle = temp->middle; | ||
| z->n = temp->n; | ||
| } | ||
|
|
||
| void mult_all_paths_index(AllPathsVex *z, | ||
| const AllPathsVex *x, GrB_Index ix, GrB_Index jx, | ||
| const AllPathsVex *y, GrB_Index iy, GrB_Index jy, | ||
| const void *theta) | ||
| { | ||
| // LG_TRY(LAGraph_Malloc((void**) &z->middle, 1, size_of(GrB_Index), msg)); | ||
| z->middle = malloc(sizeof(GrB_Index)); | ||
| z->middle[0] = jx; | ||
| z->n = 1; | ||
| } | ||
|
|
||
| void set_all_paths(AllPathsVex *z, const AllPathsVex *x, const bool *edge_exist) | ||
| { | ||
| AllPathsVex temp; | ||
| temp.middle = NULL; | ||
| temp.n = 0; | ||
|
|
||
| if (edge_exist && *edge_exist){ | ||
| temp.middle = malloc(sizeof(GrB_Index)); | ||
| temp.n = 1; | ||
| temp.middle[0] = GrB_INDEX_MAX; | ||
| } | ||
|
|
||
| z->middle = merge_all_paths(&z->n, x->middle, x->n, temp.middle, temp.n); | ||
| clear_all_paths_vex(&temp); | ||
| } | ||
|
|
||
| #define MULT_PATH_INDEX_DEFN \ | ||
| "void mult_all_paths_index(AllPathsVex *z, \n" \ | ||
| " const AllPathsVex *x, GrB_Index ix, GrB_Index jx, \n" \ | ||
| " const AllPathsVex *y, GrB_Index iy, GrB_Index jy, \n" \ | ||
| " const void *theta) \n" \ | ||
| "{ \n" \ | ||
| " //clear_all_paths_vex(z); \n" \ | ||
| " z->middle = malloc(sizeof(GrB_Index)); \n" \ | ||
| " z->middle[0] = jx; \n" \ | ||
| " z->n = 1; \n" \ | ||
| "}" | ||
|
|
||
| //A function that replaces GrB_Matrix_nvals for counting non-zero elements | ||
| GrB_Info all_paths_get_nvals(GrB_Index *nvals, const GrB_Matrix A){ | ||
| GrB_Index nnz = 0; | ||
DanyaLitva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| GrB_Index accum = 0; | ||
| GrB_Matrix_nvals(&nnz, A); | ||
| *nvals = 0; | ||
| void *val_void = NULL; | ||
|
|
||
| if(nnz==0){ | ||
| *nvals = 0; | ||
| return GrB_SUCCESS; | ||
| } | ||
|
|
||
| val_void = malloc(nnz * sizeof(AllPathsVex)); | ||
| GrB_Matrix_extractTuples(NULL, NULL, val_void, &nnz, A); | ||
| AllPathsVex *val = (AllPathsVex *) val_void; | ||
|
|
||
| size_t end = nnz; | ||
| for (size_t start = 0; start < end; ++start){ | ||
| accum+=val[start].n; | ||
| } | ||
| *nvals = accum; | ||
|
|
||
| free(val_void); | ||
|
|
||
| return GrB_SUCCESS; | ||
| } | ||
|
|
||
| GrB_Info LAGraph_CFL_AllPaths( | ||
| // Output | ||
| GrB_Matrix *outputs, // Array of matrices containing results. | ||
| // The size of the array must be equal to nonterms_count. | ||
| // | ||
| // outputs[k]: (i, j) contains a AllPathsVex structure if and only if there is a path | ||
| // from node i to node j whose edge labels form a word | ||
| // derivable from the non-terminal 'k' of the specified CFG. | ||
| // Input | ||
| const GrB_Matrix *adj_matrices, // Array of adjacency matrices representing the graph. | ||
| // The length of this array is equal to the count of | ||
| // terminals (terms_count). | ||
| // | ||
| // adj_matrices[t]: (i, j) == 1 if and only if there | ||
| // is an edge between nodes i and j with the label of | ||
| // the terminal corresponding to index 't' (where t is | ||
| // in the range [0, terms_count - 1]). | ||
| int64_t terms_count, // The total number of terminal symbols in the CFG. | ||
| int64_t nonterms_count, // The total number of non-terminal symbols in the CFG. | ||
| const LAGraph_rule_WCNF *rules, // The rules of the CFG. | ||
| int64_t rules_count, // The total number of rules in the CFG. | ||
| char *msg // Message string for error reporting. | ||
| ) | ||
| { | ||
| // Semiring components | ||
| GrB_Type AllPaths_type = NULL; | ||
| GrB_BinaryOp AllPaths_add = NULL; | ||
| GrB_BinaryOp AllPaths_add_free = NULL; | ||
| GrB_Monoid AllPaths_monoid_free = NULL; | ||
| GxB_IndexBinaryOp IAllPaths_mult = NULL; | ||
| GrB_BinaryOp AllPaths_mult = NULL; | ||
| GrB_Semiring AllPaths_semiring_free = NULL; | ||
| GrB_BinaryOp AllPaths_set = NULL; | ||
| GrB_Scalar Theta = NULL; | ||
| GrB_Scalar bottom_scalar = NULL; | ||
|
|
||
| GRB_TRY(GrB_Type_new(&AllPaths_type, sizeof(AllPathsVex))); | ||
|
|
||
| GRB_TRY(GrB_Scalar_new(&Theta, GrB_BOOL)); | ||
| GRB_TRY(GrB_Scalar_setElement_BOOL(Theta, false)); | ||
|
|
||
| AllPathsVex bottom = {0, NULL}; | ||
| GRB_TRY(GrB_Scalar_new(&bottom_scalar, AllPaths_type)); | ||
| GRB_TRY(GrB_Scalar_setElement_UDT(bottom_scalar, (void *)(&bottom))); | ||
|
|
||
| GRB_TRY(GrB_BinaryOp_new( | ||
| &AllPaths_add, | ||
| (void *)add_all_paths, | ||
| AllPaths_type, | ||
| AllPaths_type, | ||
| AllPaths_type)); | ||
|
|
||
| GRB_TRY(GrB_BinaryOp_new( | ||
| &AllPaths_add_free, | ||
| (void *)add_all_paths_free, | ||
| AllPaths_type, | ||
| AllPaths_type, | ||
| AllPaths_type)); | ||
|
|
||
| GRB_TRY(GrB_Monoid_new( | ||
| &AllPaths_monoid_free, | ||
| AllPaths_add_free, | ||
| (void *)(&bottom))); | ||
|
|
||
| GRB_TRY(GxB_IndexBinaryOp_new( | ||
| &IAllPaths_mult, | ||
| (void *)mult_all_paths_index, | ||
| AllPaths_type, | ||
| AllPaths_type, | ||
| AllPaths_type, | ||
| GrB_BOOL, | ||
| "mult_all_paths_index", | ||
| MULT_PATH_INDEX_DEFN)); | ||
|
|
||
| GRB_TRY(GxB_BinaryOp_new_IndexOp( | ||
| &AllPaths_mult, | ||
| IAllPaths_mult, | ||
| Theta)); | ||
|
|
||
| GRB_TRY(GrB_Semiring_new( | ||
| &AllPaths_semiring_free, | ||
| AllPaths_monoid_free, | ||
| AllPaths_mult)); | ||
|
|
||
| GRB_TRY(GrB_BinaryOp_new( | ||
| &AllPaths_set, | ||
| (void *)set_all_paths, | ||
| AllPaths_type, | ||
| AllPaths_type, | ||
| GrB_BOOL)); | ||
|
|
||
| CFL_Semiring semiring = {.type = AllPaths_type, | ||
| .semiring = AllPaths_semiring_free, | ||
| .add = AllPaths_add_free, | ||
| .add_eps = AllPaths_add, | ||
| .mult = AllPaths_mult, | ||
| .init_path = AllPaths_set, | ||
| .bottom_scalar = bottom_scalar, | ||
| .get_nvals = all_paths_get_nvals}; | ||
|
|
||
| LG_TRY(LAGraph_CFPQ_core(outputs, adj_matrices, terms_count, nonterms_count, rules, rules_count, &semiring, msg)); | ||
| LG_FREE_WORK; | ||
| return GrB_SUCCESS; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.