-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
218 lines (197 loc) · 4.83 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
/*
* File: main.cpp
* Author: David Kuna
*
* Created on 23. listopad 2013, 16:47
*/
#include <iostream> // std::cout
#include <fstream> // std::ifstream
#include <vector> // std::vector
#include <algorithm> // std::find
#include <sys/time.h>
using namespace std;
/**
* Zjistí zda řádek obsahuje hledané číslo
* @param search
* @param array
* @return boolean
*/
bool isInArray(unsigned long search, unsigned long *array, bool debug = false){
if(debug == true){
cout << "search = " << search << " array[0] = " << array[0] << endl;
}
for(unsigned long i = 1; i <= array[0]; i++){
if(array[i] == search) return true;
}
return false;
}
/**
* Načte datový soubor a naplní strukturu
*
* @param filepath
* @param rows
* @return boolean
*/
bool loadGraph(char *filepath, unsigned long **&rows){
unsigned long tmpNode, node, key, value;
unsigned int pairs = 0;
ifstream source (filepath);
if(source.is_open()){
while (source >> node) {
pairs++;
if(pairs % 2 == 1){
tmpNode = node;
}else{
if(tmpNode < node){
key = tmpNode;
value = node;
}else{
key = node;
value = tmpNode;
}
// Zjistíme zda uz vektor teto prvek obsahuje a pokud ano tak ho přeskočíme
if(key != value && !isInArray(value, rows[key])){
rows[key][0]++;
rows[key][rows[key][0]] = value;
}
}
pairs = pairs % 2;
}
cout << "done" << endl;
return true;
}else{
cout << "fail" << endl;
cout << "Unable to open source file (" << filepath << ")" << endl;
}
return false;
}
/**
* Načte datový soubor a naplní strukturu
*
* @param filepath
* @param rows
* @return boolean
*/
unsigned long **prepareStructure(unsigned long *columns, unsigned long countOfNodes){
unsigned long **rows = new unsigned long*[countOfNodes+1];
for(long r = 0; r <= countOfNodes; r++){
rows[r] = new unsigned long[columns[r]+1];
fill_n(rows[r], columns[r]+1, 0);
}
cout << "done" << endl;
return rows;
}
/**
* Najde vrchol s nejvyšším indexem
* @param filepath
* @return int
*/
unsigned long findMax(char *filepath){
unsigned long max = 0;
long node;
ifstream source (filepath);
if(source.is_open()){
while (source >> node) {
if(max < node) max = node;
}
cout << "done" << endl;
return max;
}else{
cout << "fail" << endl;
cout << "Unable to open source file (" << filepath << ")" << endl;
}
cout << "fail" << endl;
return 0;
}
/**
* Určí počet sousedů klíčových uzlů
* @param filepath
* @param countOfNodes
* @return array
*/
bool countColumns(char *filepath, unsigned long *columns){
unsigned long tmpNode, node;
unsigned int pairs = 0;
ifstream source (filepath);
if(source.is_open()){
while (source >> node) {
pairs++;
if(pairs % 2 == 1){
tmpNode = node;
}else{
if(tmpNode < node){
columns[tmpNode]++;
}else{
columns[node]++;
}
}
pairs = pairs % 2;
}
cout << "done" << endl;
return true;
}else{
cout << "Unable to open source file (" << filepath << ")" << endl;
}
return 0;
}
/**
* Spočítá trojůhelníky hlavních vrchlů v zadaném rozsahu
* @param rows
* @param indexFrom
* @param indexTo
* @param triangles
*/
unsigned long countTriangles(unsigned long **&rows, unsigned long countOfNodes){
struct timeval start, finish;
unsigned long triangles = 0;
long mtime, seconds, useconds;
gettimeofday(&start, NULL);
#pragma omp parallel for schedule(dynamic) reduction(+:triangles) shared(rows)
for(unsigned long i = 0; i < countOfNodes; i++){
for(unsigned long j = 1; j <= rows[i][0]; j++){
for(unsigned long k = 1; k <= rows[rows[i][j]][0]; k++){
if(isInArray(rows[rows[i][j]][k], rows[i])){
triangles++;
}
}
}
}
gettimeofday(&finish, NULL);
seconds = finish.tv_sec - start.tv_sec;
useconds = finish.tv_usec - start.tv_usec;
mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
cout << "done" << endl;
cout << "--------RESULT---------" << endl;
cout << "Execution time: " << mtime*0.001 << "s" << endl;
return triangles;
}
/**
* Vstupní bod programu
*
* @param argc
* @param argv
* @return int
*/
int main(int argc, char** argv) {
unsigned long countOfNodes, triangles = 0;
char *filename;
filename = argv[1];
countOfNodes = (unsigned long)atoi(argv[2]);
if(countOfNodes == 0){
cout << "Nodes counting... ";
countOfNodes = findMax(filename);
}
cout << "Nodes: " << countOfNodes << endl;
unsigned long *columns = new unsigned long[countOfNodes+1];
fill_n(columns, countOfNodes+1, 0);
cout << "Columns counting... ";
countColumns(filename, columns);
cout << "Preparing structure... ";
unsigned long **rows = prepareStructure(columns, countOfNodes);
cout << "Graph loading... ";
loadGraph(filename, rows);
cout << "Start of counting... ";
triangles = countTriangles(rows, countOfNodes);
cout << "Number of triangles: " << triangles << endl;
return 0;
}