X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?p=moebius2.git;a=blobdiff_plain;f=parallel.c;h=f5841eeb21040f7394a833b25d66399cd8bd23e6;hp=3c94607b845915fa06f04d3c09a1013a3b3d244a;hb=HEAD;hpb=c9f085069bc4e555689dffedbfa08af250772f2a diff --git a/parallel.c b/parallel.c index 3c94607..f5841ee 100644 --- a/parallel.c +++ b/parallel.c @@ -2,6 +2,8 @@ * Parallel processing */ +#include "common.h" + #include #include "mgraph.h" @@ -22,13 +24,21 @@ typedef struct { 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; } +static int threads_started; +static pthread_barrier_t threads_barrier; +static PerThread threads[NSECTIONS-1]; + void inparallel(const struct Vertices *vertices, Computation *separately, Computation *combine, @@ -37,25 +47,45 @@ void inparallel(const struct Vertices *vertices, ForAllThreads allthreads; SecData secdatas[nsections]; - PerThread threads[nsections]; - - int s, r; allthreads.vertices= vertices; allthreads.separately= separately; allthreads.gendata= gendata; - for (s=0; s1 && !threads_started) { + r= pthread_barrier_init(&threads_barrier, 0, NSECTIONS); + if (r) { errno=r; diee("pthread_barrier_init"); } + + for (s=0; s1) { + int r; + r= pthread_barrier_wait(&threads_barrier); + if (r && r!=PTHREAD_BARRIER_SERIAL_THREAD) + { errno=r; diee("pthread_barrier_wait"); } } }