Skip to content

Commit ddd29f5

Browse files
committed
Refactor: move CFL methods to seperate header file
1 parent f1cdd8b commit ddd29f5

File tree

5 files changed

+112
-61
lines changed

5 files changed

+112
-61
lines changed

experimental/algorithm/LAGraph_CFL_optimized_matrix_opt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// https://github.com/FormalLanguageConstrainedPathQuerying/CFPQ_PyAlgo/tree/murav/optimize-matrix
1919

2020
#include "LG_internal.h"
21+
#include "LAGraph_CFL_optimized_matrix_opt.h"
2122
#include <LAGraphX.h>
2223

2324
#define BENCH_CFL_REACHBILITY false
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
//------------------------------------------------------------------------------
2+
// LAGraph_CFL_optimized_matrix_opt.h: Header with operations for
3+
// Optimized Context-Free Language Reachability Matrix-Based Algorithm
4+
//------------------------------------------------------------------------------
5+
//
6+
// LAGraph, (c) 2019-2026 by The LAGraph Contributors, All Rights Reserved.
7+
// SPDX-License-Identifier: BSD-2-Clause
8+
9+
// Contributed by Vlasenco Daniel, Ilhom Kombaev, Semyon Grigoriev, St. Petersburg State
10+
// University.
11+
12+
//------------------------------------------------------------------------------
13+
14+
// Code is an implementation of optimized matrix operations for CFL_reachability
15+
// algorithms, described in the following paper:
16+
// * Ilia Muravev, "Optimization of the Context-Free Language Reachability Matrix-Based
17+
// Algorithm" and based on the python implementation from:
18+
// https://github.com/FormalLanguageConstrainedPathQuerying/CFPQ_PyAlgo/tree/murav/optimize-matrix
19+
20+
#include <GraphBLAS.h>
21+
22+
// Several square matrices can be concatenated into a horizontal or vertical vector of
23+
// matrices for fast multiplication
24+
//
25+
// [CELL] - the matrix is square: n x n
26+
// [VEC_HORIZ] - the matrix is a horizontal block vector: n x (n * k),
27+
// where k is the number of concatenated matrices
28+
// [VEC_VERT] - the matrix is a vertical block vector: (n * k) x n,
29+
// where k is the number of concatenated matrices
30+
enum CFL_Matrix_block { CELL, VEC_HORIZ, VEC_VERT };
31+
32+
// Matrix wrapper for CFL reachability algorithms with optional optimizations
33+
typedef struct CFL_Matrix {
34+
GrB_Matrix base; // Underlying GrB_Matrix
35+
int8_t optimizations; // Optimizations flags
36+
// Fields of base matrix
37+
GrB_Index nvals;
38+
GrB_Index nrows;
39+
GrB_Index ncols;
40+
// Fields of format optimization
41+
GrB_Matrix base_row;
42+
GrB_Matrix base_col;
43+
int32_t format;
44+
bool is_both;
45+
// Fields of lazy addition optimization
46+
struct CFL_Matrix **base_matrices;
47+
size_t base_matrices_count;
48+
bool is_lazy;
49+
// Fields of block optimization
50+
enum CFL_Matrix_block block_type;
51+
} CFL_Matrix;
52+
53+
// Creates a CFL matrix from intialized GrB_Matrix
54+
//
55+
// Creates a CFL_Matrix wrapping an already-initialized GrB_Matrix
56+
// On success, *matrix is set to a newly allocated CFL_Matrix
57+
GrB_Info CFL_matrix_from_base(CFL_Matrix **matrix, GrB_Matrix base);
58+
59+
// Creates a lazy CFL matrix from intialized GrB_Matrix
60+
//
61+
// Creates a CFL_Matrix wrapping an already-initialized GrB_Matrix
62+
// On success, *matrix is set to a newly allocated CFL_Matrix
63+
GrB_Info CFL_matrix_from_base_lazy(CFL_Matrix **matrix, GrB_Matrix base);
64+
65+
// Creates an empty CFL_Matrix of the given dimensions
66+
//
67+
// The underlying GrB_Matrix is allocated internally
68+
// On success, *matrix is set to a newly allocated CFL_Matrix
69+
GrB_Info CFL_matrix_create(CFL_Matrix **matrix, GrB_Index nrows, GrB_Index ncols);
70+
71+
// Creates an empty lazy CFL_Matrix of the given dimensions
72+
//
73+
// The underlying GrB_Matrix is allocated internally
74+
// On success, *matrix is set to a newly allocated CFL_Matrix with new CFL_Matrix
75+
GrB_Info CFL_matrix_create_lazy(CFL_Matrix **matrix, GrB_Index nrows, GrB_Index ncols);
76+
77+
// Free a CFL_Matrix and sets *matrix to NULL
78+
GrB_Info CFL_matrix_free(CFL_Matrix **matrix);
79+
80+
// Recomputes internal fields (nvals, nrows, ncols, format, block_type, etc...)
81+
// from the current state of the underlying base matrix
82+
GrB_Info CFL_matrix_update(CFL_Matrix *matrix);
83+
84+
GrB_Info CFL_mxm(CFL_Matrix *output, CFL_Matrix *first, CFL_Matrix *second, bool accum,
85+
bool swap, int8_t optimizations);
86+
GrB_Info CFL_wise(CFL_Matrix *output, CFL_Matrix *first, CFL_Matrix *second, bool accum,
87+
int8_t optimizations);
88+
89+
// Computes C = B \ A, i.e. C(i, j) = true iff B(i, j) = true and A(i, j) = false.
90+
GrB_Info CFL_rsub(CFL_Matrix *output, CFL_Matrix *mask, int8_t optimizations);
91+
GrB_Info CFL_dup(CFL_Matrix *output, CFL_Matrix *input, int8_t optimizations);
92+
93+
// Converts a matrix (lazy or regular) into its evaluated base form by merging
94+
// all underlying base matrices. If the input matrix is not lazy, returns its copy
95+
//
96+
// Parameters:
97+
// matrix_p - [out] Pointer that will be holds created matrix
98+
// input - [in] Pointer to the source matrix (may be lazy).
99+
// optimizations - [in] Bitmask specifying enabled optimizations.
100+
GrB_Info CFL_matrix_to_base(
101+
// output
102+
CFL_Matrix **matrix_p,
103+
// input
104+
CFL_Matrix *matrix, int8_t optimizations);
105+
106+
GrB_Info CFL_clear(CFL_Matrix *A, int8_t optimizations);

