chiark / gitweb /
use barriers rather than recreating threads
authorIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 28 Sep 2008 12:06:13 +0000 (13:06 +0100)
committerIan Jackson <ian@davenant.relativity.greenend.org.uk>
Sun, 28 Sep 2008 12:06:13 +0000 (13:06 +0100)
common.h
energy.c
minimise.h
parallel.c
parallel.h

index 58bc1cc11bb1396e892e827a22352923c75202ab..6414303ad755c8234366e5990e450441f2b2dbd7 100644 (file)
--- a/common.h
+++ b/common.h
@@ -8,6 +8,9 @@
 #ifndef _GNU_SOURCE
 #define _GNU_SOURCE
 #endif
+#ifndef _XOPEN_SOURCE
+#define _XOPEN_SOURCE 1000
+#endif
 
 #include <sys/types.h>
 #include <sys/stat.h>
index ae592cf4817fb6895f40cf2454cda90b190153bd..f6a4c290c5883d2c04ef0b9150717cc991f409b6 100644 (file)
--- a/energy.c
+++ b/energy.c
@@ -16,18 +16,23 @@ static void addcost(double *energy, double tweight, double tcost, int pr);
 /*---------- main energy computation, weights, etc. ----------*/
 
 typedef double CostComputation(const Vertices vertices, int section);
+typedef void PreComputation(const Vertices vertices, int section);
 
 typedef struct {
   double weight;
   CostComputation *fn;
 } CostContribution;
 
