-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
282 lines (263 loc) · 9.07 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
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>
#include <atomic>
#include <chrono>
#include "colours.h"
#include <thread>
#include <mutex>
#include <unistd.h>
#include <cstring>
#define THREAD_COUNT std::thread::hardware_concurrency()
enum class Mode {
Serial, Parallel
};
static std::string print_mode (Mode m){
switch(m){
case (Mode::Serial):
{
return "Serial";
}
case (Mode::Parallel):
{
return "Parallel";
}
}
return "";
}
template <typename TimePoint>
std::chrono::milliseconds to_ms(TimePoint tp) {
return std::chrono::duration_cast<std::chrono::milliseconds>(tp);
}
struct node {
unsigned name;
int colour;
unsigned random_id;
std::vector<unsigned> neighbours;
node() = default;
node(unsigned name, unsigned random_id) : name (name), colour(-1), random_id(random_id)
{};
void print_n() const {
std::cout << "name: " << name << "\nrand: "<< random_id << "\ncolour: " << colour << "\n\n";
}
std::string to_string () const {
std::string s = std::to_string(this->name);
s += " [style=\"filled\", color=\"";
s += colors::get_colour(this->colour) + "\"]\n";
s += std::to_string(this->name) + " -- {";
for (const auto& n : this->neighbours){
s += std::to_string(n) + " ";
}
s += "}\n\n";
return s;
}
std::string to_string (unsigned i) const {
std::string s = std::to_string(this->name);
s += " [style=\"filled\", color=\"";
s += colors::get_colour(this->colour) + "\"]\n";
s += std::to_string(this->name) + " -- {";
for (const auto& n : this->neighbours){
if (n > i){
s += std::to_string(n) + " ";
}
}
s += "}\n\n";
return s;
}
};
struct graph {
unsigned number_of_nodes, number_of_connections, number_of_colours, chunk_size;
std::vector<node> nodes;
graph() : number_of_colours(0) {
load_nodes(std::cin);
this->to_be_colored_lock = new std::mutex();
this->chunk_size = (this->number_of_nodes+THREAD_COUNT-1) / THREAD_COUNT;
}
graph(const std::string& filename) : number_of_colours(0){
if (filename.empty()) {
load_nodes(std::cin);
} else {
std::fstream in (filename);
load_nodes(in);
}
std::cout << "loading done\n";
this->to_be_colored_lock = new std::mutex();
this->chunk_size = (this->number_of_nodes+THREAD_COUNT-1) / THREAD_COUNT;
}
~graph(){
delete this->to_be_colored_lock;
}
void print_graphviz() const {
std::ofstream out("../graphviz/coloured-" + std::to_string(this->number_of_nodes) + "n.dot");
this->print_graphviz(out);
}
void print_graphviz(std::ostream& out) const {
out << "graph {\n";
for (size_t i = 0; i < nodes.size(); i++){
out << nodes[i].to_string(i);
}
out << "}";
}
void compute(unsigned number_of_iterations, Mode m){
unsigned total_time = 0;
for (size_t i = 0; i< number_of_iterations; i++){
this->clear_graph();
auto start = std::chrono::high_resolution_clock::now();
switch (m) {
case(Mode::Serial): {
this->colour_graph();
break;
}
case(Mode::Parallel): {
this->colour_graph_parallel();
break;
}
default:
{
std::cerr << "Mode not supported\n";
}
}
auto end = std::chrono::high_resolution_clock::now();
total_time += to_ms(end - start).count();
std::cout << "iteration number: " << i << " Needed " << to_ms(end - start).count() << " ms to finish " << print_mode(m) << ".\n";
}
std::cout << "Average time needed for "<< print_mode(m) <<" compute: " << total_time/number_of_iterations <<"ms. Number of colours: " << this->number_of_colours << std::endl;
std::cout << std::endl;
}
void clear_graph(){
for (auto & blob : this->nodes){
blob.colour = -1;
}
this->number_of_colours = 0;
}
void colour_graph(){
unsigned number_of_coloured_nodes = 0;
while (number_of_coloured_nodes < this->number_of_nodes){
std::vector<unsigned> to_be_colored;
for (const auto& n : this->nodes) {
if (n.colour != -1) continue;
bool local_max = true;
for (const auto& neighbour : n.neighbours){
if (this->nodes[neighbour].colour == -1 && n.random_id < this->nodes[neighbour].random_id){
local_max = false;
break;
}
}
if (local_max) {
to_be_colored.push_back(n.name);
number_of_coloured_nodes++;
}
}
for (const auto n : to_be_colored) {
this->nodes[n].colour = this->number_of_colours;
}
this->number_of_colours++;
}
}
void colour_graph_parallel(){
std::atomic<unsigned> number_of_coloured_nodes(0);
while (number_of_coloured_nodes < this->number_of_nodes){
std::vector<unsigned> to_be_colored;
std::vector<std::thread> threads;
for (unsigned i = 0; i<THREAD_COUNT; i++){
threads.emplace_back(&graph::colour_graph_compute_parallel, this, std::ref(number_of_coloured_nodes), std::ref(to_be_colored), i);
}
for (auto & thread : threads){
thread.join();
}
for (const auto n : to_be_colored) {
this->nodes[n].colour = this->number_of_colours;
}
this->number_of_colours++;
}
}
private:
std::mutex * to_be_colored_lock;
void colour_graph_compute_parallel(std::atomic<unsigned> & number_of_coloured_nodes, std::vector<unsigned> & to_be_colored, unsigned id){
for (unsigned i = id*(this->chunk_size); i < std::min(id*this->chunk_size+this->chunk_size, this->number_of_nodes); i++) {
node & n = this->nodes[i];
if (n.colour != -1) continue;
bool local_max = true;
for (const auto& neighbour : n.neighbours){
if (this->nodes[neighbour].colour == -1 && n.random_id < this->nodes[neighbour].random_id){
local_max = false;
break;
}
}
if (local_max) {
this->to_be_colored_lock->lock();
to_be_colored.push_back(i);
this->to_be_colored_lock->unlock();
number_of_coloured_nodes++;
}
}
}
void load_nodes(std::istream& in){
in >> this->number_of_nodes >> this->number_of_connections;
for (unsigned i = 0; i < this->number_of_nodes; i++){
this->nodes.emplace_back(i, rand());
}
for (unsigned i = 0; i < this->number_of_connections; i++){
unsigned from, to;
in >> from >> to;
this->nodes[from].neighbours.push_back(to);
this->nodes[to].neighbours.push_back(from);
}
}
};
struct arguments{
std::string file_name;
Mode mode;
unsigned number_of_iterations;
arguments() : mode(Mode::Serial), number_of_iterations(5)
{};
};
arguments handle_arguments(int argc, char *argv[]){
if (argc < 2){
return {};
}
if ( strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0 ){
std::cout << "This is basic program which implements Luby_Jones algorithm. For more advanced approach look into openMPI folder"
"\nProgram supports following flags\n"
"\t-f [file_name] to specify path to input file. Default is accepting input from std::in.\n"
"\t-p to enable parallel computation. Default is computation in serial\n"
"\t-i [number_of_iterations] to specify how many times the computation should be executed. Default number is 5\n";
exit(0);
}
arguments a;
int opt;
while((opt = getopt(argc, argv, ":f:pi:")) != -1)
{
switch (opt) {
case('f'):
{
a.file_name = optarg;
break;
}
case('p'):
{
a.mode = Mode::Parallel;
break;
}
case('i'):
{
a.number_of_iterations = atoi(optarg);
break;
}
default:
{
std::cerr << "flag not supported\n";
exit(-1);
}
}
}
return a;
}
int main(int argc, char *argv[]) {
arguments args = handle_arguments(argc, argv);
graph g = graph(args.file_name);
g.compute(args.number_of_iterations, args.mode);
g.print_graphviz();
return 0;
}