From: Ian Jackson Date: Sun, 28 Sep 2008 12:06:13 +0000 (+0100) Subject: use barriers rather than recreating threads X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=c3fc916a828edf56f715a88e6a20f450c8b40fbd;p=moebius2.git use barriers rather than recreating threads --- diff --git a/common.h b/common.h index 58bc1cc..6414303 100644 --- 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 #include diff --git a/energy.c b/energy.c index ae592cf..f6a4c29 100644 --- 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; cia, section); + inparallel_barrier(); + } + for (ci=0; cia, 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; cie |", evaluations); - energy= 0; + for (ci=0; ciweight != 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 #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