-static const CostContribution costs[]= {
-#define PRECOMP(compute) { 0,(compute) },
+#define NPRECOMPS ((sizeof(precomps)/sizeof(precomps[0])))
+#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
 #define COST(weight, compute) { (weight),(compute) },
 
-  PRECOMP(compute_edge_lengths)
-  PRECOMP(compute_vertex_areas)
+static PreComputation *const precomps[]= {
+  compute_edge_lengths,
+  compute_vertex_areas
+};
+
+static const CostContribution costs[]= {
 
 #if XBITS==3
 #define STOP_EPSILON 1e-6
@@ -53,9 +58,8 @@ static const CostContribution costs[]= {
                   #define EDGE_ANGLE_COST_CIRCCIRCRAT (0.5/1.3)
     COST(  1e18,   noncircular_rim_cost)
 #endif
-};
 
-#define NCOSTS ((sizeof(costs)/sizeof(costs[0])))
+};
 
 const double edge_angle_cost_circcircrat= EDGE_ANGLE_COST_CIRCCIRCRAT;
 
@@ -65,51 +69,51 @@ void energy_init(void) {
 
 /*---------- energy computation machinery ----------*/
 
-typedef struct {
-  double total;
-  const CostContribution *cc;
-} CostComputationData;
-
 void compute_energy_separately(const struct Vertices *vs,
-                        int section, void *energy_v, void *ccd_v) {
-  CostComputationData *ccd= ccd_v;
-  double *energy= energy_v;
-  *energy= ccd->cc->fn(vs->a, section);
+                        int section, void *energies_v, void *totals_v) {
+  double *energies= energies_v;
+  int ci;
+  
+  for (ci=0; ci<NPRECOMPS; ci++) {
+    costs[ci].fn(vs->a, section);
+    inparallel_barrier();
+  }
+  for (ci=0; ci<NCOSTS; ci++)
+    energies[ci]= costs[ci].fn(vs->a, section);
 }
 
 void compute_energy_combine(const struct Vertices *vertices,
-                        int section, void *energy_v, void *ccd_v) {
-  CostComputationData *ccd= ccd_v;
-  double *energy= energy_v;
-  ccd->total += *energy;
+                        int section, void *energies_v, void *totals_v) {
+  int ci;
+  double *energies= energies_v;
+  double *totals= totals_v;
+
+  for (ci=0; ci<NCOSTS; ci++)
+    totals[ci] += energies[ci];
 }
 
 double compute_energy(const struct Vertices *vs) {
   static int bests_unprinted;
 
-  double energy;
+  double totals[NCOSTS], energy;
   int ci, printing;
-  CostComputationData ccd;
 
   printing= printing_check(pr_cost,0);
 
   if (printing) printf("%15lld c>e |", evaluations);
 
-  energy= 0;
+  for (ci=0; ci<NCOSTS; ci++)
+    totals[ci]= 0;
 
-  for (ci=0; ci<NCOSTS; ci++) {
-    ccd.total= 0;
-    ccd.cc= &costs[ci];
-    
-    inparallel(vs,
-              compute_energy_separately,
-              compute_energy_combine,
-              sizeof(energy),
-              &ccd);
-
-    if (ccd.cc->weight != 0)
-      addcost(&energy, costs[ci].weight, ccd.total, printing);
-  }
+  inparallel(vs,
+            compute_energy_separately,
+            compute_energy_combine,
+            sizeof(totals) /* really, size of energies */,
+            totals);
+
+  energy= 0;
+  for (ci=0; ci<NCOSTS; ci++)
+    addcost(&energy, costs[ci].weight, totals[ci], printing);
 
   if (printing) printf("| total %# e |", energy);
 
@@ -149,16 +153,14 @@ static void addcost(double *energy, double tweight, double tcost, int pr) {
 
 /*---------- Precomputations ----------*/
 
-double compute_edge_lengths(const Vertices vertices, int section) {
+void compute_edge_lengths(const Vertices vertices, int section) {
   int v1,e,v2;
 
   FOR_EDGE(v1,e,v2, OUTER)
     edge_lengths[v1][e]= hypotD(vertices[v1],vertices[v2]);
-
-  return 0;
 }
 
-double compute_vertex_areas(const Vertices vertices, int section) {
+void compute_vertex_areas(const Vertices vertices, int section) {
   int v0,v1,v2, e1,e2;
 //  int k;
 
@@ -186,8 +188,6 @@ double compute_vertex_areas(const Vertices vertices, int section) {
     vertex_areas[v0]= total / count;
     vertex_mean_edge_lengths[v0]= edges_total / count;
   }
-
-  return 0;
 }
 
 /*---------- Edgewise vertex displacement ----------*/
index 29e320795dc587e7fcebecf7b9690f7ff1f3a194..c2e13e7b40fef83169495c5b2754fc91333c90d1 100644 (file)
@@ -13,10 +13,8 @@ void energy_init(void);
 double graph_layout_cost(const Vertices v, int section);
 void graph_layout_prepare();
 
-double compute_vertex_areas(const Vertices vertices, int section);
-double compute_edge_lengths(const Vertices vertices, int section);
- /* these don't actually return anything interesting - they're just
-  * like this so they fit into the parallel/sequential scheme */
+void compute_vertex_areas(const Vertices vertices, int section);
+void compute_edge_lengths(const Vertices vertices, int section);
 
 extern double vertex_areas[N], vertex_mean_edge_lengths[N], edge_lengths[N][V6];
 
index a79a3704732d53db09f8fbb489d89cdf500fff77..badc75d79b6f4a42bd4ed40b3dcaf69738bb8cc1 100644 (file)
@@ -2,6 +2,8 @@
  * Parallel processing
  */
 
+#include "common.h"
+
 #include <pthread.h>
 
 #include "mgraph.h"
@@ -20,16 +22,22 @@ typedef struct {
   pthread_t thread;
 } PerThread;
 
-#if NPROCESSORS != 1
 static void *routine(void *thread_v) {
   PerThread *t= thread_v;
-  ForAllThreads *a= t->allthreads;
 
-  a->separately(a->vertices, t->section, t->secdata, a->gendata);
+  for (;;) {
+    inparallel_barrier(); /* wait for work to do */
+    ForAllThreads *a= t->allthreads;
+    a->separately(a->vertices, t->section, t->secdata, a->gendata);
+    inparallel_barrier(); /* synchronise for completion */
+  }
 
   return 0;
 }
-#endif
+
+static int threads_started;
+static pthread_barrier_t threads_barrier;
+static PerThread threads[NSECTIONS-1];
 
 void inparallel(const struct Vertices *vertices,
                Computation *separately,
@@ -44,25 +52,37 @@ void inparallel(const struct Vertices *vertices,
   allthreads.separately= separately;
   allthreads.gendata= gendata;
 
-#if NPROCESSORS != 1
-  PerThread threads[nsections];
   int s, r;
 
-  for (s=0; s<nsections; s++) {
+  if (!threads_started) {
+    r= pthread_barrier_init(&threads_barrier, 0, NSECTIONS);
+    if (r) { errno=r; diee("pthread_barrier_init"); }
+
+    for (s=0; s<NSECTIONS-1; s++) {
+      r= pthread_create(&threads[s].thread,0,routine,&threads[s]);
+      if (r) { errno=r; diee("pthread_create"); }
+    }
+  }    
+
+  for (s=0; s<NSECTIONS-1; s++) {
     threads[s].allthreads= &allthreads;
     threads[s].section= s;
     threads[s].secdata= secdatas[s].secdata;
-    r= pthread_create(&threads[s].thread,0,routine,&threads[s]);
-    if (r) diee("pthread_create");
   }
 
-  for (s=0; s<nsections; s++) {
-    r= pthread_join(threads[s].thread, 0);
-    if (r) diee("pthread_join");
-    combine(vertices, s, threads[s].secdata, gendata);
-  }
-#else
-  separately(vertices, 0, &secdatas[0], gendata);
-  combine(vertices, 0, &secdatas[0], gendata);
-#endif
+  inparallel_barrier(); /* announce more work to do */
+
+  separately(vertices, NSECTIONS-1, &secdatas[NSECTIONS-1], gendata);
+
+  inparallel_barrier(); /* synchronise for completion */
+
+  for (s=0; s<nsections; s++)
+    combine(vertices, s, &secdatas[s].secdata, gendata);
+}
+
+void inparallel_barrier(void) {
+  int r;
+  r= pthread_barrier_wait(&threads_barrier);
+  if (r && r!=PTHREAD_BARRIER_SERIAL_THREAD)
+    { errno=r; diee("pthread_barrier_wait"); }
 }
index 19616e43f64b9aeba95b4dc21e973ec571eab7a9..a8a748fdce036e0ac5ce5cb3bdbe315512e75357 100644 (file)
@@ -29,7 +29,7 @@
  *   - function must not modify anything other than
  *      its return value (for cost computation functions, COST()) or
  *      its designated output (for precomputation functions, PRECOMP())
- *        (which latter it may not read)
+ *       and in the latter case it may not read other parts of its output
  *   - function must of course be reentrant
  */
 
@@ -49,6 +49,9 @@ void inparallel(const struct Vertices *vertices,
    * secdatasz, passed as secdata, uninitialised on entry.
    * After all the copies have finished, `combine' is invoked
    * nsections times sequentially, with the same sets of arguments.
+   * inparallel is NOT itself reentrant.
    */
 
+void inparallel_barrier(void);
+
 #endif /*PARALLEL_H*/