Skip to content

Commit

Permalink
solved some todos
Browse files Browse the repository at this point in the history
  • Loading branch information
Edoardo Colella s257113 committed Dec 22, 2023
1 parent 06c136d commit 81e26e1
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 62 deletions.
18 changes: 11 additions & 7 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
#include "partitioning.h"

/**
TODO
*/
* @brief load a graph from a file
* @return ptr to the graph
*/
GraphPtr commandLoadFile(){
GraphPtr g = nullptr;
std::cout << "Enter graph path" << std::endl;
Expand All @@ -20,9 +21,11 @@ GraphPtr commandLoadFile(){
}
return g;
}

/**
TODO
*/
* @brief execute the sequential partitioning algorithm
* for comparison purposes
*/
void partitioning_s_test(GraphPtr& g, int requestedPartitions){
auto start_time_s = std::chrono::high_resolution_clock::now();
partitioning_s(g, requestedPartitions);
Expand All @@ -32,8 +35,9 @@ void partitioning_s_test(GraphPtr& g, int requestedPartitions){
}

/**
TODO
*/
* @brief execute the parallel partitioning algorithm
* for comparison purposes
*/
void partitioning_p_test(GraphPtr& g, int requestedPartitions){
for(int i = 2; i<= 8; i++){
auto start_time_s = std::chrono::high_resolution_clock::now();
Expand All @@ -47,7 +51,7 @@ void partitioning_p_test(GraphPtr& g, int requestedPartitions){
int main() {

int requestedPartitions;
char command;// = 's';
char command;
GraphPtr g = nullptr;
std::vector<unsigned int> result;
int numThreads;
Expand Down
7 changes: 5 additions & 2 deletions src/output.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
#include <sstream>

/**
TODO
*/
* @brief save partitioning results to a file
* @param graph
* @param partitions
* @param requestedPartitions
*/
void save_to_file(const GraphPtr & graph, const std::vector<unsigned int> &partitions,int requestedPartitions) {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
Expand Down
59 changes: 25 additions & 34 deletions src/parallel_coarsening.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,30 +107,12 @@ unsigned int colourGraph(GraphPtr&g, int num_threads){
return last_color;
}


/**
TODO
*/
EdgePtr old_get_max_edge(const EdgePtrArr& edges, std::vector<bool>& matched_nodes){
unsigned int max_edge_weight = 0;

EdgePtr max_edge = nullptr;
for(const auto&e: edges){
if(matched_nodes[e->node1.lock()->id] || matched_nodes[e->node2.lock()->id]) continue;
if(e->weight > max_edge_weight){
max_edge_weight = e->weight;
max_edge = e;
}
}

if(max_edge == nullptr)
throw std::runtime_error("No max edge found");

return max_edge;
}

/**
TODO
* @brief find the maximum edge in the graph that is not connected to a matched node
* @param edges the edges to search
* @param matched_nodes the nodes that are already matched
* @param retVal the edge to return
* @return true if an edge was found, false otherwise
*/
bool get_max_edge(const EdgePtrArr& edges, std::vector<bool>& matched_nodes, EdgePtr& retVal){
unsigned int max_edge_weight = 0;
Expand All @@ -154,8 +136,18 @@ bool get_max_edge(const EdgePtrArr& edges, std::vector<bool>& matched_nodes, Edg
}

/**
TODO
*/
* @brief a step of the coarsening algorithm
* @param original_graph the original graph
* @param coarse_graph the coarse graph to build
* @param start
* @param num_threads the number of threads to use
* @param mtx the mutex to use
* @param b the barrier to use
* @param max_colour
* @param matched_nodes
* @param matched_index
* @param n_index
*/
void coarse_step(const GraphPtr& original_graph, const GraphPtr& coarse_graph, int start, int num_threads,
std::mutex&mtx, std::barrier<>&b, int max_colour, std::vector<bool>&matched_nodes,
std::vector<unsigned int>&matched_index, int&n_index){
Expand Down Expand Up @@ -226,17 +218,17 @@ void coarse_step(const GraphPtr& original_graph, const GraphPtr& coarse_graph, i
}

/**
* Si calcola un coarsed graph in maniera parallela
* @param g il grafo di partenza
* @param num_threads il numero di thread da usare
* @return il grafo calcolato
* Computes a coarsened graph in a parallel manner.
* @param g The input graph
* @param num_threads The number of threads to use
* @return The computed graph
*/
GraphPtr coarseGraph_p(GraphPtr&g, int num_threads){

g->colours = std::vector<int>(g->V(), -1);

//prima si colora il grafo attraverso una versione modificata dell'algoritmo di Luby
g->num_colours = colourGraph(g, num_threads);
//first we color the graph through a modified version of Luby's algorithm
g->num_colours = colourGraph(g, num_threads);
auto coarse_graph = std::make_shared<Graph>();

std::mutex mtx;
Expand All @@ -247,16 +239,15 @@ GraphPtr coarseGraph_p(GraphPtr&g, int num_threads){
int n_index = 0;
std::vector<std::thread> threads;

//vengono instanziati un tot di thread per effettuare il partizionamento basato sul colore
for(int i = 0; i<num_threads; i++)
//a number of threads are instantiated to perform color-based partitioning for(int i = 0; i<num_threads; i++)
threads.emplace_back(coarse_step, std::ref(g), std::ref(coarse_graph), i,
num_threads, std::ref(mtx), std::ref(b), g->num_colours,
std::ref(matched_nodes), std::ref(matched_index), std::ref(n_index));

for(auto&t: threads)
t.join();

//vengono calcolate le distanze tra i nodi nel nuovo grafo
//the distances between nodes in the new graph are calculated
for(const auto& e : g->edges){
if(e->node1.lock()->child->id != e->node2.lock()->child->id)
coarse_graph->add_or_sum_edge(e->node1.lock()->child, e->node2.lock()->child, e->weight);
Expand Down
21 changes: 14 additions & 7 deletions src/parallel_partitioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@
#include <vector>

/**
TODO
*/
* @brief verify if end condition is reached, based on metis implementation
* @param num_nodes number of nodes in the graph
* @param num_partitions number of partitions
* @return true if end condition is reached, false otherwise
*/
unsigned int calculate_end_condition_p(unsigned int num_nodes, unsigned int num_partitions){
return std::max(30*num_partitions, num_nodes/(40* (unsigned int)log2(num_partitions)));
}

/**
TODO
*/
* @brief One step of the parallel uncorsening algorithm
* @param g
* @param partitions
* @param newPartitions
* @param num_nodes
* @param start
* @param step
*/
void uncoarsen_graph_step(const GraphPtr& g, std::vector<unsigned int> &partitions,
std::vector<unsigned int> &newPartitions, int num_nodes, int start, int step) {
int i = start;
Expand All @@ -25,9 +34,7 @@ void uncoarsen_graph_step(const GraphPtr& g, std::vector<unsigned int> &partitio
}
}

/**
TODO
*/

std::vector<unsigned int> uncoarsen_graph_p(const GraphPtr& g, std::vector<unsigned int> &partitions, int num_thread) {
unsigned int num_nodes = g->V();
std::vector<unsigned int> newPartitions(num_nodes);
Expand Down
21 changes: 9 additions & 12 deletions src/sequential_partitioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@
#include <vector>

/**
TODO
*/
* @brief verify if end condition is reached, based on metis implementation
* @param num_nodes number of nodes in the graph
* @param num_partitions number of partitions
* @return true if end condition is reached, false otherwise
*/
unsigned int calculate_end_condition_s(unsigned int num_nodes, unsigned int num_partitions){
return std::max(30*num_partitions, num_nodes/(40* (unsigned int)log2(num_partitions)));
}

/**
TODO
*/
struct RetrieveKey {
template<typename T> typename T::first_type operator()(T keyValuePair) const {
return keyValuePair.first;
}
};

/**
* Funzione che aggiorna le partizioni di un grafo padre basandosi sui valori del grafo figlio
* @param g grafo padre
* @param partitions vettore partizioni del grafo figlio
* @return vettore partizioni del grafo padre
* Function that updates the partitions of a parent graph based on the values of the child graph.
* @param g Parent graph
* @param partitions Vector of partitions of the child graph
* @return Vector of partitions of the parent graph
*/
std::vector<unsigned int> uncoarsenGraph(const GraphPtr& g, std::vector<unsigned int> &partitions) {
std::vector<unsigned int> newPartitions(g->V());
Expand Down Expand Up @@ -71,7 +71,6 @@ void initial_partitioning_s(const GraphPtr& g, std::vector<unsigned int> &partit

std::vector<int> keys;


// Retrieve all keys
transform(cluster_hashMap[selected.clusterB].begin(), cluster_hashMap[selected.clusterB].end(), back_inserter(keys), RetrieveKey());

Expand Down Expand Up @@ -147,6 +146,4 @@ std::vector<unsigned int> partitioning_s(const GraphPtr& g, int requestedPartit
}

return partitions;


}

0 comments on commit 81e26e1

Please sign in to comment.