Parallel Programming - Finished OpenMP
[matches/honours.git] / course / semester2 / pprog / assignment1 / single-thread / nbody.c
index ee510ed..30e5c76 100644 (file)
@@ -37,11 +37,9 @@ inline void Body_Print(Body * a, FILE * out)
  * @param a - The Body
  * @param b - The System
  */
-inline void Body_Force(Body * a, System * s) 
+inline void Body_Forces(Body * a, System * s) 
 {
-       double distance;
-       double con;
-       double gd;
+
 
        for (unsigned i = 0; i < DIMENSIONS; ++i) //Set Force to zero
                a->F[i] = 0;
@@ -52,18 +50,32 @@ inline void Body_Force(Body * a, System * s)
                if (b == a)
                        continue; //Otherwise we would have infinite force
                
-               //Calculate distance between a and b
-               distance = 0.0;
-               for (unsigned i = 0; i < DIMENSIONS; ++i)
-                       distance += square(b->x[i] - a->x[i]);
-               distance = sqrt(distance);
-               con = G * a->mass * b->mass / square(distance);
-               gd = con / distance;
-               for (unsigned i = 0; i < DIMENSIONS; ++i) //Add force from b to a's net force
-                       a->F[i] += gd * (b->x[i] - a->x[i]);
+               Body_Force(a, b);
        }
 }
 
+/**
+ * @function Body_Force
+ * @purpose Compute force on body a due to body b
+ * @param a - Body a
+ * @param b - Body b
+ */
+inline void Body_Force(Body * a, Body * b)
+{
+       double distance;
+       double con;
+       double gd;
+       //Calculate distance between a and b
+       distance = 0.0;
+       for (unsigned i = 0; i < DIMENSIONS; ++i)
+               distance += square(b->x[i] - a->x[i]);
+       distance = sqrt(distance);
+       con = G * a->mass * b->mass / square(distance);
+       gd = con / distance;
+       for (unsigned i = 0; i < DIMENSIONS; ++i) //Add force from b to a's net force
+               a->F[i] += gd * (b->x[i] - a->x[i]);
+}
+
 /**
  * @function Body_Velocity
  * @purpose Compute velocity of a body
@@ -110,7 +122,7 @@ inline void System_Forces(System * s1, System * s2)
        //printf("Compute forces for %p - %d bodies...\n", (void*)s1, s1->N);
        for (unsigned i = 0; i < s1->N; ++i)
        {
-               Body_Force(s1->body+i, s2);
+               Body_Forces(s1->body+i, s2);
                Body_Velocity(s1->body+i);
        }
        //printf("Compute positions for %p - Done!\n", (void*)s1);
@@ -262,3 +274,35 @@ inline bool ExitCondition(void)
        //fprintf(stderr,"runstate %d\n timeout %d\n steps %d\n", (int)(runstate != RUN), (int)(options.timeout > 0.00 && ((unsigned)(time(NULL) - options.start_time.tv_sec) >= options.timeout)), (int)(options.num_steps > 0 && universe.steps > options.num_steps));
        return result;
 }
+
+/**
+ * @function Split_System
+ * @purpose Helper to divide one system into an array of systems
+ *     Each sub system will have N = (s->N / n) bodies in it
+ * @param s - The original system (typically &universe)
+ * @param n - The number of sub systems in the array
+ *
+ * WARNING: It is the caller's responsibility to free() the returned array
+ */
+System * Split_System(System * s, unsigned n)
+{
+       System * result = (System*)(calloc(n, sizeof(System)));
+       if (result == NULL)
+       {
+               perror("Couldn't create array of sub systems");
+               exit(EXIT_FAILURE);
+       }
+
+       unsigned n_per_system = (s->N) / n;
+       unsigned remainder = (s->N) % n;
+
+       for (unsigned i = 0; i < n; ++i)        
+       {
+               result[i].N = n_per_system;
+               if (i == n-1)
+                       result[i].N += remainder;
+               result[i].body = (s->body) + (n_per_system * i);
+               result[i].steps = 0;
+       }
+       return result;
+}

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