experimental/algorithm/LAGraph_CFL_reachability_advanced.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
LG_FREE_WORK; \
4747
}
4848

49+
#include "LAGraph_CFL_optimized_matrix_opt.h"
4950
#include "LG_internal.h"
5051
#include <LAGraphX.h>
5152

@@ -533,8 +534,9 @@ static GrB_Info get_new_adj_matrices(const GrB_Matrix *adj_matrices, CFL_Symbol
533534
// new_rules - [out] Allocated output rule array. Caller must free
534535
// new_rules_count - [out] Number of rules written to new_rules
535536
static GrB_Info get_new_rules(const LAGraph_rule_EWCNF *rules, size_t rules_count,
536-
CFL_Symbol *map, size_t map_size, LAGraph_rule_EWCNF **new_rules,
537-
size_t *new_rules_count, char *msg, int8_t optimizations) {
537+
CFL_Symbol *map, size_t map_size,
538+
LAGraph_rule_EWCNF **new_rules, size_t *new_rules_count,
539+
char *msg, int8_t optimizations) {
538540
*new_rules_count = 0;
539541
#undef FREE_INNER
540542

experimental/test/test_CFL_optimized_matrix_opt.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
//------------------------------------------------------------------------------
1414

15+
#include "../experimental/algorithm/LAGraph_CFL_optimized_matrix_opt.h"
1516
#include <LAGraph.h>
1617
#include <LAGraphX.h>
1718
#include <LAGraph_test.h>

include/LAGraphX.h

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,65 +1185,6 @@ GrB_Info LAGraph_CFL_reachability
11851185
char *msg // Message string for error reporting.
11861186
) ;
11871187

1188-
enum CFL_Matrix_block { CELL, VEC_HORIZ, VEC_VERT };
1189-
1190-
// Struct for matrix for CFL algorithms with optimizations
1191-
typedef struct CFL_Matrix {
1192-
GrB_Matrix base; // Base GrB_Matrix from we create CFL_Matrix
1193-
int8_t optimizations; // Optimizations flags
1194-
// Fields of base matrix
1195-
GrB_Index nvals;
1196-
GrB_Index nrows;
1197-
GrB_Index ncols;
1198-
// Fields of format optimization
1199-
GrB_Matrix base_row;
1200-
GrB_Matrix base_col;
1201-
int32_t format;
1202-
bool is_both;
1203-
// Fields of lazy addition optimization
1204-
struct CFL_Matrix **base_matrices;
1205-
size_t base_matrices_count;
1206-
bool is_lazy;
1207-
// Fields of block optimization
1208-
enum CFL_Matrix_block block_type;
1209-
} CFL_Matrix;
1210-
1211-
GrB_Info CFL_matrix_from_base(CFL_Matrix **matrix, GrB_Matrix base);
1212-
GrB_Info CFL_matrix_from_base_lazy(CFL_Matrix **matrix, GrB_Matrix base);
1213-
GrB_Info CFL_matrix_create(CFL_Matrix **matrix, GrB_Index nrows, GrB_Index ncols);
1214-
GrB_Info CFL_matrix_create_lazy(CFL_Matrix **matrix, GrB_Index nrows, GrB_Index ncols);
1215-
GrB_Info CFL_matrix_free(CFL_Matrix **matrix);
1216-
1217-
GrB_Info CFL_matrix_update(CFL_Matrix *matrix);
1218-
1219-
1220-
GrB_Info CFL_mxm(CFL_Matrix *output, CFL_Matrix *first, CFL_Matrix *second, bool accum,
1221-
bool swap, int8_t optimizations);
1222-
GrB_Info CFL_wise(CFL_Matrix *output, CFL_Matrix *first, CFL_Matrix *second, bool accum,
1223-
int8_t optimizations);
1224-
GrB_Info CFL_rsub(CFL_Matrix *output, CFL_Matrix *mask, int8_t optimizations);
1225-
GrB_Info CFL_dup(CFL_Matrix *output, CFL_Matrix *input, int8_t optimizations);
1226-
1227-
// Converts a matrix (lazy or regular) into its evaluated base form by merging
1228-
// all underlying base matrices. If the input matrix is not lazy, returns its copy.
1229-
//
1230-
// Parameters:
1231-
// matrix_p - [out] Pointer that will be holds created matrix
1232-
// input - [in] Pointer to the source matrix (may be lazy).
1233-
// optimizations - [in] Bitmask specifying enabled optimizations.
1234-
//
1235-
// Returns:
1236-
// GrB_Info.
1237-
GrB_Info CFL_matrix_to_base
1238-
(
1239-
// output
1240-
CFL_Matrix **matrix_p,
1241-
// input
1242-
CFL_Matrix *matrix,
1243-
int8_t optimizations
1244-
) ;
1245-
1246-
GrB_Info CFL_clear(CFL_Matrix *A, int8_t optimizations);
12471188

12481189
// LAGraph_CFL_reachability_adv: Context-Free Language Reachability Matrix-Based
12491190
// Algorithm

0 commit comments

Comments
 (0)