-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIS_parallel.cpp
More file actions
executable file
·331 lines (291 loc) · 10.3 KB
/
MIS_parallel.cpp
File metadata and controls
executable file
·331 lines (291 loc) · 10.3 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
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
320
321
322
323
324
325
326
327
328
329
330
331
using namespace std;
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <sstream>
#include "MIS_parallel.h"
#include "status.h"
#include "debug_helpers.h"
/*
reads input file and allocate node_array (list of neighbors) and
index_array (number of neighbors) to represent the graph
** pass reference to int* in order to get the address of the allocated arrays
returns the number of nodes in the graph
*/
int read_input_file(string filename, int **addr_node_array, int **addr_index_array)
{
ifstream myfile;
myfile.open(filename.c_str());
if(!myfile.is_open()){
cout << "Error opening file!" << endl;
return 1;
}
string line;
getline(myfile, line);
istringstream iss(line);
int numofnodes = 0;
int total_numofneighbors = 0;
iss >> numofnodes;
iss >> total_numofneighbors; // number is INCORRECT right now
#if DEBUG
cout << "read_input_file: " << numofnodes << " " << total_numofneighbors <<endl;
#endif
int* node_array = new int[total_numofneighbors * 2]; //neighbours of each vertex, CSR representation
//int* node_array = new int[total_numofneighbors]; //neighbours of each vertex, CSR representation
int* index_array = new int[numofnodes + 1]; //index values(address values) for each node, CSR representation
int node_index = 0;
int accum_numofneighbors = 0;
while(!myfile.eof())
{
getline(myfile,line);
istringstream iss(line);
if (node_index < numofnodes) {
index_array[node_index] = accum_numofneighbors;
}
int temp;
while (iss >> temp)
{
node_array[accum_numofneighbors] = temp - 1;
accum_numofneighbors++;
}
node_index++;
}
index_array[numofnodes] = accum_numofneighbors;
#if DEBUG
cout << "read_input_file:" << endl;
for(int i = 0; i < numofnodes; i++)
{
cout << "node "<< i << ": ";
for(int j = 0; j < index_array[i+1] - index_array[i]; j++)
cout << node_array[index_array[i] + j] << " ";
cout << endl;
}
#endif
*addr_node_array = node_array;
*addr_index_array = index_array;
return numofnodes;
}
/*
this function is to check if all the nodes with status SELECTED are actually independent
*/
bool check_independence(int *node_array, int *index_array, int *status_array, int numofnodes){
bool independent = true;
// true until found a node with its neighbor also SELECTED
for(int i = 0; i < numofnodes; i++){
if(status_array[i] == SELECTED){
int numofneighbors = index_array[i+1] - index_array[i];
for(int j = 0; j < numofneighbors; j++){
#if DEBUG
cout << "Node " << i << " neighbor node " << node_array[index_array[i] + j] << " status " << status_array[node_array[index_array[i] + j]] << endl;
#endif
if( status_array[node_array[index_array[i] + j]] == SELECTED )
independent = false;
}
}
}
return independent;
}
/*
this function checks if all the nodes that are NOT SELECTED
have at least one neighbor that is SELECTED
*/
bool check_maximal(int *node_array, int *index_array, int *status_array, int numofnodes){
bool maximal = true;
for(int i = 0; i < numofnodes; i++){
if(status_array[i] == INACTIVE){
int numofneighbors = index_array[i+1] - index_array[i];
bool neighbor_selected = false;
for(int j = 0; j < numofneighbors; j++){
if ( status_array[node_array[index_array[i] + j]] == SELECTED )
neighbor_selected = true;
}
if(!neighbor_selected)
maximal = false;
}
}
return maximal;
}
void write_output(string filename, int *status_array, int numofnodes){
ofstream ofs(filename.c_str(), ofstream::out);
int count = 0;
for(int i = 0; i < numofnodes; i++){
if(status_array[i] == SELECTED)
count++;
}
ofs << "(" << count << "): ";
for(int i = 0; i < numofnodes; i++){
if(status_array[i] == SELECTED)
ofs << i + 1 << " ";
}
ofs << endl;
}
int main(int argc, char *argv[]) {
string outFilename;
string inFilename;
string logFileName;
if (argc <= 3) {
cout << "not enough arguments" << endl;
cout <<" you need to provide the sparse graph file name and then desired output file name and a logfile name"<<endl;
exit (EXIT_FAILURE);
}else{
inFilename = argv[1];
outFilename = argv[2];
logFileName = argv[3];
}
int *nodes, *index_array;
int numofnodes = read_input_file(inFilename, &nodes, &index_array);
#if DEBUG
cout << "MAIN: " <<endl;
cout << "numofnodes: " << numofnodes << endl;
for(int i = 0; i < numofnodes; i++)
{
cout << "node "<< i << ": ";
for(int j = 0; j < index_array[i+1] - index_array[i]; j++)
cout << nodes[index_array[i] + j] << " ";
cout << endl;
}
#endif
// setup random array
float *nodes_randvalues = new float[numofnodes];
srand (static_cast <unsigned> (time(0)));
for(int i = 0; i < numofnodes; i++)
nodes_randvalues[i]= static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/20));
// setup status array
int *nodes_status = new int[numofnodes];
std::fill_n(nodes_status, numofnodes, ACTIVE);
#if DEBUG
cout << "MAIN: " <<endl;
for(int p = 0; p < numofnodes; p++)
{
printf("RandValues[%d] = %f\t",p,nodes_randvalues[p]);
printf("Status[%d] = %d ",p,nodes_status[p]);
printf("\n");
}
#endif
/*PARALLEL EXECUTION*/
// reset status
int *nodes_status_parallel = new int[numofnodes];
int *nodes_execute = new int[numofnodes];
std::fill_n(nodes_status_parallel, numofnodes, ACTIVE);
#if DEBUG
printf("Before Parallel Exeuction\nNumOfNOdes = %d\n",numofnodes);
for(int p=0; p<numofnodes; p++)
{
printf("RandValues[%d]=%f ",p,nodes_randvalues[p]);
printf("Status[%d]=%d\n",p,nodes_status_parallel[p]);
}
#endif
int gpu_remainingnodes = numofnodes;
int step = 0;
SNK_INIT_LPARM(lparm,numofnodes);
//Launch_params_t lparm={.ndim=1,.gdims={numofnodes},.ldims={256}};
while(gpu_remainingnodes > 0){
// randomize array
//for(int i = 0; i < numofnodes; i++)
// nodes_randvalues[i]= static_cast <float> (rand()) / (static_cast <float> (RAND_MAX/40));
mis_parallel(nodes,nodes_randvalues,nodes_status_parallel, index_array,nodes_execute,lparm);
deactivate_neighbors(nodes,nodes_randvalues,nodes_status_parallel,&gpu_remainingnodes, index_array,nodes_execute,lparm);
#if DEBUG
showNodesInfo(nodes_status_parallel, nodes_randvalues, numofnodes, "all");
#endif
//writing the random values in the log file
writeToFileNodeInfo(nodes_status_parallel, nodes_randvalues, numofnodes,logFileName, "all");
//showNodesInfo(nodes_status_parallel, nodes_randvalues, nodes_execute, numofnodes, "all");
#if DEBUG
for(int p=0;p<numofnodes;p++)
{
printf("RandValues[%d]=%f ",p,nodes_randvalues[p]);
printf("Status[%d]=%d\n",p,nodes_status_parallel[p]);
}
int count = 0;
for(int i = 0; i < numofnodes; i++)
if( nodes_status_parallel[i] == 1)
count++;
printf("remaining: %d\n", gpu_remainingnodes);
printf("count: %d\n", count);
cout << "gpu remaining nodes: " << gpu_remainingnodes << endl;
#endif
step++;
}
cout << "Total number of steps parallel: " << step << endl;
#if DEBUG
printf("~~Parallel Result~~\n");
for(int y = 0; y < numofnodes; y++){
if(nodes_status_parallel[y] == SELECTED)
printf("%d\n",y);
}
printf("After Parallel Exeuction\nNumOfNOdes = %d\n",numofnodes);
for(int p=0; p < numofnodes; p++)
{
printf("RandValues[%d]=%f ",p,nodes_randvalues[p]);
printf("Status[%d]=%d\n",p,nodes_status_parallel[p]);
}
printf("\n");
#endif
// /* VERIFY RESULT */
//
// bool match = true;
// int serial_count = 0;
// int parallel_count = 0;
// for(int i = 0; i < numofnodes; i++){
// if(nodes_status[i] == SELECTED)
// serial_count++;
// if(nodes_status_parallel[i] == SELECTED)
// parallel_count++;
// else if (nodes_status_parallel[i] == ACTIVE){
// cout << "SOMETHING IS WRONG!!!!" <<endl;
// //return 1;
// }
//
// if(nodes_status[i] != nodes_status_parallel[i]){
// match = false;
// //cout << "index " << i << " different." <<endl;
// }
// }
//
// if(match)
// cout << "Parallel MATCHESS Serial" << endl;
// else
// cout << "Parallel DOES NOT match Serial" << endl;
//
// cout << "Serial Count " << serial_count << endl;
// cout << "Parallel Count " << parallel_count << endl;
//
// if(check_independence(nodes, index_array, nodes_status, numofnodes))
// cout << "Serial Independent!" << endl;
// else
// cout << "Serial Not independent!" << endl;
//
// if(check_independence(nodes, index_array, nodes_status_parallel, numofnodes))
// cout << "Parallel Independent!" << endl;
// else
// cout << "Parallel Not independent!" << endl;
//
// if(check_maximal(nodes, index_array, nodes_status, numofnodes))
// cout << "Serial Maximal" << endl;
// else
// cout << "Serial NOT Maximal" << endl;
//
// if(check_maximal(nodes, index_array, nodes_status_parallel, numofnodes))
// cout << "Parallel Maximal" << endl;
// else
// cout << "Parallel NOT Maximal" << endl;
//
// write_output(outFilename + "_serial", nodes_status, numofnodes);
write_output(outFilename , nodes_status_parallel, numofnodes);
///things zad changed
//writeToFileNodesStatus(nodes_status_parallel, numofnodes,logFileName);
//showNodesStatus(nodes_status_parallel, numofnodes);
/// end of things zad changed
delete[] nodes;
delete[] nodes_randvalues;
delete[] nodes_status;
delete[] nodes_status_parallel;
delete[] index_array;
return 0;
}