Parallel Programming - Tidied things up
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / nbody.h
index e288c6d..b8c9813 100644 (file)
@@ -7,8 +7,20 @@
  * @purpose N-Body simulator: declarations of simulation related parameters
  */
 
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <time.h>
+#include <sys/time.h> //POSIX time
+
+          
+
 #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
+#define QuitProgram(error) (runstate = (error == true) ? QUIT : QUIT_ERROR) //Prepares to exit program, is thread safe in multithreaded versions
+
 #define M_PI        3.14159265358979323846264338327950288   /* pi */
 #define G 6.67428E-11
 #define DELTA_T 0.05
@@ -35,29 +47,63 @@ typedef struct
 
 /**
  * Structure to store an array of bodies, along with the size of the array.
- * The universe is represented in a single System. 
+ * The universe is represented by a single System. 
+ * In the multithreaded program, the universe is subdivided into one system for each working thread to use.
  * @param N - Size of the array
  * @param body - The array of bodies
  */
 typedef struct
 {
-       unsigned N;
-       Body * body;
+       unsigned N; // Number of bodies in the System
+       Body * body; // Array of bodies
+
+       unsigned steps; //Number of steps simulated
 
 } System;
 
-void Body_Print(Body * a); //Print body a
+/**
+ * Structure to represent options passed to the program. 
+ */
+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)
+       bool draw_graphics; // whether or not to actually draw graphics
+       bool print_positions; // print positions of bodies to stdout on every step
+       unsigned 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
+} 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_Velocity(Body * a); //Compute velocity of body a
 void Body_Position(Body * a); //Compute position of body a
 
-void System_Init(System * s, char * fileName); //Initialise System (array of bodies) from a text file
+void System_Init(System * s, const char * fileName); //Initialise System (array of bodies) from a text file
+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
+
+
+void Universe_Cleanup(); //Cleanup universe and write bodies to file
+
+void DisplayStatistics(); // Print information about steps computed, total (real) runtime, and CPU cycles
 
-void System_Compute(System * system); //Perform a single computation step for a System of bodies
 
-void Universe_Cleanup();
+
+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.
+// This is fairly redundant in the single threaded version, but useful for making sure *all* threads know to exit in the multi-threaded version
 
 
 extern System universe; // The main array of bodies; global variable.
+extern Options options; // Parameters passed to program
+
+
 
 #endif //_NBODY_H

UCC git Repository :: git.ucc.asn.au