X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=parallel.c;h=f5841eeb21040f7394a833b25d66399cd8bd23e6;hb=4c2fa1edde3eff50648b6782e0b6e63add9b4943;hp=a79a3704732d53db09f8fbb489d89cdf500fff77;hpb=f459beb7f9ad44f4441af774dc21c3ca8136fd08;p=moebius2.git diff --git a/parallel.c b/parallel.c index a79a370..f5841ee 100644 --- a/parallel.c +++ b/parallel.c @@ -2,6 +2,8 @@ * Parallel processing */ +#include "common.h" + #include #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,40 @@ void inparallel(const struct Vertices *vertices, allthreads.separately= separately; allthreads.gendata= gendata; -#if NPROCESSORS != 1 - PerThread threads[nsections]; int s, r; - 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"); } } -#else - separately(vertices, 0, &secdatas[0], gendata); - combine(vertices, 0, &secdatas[0], gendata); -#endif }