X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fopenmp%2Fnbody.c;h=c4872010f3382df343e2af7c9d2c7324c23d755a;hb=83cf58f62ddc5ce9fe44de27327fde27f767ebb6;hp=6eb9ed4a66e191579fa4c1df2d2e61b0fa5168c6;hpb=19e590cc4f7dbc186cad9418bf5a2321bfa3fe1a;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/openmp/nbody.c b/course/semester2/pprog/assignment1/openmp/nbody.c index 6eb9ed4a..c4872010 100644 --- a/course/semester2/pprog/assignment1/openmp/nbody.c +++ b/course/semester2/pprog/assignment1/openmp/nbody.c @@ -8,6 +8,9 @@ #include "../single-thread/nbody.c" // Include original code #include "graphics.h" // For Graphics_Run function only +omp_lock_t graphics_lock; +bool graphics_busy = false; + /** * @function Simulation_Run * @purpose Start the simulation @@ -32,7 +35,7 @@ void Simulation_Run(int argc, char ** argv) omp_set_num_threads(1); - + omp_init_lock(&graphics_lock); #pragma omp parallel sections { @@ -50,9 +53,7 @@ void Simulation_Run(int argc, char ** argv) break; } - - if (options.draw_graphics == false && options.verbosity != 0 - && universe.steps % options.verbosity == 1) + if (options.verbosity != 0 && universe.steps % options.verbosity == 1) { DisplayStatistics(); } @@ -71,6 +72,8 @@ void Simulation_Run(int argc, char ** argv) } } + + omp_destroy_lock(&graphics_lock); } /** @@ -93,6 +96,13 @@ void Compute(void) System_Forces(&s, &universe); } + // Wait for graphics to finish drawing + + if (options.draw_graphics && options.pedantic_graphics) + { + while (graphics_busy); + } + #pragma omp parallel for for (unsigned i = 0; i < options.num_threads; ++i) { @@ -105,4 +115,39 @@ void Compute(void) universe.steps += 1; + // Tell graphics to continue + if (options.draw_graphics && options.pedantic_graphics) + { + omp_set_lock(&graphics_lock); + graphics_busy = true; + omp_unset_lock(&graphics_lock); + + } + +} + +/** + * @function BeforeDraw + * @purpose Called before the graphics thread draws bodies. + * If pedantic graphics are used, will wait until graphics_busy is set to true before drawing + */ +void BeforeDraw() +{ + if (!options.pedantic_graphics) + return; + while (graphics_busy == false); +} + +/** + * @function AfterDraw + * @purpose Called after the graphics thread draws bodies + * If pedantic graphics used, will unset graphics_busy when done drawing + */ +void AfterDraw() +{ + if (!options.pedantic_graphics) + return; + omp_set_lock(&graphics_lock); + graphics_busy = false; + omp_unset_lock(&graphics_lock); }