forked from bkase/CUDA-grep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nfa.cpp
306 lines (237 loc) · 9.11 KB
/
nfa.cpp
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
* Regular expression implementation.
* Supports only ( | ) * + ?. No escapes.
* Compiles to NFA and then simulates NFA
* using Thompson's algorithm.
*
* See also http://swtch.com/~rsc/regexp/ and
* Thompson, Ken. Regular Expression Search Algorithm,
* Communications of the ACM 11(6) (June 1968), pp. 419-422.
*
* Copyright (c) 2007 Russ Cox.
*
* Can be distributed under the MIT license, see bottom of file.
*/
#include <cuda_runtime_api.h>
#include <cuda.h>
#include "pnfa.h"
#include "cycleTimer.h"
char *post;
int checkCmdLine(int argc, char **argv, char **fileName, char **regexFile, int *time) {
int visualize, simplified, postfix;
SimpleReBuilder builder;
State *start;
parseCmdLine(argc, argv, &visualize, &postfix, time, &simplified, fileName, regexFile);
// argv index at which regex is present
int regexIndex = 1 + visualize + postfix + *time + simplified;
if (fileName != NULL)
regexIndex += 2;
if (argc <= regexIndex) {
usage (argv[0]);
exit(EXIT_SUCCESS);
}
char * regexBuffer = (char*)malloc(strlen(argv[regexIndex])+1);
strcpy(regexBuffer, argv[regexIndex]);
simplifyRe(®exBuffer, &builder);
free(regexBuffer);
post = re2post(builder.re);
if(post == NULL){
fprintf(stderr, "bad regexp %s\n", argv[regexIndex]);
return 1;
}
if (simplified == 1) {
char * clean_simplified = stringifyRegex(builder.re);
printf("\nSimplified Regex: %s\n", clean_simplified);
free(clean_simplified);
exit(0);
}
/* destruct the simpleRe */
_simpleReBuilder(&builder);
if (postfix == 1) {
char * clean_post = stringifyRegex(post);
printf("\nPostfix buffer: %s\n", clean_post);
free(clean_post);
exit(0);
}
if (visualize == 1) {
start = post2nfa(post);
visualize_nfa(start);
exit(0);
}
return regexIndex;
}
int
main(int argc, char **argv)
{
int i, timerOn, parallel = 1;
char *fileName = NULL, *regexFile = NULL, **lines = NULL, **regexs = NULL;
int num_lines, num_regexs;
SimpleReBuilder builder;
State *start;
double startTime, endTime, endReadFile, endCopyStringsToDevice, endPMatch;
int regexIndex = checkCmdLine(argc, argv, &fileName, ®exFile, &timerOn);
// sequential matching
if (parallel != 1) {
start = post2nfa(post);
if(start == NULL){
fprintf(stderr, "error in post2nfa %s\n", post);
return 1;
}
// if no file is specified
if (fileName == NULL) {
startTime = CycleTimer::currentSeconds();
for(i=regexIndex+1; i<argc; i++) {
if(anyMatch(start, argv[i]))
printf("%d: %s\n", i-(regexIndex), argv[i]);
}
endTime = CycleTimer::currentSeconds();
}
else {
startTime = CycleTimer::currentSeconds();
readFile(fileName, &lines, &num_lines);
unsigned char result[num_lines];
for (i = 0; i < num_lines; i++) {
if (anyMatch(start, lines[i]))
result[i] = 1;
else
result[i] = 0;
}
endTime = CycleTimer::currentSeconds();
for ( i = 0; i < num_lines; i++) {
if(result[i] == 1)
printf("%s", lines[i]);
}
}
}
// parallel matching
else {
if (fileName == NULL) {
printf("Enter a file \n");
exit(EXIT_SUCCESS);
}
// match just a single regex
if (regexFile == NULL) {
char * regexBuffer = (char*)malloc(strlen(argv[regexIndex])+1);
strcpy(regexBuffer, argv[regexIndex]);
simplifyRe(®exBuffer, &builder);
free(regexBuffer);
char *device_regex;
int postsize = (strlen(builder.re) + 1) * sizeof (char);
cudaMalloc((void **) &device_regex, postsize);
cudaMemcpy(device_regex, builder.re, postsize, cudaMemcpyHostToDevice);
startTime = CycleTimer::currentSeconds();
readFile(fileName, &lines, &num_lines);
endReadFile = CycleTimer::currentSeconds();
char * device_line;
u32 * device_line_table;
//copyStringsToDevice(lines, num_lines, &device_line, &device_line_table);
u32 * table = (u32 *) malloc(sizeof(u32) * strlen(*lines));
table[0] = 0;
int num_lines = 0;
int len = strlen(lines[0]);
for (int i = 0; i < len; i++) {
if ((lines[0])[i] == '\n') {
table[++num_lines] = i+1;
lines[0][i] = 0;
}
}
if((lines[0])[len-1] == '\n')/*if at the end file not '\n', then we not forgot last offset */
--num_lines;
cudaMalloc((void**)&device_line_table, sizeof (u32) * (len ));
cudaMalloc((void**)&device_line, sizeof (char) * (len + 1));
cudaMemcpy(device_line_table, table, sizeof(u32) * (len), cudaMemcpyHostToDevice);
cudaMemcpy(device_line, *lines, sizeof(char) * (len + 1), cudaMemcpyHostToDevice);
endCopyStringsToDevice = CycleTimer::currentSeconds();
u32 host_regex_table[1]; /*offsets to regexes on host*/
u32 *device_regex_table; /*this array will contain host_regex_table*/
host_regex_table[0]=0; /*in case of one regex offset must be 0*/
cudaMalloc((void**)&device_regex_table, sizeof (u32) );
cudaMemcpy(device_regex_table, host_regex_table, sizeof(u32), cudaMemcpyHostToDevice);/*copy regex offset to device*/
pMatch(device_line, device_line_table, num_lines, 1, timerOn, device_regex, device_regex_table, lines, table);
endPMatch = CycleTimer::currentSeconds();
}
// match a bunch of regexs
else {
startTime = CycleTimer::currentSeconds();
readFile(regexFile, ®exs, &num_regexs);
readFile(fileName, &lines, &num_lines);
endReadFile = CycleTimer::currentSeconds();
char * device_line;
u32 * device_line_table;
//copyStringsToDevice(lines, num_lines, &device_line, &device_line_table);
u32 * host_line_table = (u32 *) malloc(sizeof(u32) * strlen(*lines));
host_line_table[0] = 0;
int num_lines = 0;
int len = strlen(lines[0]);
for (int i = 0; i < len; i++) {
if ((lines[0])[i] == '\n' || lines[0][i] == 0) {
host_line_table[++num_lines] = i+1;
lines[0][i] = 0;
}
}
cudaMalloc((void**)&device_line_table, sizeof (u32) * (len ));
cudaMalloc((void**)&device_line, sizeof (char) * (len + 1));
cudaMemcpy(device_line_table, host_line_table, sizeof(u32) * (len), cudaMemcpyHostToDevice);
cudaMemcpy(device_line, *lines, sizeof(char) * (len + 1), cudaMemcpyHostToDevice);
char * device_regex;
u32 * device_regex_table;
u32 * host_regex_table = (u32 *) malloc(sizeof(u32) * strlen(*regexs));
host_regex_table[0] = 0;
int num_regexs = 0;
len = strlen(regexs[0]);
for (int i = 0; i < len; i++) {
if (regexs[0][i] == '\n' || regexs[0][i] == 0) {
host_regex_table[++num_regexs] = i+1;
regexs[0][i] = 0;
char * regexBuffer = (char*)malloc(strlen(regexs[0] + host_regex_table[num_regexs-1])+1);
strcpy(regexBuffer, regexs[0] + host_regex_table[num_regexs-1]);
simplifyRe(®exBuffer, &builder);
free(regexBuffer);
strcpy(regexs[0] + host_regex_table[num_regexs-1],builder.re);
}
}
//for (int i = 0; i < num_regexs; i++) {}
cudaMalloc((void**)&device_regex_table, sizeof (u32) * (len ));
cudaMalloc((void**)&device_regex, sizeof (char) * (len + 1));
cudaMemcpy(device_regex_table, host_regex_table, sizeof(u32) * (len), cudaMemcpyHostToDevice);
cudaMemcpy(device_regex, *regexs, sizeof(char) * (len + 1), cudaMemcpyHostToDevice);
endCopyStringsToDevice = CycleTimer::currentSeconds();
pMatch(device_line, device_line_table, num_lines, num_regexs, timerOn, device_regex, device_regex_table, lines, host_line_table);
endPMatch = CycleTimer::currentSeconds();
}
}
// print timing details
if (timerOn && !parallel) {
printf("\nSequential Time taken %.4f \n\n", (endTime - startTime));
}
else if (timerOn && parallel) {
printf("\nParallel ReadFile Time taken %.4f \n", (endReadFile - startTime));
printf("\nParallel CopyStringsToDevice Time taken %.4f \n", (endCopyStringsToDevice - endReadFile));
printf("\nParallel pMatch Time taken %.4f \n\n", (endPMatch - endCopyStringsToDevice));
printf("\nParallel Total Time taken %.4f \n\n", (endPMatch - startTime));
}
return EXIT_SUCCESS;
}
/*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the
* Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall
* be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/