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 );
0 commit comments