Parallel Programming - Stuff happened
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / nbody.h
1 #ifndef _NBODY_H
2 #define _NBODY_H
3
4 /**
5  * @file nbody.h
6  * @author Sam Moore (205030628)
7  * @purpose N-Body simulator: declarations of simulation related parameters
8  */
9
10 #include <stdlib.h>
11 #include <stdbool.h>
12 #include <stdio.h>
13 #include <time.h>
14 #include <sys/time.h> //POSIX time
15
16           
17
18 #define SINGLE_THREADED
19
20 // --- The below macros will be undefined by the multithreaded versions, and replaced with functions --- //
21
22 //Sets up the simulation; in multithreaded versions, will spawn threads
23 #define Simulation_Run(argc, argv) \
24 if (options.draw_graphics) \
25         Graphics_Run(argc, argv); \
26 else \
27 { \
28         while (ExitCondition() == false) \
29         { BeforeDraw(); AfterDraw(); } \
30 }
31
32 #define QuitProgram(error) (runstate = (error == true) ? QUIT : QUIT_ERROR) //Prepares to exit program, is thread safe in multithreaded versions
33
34 //Macro to be overwritten in multithreaded versions, called before the graphics is allowed to draw anything
35 #define BeforeDraw() \
36 if (options.verbosity != 0 && universe.steps % options.verbosity == 0) \
37         DisplayStatistics(); \
38 System_Compute(&universe);
39
40
41
42 //Macro to be overwritten in multithreaded versions, called after the graphics has finished drawing.
43 #define AfterDraw()     
44
45 // --- Constants and other Macros --- //        
46
47 #define M_PI        3.14159265358979323846264338327950288   /* pi */
48 #define G 6.67428E-11
49 #define DELTA_T 0.05
50 #define DIMENSIONS 3
51 #define LINE_SIZE 1000
52 #define square(x) ((x)*(x))
53
54
55 /**
56  * Structure to represent a single Body
57  * @param mass - Mass of the body
58  * @param x - Position vector (array)
59  * @param v - Velocity vector (array)
60  * @param F - Net force vector (array)
61  */
62 typedef struct 
63 {
64
65         double mass;
66         double x[DIMENSIONS];
67         double v[DIMENSIONS];
68         double F[DIMENSIONS];
69
70 } Body;
71
72 /**
73  * Structure to store an array of bodies, along with the size of the array.
74  * The universe is represented by a single System. 
75  * In the multithreaded program, the universe is subdivided into one system for each working thread to use.
76  * @param N - Size of the array
77  * @param body - The array of bodies
78  */
79 typedef struct
80 {
81         unsigned N; // Number of bodies in the System
82         Body * body; // Array of bodies
83
84         unsigned steps; //Number of steps simulated
85
86 } System;
87
88 /**
89  * Structure to represent options passed to the program. 
90  */
91 typedef struct
92 {
93         const char * input; // initial body field
94         const char * output; // file to write final positions / velocities of bodies to
95         const char * program; // program name
96         unsigned num_threads; // number of worker threads to spawn (must be greater than 1 for any to be spawned)
97         unsigned nested_threads; // number of threads to nest computations with (must be greater than 1 for any to be spawned)
98         unsigned num_steps; // number of steps to run before stopping (run indefinately if equal to zero)
99         unsigned timeout; // number of seconds to run before stopping (run indefinately if equal to zero)
100         bool draw_graphics; // whether or not to actually draw graphics
101         bool pedantic_graphics; // whether the graphics thread will synchronise with the computation thread (true) or just draw as fast as possible (false)
102         bool print_positions; // print positions of bodies to stdout on every step
103         unsigned verbosity; // print statistics every number of steps indicated by this variable
104         clock_t start_clock;  // clock cycles done when simulation starts
105         struct timeval start_time; // time at which simulation starts
106 } Options;
107
108 void Body_Print(Body * a, FILE * out); //Print body a
109 void Body_Force(Body * a, System * s); //Compute force on body a due to system of bodies s
110 void Body_Velocity(Body * a); //Compute velocity of body a
111 void Body_Position(Body * a); //Compute position of body a
112
113 void System_Init(System * s, const char * fileName); //Initialise System (array of bodies) from a text file
114 void System_Compute(System * s);
115 void System_Forces(System * s1, System * s2); //Compute forces for bodies in s1 due to bodies in s2 (also updates velocities)
116 void System_Positions(System * s); //Update positions for bodies in s1
117
118
119 void Universe_Cleanup(); //Cleanup universe and write bodies to file
120
121 void DisplayStatistics(); // Print information about steps computed, total (real) runtime, and CPU cycles
122
123
124 bool ExitCondition(void); //Checks whether the program is supposed to exit automatically yet (ie: other than the user pressing "quit")
125
126
127 typedef enum {RUN, QUIT, QUIT_ERROR} RUNSTATE;
128 extern RUNSTATE runstate; // Set runstate to QUIT or QUIT_ERROR and the simulation will stop on the next step.
129 // This is fairly redundant in the single threaded version, but useful for making sure *all* threads know to exit in the multi-threaded version
130
131
132 extern System universe; // The main array of bodies; global variable.
133 extern Options options; // Parameters passed to program
134
135
136
137 #endif //_NBODY_H

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