Parallel Programming - Commit before I break things
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / main.c
index 4b0c48a..392c27d 100644 (file)
@@ -9,6 +9,7 @@
 #include <stdio.h>
 #include <time.h>
 #include <signal.h>
+#include <string.h> // For parsing arguments
 
 #include "nbody.h"
 #include "graphics.h"
@@ -20,7 +21,7 @@ Options options; // global variable declared in "nbody.h" - Options passed to pr
 
 // --- Function forward declarations --- //
 void HandleArguments(int argc, char ** argv); //Interprets program arguments and sets up the "options" variable
-void UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store); //Helper function to get switch value for unsigned
+unsigned UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store, unsigned * store2); //Helper function to get switch value for unsigned
 void FloatArgument(unsigned * i, int argc, char ** argv, float * store); //Helper function to get switch value for float
 void DisplayStatistics(); //Called on exit of program, displays information about computation time, steps computed, etc
 void Interrupt(int dummy); // Interrupt handler function, called when SIGINT (Ctrl-C) is sent to program
@@ -41,9 +42,11 @@ int main(int argc, char** argv)
        options.input = NULL;
        options.output = NULL;
        options.num_threads = 0;
+       options.nested_threads = 0;
        options.num_steps = 0;
        options.timeout = 0;
        options.draw_graphics = true;
+       options.pedantic_graphics = false;
        options.print_positions = false;
        options.verbosity = 0;
 
@@ -116,25 +119,49 @@ void HandleArguments(int argc, char ** argv)
                switch (argv[i][1]) //The argument is a switch if we get here
                {
                        case 'n': //Number of threads switch
-                               UnsignedArgument(&i, argc, argv, &(options.num_threads));
+                               UnsignedArgument(&i, argc, argv, &(options.num_threads), &(options.nested_threads));
                                #ifdef SINGLE_THREADED
                                fprintf(stderr, "Warning: -%c switch has no effect in the single-threaded program.\n", argv[i][1]);
-                               options.num_threads = 1;
+                               
                                #endif //SINGLE_THREADED
                                break;
                        case 's': //Number of steps switch
-                               UnsignedArgument(&i, argc, argv, &(options.num_steps));
+                               UnsignedArgument(&i, argc, argv, &(options.num_steps), NULL);
                                break;
                        case 't': //Timeout switch (in seconds)
-                               UnsignedArgument(&i, argc, argv, &(options.timeout));
+                               UnsignedArgument(&i, argc, argv, &(options.timeout), NULL);
                                break;
                        case 'g': //Graphics switch
                                options.draw_graphics = !options.draw_graphics;
                                break;
                        case 'v': //Verbosity switch
-                               UnsignedArgument(&i, argc, argv, &(options.verbosity));
+                               UnsignedArgument(&i, argc, argv, &(options.verbosity), NULL);
                                break;
 
+                       case '-':
+                               if (strcmp(argv[i]+1, "pedantic-graphics"))
+                               {
+                                       options.pedantic_graphics = true;
+                                       #ifdef SINGLE_THREADED
+                                       fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
+                                       #endif //SINGLE_THREADED
+                               }
+                               else if (strcmp(argv[i]+1, "fast-graphics"))
+                               {
+                                       options.pedantic_graphics = false;
+                                       #ifdef SINGLE_THREADED
+                                       fprintf(stderr, "Warning: %s switch has no effect in the single threaded program.\n", argv[i]);
+                                       #endif //SINGLE_THREADED
+
+                               }
+                               else
+                               {
+                                       fprintf(stderr, "Unrecognised switch -%s\n", argv[i]);
+                                       exit(EXIT_FAILURE);
+                               }
+                               break;
+
+
                        default:
                                fprintf(stderr, "Unrecognised switch -%c\n", argv[i][1]);
                                exit(EXIT_FAILURE);
@@ -151,22 +178,46 @@ void HandleArguments(int argc, char ** argv)
  * @param argc - number of arguments
  * @param argv - argument strings
  * @param store - pointer to unsigned to store result in
+ * @param store2 - pointer to second unsigned
+ * @returns 1 if store was filled, 2 if both store1 and store2 were filled
  */
-void UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store)
+unsigned UnsignedArgument(unsigned * i, int argc, char ** argv, unsigned * store, unsigned * store2)
 {
        if (*i >= argc-1)
        {
                fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
                exit(EXIT_FAILURE);
        }
+
+       char * seperator = strstr(argv[*i+1], ":");
+       if (seperator != NULL)
+       {
+               if (store2 == NULL)
+               {
+                       fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
+                       exit(EXIT_FAILURE);     
+               }
+               int val = atoi(seperator+1);
+               if (val <= 0)
+               {
+                       fprintf(stderr,"Supply a positive integer for the -%c switch.\n", argv[*i][1]);
+                       exit(EXIT_FAILURE);     
+               }       
+               *store2 = (unsigned)val;
+
+               *seperator = '\0';
+       }
+       
        int val = atoi(argv[*i+1]);
        if (val <= 0)
        {
                fprintf(stderr,"Supply a positive integer for the -%c switch. %s is invalid.\n", argv[*i][1], argv[*i+1]);
                exit(EXIT_FAILURE);
        }
-       *store = val;
+       *store = (unsigned)val;
        *i += 1;
+
+       return (seperator == NULL) ? 1 : 2;
 }
 /**
  * @function FloatArgument

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