From 696deabb91b04fde200130cd8b457a7fc34856bd Mon Sep 17 00:00:00 2001 From: Ivaylo Ivanov Date: Sun, 13 Jan 2019 15:23:32 +0100 Subject: [PATCH] Add generator functionality --- fb_arc_set/generator.c | 36 +++++++++++++++++++++++++++++++----- fb_arc_set/supervisor.c | 1 + 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/fb_arc_set/generator.c b/fb_arc_set/generator.c index 34b0b2a..d411f28 100644 --- a/fb_arc_set/generator.c +++ b/fb_arc_set/generator.c @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) { } num_nodes++; ///< Our first node is labeled 0, so we need to add 1 more - /// Declare arrays for unique nodes + /// Declare array for unique nodes int unique_nodes[num_nodes]; /// Filling the node array with the nodes @@ -101,9 +101,9 @@ int main(int argc, char *argv[]) { /// Declaring buffer arrays and variables int current_best = num_edges - 1; ///< Set a basic best solution + int new_best = 0; int buf_nodes[num_nodes]; edge_t buf_edges[num_edges]; - int new_best = 0; while(terminate != true && circ_buffer -> finished != true) { ///< Loop until told to stop or until an acyclic solution is discovered /// Copy the original into a buffer array and shuffle it @@ -113,7 +113,7 @@ int main(int argc, char *argv[]) { /// Reset the chosen status of the edges memcpy(buf_edges, edges, sizeof(edges)); - new_best = 0; ///< Reset the new_besy counter + new_best = 0; ///< Reset the new_best counter for(int i=0; i < num_edges; i++) { if(new_best >= SET_MAX_SIZE || new_best >= num_edges) break; ///< Skip if the solution is faulty @@ -130,11 +130,37 @@ int main(int argc, char *argv[]) { } /// Construct the new feedback arc set - if(new_best < current_best && new_best <= SET_MAX_SIZE) { + if(new_best < current_best && new_best <= SET_MAX_SIZE && current_best < circ_buffer -> best_arc_len) { current_best = new_best; + + fb_arc_set_t res; + memset(&res, 0, sizeof(struct fb_arc_set)); /// Removes faulty data from the buffer later + for(int i=0; i < num_edges; i++) { + if(buf_edges[i].chosen == true) { + res.edge_num++; + res.edges[--new_best] = edges[i]; ///< We begin counting from 0 + } + } + res.valid = true; + + wait_sem(mutex); ///< Say that it's our turn to write + wait_sem(free_sem); ///< Lock the free space semaphore + + /// Find a free space to write to + while(circ_buffer->sets[wr_pos].valid == true) { + wr_pos = (wr_pos + 1) % BUFF_SIZE; + } + circ_buffer->sets[wr_pos] = res; + + post_sem(use_sem); + wr_pos = (wr_pos + 1) % BUFF_SIZE; + post_sem(mutex); + + if(current_best == 0) { + signal_handler(15); + } } } - exit(EXIT_SUCCESS); } diff --git a/fb_arc_set/supervisor.c b/fb_arc_set/supervisor.c index 09941bd..166c633 100644 --- a/fb_arc_set/supervisor.c +++ b/fb_arc_set/supervisor.c @@ -79,6 +79,7 @@ int main(int argc, char *argv[]) { if(fb_arc.edge_num == 0) { /// If the graph is acyclic, set the terminate flag puts("The graph is acyclic!"); + circ_buffer -> finished = true; signal_handler(SIGINT); } else if(fb_arc.edge_num < circ_buffer->best_arc_len) { /// Save ther best feedback arc length and print a solution