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