chiark / gitweb /
Merge multiprocessor support; fix up Makefile runes
[moebius2.git] / parallel.c
1 /*
2  * Parallel processing
3  */
4
5 #include <pthread.h>
6
7 #include "mgraph.h"
8 #include "parallel.h"
9
10 typedef struct {
11   const struct Vertices *vertices;
12   Computation *separately;
13   void *gendata;
14 } ForAllThreads;
15
16 typedef struct {
17   ForAllThreads *allthreads;
18   int section;
19   void *secdata;
20   pthread_t thread;
21 } PerThread;
22
23 static void *routine(void *thread_v) {
24   PerThread *t= thread_v;
25   ForAllThreads *a= t->allthreads;
26
27   a->separately(a->vertices, t->section, t->secdata, a->gendata);
28
29   return 0;
30 }
31
32 void inparallel(const struct Vertices *vertices,
33                 Computation *separately,
34                 Computation *combine,
35                 size_t secdatasz, void *gendata) {
36   typedef struct { unsigned char secdata[secdatasz]; } SecData;
37
38   ForAllThreads allthreads;
39   SecData secdatas[nsections];
40   PerThread threads[nsections];
41
42   int s, r;
43
44   allthreads.vertices= vertices;
45   allthreads.separately= separately;
46   allthreads.gendata= gendata;
47
48   for (s=0; s<nsections; s++) {
49     threads[s].allthreads= &allthreads;
50     threads[s].section= s;
51     threads[s].secdata= secdatas[s].secdata;
52     r= pthread_create(&threads[s].thread,0,routine,&threads[s]);
53     if (r) diee("pthread_create");
54   }
55
56   for (s=0; s<nsections; s++) {
57     r= pthread_join(threads[s].thread, 0);
58     if (r) diee("pthread_join");
59     combine(vertices, s, threads[s].secdata, gendata);
60   }
61 }