Skip to content

Commit

Permalink
refactoring: starting transition to unsigned domain
Browse files Browse the repository at this point in the history
  • Loading branch information
Edoardo Colella s257113 committed Sep 26, 2023
1 parent 08a85e4 commit 12a4307
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 64 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
16 changes: 8 additions & 8 deletions src/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,28 +19,28 @@ vector<Node *> 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<Edge> Graph::add_edge(int source, int dest, int distance) {
shared_ptr<Edge> Graph::add_edge(unsigned int source, unsigned int dest, unsigned int distance) {
Node *node1 = nodes[source];
Node *node2 = nodes[dest];
shared_ptr<Edge> e = make_shared<Edge>(distance, node1, node2);
Expand All @@ -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)
Expand Down Expand Up @@ -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();
}
26 changes: 13 additions & 13 deletions src/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<shared_ptr<struct Edge>> edges;
vector<Node *> 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<int> colours;
vector<int> partitions_size;
vector<Node *> nodes;
vector<shared_ptr<Edge>> edges;
Node *add_node(int id, int weight);
Node *add_node_with_index(int id, int weight);
shared_ptr<Edge> 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<Edge> 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();
Expand Down
36 changes: 20 additions & 16 deletions src/loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -28,20 +29,22 @@ Graph* loadFromFile(string path) {
throw std::runtime_error("Impossible to get file size");
}

int* intData = static_cast<int*>(mmap(nullptr, fileStat.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
auto intData = static_cast<unsigned int*>(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<unsigned long>(intData[2]) << 32 | intData[1];

auto* g = new Graph();
g->nodes.resize(num_nodes);

vector<thread> readers(num_threads);
barrier bar(num_threads);
mutex mtx_e;


for(int i = 0; i<num_threads; i++){
readers[i] = thread(thread_reader, g, intData, num_nodes, num_edges, i, num_threads, ref(bar), ref(mtx_e));
}
Expand All @@ -61,13 +64,13 @@ Graph* loadFromFile(string path) {



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){


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);
Expand All @@ -76,8 +79,8 @@ void thread_reader(Graph*g, const int*filedata, int num_nodes, int num_edges, in

m_edge e_temp{};
vector<m_edge> 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];
Expand All @@ -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();
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "partitioning.h"
#include "timing/timing.h"

#define PARALLEL_BUILD

using namespace std;
int main(int argc, char **argv){
Expand All @@ -13,6 +12,7 @@ int main(int argc, char **argv){
}

auto g = loadFromFile(argv[1]);

int requestedPartitions = 100;
timing total_time;

Expand Down
2 changes: 1 addition & 1 deletion src/partitioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<shared_ptr<Edge>> sortEdge(const vector<shared_ptr<Edge>>& edges);
Expand Down
18 changes: 9 additions & 9 deletions src/tools/ascii_bin_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -48,9 +48,9 @@ int main(int argc, char *argv[]){
int distance;
cout << "Writing nodes..." << endl;
for(int i = 0; i<num_nodes; i++){
output.write((char*)&i, sizeof(int));
output.write((char*)&i, sizeof(unsigned int));
int random = distribution(gen);
output.write((char*)&random, sizeof(int));
output.write((char*)&random, sizeof(unsigned int));
}

cout << "Writing edges..." << endl;
Expand All @@ -59,9 +59,9 @@ int main(int argc, char *argv[]){
>> 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);
}

Expand Down
34 changes: 18 additions & 16 deletions src/tools/read_bin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ; i++) {
input.read((char*)&node, sizeof(int));
input.read((char*)&node_weight, sizeof(int));
input.read((char*)&node, sizeof(unsigned int));
input.read((char*)&node_weight, sizeof(unsigned int));

cout << node << " " << node_weight << endl;
//output << node << " " << node_weight << endl;
Expand All @@ -42,17 +45,16 @@ int main(int argc, char**argv){
return 1;
}



}

int source;
int dest;
int distance;

unsigned int source;
unsigned int dest;
unsigned int distance;
for(int i = 0; i<num_edges; i++){
input.read((char*)&source, sizeof(int));
input.read((char*)&dest, sizeof(int));
input.read((char*)&distance, sizeof(int));
input.read((char*)&source, sizeof(unsigned int));
input.read((char*)&dest, sizeof(unsigned int));
input.read((char*)&distance, sizeof(unsigned int));

if(source > num_nodes-1) {
cout << "Error: source " << source << " is greater than num_nodes " << num_nodes << endl;
Expand Down

0 comments on commit 12a4307

Please sign in to comment.