Parallel Programming - Finished(?) pthread version
[matches/honours.git] / course / semester2 / pprog / assignment1 / mthread / nbody.c
index 290eeb6..cd0656a 100644 (file)
 #include <time.h>
 #include <string.h>
 #include <stdbool.h>
+#include <stdlib.h>
+#include <unistd.h>
 
 #include "nbody.h"
 
+//Stuff to do with worker threads
+// The worker threads (if used) are spawned by the computation thread, not the main thread
+// The main thread (which handles initialisation, graphics, user input, cleanup and exit) does not need to know about these.
+#if NUM_THREADS > 1
+       
+       pthread_t worker_thread[NUM_THREADS]; //Worker threads responsible for Force and Position updates
+       System sub_system[NUM_THREADS]; //Array of System's used to divide up the main "universe" System for worker threads
+       pthread_mutex_t mutex_workers;
+       pthread_cond_t workers_done_cv;
+       unsigned worker_count;
+#endif 
+
 /**
  * Prints the body on screen
  */
@@ -70,21 +84,28 @@ void Body_Position(Body * a)
 }
 
 /**
- * Main compute function
+ * Compute forces on each object in System s1 due to all bodies in System s2
  */
-void System_Compute(System * s
+void System_Forces(System * s1, System * s2
 {
-//     clock_t start, finish;
-
-//     start = clock();
-
-       for (unsigned i = 0; i < s->N; ++i)
+       //printf("Compute forces for %p - %d bodies...\n", (void*)s1, s1->N);
+       for (unsigned i = 0; i < s1->N; ++i)
        {
-               Body_Force(s->body+i, s);
-               Body_Velocity(s->body+i);
-               Body_Position(s->body+i);
+               Body_Force(s1->body+i, s2);
+               Body_Velocity(s1->body+i);
        }
+       //printf("Compute positions for %p - Done!\n", (void*)s1);
+}
 
+/**
+ * Compute positions of all objects in System
+ */
+void System_Positions(System * s)
+{
+       //printf("Compute positions for %p - %d bodies...\n", (void*)s, s->N);
+       for (unsigned i = 0; i < s->N; ++i)
+               Body_Position(s->body+i);
+       //printf("Compute positions for %p - Done!\n", (void*)s);
 }
 
 
@@ -140,20 +161,137 @@ void System_Init(System * s, char *fileName)
 void Universe_Cleanup()
 {
        free(universe.body);
-       pthread_exit(NULL);
+       
 }
 
 /**
  * Thread - Continuously computes steps for a system of bodies
  */
-void * Compute_Thread(void * s)
+void * Compute_Thread(void * arg)
 {
+
+       System * s = (System*)(arg); //cast argument to a System*
+       // First set up the "sub_system" array
+       #if NUM_THREADS > 1
+               unsigned bodies_per_system = (s->N) / NUM_THREADS;
+               unsigned remainder = (s->N) % NUM_THREADS;
+               for (unsigned i = 0; i < NUM_THREADS; ++i)
+               {
+                       sub_system[i].body = (s->body)+(i*bodies_per_system);
+                       sub_system[i].N = bodies_per_system;
+                       if (i == NUM_THREADS - 1)
+                               sub_system[i].N += remainder;
+               }
+
+               pthread_attr_t attr; //thread attribute for the workers
+               pthread_attr_init(&attr);
+               pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+
+       #endif
        while (true)
        {
-               if (terminate) pthread_exit(NULL);
-               System_Compute((System*)s);
+               
+               if (runstate != RUN) pthread_exit(NULL); //Check whether the main thread wants to exit
+       
+
+       
+               #if NUM_THREADS <= 1
+                       //No worker threads; do everything in this thread
+                       System_Forces(s, s);
+                       System_Positions(s);
+               #else
+
+               
+
+               
+               worker_count = NUM_THREADS; //All threads working
+
+               //Compute forces
+               for (unsigned i = 0; i < NUM_THREADS; ++i)
+               {
+                       if (pthread_create(worker_thread+i, &attr, Force_Thread, (void*)(sub_system+i)) != 0)
+                       {
+                               perror("In compute thread, couldn't create worker thread (force)");
+                               QuitProgram(true);
+                               pthread_exit(NULL);
+                       }       
+               }
+
+
+
+               //Wait for forces to be computed
+               pthread_mutex_lock(&mutex_workers);
+               while (worker_count > 0)
+                       pthread_cond_wait(&workers_done_cv, &mutex_workers);
+               pthread_mutex_unlock(&mutex_workers);
+               
+               worker_count = NUM_THREADS; //All threads working
+
+               //Compute positions
+               for (unsigned i = 0; i < NUM_THREADS; ++i)
+               {
+                       if (pthread_create(worker_thread+i, &attr, Position_Thread, (void*)(sub_system+i)) != 0)
+                       {
+                               perror("In compute thread, couldn't create worker thread (position)");
+                               QuitProgram(true);
+                               pthread_exit(NULL);
+                       }       
+               }
+
+               //Wait for positions to be computed
+               pthread_mutex_lock(&mutex_workers);
+               while (worker_count > 0)
+                       pthread_cond_wait(&workers_done_cv, &mutex_workers);
+               pthread_mutex_unlock(&mutex_workers);           
+
+               
+               #endif
+
        }
 }
 
+void * Force_Thread(void * s)
+{
+       
+       System_Forces((System*)s, &universe);
+       pthread_mutex_lock(&mutex_workers);
+       worker_count -= 1;
+       if (worker_count == 0)
+       {
+               pthread_cond_signal(&workers_done_cv);
+       }
+       pthread_mutex_unlock(&mutex_workers);
+       return NULL;
+}
+
+void * Position_Thread(void * s)
+{
+       
+       System_Positions((System*)s);
+       pthread_mutex_lock(&mutex_workers);
+       worker_count -= 1;
+       if (worker_count == 0)
+       {
+               pthread_cond_signal(&workers_done_cv);
+       }
+       pthread_mutex_unlock(&mutex_workers);
+       return NULL;
+}      
+
+/**
+ * Child threads can call this to signal the main thread to quit the program
+ * The main thread also uses this to tell child threads that the program is quitting
+ *     Note that the function doesn't call exit(), that is done by the main thread
+ */
+void QuitProgram(bool error)
+{
+       
+       pthread_mutex_lock(&mutex_runstate);
+       if (error)
+               runstate = QUIT_ERROR;
+       else
+               runstate = QUIT;
+       pthread_mutex_unlock(&mutex_runstate);
+}
 
 

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