|
7 | 7 | #include "regular_path_query.hpp" |
8 | 8 |
|
9 | 9 | cuBool_Matrix regular_path_query_with_transposed( |
10 | | - // vector of sparse graph matrices for each label |
11 | | - const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, |
12 | | - // vector of sparse automaton matrices for each label |
13 | | - const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, |
14 | | - // transposed matrices for graph and automaton |
15 | | - const std::vector<cuBool_Matrix> &graph_transposed, |
16 | | - const std::vector<cuBool_Matrix> &automaton_transposed, |
17 | | - |
18 | | - const std::vector<bool> &inversed_labels_input, bool all_labels_are_inversed) { |
19 | | - cuBool_Status status; |
20 | | - |
21 | | - auto inversed_labels = inversed_labels_input; |
22 | | - inversed_labels.resize(std::max(graph.size(), automaton.size())); |
23 | | - |
24 | | - for (uint32_t i = 0; i < inversed_labels.size(); i++) { |
25 | | - bool is_inverse = inversed_labels[i]; |
26 | | - is_inverse ^= all_labels_are_inversed; |
27 | | - inversed_labels[i] = is_inverse; |
28 | | - } |
29 | | - |
30 | | - cuBool_Index graph_nodes_number = 0; |
31 | | - cuBool_Index automaton_nodes_number = 0; |
32 | | - |
33 | | - // get number of graph nodes |
34 | | - for (auto label_matrix : graph) { |
35 | | - if (label_matrix != nullptr) { |
36 | | - cuBool_Matrix_Nrows(label_matrix, &graph_nodes_number); |
37 | | - break; |
| 10 | + // vector of sparse graph matrices for each label |
| 11 | + const std::vector<cuBool_Matrix>& graph, const std::vector<cuBool_Index>& source_vertices, |
| 12 | + // vector of sparse automaton matrices for each label |
| 13 | + const std::vector<cuBool_Matrix>& automaton, const std::vector<cuBool_Index>& start_states, |
| 14 | + // transposed matrices for graph and automaton |
| 15 | + const std::vector<cuBool_Matrix>& graph_transposed, |
| 16 | + const std::vector<cuBool_Matrix>& automaton_transposed, |
| 17 | + |
| 18 | + const std::vector<bool>& inversed_labels_input, bool all_labels_are_inversed) { |
| 19 | + cuBool_Status status; |
| 20 | + |
| 21 | + auto inversed_labels = inversed_labels_input; |
| 22 | + inversed_labels.resize(std::max(graph.size(), automaton.size())); |
| 23 | + |
| 24 | + for (uint32_t i = 0; i < inversed_labels.size(); i++) { |
| 25 | + bool is_inverse = inversed_labels[i]; |
| 26 | + is_inverse ^= all_labels_are_inversed; |
| 27 | + inversed_labels[i] = is_inverse; |
38 | 28 | } |
39 | | - } |
40 | 29 |
|
41 | | - // get number of automaton nodes |
42 | | - for (auto label_matrix : automaton) { |
43 | | - if (label_matrix != nullptr) { |
44 | | - cuBool_Matrix_Nrows(label_matrix, &automaton_nodes_number); |
45 | | - break; |
46 | | - } |
47 | | - } |
48 | | - |
49 | | - // this will be answer |
50 | | - cuBool_Matrix reacheble {}; |
51 | | - status = cuBool_Matrix_New(&reacheble, automaton_nodes_number, graph_nodes_number); |
52 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
53 | | - |
54 | | - // allocate neccessary for algorithm matrices |
55 | | - cuBool_Matrix frontier {}, symbol_frontier {}, next_frontier {}; |
56 | | - status = cuBool_Matrix_New(&next_frontier, automaton_nodes_number, graph_nodes_number); |
57 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
58 | | - status = cuBool_Matrix_New(&frontier, automaton_nodes_number, graph_nodes_number); |
59 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
60 | | - status = cuBool_Matrix_New(&symbol_frontier, automaton_nodes_number, graph_nodes_number); |
61 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
62 | | - |
63 | | - // init start values of algorithm matricies |
64 | | - for (const auto state : start_states) { |
65 | | - for (const auto vert : source_vertices) { |
66 | | - assert(state < automaton_nodes_number); |
67 | | - assert(vert < graph_nodes_number); |
68 | | - cuBool_Matrix_SetElement(next_frontier, state, vert); |
69 | | - cuBool_Matrix_SetElement(reacheble, state, vert); |
70 | | - } |
71 | | - } |
| 30 | + cuBool_Index graph_nodes_number = 0; |
| 31 | + cuBool_Index automaton_nodes_number = 0; |
72 | 32 |
|
73 | | - cuBool_Index states = source_vertices.size(); |
| 33 | + // get number of graph nodes |
| 34 | + for (auto label_matrix : graph) { |
| 35 | + if (label_matrix != nullptr) { |
| 36 | + cuBool_Matrix_Nrows(label_matrix, &graph_nodes_number); |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
74 | 40 |
|
75 | | - // temporary matrix for write result of cubool functions |
76 | | - cuBool_Matrix result; |
77 | | - status = cuBool_Matrix_New(&result, automaton_nodes_number, graph_nodes_number); |
78 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
| 41 | + // get number of automaton nodes |
| 42 | + for (auto label_matrix : automaton) { |
| 43 | + if (label_matrix != nullptr) { |
| 44 | + cuBool_Matrix_Nrows(label_matrix, &automaton_nodes_number); |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
79 | 48 |
|
80 | | - const auto label_number = std::min(graph.size(), automaton.size()); |
81 | | - while (states > 0) { |
82 | | - std::swap(frontier, next_frontier); |
| 49 | + // this will be answer |
| 50 | + cuBool_Matrix reacheble{}; |
| 51 | + status = cuBool_Matrix_New(&reacheble, automaton_nodes_number, graph_nodes_number); |
| 52 | + assert(status == CUBOOL_STATUS_SUCCESS); |
83 | 53 |
|
84 | | - // clear next_frontier |
85 | | - status = cuBool_Matrix_Build(next_frontier, nullptr, nullptr, 0, CUBOOL_HINT_NO); |
| 54 | + // allocate neccessary for algorithm matrices |
| 55 | + cuBool_Matrix frontier{}, symbol_frontier{}, next_frontier{}; |
| 56 | + status = cuBool_Matrix_New(&next_frontier, automaton_nodes_number, graph_nodes_number); |
| 57 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 58 | + status = cuBool_Matrix_New(&frontier, automaton_nodes_number, graph_nodes_number); |
| 59 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 60 | + status = cuBool_Matrix_New(&symbol_frontier, automaton_nodes_number, graph_nodes_number); |
86 | 61 | assert(status == CUBOOL_STATUS_SUCCESS); |
87 | 62 |
|
88 | | - for (int i = 0; i < label_number; i++) { |
89 | | - if (graph[i] == nullptr || automaton[i] == nullptr) { |
90 | | - continue; |
91 | | - } |
92 | | - |
93 | | - cuBool_Matrix automaton_matrix = all_labels_are_inversed ? automaton[i] : automaton_transposed[i]; |
94 | | - status = cuBool_MxM(symbol_frontier, automaton_matrix, frontier, CUBOOL_HINT_NO); |
95 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
96 | | - |
97 | | - // next_frontier += (symbol_frontier * graph[i]) & (!reachible) |
98 | | - // multiply 2 matrices |
99 | | - cuBool_Matrix graph_matrix = inversed_labels[i] ? graph_transposed[i] : graph[i]; |
100 | | - status = cuBool_MxM(next_frontier, symbol_frontier, graph_matrix, CUBOOL_HINT_ACCUMULATE); |
101 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
102 | | - // apply invert mask |
103 | | - status = cuBool_Matrix_EWiseMulInverted(result, next_frontier, reacheble, CUBOOL_HINT_NO); |
104 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
105 | | - std::swap(result, next_frontier); |
| 63 | + // init start values of algorithm matricies |
| 64 | + for (const auto state : start_states) { |
| 65 | + for (const auto vert : source_vertices) { |
| 66 | + assert(state < automaton_nodes_number); |
| 67 | + assert(vert < graph_nodes_number); |
| 68 | + cuBool_Matrix_SetElement(next_frontier, state, vert); |
| 69 | + cuBool_Matrix_SetElement(reacheble, state, vert); |
| 70 | + } |
106 | 71 | } |
107 | 72 |
|
108 | | - // this must be accumulate with mask and save old value: reacheble += next_frontier & reacheble |
109 | | - status = cuBool_Matrix_EWiseAdd(result, reacheble, next_frontier, CUBOOL_HINT_NO); |
| 73 | + cuBool_Index states = source_vertices.size(); |
| 74 | + |
| 75 | + // temporary matrix for write result of cubool functions |
| 76 | + cuBool_Matrix result; |
| 77 | + status = cuBool_Matrix_New(&result, automaton_nodes_number, graph_nodes_number); |
110 | 78 | assert(status == CUBOOL_STATUS_SUCCESS); |
111 | | - std::swap(result, reacheble); |
112 | 79 |
|
113 | | - cuBool_Matrix_Nvals(next_frontier, &states); |
114 | | - } |
| 80 | + const auto label_number = std::min(graph.size(), automaton.size()); |
| 81 | + while (states > 0) { |
| 82 | + std::swap(frontier, next_frontier); |
| 83 | + |
| 84 | + // clear next_frontier |
| 85 | + status = cuBool_Matrix_Build(next_frontier, nullptr, nullptr, 0, CUBOOL_HINT_NO); |
| 86 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 87 | + |
| 88 | + for (int i = 0; i < label_number; i++) { |
| 89 | + if (graph[i] == nullptr || automaton[i] == nullptr) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + |
| 93 | + cuBool_Matrix automaton_matrix = all_labels_are_inversed ? automaton[i] : automaton_transposed[i]; |
| 94 | + status = cuBool_MxM(symbol_frontier, automaton_matrix, frontier, CUBOOL_HINT_NO); |
| 95 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 96 | + |
| 97 | + // next_frontier += (symbol_frontier * graph[i]) & (!reachible) |
| 98 | + // multiply 2 matrices |
| 99 | + cuBool_Matrix graph_matrix = inversed_labels[i] ? graph_transposed[i] : graph[i]; |
| 100 | + status = cuBool_MxM(next_frontier, symbol_frontier, graph_matrix, CUBOOL_HINT_ACCUMULATE); |
| 101 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 102 | + // apply invert mask |
| 103 | + status = cuBool_Matrix_EWiseMulInverted(result, next_frontier, reacheble, CUBOOL_HINT_NO); |
| 104 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 105 | + std::swap(result, next_frontier); |
| 106 | + } |
| 107 | + |
| 108 | + // this must be accumulate with mask and save old value: reacheble += next_frontier & reacheble |
| 109 | + status = cuBool_Matrix_EWiseAdd(result, reacheble, next_frontier, CUBOOL_HINT_NO); |
| 110 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 111 | + std::swap(result, reacheble); |
| 112 | + |
| 113 | + cuBool_Matrix_Nvals(next_frontier, &states); |
| 114 | + } |
115 | 115 |
|
116 | | - // free matrix necessary for algorithm |
117 | | - cuBool_Matrix_Free(next_frontier); |
118 | | - cuBool_Matrix_Free(frontier); |
119 | | - cuBool_Matrix_Free(symbol_frontier); |
120 | | - cuBool_Matrix_Free(result); |
| 116 | + // free matrix necessary for algorithm |
| 117 | + cuBool_Matrix_Free(next_frontier); |
| 118 | + cuBool_Matrix_Free(frontier); |
| 119 | + cuBool_Matrix_Free(symbol_frontier); |
| 120 | + cuBool_Matrix_Free(result); |
121 | 121 |
|
122 | | - return reacheble; |
| 122 | + return reacheble; |
123 | 123 | } |
124 | 124 |
|
125 | 125 |
|
126 | 126 | cuBool_Matrix regular_path_query( |
127 | | - // vector of sparse graph matrices for each label |
128 | | - const std::vector<cuBool_Matrix> &graph, const std::vector<cuBool_Index> &source_vertices, |
129 | | - // vector of sparse automaton matrices for each label |
130 | | - const std::vector<cuBool_Matrix> &automaton, const std::vector<cuBool_Index> &start_states, |
131 | | - // work with inverted labels |
132 | | - const std::vector<bool> &inversed_labels_input, bool all_labels_are_inversed) { |
133 | | - cuBool_Status status; |
134 | | - |
135 | | - // transpose graph matrices |
136 | | - std::vector<cuBool_Matrix> graph_transposed; |
137 | | - graph_transposed.reserve(graph.size()); |
138 | | - for (uint32_t i = 0; i < graph.size(); i++) { |
139 | | - graph_transposed.emplace_back(); |
140 | | - |
141 | | - auto label_matrix = graph[i]; |
142 | | - if (label_matrix == nullptr) { |
143 | | - continue; |
| 127 | + // vector of sparse graph matrices for each label |
| 128 | + const std::vector<cuBool_Matrix>& graph, const std::vector<cuBool_Index>& source_vertices, |
| 129 | + // vector of sparse automaton matrices for each label |
| 130 | + const std::vector<cuBool_Matrix>& automaton, const std::vector<cuBool_Index>& start_states, |
| 131 | + // work with inverted labels |
| 132 | + const std::vector<bool>& inversed_labels_input, bool all_labels_are_inversed) { |
| 133 | + cuBool_Status status; |
| 134 | + |
| 135 | + // transpose graph matrices |
| 136 | + std::vector<cuBool_Matrix> graph_transposed; |
| 137 | + graph_transposed.reserve(graph.size()); |
| 138 | + for (uint32_t i = 0; i < graph.size(); i++) { |
| 139 | + graph_transposed.emplace_back(); |
| 140 | + |
| 141 | + auto label_matrix = graph[i]; |
| 142 | + if (label_matrix == nullptr) { |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + cuBool_Index nrows, ncols; |
| 147 | + cuBool_Matrix_Nrows(label_matrix, &nrows); |
| 148 | + cuBool_Matrix_Ncols(label_matrix, &ncols); |
| 149 | + |
| 150 | + status = cuBool_Matrix_New(&graph_transposed.back(), ncols, nrows); |
| 151 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 152 | + status = cuBool_Matrix_Transpose(graph_transposed.back(), label_matrix, CUBOOL_HINT_NO); |
| 153 | + assert(status == CUBOOL_STATUS_SUCCESS); |
144 | 154 | } |
145 | 155 |
|
146 | | - cuBool_Index nrows, ncols; |
147 | | - cuBool_Matrix_Nrows(label_matrix, &nrows); |
148 | | - cuBool_Matrix_Ncols(label_matrix, &ncols); |
149 | | - |
150 | | - status = cuBool_Matrix_New(&graph_transposed.back(), ncols, nrows); |
151 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
152 | | - status = cuBool_Matrix_Transpose(graph_transposed.back(), label_matrix, CUBOOL_HINT_NO); |
153 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
154 | | - } |
155 | | - |
156 | | - // transpose automaton matrices |
157 | | - std::vector<cuBool_Matrix> automaton_transposed; |
158 | | - automaton_transposed.reserve(automaton.size()); |
159 | | - for (auto label_matrix : automaton) { |
160 | | - automaton_transposed.emplace_back(); |
161 | | - if (label_matrix == nullptr) { |
162 | | - continue; |
| 156 | + // transpose automaton matrices |
| 157 | + std::vector<cuBool_Matrix> automaton_transposed; |
| 158 | + automaton_transposed.reserve(automaton.size()); |
| 159 | + for (auto label_matrix : automaton) { |
| 160 | + automaton_transposed.emplace_back(); |
| 161 | + if (label_matrix == nullptr) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + cuBool_Index nrows, ncols; |
| 166 | + cuBool_Matrix_Nrows(label_matrix, &nrows); |
| 167 | + cuBool_Matrix_Ncols(label_matrix, &ncols); |
| 168 | + |
| 169 | + status = cuBool_Matrix_New(&automaton_transposed.back(), ncols, nrows); |
| 170 | + assert(status == CUBOOL_STATUS_SUCCESS); |
| 171 | + status = cuBool_Matrix_Transpose(automaton_transposed.back(), label_matrix, CUBOOL_HINT_NO); |
| 172 | + assert(status == CUBOOL_STATUS_SUCCESS); |
163 | 173 | } |
164 | 174 |
|
165 | | - cuBool_Index nrows, ncols; |
166 | | - cuBool_Matrix_Nrows(label_matrix, &nrows); |
167 | | - cuBool_Matrix_Ncols(label_matrix, &ncols); |
168 | | - |
169 | | - status = cuBool_Matrix_New(&automaton_transposed.back(), ncols, nrows); |
170 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
171 | | - status = cuBool_Matrix_Transpose(automaton_transposed.back(), label_matrix, CUBOOL_HINT_NO); |
172 | | - assert(status == CUBOOL_STATUS_SUCCESS); |
173 | | - } |
174 | | - |
175 | | - auto result = regular_path_query_with_transposed( |
176 | | - graph, source_vertices, |
177 | | - automaton, start_states, |
178 | | - graph_transposed, automaton_transposed, |
179 | | - inversed_labels_input, all_labels_are_inversed); |
| 175 | + auto result = regular_path_query_with_transposed( |
| 176 | + graph, source_vertices, |
| 177 | + automaton, start_states, |
| 178 | + graph_transposed, automaton_transposed, |
| 179 | + inversed_labels_input, all_labels_are_inversed); |
180 | 180 |
|
181 | | - for (cuBool_Matrix matrix : graph_transposed) { |
182 | | - if (matrix != nullptr) { |
183 | | - cuBool_Matrix_Free(matrix); |
| 181 | + for (cuBool_Matrix matrix : graph_transposed) { |
| 182 | + if (matrix != nullptr) { |
| 183 | + cuBool_Matrix_Free(matrix); |
| 184 | + } |
184 | 185 | } |
185 | | - } |
186 | | - for (cuBool_Matrix matrix : automaton_transposed) { |
187 | | - if (matrix != nullptr) { |
188 | | - cuBool_Matrix_Free(matrix); |
| 186 | + for (cuBool_Matrix matrix : automaton_transposed) { |
| 187 | + if (matrix != nullptr) { |
| 188 | + cuBool_Matrix_Free(matrix); |
| 189 | + } |
189 | 190 | } |
190 | | - } |
191 | 191 |
|
192 | | - return result; |
| 192 | + return result; |
193 | 193 | } |
0 commit comments