-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
319 lines (264 loc) · 8.3 KB
/
main.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
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "timer.h"
#include "utils.h"
#include "ac.h"
using namespace std;
const int num_cols = 30;
const int num_rows = 5001;
void generate_DFA(int* dfa, string output[], vector<string> input);
void generate_fail_states(int* dfa, string output[], int fail_state[]);
int get_state_as_int(char ch);
char get_state_as_char(int st);
float
ms_difference(struct timeval start, struct timeval end) {
float ms = (end.tv_sec - start.tv_sec) * 1000;
ms += (end.tv_usec - start.tv_usec) / 1000;
return ms;
}
void
print_usage() {
printf("usage: ./acp [-l filename] [-p] [-t]\n"
" -l load the filename to create dfa\n"
" -p run performance tests\n"
" -t run test cases\n"
);
}
std::vector<std::string>
load_input_file(const char* filename, int* n, int num_words_to_load) {
fstream in;
in.open(filename, fstream::in);
vector<string> word_list;
string word;
int idx = 0;
while(std::getline(in, word)) {
word_list.push_back(word);
idx += 1;
if(idx > num_words_to_load)
break;
}
*n = idx;
in.close();
return word_list;
}
char*
load_tweets(const char* filename, int num, int tweet_length) {
FILE * tweetfile;
tweetfile = fopen(filename, "r");
char* tweets;
tweets = (char *) malloc(num*tweet_length*sizeof(char));
int idx = 0;
char ch;
while(idx < num) {
if(feof(tweetfile))
break;
int i = 0;
do{
ch = fgetc(tweetfile);
tweets[idx*tweet_length + i] = ch;
i+=1;
}while(i<tweet_length && ch!=10 && ch!='\n');
while(i<tweet_length) {
tweets[idx*tweet_length + i] = ' ';
i+= 1;
}
idx += 1;
}
fclose(tweetfile);
return tweets;
}
void
print_dfa(int* dfa, int rows, int cols) {
const char* format_int = "%4d|";
const char* format_char = "%4c|";
const char* format_str = "%4s|";
printf(format_str, " ");
printf(format_char, '0');
for(int i=1; i<cols; i++) {
printf(format_char, get_state_as_char(i));
}
cout<<endl;
for(int i=0; i<cols+1; i++) {
printf("%4s", "-----");
}
cout<<endl;
for(int i=0; i<rows; i++) {
printf(format_int, i);
printf(format_int, dfa[i*num_cols + 0]);
for(int j=1; j<cols; j++) {
if(dfa[i*num_cols + j] != 0)
printf("%4d|", dfa[i*num_cols + j]);
else
printf(format_str, ".");
}
cout<<endl;
}
}
void
print_word_list(vector<string> word_list, int num, int total_words) {
if(num > total_words){
num = total_words;
}
cout<<"total number of words: "<<total_words<<endl;
cout<<"printing words: "<<num<<endl;
int start = 0;
for(vector<string>::iterator it=word_list.begin(); start < num; ++it, start++) {
cout<<*it<<endl;
}
}
void
print_state_outputs(int* dfa, string output[], int num_rows) {
for(int i=0; i<num_rows; i++) {
if(dfa[i*num_cols + 0]!=0) {
cout<<"state:"<<i<<" "<<output[i]<<endl;
}
}
}
void
print_fail_states(int fail_state[], int num_states) {
for(int i=0; i<num_states; i++) {
cout<<i<<" "<<fail_state[i]<<endl;
}
}
void
performance_test(int* dfa, int* fail_state, char* tweets, bool* valid_state, int num_tweets, int tweet_length, int num_of_words) {
GpuTimer timer;
fflush(stdout);
timer.Start();
cout<<"SERIAL"<<","<<num_tweets<<","<<tweet_length<<","<<num_of_words<<",";
profanity_filter_serial(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length);
timer.Stop();
printf("%f\n", timer.Elapsed());
memset(valid_state, false, num_tweets*sizeof(bool));
fflush(stdout);
timer.Start();
cout<<"CUDA"<<","<<num_tweets<<","<<tweet_length<<","<<num_of_words<<",";
profanity_filter_parallel(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length, 1024, 256);
timer.Stop();
printf("%f\n", timer.Elapsed());
fflush(stdout);
timer.Start();
cout<<"OpenACC"<<","<<num_tweets<<","<<tweet_length<<","<<num_of_words<<",";
profanity_filter_acc_parallel(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length);
timer.Stop();
printf("%f\n", timer.Elapsed());
}
void
cuda_optimal_space_search(int* dfa, int* fail_state, char* tweets, bool* valid_state, int num_tweets, int tweet_length, int num_of_words, int num_threads, int num_blocks) {
GpuTimer timer;
fflush(stdout);
timer.Start();
profanity_filter_parallel(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length, num_threads, num_blocks);
timer.Stop();
printf("%f", timer.Elapsed());
}
int main(int argc, char **argv) {
bool lflag=false, tflag=false, sflag=false;
int num_of_words;
int c;
int* fail_state;
char* input_filename;
int tweet_length;
char* tweets;
int* dfa;
bool* valid_state;
int num_tweets;
int num_words_to_load;
while((c = getopt(argc, argv, "l:ts?")) != -1) {
switch(c) {
case 'l':
lflag = true;
input_filename = optarg;
break;
case 't':
tflag = true;
break;
case 's':
sflag = true;
break;
case '?':
print_usage();
exit(0);
break;
default:
exit(0);
break;
}
}
if(!lflag) {
cout<<"Please provide list of words to load"<<endl;
exit(0);
}
if(sflag) {
cout<<"# of records"<<","<<"# of characters in each record"<<","<<"# of patterns"<<","<<"Block size"<<","<<"# of threads"<<","<<"Runtime (in ms)"<<endl;
for(int num_blocks=64; num_blocks<=256; num_blocks+=32) {
for(int num_threads=128; num_threads<=1024; num_threads+= 64) {
num_tweets = 1000000;
tweet_length = 800;
num_words_to_load = 800;
vector<string> word_list;
string output[num_rows];
fail_state = (int *) malloc(num_rows*sizeof(int));
dfa = (int *) malloc(num_rows*num_cols*sizeof(int));
memset(dfa, 0, sizeof(int) * num_rows * num_cols);
memset(fail_state, 0, sizeof(fail_state));
word_list = load_input_file(input_filename, &num_of_words, num_words_to_load);
generate_DFA(dfa, output, word_list);
generate_fail_states(dfa, output, fail_state);
valid_state = (bool *) malloc(num_tweets * sizeof(bool));
tweets = load_tweets("data/tweets_big_800", num_tweets, tweet_length);
memset(valid_state, false, sizeof(valid_state));
cout<<num_tweets<<","<<tweet_length<<","<<num_words_to_load<<","<<num_blocks<<","<<num_threads<<",";
cuda_optimal_space_search(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length, num_words_to_load, num_threads, num_blocks);
cout<<endl;
free(dfa);
free(fail_state);
free(tweets);
free(valid_state);
}
}
exit(1);
}
cout<<"Type"<<","<<"# of records"<<","<<"# of characters in each record"<<","<<"# of patterns"<<","<<"Runtime (in ms)"<<endl;
for( num_tweets=100000; num_tweets<=1000000; num_tweets+=100000) {
for(tweet_length=100; tweet_length<=800; tweet_length+=100) {
for(num_words_to_load=200; num_words_to_load<=800; num_words_to_load+=200) {
vector<string> word_list;
string output[num_rows];
fail_state = (int *) malloc(num_rows*sizeof(int));
dfa = (int *) malloc(num_rows*num_cols*sizeof(int));
memset(dfa, 0, sizeof(int) * num_rows * num_cols);
memset(fail_state, 0, sizeof(fail_state));
word_list = load_input_file(input_filename, &num_of_words, num_words_to_load);
//print_word_list(word_list, 2, num_of_words);
generate_DFA(dfa, output, word_list);
//print_dfa(dfa, 100, num_cols);
generate_fail_states(dfa, output, fail_state);
//print_state_outputs(dfa, output, num_rows);
//print_fail_states(fail_state, num_cols);
valid_state = (bool *) malloc(num_tweets * sizeof(bool));
if(tweet_length == 100)
tweets = load_tweets("data/tweets_big_100", num_tweets, tweet_length);
if(tweet_length == 200)
tweets = load_tweets("data/tweets_big_200", num_tweets, tweet_length);
if(tweet_length == 300)
tweets = load_tweets("data/tweets_big_300", num_tweets, tweet_length);
if(tweet_length == 400)
tweets = load_tweets("data/tweets_big_400", num_tweets, tweet_length);
if(tweet_length == 500)
tweets = load_tweets("data/tweets_big_500", num_tweets, tweet_length);
if(tweet_length == 600)
tweets = load_tweets("data/tweets_big_600", num_tweets, tweet_length);
if(tweet_length == 700)
tweets = load_tweets("data/tweets_big_700", num_tweets, tweet_length);
if(tweet_length == 800)
tweets = load_tweets("data/tweets_big_800", num_tweets, tweet_length);
memset(valid_state, false, sizeof(valid_state));
performance_test(dfa, fail_state, tweets, valid_state, num_tweets, tweet_length, num_words_to_load);
free(dfa);
free(fail_state);
free(tweets);
free(valid_state);
}
}
}
return 1;
}