Parallel Programming - Stuff happened
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / nbody.c
index 7504928..301cd8b 100644 (file)
@@ -18,7 +18,13 @@ System * sub_system = NULL; //Array of Systems used to divide up the main "unive
 
 pthread_mutex_t mutex_runstate; // Mutex around the runstate
 
+#ifdef PERSISTENT_THREADS
+Barrier force_barrier; // I laughed at this variable name. A bit sad really.
+Barrier position_barrier;
+#else
 Barrier worker_barrier;
+#endif //PERSISTENT_THREADS
+
 Barrier graphics_barrier;
 
 
@@ -47,6 +53,10 @@ void * Compute_Thread(void * arg)
                options.num_threads = s->N;
        }
        
+       pthread_attr_t attr; //thread attribute for the workers. 
+       pthread_attr_init(&attr);
+       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); //Needs to be detached, so that memory can be reused.
+       
        if (options.num_threads > 1) // Allocate worker threads and sub systems, as long as there would be more than 1
        {
                worker_thread = (pthread_t*)(calloc(options.num_threads, sizeof(pthread_t)));
@@ -75,17 +85,39 @@ void * Compute_Thread(void * arg)
                        if (i == options.num_threads - 1)
                                sub_system[i].N += remainder; // The last thread gets the remainder
 
+                       #ifdef PERSISTENT_THREADS
+                       if (pthread_create(worker_thread+i, & attr, Worker_Thread, (void*)(sub_system+i)) != 0)
+                       {
+                               perror("In compute thread, couldn't create worker thread");
+                               QuitProgram(true);
+                               pthread_exit(NULL);
+                       }
+                       #endif //PERSISTENT_THREADS
+       
                }
-       }
-
 
-       pthread_attr_t attr; //thread attribute for the workers. 
-       pthread_attr_init(&attr);
-       pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); //Needs to be detached, so that memory can be reused.
+               
+       }
+       #ifdef PERSISTENT_THREADS
+       else
+       {
+               while (!ExitCondition())
+               {
+                       if (options.verbosity != 0 && universe.steps % options.verbosity == 1)
+                       {
+                               DisplayStatistics();
+                       }
 
-       
-       
+                       // Just do everything in this thread 
+                       System_Forces(s, s);
+                       System_Positions(s);
+                       continue;
+               }
+               QuitProgram(false);
+               pthread_exit(NULL);
+       }
 
+       #else
        // The main computation loop
        while (true)
        {
@@ -110,6 +142,8 @@ void * Compute_Thread(void * arg)
                        continue;
                }
 
+               
+
                //Compute forces
                for (unsigned i = 0; i < options.num_threads; ++i)
                {
@@ -154,8 +188,13 @@ void * Compute_Thread(void * arg)
                if (options.draw_graphics && options.pedantic_graphics)
                        Barrier_Leave(&graphics_barrier);
 
+               
+
 
        }
+       #endif //PERSISTENT_THREADS
+       
+       return NULL;
 }
 
 /**
@@ -166,10 +205,19 @@ void * Compute_Thread(void * arg)
  */
 void BeforeDraw()
 {
+       if (options.verbosity != 0 && universe.steps % options.verbosity == 0)
+               DisplayStatistics();
+       //printf("BEFORE DRAW\n");
        if (!options.pedantic_graphics)
                return;
+       #ifdef PERSISTENT_THREADS
+       Barrier_Wait(&position_barrier);
+       
+       #else
        Barrier_Wait(&graphics_barrier);
-       Barrier_Enter(&graphics_barrier);
+       #endif //PERSISTENT_THREADS
+
+       Barrier_Enter(&graphics_barrier);       
 }
 
 /**
@@ -181,11 +229,54 @@ void BeforeDraw()
  */
 void AfterDraw()
 {
+       universe.steps += 1;
        if (!options.pedantic_graphics)
                return;
        Barrier_Leave(&graphics_barrier);
+       
 }
 
+#ifdef PERSISTENT_THREADS
+
+/**
+ * @function Worker_Thread
+ * @purpose Thread - Calculate stuff
+ */
+void * Worker_Thread(void * arg)
+{
+       System * s = (System*)(arg);
+       while (!ExitCondition())
+       {
+               
+               Barrier_Enter(&force_barrier);
+
+               System_Forces(s, &universe);
+               
+               Barrier_Leave(&force_barrier);
+
+               //printf("Computed forces for %p\n", arg);
+               Barrier_Wait(&force_barrier);
+               //printf("Computed ALL forces\n");
+
+               if (options.draw_graphics && options.pedantic_graphics)
+                       Barrier_Wait(&graphics_barrier);
+
+               Barrier_Enter(&position_barrier);
+               
+               System_Positions(s);
+
+               Barrier_Leave(&position_barrier);
+               //printf("Computed positions for %p\n", arg);
+               Barrier_Wait(&position_barrier);
+               //printf("Computed ALL positions\n");
+       }
+       QuitProgram(false);
+       pthread_exit(NULL);
+}
+
+
+#else
+
 /**
  * @function Force_Thread
  * @purpose Thread - Calculates the forces on objects in a System
@@ -215,6 +306,8 @@ void * Position_Thread(void * s)
        return NULL;
 }      
 
+#endif //PERSISTENT_THREADS
+
 /**
  * @function QuitProgram
  * @purpose This function can either be called by the main thread in order to signal other threads
@@ -262,21 +355,34 @@ void Simulation_Run(int argc, char ** argv)
 {
        atexit(Thread_Cleanup);
 
+       #ifdef PERSISTENT_THREADS
+       Barrier_Init(&force_barrier);
+       Barrier_Init(&position_barrier);
+       #else
        Barrier_Init(&worker_barrier);
+       #endif //PERSISTENT_THREADS
        Barrier_Init(&graphics_barrier);
 
+       
        if (options.draw_graphics)
        {
                // The graphics are enabled, so create a thread to do computations
                // Graphics are done in the main loop
+               //printf("Graphics are enabled\n");
+               #ifdef PERSISTENT_THREADS
+               Compute_Thread((void*)(&universe));
+               #else
                if (pthread_create(&compute_thread, NULL, Compute_Thread, (void*)&universe) != 0)
                {
                        perror("Error creating compute thread");
                        exit(EXIT_FAILURE);
                }
+               #endif //PERSISTENT_THREADS
+               //printf("Run compute thread\n");
                Graphics_Run(argc, argv);
        }
        else
+       
                Compute_Thread((void*)(&universe)); // Graphics are disabled, so do computations in the main thread
 }
 

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