-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry_point.cu
executable file
·316 lines (278 loc) · 10.1 KB
/
entry_point.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
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
#include <cstring>
#include <stdexcept>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <vector>
#include "utils.h"
#include "cuda_error_check.cuh"
#include "initial_graph.hpp"
#include "parse_graph.hpp"
#include "opt.cu"
#include "impl2.cu"
#include "impl1.cu"
#define SSSP_INF 1073741824
enum class ProcessingType {Push, Neighbor, Own, Unknown};
enum SyncMode {InCore, OutOfCore};
enum SyncMode syncMethod;
enum SmemMode {UseSmem, UseNoSmem};
enum SmemMode smemMethod;
// Open files safely.
template <typename T_file>
void openFileToAccess( T_file& input_file, std::string file_name ) {
input_file.open( file_name.c_str() );
if( !input_file )
throw std::runtime_error( "Failed to open specified file: " + file_name + "\n" );
}
void myTestCorrectness(std::vector<initial_vertex> * parsedGraph, const char* outputFileName) {
std::cout << std::endl << "TESTING CORRECTNESS" << std::endl;
std::cout << "RUNNING SEQUENTIAL BMF..." << std::endl;
int vertex_size = (*parsedGraph).size();
int *d= new int[vertex_size];
for (int i = 0; i < vertex_size; ++i)
d[i] = SSSP_INF;
d[0]=0;
int change = 0;
for (int k = 1; k < vertex_size; k++){
for (int i = 0; i < vertex_size; i++){
std::vector<neighbor> nbrs = (*parsedGraph)[i].nbrs;
for (int j = 0; j < nbrs.size(); ++j){
int u = nbrs[j].srcIndex;
int v = i;
int w = nbrs[j].edgeValue.weight;
if ((d[u] + w) < d[v]){
d[v] = d[u]+w;
change = 1;
}
}
}
if (change == 0)
break;
change = 0;
}
//Compare the distance array and the parallel output file
std::ifstream outputFile;
openFileToAccess< std::ifstream >( outputFile, std::string( outputFileName ) );
std::string line;
int i = 0;
int incorrect = 0;
while (getline(outputFile,line)) {
std::string curr = (d[i] < SSSP_INF) ? (std::to_string(i) + ":" + std::to_string(d[i])):(std::to_string(i) +":" + "INF");
// std::cout << std::to_string(line.compare(curr)) << std::endl;
if(line.compare(curr) != 0) {
incorrect++;
std::cout << "Correct: " << curr << "\tYours: " << line << std::endl;
}
i++;
}
if(i != vertex_size) {
std::cout << "Insufficient vertices found in outputfile" << std::endl;
std::cout << "Expected: " << vertex_size << "Found: " << i << std::endl;
return;
}
std::cout << "Correct: " << std::to_string(vertex_size-incorrect) << "\t Incorrect: " << std::to_string(incorrect) << " \t Total: " << std::to_string(vertex_size) << std::endl;
outputFile.close();
}
void testCorrectness(edge_node *edges, const char* outputFileName, uint nVertices, uint nEdges) {
std::cout << std::endl << "TESTING CORRECTNESS" << std::endl;
std::cout << "RUNNING SEQUENTIAL BMF..." << std::endl;
unsigned int *d= new unsigned int[nVertices];
d[0]=0;
for (int i = 1; i < nVertices; ++i){
d[i] = UINT_MAX;
}
int change = 0;
for(int i = 1; i < nVertices; i++){
for(int j = 0; j < nEdges; j++){
int u = edges[j].srcIndex;
int v = edges[j].destIndex;
int w = edges[j].weight;
if(d[u] == UINT_MAX){
continue;
} else if(d[u]+w < d[v]){
d[v] = d[u]+w;
change = 1;
}
}
if(!change){
break;
}
change = 0;
}
//Compare the distance array and the parallel output file
std::ifstream outputFile;
openFileToAccess< std::ifstream >( outputFile, std::string( outputFileName ) );
std::string line;
int i = 0;
int incorrect = 0;
while (getline(outputFile,line)) {
std::string curr = (d[i] < UINT_MAX) ? (std::to_string(i) + ":" + std::to_string(d[i])):(std::to_string(i) +":" + "INF");
if(line.compare(curr) != 0) {
incorrect++;
// std::cout << "Correct: " << curr << "\tYours: " << line << std::endl;
}
i++;
}
if(i != nVertices) {
std::cout << "Insufficient vertices found in outputfile" << std::endl;
std::cout << "Expected: " << nVertices << "Found: " << i << std::endl;
return;
}
std::cout << "Correct: " << std::to_string(nVertices-incorrect) << "\t Incorrect: " << std::to_string(incorrect) << " \t Total: " << std::to_string(nVertices) << std::endl;
outputFile.close();
}
// Execution entry point.
int main( int argc, char** argv )
{
std::string usage =
"\tRequired command line arguments:\n\
Input file: E.g., --input in.txt\n\
Block size: E.g., --bsize 512\n\
Block count: E.g., --bcount 192\n\
Output path: E.g., --output output.txt\n\
Processing method: E.g., --method bmf (bellman-ford), or tpe (to-process-edge), or opt (one further optimizations)\n\
Shared memory usage: E.g., --usesmem yes, or no \n\
Sync method: E.g., --sync incore, or outcore\n\
Sort method: E.g., --sortby src, or dest\n";
try {
std::ifstream inputFile;
std::ofstream outputFile;
std::string outputFileName;
int selectedDevice = 0;
int bsize = 0, bcount = 0;
int vwsize = 32;
int threads = 1;
long long arbparam = 0;
bool nonDirectedGraph = false; // By default, the graph is directed.
ProcessingType processingMethod = ProcessingType::Unknown;
syncMethod = OutOfCore;
smemMethod = UseNoSmem;
bool sortBySource = false;
/********************************
* GETTING INPUT PARAMETERS.
********************************/
for( int iii = 1; iii < argc; ++iii )
if ( !strcmp(argv[iii], "--method") && iii != argc-1 ) {
if ( !strcmp(argv[iii+1], "bmf") )
processingMethod = ProcessingType::Push;
else if ( !strcmp(argv[iii+1], "tpe") )
processingMethod = ProcessingType::Neighbor;
else if ( !strcmp(argv[iii+1], "opt") )
processingMethod = ProcessingType::Own;
else{
std::cerr << "\n Un-recognized method parameter value \n\n";
exit(EXIT_FAILURE);
}
} else if ( !strcmp(argv[iii], "--sync") && iii != argc-1 ) {
if ( !strcmp(argv[iii+1], "incore") ){
syncMethod = InCore;
} else if ( !strcmp(argv[iii+1], "outcore") ){
syncMethod = OutOfCore;
} else {
std::cerr << "\n Un-recognized sync parameter value \n\n";
exit(EXIT_FAILURE);
}
} else if ( !strcmp(argv[iii], "--usesmem") && iii != argc-1 ) {
if ( !strcmp(argv[iii+1], "yes") ){
smemMethod = UseSmem;
} else if ( !strcmp(argv[iii+1], "no") ){
smemMethod = UseNoSmem;
} else{
std::cerr << "\n Un-recognized usesmem parameter value \n\n";
exit(EXIT_FAILURE);
}
} else if( !strcmp( argv[iii], "--input" ) && iii != argc-1 /*is not the last one*/){
openFileToAccess< std::ifstream >( inputFile, std::string( argv[iii+1] ) );
} else if( !strcmp( argv[iii], "--output" ) && iii != argc-1 /*is not the last one*/){
openFileToAccess< std::ofstream >( outputFile, std::string( argv[iii+1] ) );
outputFileName = std::string(argv[iii+1]);
} else if( !strcmp( argv[iii], "--bsize" ) && iii != argc-1 /*is not the last one*/){
bsize = std::atoi( argv[iii+1] );
} else if( !strcmp( argv[iii], "--bcount" ) && iii != argc-1 /*is not the last one*/){
bcount = std::atoi( argv[iii+1] );
} else if( !strcmp( argv[iii], "--sortby" ) && iii != argc-1 /*is not the last one*/){
if( !strcmp(argv[iii+1], "src" )){
sortBySource = true;
} else if( !strcmp(argv[iii+1], "dest")){
sortBySource = false;
} else {
std::cerr << "\n Unrecognized sortby parameter value\n\n";
exit(EXIT_FAILURE);
}
}
if(bsize <= 0 || bcount <= 0){
std::cerr << "Usage: " << usage;
exit(EXIT_FAILURE);
throw std::runtime_error("\nAn initialization error happened.\nExiting.");
}
if( !inputFile.is_open() || processingMethod == ProcessingType::Unknown ) {
std::cerr << "Usage: " << usage;
throw std::runtime_error( "\nAn initialization error happened.\nExiting." );
}
if( !outputFile.is_open() ){
openFileToAccess< std::ofstream >( outputFile, "out.txt" );
outputFileName = "out.txt";
}
CUDAErrorCheck( cudaSetDevice( selectedDevice ) );
std::cout << "Device with ID " << selectedDevice << " is selected to process the graph.\n";
/********************************
* Read the input graph file.
********************************/
std::cout << "Collecting the input graph ...\n";
std::vector<initial_vertex> parsedGraph( 0 );
uint nEdges = parse_graph::parse(
inputFile, // Input file.
parsedGraph, // The parsed graph.
arbparam,
nonDirectedGraph ); // Arbitrary user-provided parameter.
std::cout << "Input graph collected with " << parsedGraph.size() << " vertices and " << nEdges << " edges.\n";
/********************************
* Process the graph.
********************************/
switch(processingMethod){
case ProcessingType::Push:
if(smemMethod == UseSmem){
cout << "USE SMEM" << endl;
puller_usesmem(&parsedGraph, bsize, bcount, outputFile);
} else if(syncMethod == OutOfCore){
puller(&parsedGraph, bsize, bcount, outputFile, sortBySource);
} else if(syncMethod == InCore){
puller_incore(&parsedGraph, bsize, bcount, outputFile, sortBySource);
} else {
cout << "syncMethod not specified" << endl;
exit(EXIT_FAILURE);
}
break;
case ProcessingType::Neighbor:
if(syncMethod == OutOfCore){
impl2_outcore(&parsedGraph, bsize, bcount, outputFile, sortBySource);
} else if(syncMethod == InCore){
impl2_incore(&parsedGraph, bsize, bcount, outputFile, sortBySource);
} else {
cout << "syncMethod not specified" << endl;
exit(EXIT_FAILURE);
}
break;
default:
own(&parsedGraph, bsize, bcount);
}
/********************************
* It's done here.
********************************/
edge_node *testEdgeList = new edge_node[nEdges];
pull_edges(parsedGraph, testEdgeList, nEdges);
testCorrectness(testEdgeList, outputFileName.c_str(), parsedGraph.size(), nEdges);
// myTestCorrectness(&parsedGraph, outputFileName.c_str());
CUDAErrorCheck( cudaDeviceReset() );
std::cout << "Done.\n";
return( EXIT_SUCCESS );
}
catch( const std::exception& strException ) {
std::cerr << strException.what() << "\n";
return( EXIT_FAILURE );
}
catch(...) {
std::cerr << "An exception has occurred." << std::endl;
return( EXIT_FAILURE );
}
}