X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=course%2Fsemester2%2Fpprog%2Fassignment1%2Fsingle-thread%2Fnbody.h;h=2ff81d7bfadc28375f8aa6f7c674d72d8897e98e;hb=HEAD;hp=b8c9813578d105ac20b9071af49f581429e55f36;hpb=c2c6a38870351a5702913ce8a97c25c51662a59c;p=matches%2Fhonours.git diff --git a/course/semester2/pprog/assignment1/single-thread/nbody.h b/course/semester2/pprog/assignment1/single-thread/nbody.h index b8c98135..2ff81d7b 100644 --- a/course/semester2/pprog/assignment1/single-thread/nbody.h +++ b/course/semester2/pprog/assignment1/single-thread/nbody.h @@ -17,14 +17,40 @@ #define SINGLE_THREADED -//These macros will be undefined by the multithreaded versions -#define Simulation_Run() //Sets up the simulation; in multithreaded versions, will spawn threads +// --- The below macros will be undefined by the multithreaded versions, and replaced with functions --- // + +//Sets up the simulation; in multithreaded versions, will spawn threads +#define Simulation_Run(argc, argv) \ +if (options.draw_graphics) \ + Graphics_Run(argc, argv); \ +else \ +{ \ + while (ExitCondition() == false) \ + { BeforeDraw(); AfterDraw(); } \ +} + #define QuitProgram(error) (runstate = (error == true) ? QUIT : QUIT_ERROR) //Prepares to exit program, is thread safe in multithreaded versions +//Macro to be overwritten in multithreaded versions, called before the graphics is allowed to draw anything +#define BeforeDraw() \ +System_Compute(&universe); \ +universe.steps += 1; \ +if (options.verbosity != 0 && universe.steps % options.verbosity == 0) \ + DisplayStatistics(); \ + + + + +//Macro to be overwritten in multithreaded versions, called after the graphics has finished drawing. +#define AfterDraw() + +// --- Constants and other Macros --- // + #define M_PI 3.14159265358979323846264338327950288 /* pi */ #define G 6.67428E-11 #define DELTA_T 0.05 #define DIMENSIONS 3 +#define LINE_SIZE 1000 #define square(x) ((x)*(x)) @@ -69,32 +95,40 @@ typedef struct const char * input; // initial body field const char * output; // file to write final positions / velocities of bodies to const char * program; // program name - unsigned num_threads; // number of worker threads to spawn (must be greater than 1 for any to be spawned) - unsigned num_steps; // number of steps to run before stopping (run indefinately if equal to zero) - unsigned timeout; // number of seconds to run before stopping (run indefinately if equal to zero) + int num_threads; // number of worker threads to spawn (must be greater than 1 for any to be spawned) + int nested_threads; // number of threads to nest computations with (must be greater than 1 for any to be spawned) + int num_steps; // number of steps to run before stopping (run indefinately if less than zero) + int timeout; // number of seconds to run before stopping (run indefinately if less than zero) bool draw_graphics; // whether or not to actually draw graphics + bool pedantic_graphics; // whether the graphics thread will synchronise with the computation thread (true) or just draw as fast as possible (false) bool print_positions; // print positions of bodies to stdout on every step - unsigned verbosity; // print statistics every number of steps indicated by this variable + int verbosity; // print statistics every number of steps indicated by this variable clock_t start_clock; // clock cycles done when simulation starts struct timeval start_time; // time at which simulation starts + float theta; // Parameter used for Barns Hut algorithm + int random; //Used to randomly create bodies } Options; void Body_Print(Body * a, FILE * out); //Print body a -void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s +void Body_Forces(Body * a, System * s); //Compute force on body a due to system of bodies s +void Body_Force(Body * a, Body * b); // Compute force on body a due to body b void Body_Velocity(Body * a); //Compute velocity of body a void Body_Position(Body * a); //Compute position of body a void System_Init(System * s, const char * fileName); //Initialise System (array of bodies) from a text file +void System_Random(System * s, int r); //Randomly create bodies void System_Compute(System * s); void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities) void System_Positions(System * s); //Update positions for bodies in s1 - +System * Split_System(System * s, unsigned n); // Splits one system into a number of other systems, returns an array of size n void Universe_Cleanup(); //Cleanup universe and write bodies to file void DisplayStatistics(); // Print information about steps computed, total (real) runtime, and CPU cycles +bool ExitCondition(void); //Checks whether the program is supposed to exit automatically yet (ie: other than the user pressing "quit") + typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE; extern RUNSTATE runstate; // Set runstate to QUIT or QUIT_ERROR and the simulation will stop on the next step.