Automatic commit. Tue Oct 9 00:00:05 WST 2012
[matches/honours.git] / course / semester2 / pprog / assignment1 / nbody-bh / 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         options.theta = 0.50;
54
55         //If there are no arguments, print information about usage
56         if (argc == 1)
57         {
58                 printf("Usage: %s [OPTIONS] field\n", argv[0]);
59                 printf("Controls:\n X - exit\n I, J, K, M - rotate\n W, Z, A, S - move to view"
60                         " point\n ./, - zoom in/out\n +/- - scaled zoom in/out\n");
61                 exit(EXIT_SUCCESS);
62         }
63
64         //Parse the arguments
65         HandleArguments(argc, argv);
66         
67         //Check there is an initial field.
68         if (options.input == NULL)
69         {
70                 fprintf(stderr, "Usage: %s [OPTIONS] field\n", argv[0]);
71                 fprintf(stderr, " (You did not provide a file for the initial field of bodies)\n");
72                 exit(EXIT_FAILURE);
73         }
74
75
76
77         signal(SIGINT, Interrupt); //Handle SIGINT signals
78         atexit(Universe_Cleanup); //On exit, cleanup universe (and write positions of bodies to file if supplied).
79         atexit(DisplayStatistics); //On exit, print information about the computations done
80         System_Init(&universe,options.input); //Initialise the universe from the initial field
81         
82         //Setup the time of day at which the simulation starts
83         if (gettimeofday(&(options.start_time), NULL) != 0)
84         {
85                 perror("Couldn't get time of day");
86                 exit(EXIT_FAILURE);
87         }
88         
89         options.start_clock = clock(); //Get CPU cycles executed before simulation starts
90         Simulation_Run(argc, argv); // Start the simulation
91         
92         //printf("Main thread done!\n");
93         
94         //pthread_exit(NULL);
95         exit(EXIT_SUCCESS); //Should never get to this line
96 }
97
98
99 /**
100  * @function HandleArguments
101  * @purpose Fill the "options" variable by parsing command line arguments
102  * @param argc - Number of arguments
103  * @param argv - Argument strings
104  */
105 void HandleArguments(int argc, char ** argv)
106 {
107         for (unsigned i = 1; i < argc; ++i) //For all arguments
108         {
109                 if (argv[i][0] != '-') //If the argument is not a switch value...
110                 {
111                         //Interpret first non switch as input file, and second as output file.
112                         //If there are too many non switch arguments, give an error.
113                         if (options.input == NULL) 
114                                 options.input = argv[i];
115                         else if (options.output == NULL)
116                                 options.output = argv[i];
117                         else
118                         {
119                                 fprintf(stderr,"Usage: %s [OPTIONS] field [output]\n", argv[0]);
120                                 fprintf(stderr," (You provided too many file names)\n");
121                                 exit(EXIT_FAILURE);
122                         }
123                         continue;
124                 }
125                 switch (argv[i][1]) //The argument is a switch if we get here
126                 {
127                         case 'n': //Number of threads switch
128                                 IntegerArgument(&i, argc, argv, &(options.num_threads), &(options.nested_threads));
129                                 #ifdef SINGLE_THREADED
130                                 fprintf(stderr, "Warning: -%c switch has no effect in the single-threaded program.\n", 'n');
131                                 
132                                 #else
133                                 if (options.num_threads <= 0)
134                                 {
135                                         fprintf(stderr, "Require at least one thread (-%c %s is invalid).\n",'n', argv[i]);
136                                         exit(EXIT_FAILURE);
137                                 }
138                                 #endif //SINGLE_THREADED
139                                 break;
140                         case 's': //Number of steps switch
141                                 IntegerArgument(&i, argc, argv, &(options.num_steps), NULL);
142                                 if (options.num_steps < 0)
143                                 {
144                                         fprintf(stderr, "Require zero or more steps to run (-%c %s is invalid).\n", 's', argv[i]);
145                                         exit(EXIT_FAILURE);
146                                 }
147                                 break;
148                         case 't': //Timeout switch (in seconds)
149                                 IntegerArgument(&i, argc, argv, &(options.timeout), NULL);
150                                 if (options.timeout < 0)
151                                 {
152                                         fprintf(stderr, "Require a timeout greater or equal to zero (-%c %s is invalid).", 't', argv[i]);
153                                         exit(EXIT_FAILURE);
154                                 }                               
155                                 break;
156                         case 'g': //Graphics switch
157                                 options.draw_graphics = !options.draw_graphics;
158                                 break;
159                         case 'v': //Verbosity switch
160                                 IntegerArgument(&i, argc, argv, &(options.verbosity), NULL);
161                                 break;
162
163                         case '-':
164                                 if (strcmp(argv[i]+2, "pedantic-graphics") == 0)
165                                 {
166                                         options.pedantic_graphics = true;
167                                         #ifdef SINGLE_THREADED
168                                         fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
169                                         #endif //SINGLE_THREADED
170                                 }
171                                 else if (strcmp(argv[i]+2, "fast-graphics") == 0)
172                                 {
173                                         options.pedantic_graphics = false;
174                                         #ifdef SINGLE_THREADED
175                                         fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
176                                         #endif //SINGLE_THREADED
177
178                                 }
179                                 else if (strcmp(argv[i]+2, "theta") == 0)
180                                 {
181                                         FloatArgument(&i, argc, argv, &(options.theta));
182                                         #ifndef BARNES_HUT
183                                         fprintf(stderr, "Warning: %s switch only works in Barnes Hut version.\n", argv[i-1]);
184                                         #else
185                                         if (options.theta < 0)
186                                         {
187                                                 fprintf(stderr, "Require a theta value greater or equal to zero (%s %s is invalid).\n", argv[i-1], argv[i]);
188                                                 exit(EXIT_FAILURE);
189                                         }
190                                         
191                                         #endif //BARNS_HUT
192                                         
193                                 }
194                                 else
195                                 {
196                                         fprintf(stderr, "Unrecognised switch %s\n", argv[i]);
197                                         exit(EXIT_FAILURE);
198                                 }
199                                 break;
200
201
202                         default:
203                                 fprintf(stderr, "Unrecognised switch -%c\n", argv[i][1]);
204                                 exit(EXIT_FAILURE);
205                                 break;
206                 }
207                 
208         }
209 }
210
211 /**
212  * @function IntegerArgument
213  * @purpose Helper function to get up to two integers following a argument switch, seperated by ':'
214  * @param i - position in the argument array, will be updated after this function
215  * @param argc - number of arguments
216  * @param argv - argument strings
217  * @param store - pointer to unsigned to store result in
218  * @param store2 - pointer to second integer (set to NULL if there is only one integer)
219  * @returns 1 if store was filled, 2 if both store1 and store2 were filled
220  */
221 unsigned IntegerArgument(unsigned * i, int argc, char ** argv, int * store, int * store2)
222 {
223         if (*i >= argc-1)
224         {
225                 fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
226                 exit(EXIT_FAILURE);
227         }
228
229         char * seperator = strstr(argv[*i+1], ":");
230         if (seperator != NULL)
231         {
232                 if (store2 == NULL)
233                 {
234                         fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
235                         exit(EXIT_FAILURE);     
236                 }
237                 int val = atoi(seperator+1);
238                 if (val <= 0)
239                 {
240                         fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
241                         exit(EXIT_FAILURE);     
242                 }       
243                 *store2 = (unsigned)val;
244
245                 *seperator = '\0';
246         }
247         
248         int val = atoi(argv[*i+1]);
249         if (val <= 0 && strcmp("0", argv[*i+1]) != 0)
250         {
251                 fprintf(stderr,"Supply a positive integer for the -%c switch. %s is invalid.\n", argv[*i][1], argv[*i+1]);
252                 exit(EXIT_FAILURE);
253         }
254         *store = val;
255         *i += 1;
256
257         return (seperator == NULL) ? 1 : 2;
258 }
259 /**
260  * @function FloatArgument
261  * @purpose Helper function to get a float following a argument switch
262  * @param i - position in the argument array, will be updated after this function
263  * @param argc - number of arguments
264  * @param argv - argument strings
265  * @param store - pointer to float to store result in
266  */
267 void FloatArgument(unsigned * i, int argc, char ** argv, float * store)
268 {
269         if (*i >= argc-1)
270         {
271                 fprintf(stderr,"Supply a float for the -%c switch.\n", argv[*i][1]);
272                 exit(EXIT_FAILURE);
273         }
274         *store = atof(argv[*i+1]);
275         if (*store == 0.0 && argv[*i+1][0] != '0')
276         {
277                 fprintf(stderr,"Supply a float for the -%c switch.\n", argv[*i][1]);
278                 exit(EXIT_FAILURE);
279         }
280         *i += 1;
281 }
282
283
284
285 /**
286  * @function Interrupt
287  * @purpose Handle SIGINT signal; quit the program gracefully, so that statistics are still displayed
288  * @param dummy - a dummy
289  */
290
291 void Interrupt(int dummy)
292 {
293         QuitProgram(false);
294 }
295
296

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