Parallel Programming - Finished pthreads
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / main.c
1 /**
2  * @file main.c
3  * @author Sam Moore (20503628) - 2012
4  * @purpose Contains main function, argument handling
5  * NOTE: This file is identical for both the single-threaded and multi-threaded versions of the program
6  */
7
8 #include <stdlib.h>
9 #include <stdio.h>
10 #include <time.h>
11 #include <signal.h>
12 #include <string.h> // For parsing arguments
13
14 #include "nbody.h"
15 #include "graphics.h"
16
17 // --- Variable definitions --- //
18 System universe; // global variable declared in "nbody.h" - The universe of bodies
19 Options options; // global variable declared in "nbody.h" - Options passed to program through arguments
20
21
22 // --- Function forward declarations --- //
23 void HandleArguments(int argc, char ** argv); //Interprets program arguments and sets up the "options" variable
24 unsigned IntegerArgument(unsigned * i, int argc, char ** argv, int * store, int * store2); //Helper function to get integer switch
25 void FloatArgument(unsigned * i, int argc, char ** argv, float * store); //Helper function to get switch value for float
26 void DisplayStatistics(); //Called on exit of program, displays information about computation time, steps computed, etc
27 void Interrupt(int dummy); // Interrupt handler function, called when SIGINT (Ctrl-C) is sent to program
28
29
30 // --- Function implementations --- //
31
32 /**
33  * @function main
34  * @purpose The main function. Calls HandleArguments to set up options, 
35  *              sets up functions to call at exit, starts simulation and graphics loops
36  * @param argc - Number of arguments
37  * @param argv - The argument strings
38  */
39 int main(int argc, char** argv)
40 {
41         //Set default options values here
42         options.program = argv[0];
43         options.input = NULL;
44         options.output = NULL;
45         options.num_threads = 0;
46         options.nested_threads = 0;
47         options.num_steps = -1; // Negative values => simulation runs forever unless otherwise specified
48         options.timeout = -1;
49         options.draw_graphics = true;
50         options.pedantic_graphics = false;
51         options.print_positions = false;
52         options.verbosity = 0;
53
54         //If there are no arguments, print information about usage
55         if (argc == 1)
56         {
57                 printf("Usage: %s [OPTIONS] field\n", argv[0]);
58                 printf("Controls:\n X - exit\n I, J, K, M - rotate\n W, Z, A, S - move to view"
59                         " point\n ./, - zoom in/out\n +/- - scaled zoom in/out\n");
60                 exit(EXIT_SUCCESS);
61         }
62
63         //Parse the arguments
64         HandleArguments(argc, argv);
65         
66         //Check there is an initial field.
67         if (options.input == NULL)
68         {
69                 fprintf(stderr, "Usage: %s [OPTIONS] field\n", argv[0]);
70                 fprintf(stderr, " (You did not provide a file for the initial field of bodies)\n");
71                 exit(EXIT_FAILURE);
72         }
73
74
75
76         signal(SIGINT, Interrupt); //Handle SIGINT signals
77         atexit(Universe_Cleanup); //On exit, cleanup universe (and write positions of bodies to file if supplied).
78         atexit(DisplayStatistics); //On exit, print information about the computations done
79         System_Init(&universe,options.input); //Initialise the universe from the initial field
80         
81         //Setup the time of day at which the simulation starts
82         if (gettimeofday(&(options.start_time), NULL) != 0)
83         {
84                 perror("Couldn't get time of day");
85                 exit(EXIT_FAILURE);
86         }
87         
88         options.start_clock = clock(); //Get CPU cycles executed before simulation starts
89         Simulation_Run(argc, argv); // Start the simulation
90         
91         //printf("Main thread done!\n");
92         
93         //pthread_exit(NULL);
94         exit(EXIT_SUCCESS); //Should never get to this line
95 }
96
97
98 /**
99  * @function HandleArguments
100  * @purpose Fill the "options" variable by parsing command line arguments
101  * @param argc - Number of arguments
102  * @param argv - Argument strings
103  */
104 void HandleArguments(int argc, char ** argv)
105 {
106         for (unsigned i = 1; i < argc; ++i) //For all arguments
107         {
108                 if (argv[i][0] != '-') //If the argument is not a switch value...
109                 {
110                         //Interpret first non switch as input file, and second as output file.
111                         //If there are too many non switch arguments, give an error.
112                         if (options.input == NULL) 
113                                 options.input = argv[i];
114                         else if (options.output == NULL)
115                                 options.output = argv[i];
116                         else
117                         {
118                                 fprintf(stderr,"Usage: %s [OPTIONS] field [output]\n", argv[0]);
119                                 fprintf(stderr," (You provided too many file names)\n");
120                                 exit(EXIT_FAILURE);
121                         }
122                         continue;
123                 }
124                 switch (argv[i][1]) //The argument is a switch if we get here
125                 {
126                         case 'n': //Number of threads switch
127                                 IntegerArgument(&i, argc, argv, &(options.num_threads), &(options.nested_threads));
128                                 #ifdef SINGLE_THREADED
129                                 fprintf(stderr, "Warning: -%c switch has no effect in the single-threaded program.\n", 'n');
130                                 
131                                 #else
132                                 if (options.num_threads <= 0)
133                                 {
134                                         fprintf(stderr, "Require at least one thread (-%c %s is invalid).\n",'n', argv[i]);
135                                         exit(EXIT_FAILURE);
136                                 }
137                                 #endif //SINGLE_THREADED
138                                 break;
139                         case 's': //Number of steps switch
140                                 IntegerArgument(&i, argc, argv, &(options.num_steps), NULL);
141                                 if (options.num_steps < 0)
142                                 {
143                                         fprintf(stderr, "Require zero or more steps to run (-%c %s is invalid).\n", 's', argv[i]);
144                                         exit(EXIT_FAILURE);
145                                 }
146                                 break;
147                         case 't': //Timeout switch (in seconds)
148                                 IntegerArgument(&i, argc, argv, &(options.timeout), NULL);
149                                 if (options.timeout < 0)
150                                 {
151                                         fprintf(stderr, "Require a timeout greater or equal to zero (-%c %s is invalid).", 't', argv[i]);
152                                         exit(EXIT_FAILURE);
153                                 }                               
154                                 break;
155                         case 'g': //Graphics switch
156                                 options.draw_graphics = !options.draw_graphics;
157                                 break;
158                         case 'v': //Verbosity switch
159                                 IntegerArgument(&i, argc, argv, &(options.verbosity), NULL);
160                                 break;
161
162                         case '-':
163                                 if (strcmp(argv[i]+2, "pedantic-graphics") == 0)
164                                 {
165                                         options.pedantic_graphics = true;
166                                         #ifdef SINGLE_THREADED
167                                         fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
168                                         #endif //SINGLE_THREADED
169                                 }
170                                 else if (strcmp(argv[i]+2, "fast-graphics") == 0)
171                                 {
172                                         options.pedantic_graphics = false;
173                                         #ifdef SINGLE_THREADED
174                                         fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
175                                         #endif //SINGLE_THREADED
176
177                                 }
178                                 else
179                                 {
180                                         fprintf(stderr, "Unrecognised switch %s\n", argv[i]);
181                                         exit(EXIT_FAILURE);
182                                 }
183                                 break;
184
185
186                         default:
187                                 fprintf(stderr, "Unrecognised switch -%c\n", argv[i][1]);
188                                 exit(EXIT_FAILURE);
189                                 break;
190                 }
191                 
192         }
193 }
194
195 /**
196  * @function IntegerArgument
197  * @purpose Helper function to get up to two integers following a argument switch, seperated by ':'
198  * @param i - position in the argument array, will be updated after this function
199  * @param argc - number of arguments
200  * @param argv - argument strings
201  * @param store - pointer to unsigned to store result in
202  * @param store2 - pointer to second integer (set to NULL if there is only one integer)
203  * @returns 1 if store was filled, 2 if both store1 and store2 were filled
204  */
205 unsigned IntegerArgument(unsigned * i, int argc, char ** argv, int * store, int * store2)
206 {
207         if (*i >= argc-1)
208         {
209                 fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
210                 exit(EXIT_FAILURE);
211         }
212
213         char * seperator = strstr(argv[*i+1], ":");
214         if (seperator != NULL)
215         {
216                 if (store2 == NULL)
217                 {
218                         fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
219                         exit(EXIT_FAILURE);     
220                 }
221                 int val = atoi(seperator+1);
222                 if (val <= 0)
223                 {
224                         fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
225                         exit(EXIT_FAILURE);     
226                 }       
227                 *store2 = (unsigned)val;
228
229                 *seperator = '\0';
230         }
231         
232         int val = atoi(argv[*i+1]);
233         if (val <= 0 && strcmp("0", argv[*i+1]) != 0)
234         {
235                 fprintf(stderr,"Supply a positive integer for the -%c switch. %s is invalid.\n", argv[*i][1], argv[*i+1]);
236                 exit(EXIT_FAILURE);
237         }
238         *store = val;
239         *i += 1;
240
241         return (seperator == NULL) ? 1 : 2;
242 }
243 /**
244  * @function FloatArgument
245  * @purpose Helper function to get a float following a argument switch
246  * @param i - position in the argument array, will be updated after this function
247  * @param argc - number of arguments
248  * @param argv - argument strings
249  * @param store - pointer to float to store result in
250  */
251 void FloatArgument(unsigned * i, int argc, char ** argv, float * store)
252 {
253         if (*i >= argc-1)
254         {
255                 fprintf(stderr,"Supply a float for the -%c switch.\n", argv[*i][1]);
256                 exit(EXIT_FAILURE);
257         }
258         *store = atof(argv[*i+1]);
259         if (*store == 0.0 && argv[*i+1][0] != '0')
260         {
261                 fprintf(stderr,"Supply a float for the -%c switch.\n", argv[*i][1]);
262                 exit(EXIT_FAILURE);
263         }
264         *i += 1;
265 }
266
267
268
269 /**
270  * @function Interrupt
271  * @purpose Handle SIGINT signal; quit the program gracefully, so that statistics are still displayed
272  * @param dummy - a dummy
273  */
274
275 void Interrupt(int dummy)
276 {
277         QuitProgram(false);
278 }
279
280

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