diff --git a/CMakeLists.txt b/CMakeLists.txt index f6bd1f5..df4e7a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,3 +23,5 @@ add_executable(main src/menu.cpp src/clusterCutSize.h src/clusterCutSize.cpp src/output.cpp) add_executable(testColoring src/testColoring.cpp src/parallel_coarsening.cpp src/Graph.cpp src/Graph.h) + +add_executable(readBin src/tools/read_bin.cpp) \ No newline at end of file diff --git a/src/Graph.cpp b/src/Graph.cpp index 1067282..de72225 100644 --- a/src/Graph.cpp +++ b/src/Graph.cpp @@ -3,7 +3,7 @@ using namespace std; -Node::Node(int id, int weight) { +Node::Node(unsigned int id,unsigned int weight) { this->id = id; this->weight = weight; child = nullptr; @@ -19,28 +19,28 @@ vector Node::get_neighbors() { return neighbors; } -Edge::Edge(int weight, Node *node1, Node *node2) { +Edge::Edge(unsigned int weight, Node *node1, Node *node2) { this->weight = weight; this->node1 = node1; this->node2 = node2; this->flag = false; } -Node *Graph::add_node(int id, int weight) { +Node *Graph::add_node( unsigned int id, unsigned int weight) { Node *n = new Node(id, weight); nodes.push_back(n); this->node_weight_global += weight; return n; } -Node *Graph::add_node_with_index(int id, int weight) { +Node *Graph::add_node_with_index( unsigned int id, unsigned int weight) { Node *n = new Node(id, weight); nodes[id] = n; this->node_weight_global += weight; return n; } -shared_ptr Graph::add_edge(int source, int dest, int distance) { +shared_ptr Graph::add_edge(unsigned int source, unsigned int dest, unsigned int distance) { Node *node1 = nodes[source]; Node *node2 = nodes[dest]; shared_ptr e = make_shared(distance, node1, node2); @@ -56,7 +56,7 @@ void Graph::print() { Node *n = nodes[i]; cout << "Node " << n->id << " with weight " << n->weight << endl; for (auto &edge : n->edges) { - int source, dest; + unsigned int source, dest; source = edge->node1->id; dest = edge->node2->id; if (source != n->id) @@ -97,10 +97,10 @@ int Graph::max_node_degree() { return _max_node_degree; } -int Graph::V(){ +unsigned int Graph::V() const{ return (int)nodes.size(); } -int Graph::E(){ +unsigned long Graph::E(){ return (int)edges.size(); } \ No newline at end of file diff --git a/src/Graph.h b/src/Graph.h index 4b47108..11e26fd 100644 --- a/src/Graph.h +++ b/src/Graph.h @@ -9,35 +9,35 @@ using namespace std; struct Node { - Node(int id, int weight); - int id; - int weight; + Node(unsigned int id, unsigned int weight); + unsigned int id; + unsigned int weight; Node *child; vector> edges; vector get_neighbors(); }; struct Edge { - Edge(int weight, Node *node1, Node *node2); - int weight; + Edge(unsigned int weight, Node *node1, Node *node2); + unsigned int weight; Node *node1; Node *node2; bool flag; }; struct Graph { - int V(); // nodes_num - int E(); // edges_num - int node_weight_global = 0; - int _max_node_degree = 0; - int num_colours = 0; + unsigned int V() const; // nodes_num + unsigned long E(); // edges_num + unsigned int node_weight_global = 0; + unsigned int _max_node_degree = 0; + unsigned int num_colours = 0; vector colours; vector partitions_size; vector nodes; vector> edges; - Node *add_node(int id, int weight); - Node *add_node_with_index(int id, int weight); - shared_ptr add_edge(int source, int dest, int distance); + Node *add_node(unsigned int id, unsigned int weight); + Node *add_node_with_index(unsigned int id, unsigned int weight); + shared_ptr add_edge(unsigned int source, unsigned int dest, unsigned int distance); void print(); void add_or_sum_edge(Node *n1, Node *n2, int distance); int max_node_degree(); diff --git a/src/loader.cpp b/src/loader.cpp index 12bf94e..ba546ab 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -9,13 +9,14 @@ using namespace std; struct m_edge{ - int node1; - int node2; - int weight; + unsigned int node1; + unsigned int node2; + unsigned int weight; }; -void thread_reader(Graph*g, const int*filedata, int num_nodes, int num_edges, int start, int step, barrier<>& bar, mutex& mtx_e); +void thread_reader(Graph*g, const unsigned int*filedata, unsigned int num_nodes, unsigned long num_edges, int start, int step, barrier<>& bar, mutex& mtx_e); + +Graph* loadFromFile(const string& path) { -Graph* loadFromFile(string path) { int num_threads = 4; auto start_time = chrono::high_resolution_clock::now(); int fd = open(path.c_str(), O_RDONLY); @@ -28,13 +29,13 @@ Graph* loadFromFile(string path) { throw std::runtime_error("Impossible to get file size"); } - int* intData = static_cast(mmap(nullptr, fileStat.st_size, PROT_READ, MAP_PRIVATE, fd, 0)); + auto intData = static_cast(mmap(nullptr, fileStat.st_size, PROT_READ, MAP_PRIVATE, fd, 0)); if (intData == MAP_FAILED) { throw std::runtime_error("Impossible to map file"); } - int num_nodes = intData[0]; - int num_edges = intData[1]; + auto num_nodes = intData[0]; + auto num_edges = static_cast(intData[2]) << 32 | intData[1]; auto* g = new Graph(); g->nodes.resize(num_nodes); @@ -42,6 +43,8 @@ Graph* loadFromFile(string path) { vector readers(num_threads); barrier bar(num_threads); mutex mtx_e; + + for(int i = 0; i& bar, mutex& mtx_e){ +void thread_reader(Graph*g, const unsigned int*filedata, unsigned int num_nodes, unsigned long num_edges, int start, int step, barrier<>& bar, mutex& mtx_e){ - int n_id; - int n_weight; - int cursor = start*2 + 2; - while(cursor< num_nodes*2 + 2){ + unsigned int n_id; + unsigned int n_weight; + unsigned int cursor = start*2 + 3; + while(cursor< num_nodes*2 + 3){ n_id = filedata[cursor]; n_weight = filedata[cursor+1]; g->add_node_with_index(n_id, n_weight); @@ -76,8 +79,8 @@ void thread_reader(Graph*g, const int*filedata, int num_nodes, int num_edges, in m_edge e_temp{}; vector edges; - cursor = start * 3 + 2 + num_nodes*2; - while(cursor< num_nodes*2 + 2 + num_edges*3){ + cursor = start * 3 + 3 + num_nodes*2; + while(cursor< num_nodes*2 + 3 + num_edges*3){ e_temp.node1 = filedata[cursor]; e_temp.node2 = filedata[cursor+1]; e_temp.weight = filedata[cursor+2]; @@ -88,7 +91,8 @@ void thread_reader(Graph*g, const int*filedata, int num_nodes, int num_edges, in bar.arrive_and_wait(); mtx_e.lock(); - for (auto &e : edges) + for (auto &e : edges) { g->add_edge(e.node1, e.node2, e.weight); + } mtx_e.unlock(); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 1c02819..8fa8cc3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,7 +3,6 @@ #include "partitioning.h" #include "timing/timing.h" -#define PARALLEL_BUILD using namespace std; int main(int argc, char **argv){ @@ -13,6 +12,7 @@ int main(int argc, char **argv){ } auto g = loadFromFile(argv[1]); + int requestedPartitions = 100; timing total_time; diff --git a/src/partitioning.h b/src/partitioning.h index c8ec5d5..3d52e11 100644 --- a/src/partitioning.h +++ b/src/partitioning.h @@ -3,7 +3,7 @@ #include "Graph.h" -Graph* loadFromFile(string path); +Graph* loadFromFile(const string& path); Graph* coarseGraph_s(Graph* graph); Graph* coarseGraph_p(Graph* graph, int num_threads); vector> sortEdge(const vector>& edges); diff --git a/src/tools/ascii_bin_converter.cpp b/src/tools/ascii_bin_converter.cpp index 0be507a..72ee292 100644 --- a/src/tools/ascii_bin_converter.cpp +++ b/src/tools/ascii_bin_converter.cpp @@ -31,15 +31,15 @@ int main(int argc, char *argv[]){ skip_lines(input, 4); string useless_chars; - int num_nodes; - int num_edges; + unsigned int num_nodes; + unsigned long num_edges; char end_line; input >> useless_chars >> useless_chars >> num_nodes >> num_edges >> end_line; num_edges /= 2; - output.write((char*)&num_nodes, sizeof(int)); - output.write((char*)&num_edges, sizeof(int)); + output.write((char*)&num_nodes, sizeof(unsigned int)); + output.write((char*)&num_edges, sizeof(unsigned long)); cout << num_nodes << " " << num_edges << endl; skip_lines(input, 2); @@ -48,9 +48,9 @@ int main(int argc, char *argv[]){ int distance; cout << "Writing nodes..." << endl; for(int i = 0; i> source >> dest >> distance >> end_line; source--; dest--; - output.write((char *) &source, sizeof(int)); - output.write((char *) &dest, sizeof(int)); - output.write((char *) &distance, sizeof(int)); + output.write((char *) &source, sizeof(unsigned int)); + output.write((char *) &dest, sizeof(unsigned int)); + output.write((char *) &distance, sizeof(unsigned int)); skip_lines(input, 1); } diff --git a/src/tools/read_bin.cpp b/src/tools/read_bin.cpp index af04a6f..a740e5d 100644 --- a/src/tools/read_bin.cpp +++ b/src/tools/read_bin.cpp @@ -20,19 +20,22 @@ int main(int argc, char**argv){ return 1; } - int num_nodes; - int num_edges; - input.read((char*)&num_nodes, sizeof(int)); - input.read((char*)&num_edges, sizeof(int)); + unsigned int num_nodes; + unsigned long num_edges; + input.read((char*)&num_nodes, sizeof(unsigned int)); + input.read((char*)&num_edges, sizeof(unsigned long)); cout << "num_nodes " << num_nodes << endl; cout << "num_edges " << num_edges << endl; output << num_nodes << " " << num_edges << endl; - int node; - int node_weight; + return 0; + + + unsigned int node; + unsigned int node_weight; for (int i = 0; i num_nodes-1) { cout << "Error: source " << source << " is greater than num_nodes " << num_nodes << endl;