-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cu
147 lines (132 loc) · 4.28 KB
/
main.cu
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
#include <iostream>
#include <fstream>
#include <vector>
#include <numeric>
#include "cuda_error_check.h"
#include "implementation.h"
#include "utils.h"
#include "sequential.cpp"
using namespace std;
int main(int argc, char** argv){
try {
//declare and initialize variabls
string usage =
"\tCommand line arguments:\n\
Input file: E.g., --input in.txt\n\
Output path: E.g., --output out.txt\n\
Block size: E.g., --bsize 1024\n\
Block count: E.g., --bcount 2\n\
Method: E.g., --method 1 or 2\n";
string inputFileName;
string outputFileName;
ifstream inputFile;
ofstream outputFile;
int bsize = 0, bcount = 0;
int method = 0;
int deviceID = 0;
cudaDeviceProp deviceProp;
char* deviceName = NULL;
//check that CUDA is supported and get the name of the device
CUDAErrorCheck(cudaSetDevice(deviceID));
CUDAErrorCheck(cudaGetDeviceProperties(&deviceProp, deviceID));
deviceName = deviceProp.name;
//parse program arguments
for( int i = 1; i < argc; i++ ){
if ( !strcmp(argv[i], "--input") && i != argc-1 ) {
inputFileName = string(argv[i+1]);
inputFile.open(inputFileName.c_str());
} else if( !strcmp(argv[i], "--output") && i != argc-1 ) {
outputFileName = string(argv[i+1]);
outputFile.open(outputFileName.c_str());
} else if( !strcmp(argv[i], "--bsize") && i != argc-1 ) {
bsize = atoi( argv[i+1] );
} else if( !strcmp(argv[i], "--bcount") && i != argc-1 ) {
bcount = atoi( argv[i+1] );
} else if( !strcmp(argv[i], "--method") && i != argc-1 ) {
method = atoi( argv[i+1] );
}
}
//verify program arguments
if(!inputFile){
throw runtime_error("Failed to open specified file: " + inputFileName);
}
if(!outputFile){
throw runtime_error("Failed to open specified file: " + outputFileName);
}
if(!inputFile.is_open() || !outputFile.is_open()){
cerr << "Usage: " << usage;
throw runtime_error("Initialization error happened: input/output file");
}
if(bsize <= 0 || bcount <= 0){
cerr << "Usage: " << usage;
throw runtime_error("Initialization error happened: block size/count");
}
if(method == 0){
cerr << "Usage: " << usage;
throw runtime_error("Initialization error happened: method");
}
//parse input file
vector<string> strings = parseFile(inputFile);
inputFile.close();
char* text; //all the strings concatenated into a single string
int* indices; //the starting index of each string
int* suffixes; //the starting index of each suffix
int totalLength; //length of text (includes term sequence)
int numStrings; //number of strings
int numSuffixes; //number of suffixes
parseStrings(strings,text,indices,suffixes,totalLength,numStrings,numSuffixes);
//print program properties
cout << "Device: " << deviceName;
cout << ", bsize: " << bsize << ", bcount: " << bcount;
cout << ", method: " << method << endl;
cout << "Input file: " << inputFileName;
cout << ", Number of strings: " << numStrings;
cout << ", Number of suffixes: " << numSuffixes;
cout << ", total length: " << totalLength << endl;
CUDAErrorCheck(cudaDeviceSetLimit(cudaLimitMallocHeapSize, 100000000));
CUDAErrorCheck(cudaDeviceSetLimit(cudaLimitStackSize, 5000));
size_t limit = 0;
cudaDeviceGetLimit(&limit, cudaLimitMallocHeapSize);
printf("cudaLimitMallocHeapSize: %u\n", (unsigned)limit);
cudaDeviceGetLimit(&limit, cudaLimitStackSize);
printf("cudaLimitStackSize: %u\n", (unsigned)limit);
print_seq_runtime(text);
char* output = NULL;
//process method
switch(method){
case 1:
output = impl1(text,
indices,
totalLength,
numStrings,
bsize, bcount);
break;
case 2:
output = impl2(text,
indices,
suffixes,
totalLength,
numStrings,
numSuffixes,
bsize, bcount);
break;
default:
cout << "Method " << method << " does not exist. Try method 1 or 2.\n";
break;
}
saveResults(outputFile, output);
outputFile.close();
//clean program memory
free(text);
free(indices);
free(suffixes);
CUDAErrorCheck(cudaDeviceReset());
} catch(const exception& e){
cerr << e.what() << endl;
return EXIT_FAILURE;
} catch(...) {
cerr << "An exception has occurred." << endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}