Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/lib/parallel-target/main.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/lib/parallel-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1216 - (view) (download) (as text)

1 : jhr 1198 /*! \file main.c
2 :     *
3 :     * \author John Reppy
4 :     */
5 :    
6 :     /*
7 :     * COPYRIGHT (c) 2011 The Diderot Project (http://diderot-language.cs.uchicago.edu)
8 :     * All rights reserved.
9 :     */
10 :    
11 :     #include <string.h>
12 :     #include <stdio.h>
13 :     #include <assert.h>
14 :     #include <Diderot/diderot.h>
15 :     #include <pthread.h>
16 :    
17 : jhr 1215 // #ifdef HAVE_BUILTIN_ATOMIC_OPS
18 :     // STATIC_INLINE uint32_t AtomicInc (uint32_t *x)
19 :     // {
20 :     // return __sync_add_and_fetch(x, 1);
21 :     // }
22 :     // STATIC_INLINE uint32_t AtomicDec (uint32_t *x)
23 :     // {
24 :     // return __sync_sub_and_fetch(x, 1);
25 :     // }
26 :     // #else
27 :     // # error atomic operations not supported
28 :     // #endif
29 : jhr 1209
30 : jhr 1198 // The number of strands a worker will take for processing at one time
31 : jhr 1214 #define BLOCK_SIZE 256
32 : jhr 1198
33 :     struct struct_world {
34 : jhr 1214 bool isArray; // is the initialization an array or collection?
35 :     uint32_t nDims; // depth of iteration nesting
36 :     int32_t *base; // nDims array of base indices
37 :     uint32_t *size; // nDims array of iteration sizes
38 :     uint32_t numStrands; // number of strands in the world
39 :     void **inState;
40 :     void **outState;
41 :     uint8_t *status; // array of strand status flags
42 :     uint32_t numWorkers; // number of worker threads
43 :     uint32_t nSteps; // number of super steps
44 :     // synchronization state
45 :     uint32_t nextStrand __attribute__((aligned(64))); // index of next strand to update
46 :     uint32_t numActive __attribute__((aligned(64))); // # active strands
47 :     uint32_t numAvail __attribute__((aligned(64))); // # unevaluated strands
48 :     uint32_t numIdle __attribute__((aligned(64))); // # idle workers
49 :     pthread_mutex_t lock; // big lock to protect wrld from multiple accesses
50 :     pthread_cond_t barrier; // workers wait on this when they have no work
51 :     pthread_cond_t mainWait; // used to signal main when the workers have finished
52 : jhr 1198 };
53 :    
54 : jhr 1214 typedef struct {
55 :     int id;
56 :     Diderot_World_t *wrld;
57 :     } WorkerArg_t;
58 : jhr 1198
59 :     /* Function which processes active strands. */
60 : jhr 1214 static void *Worker (void *arg)
61 : jhr 1198 {
62 : jhr 1214 WorkerArg_t *myArg = (WorkerArg_t *)arg;
63 :     Diderot_World_t *wrld = myArg->wrld;
64 :     bool existsStabilizing;
65 : jhr 1198
66 :     while (true) {
67 : jhr 1214 // barrier synchronization at start of super step
68 :     pthread_mutex_lock (&wrld->lock);
69 :     if (wrld->numIdle+1 < wrld->numWorkers) {
70 :     wrld->numIdle++;
71 :     pthread_cond_wait (&wrld->barrier, &wrld->lock);
72 : jhr 1198 }
73 : jhr 1214 else {
74 :     // all other workers are idle, so we can proceed after some initialization
75 :     wrld->numIdle = 0;
76 : jhr 1215 wrld->numAvail = wrld->numStrands; // includes inactive strands
77 : jhr 1214 wrld->nextStrand = 0;
78 :     // swap in and out
79 :     void **tmp = wrld->inState;
80 :     wrld->inState = wrld->outState;
81 :     wrld->outState = tmp;
82 :     pthread_cond_broadcast (&wrld->barrier);
83 : jhr 1198 }
84 : jhr 1214 pthread_mutex_unlock (&wrld->lock);
85 : jhr 1198
86 : jhr 1214 // if there are no active strands left, then we're done
87 :     if (wrld->numActive == 0) {
88 :     pthread_cond_signal (&wrld->mainWait);
89 :     pthread_exit (0);
90 : jhr 1198 }
91 :    
92 : jhr 1214 // iterate until there is no more work to do
93 :     int blkStart, blkSize;
94 :     existsStabilizing = false;
95 : jhr 1215 int numDead = 0;
96 : jhr 1214 do {
97 :     // grab some work
98 :     pthread_mutex_lock (&wrld->lock);
99 : jhr 1215 blkStart = wrld->nextStrand;
100 :     blkSize = (wrld->numAvail >= BLOCK_SIZE) ? BLOCK_SIZE : wrld->numAvail;
101 :     wrld->numAvail -= blkSize;
102 :     wrld->nextStrand += blkSize;
103 : jhr 1214 pthread_mutex_unlock (&wrld->lock);
104 :     // update the strands
105 :     for (int i = blkStart; i < blkStart+blkSize; i++) {
106 :     if (! wrld->status[i]) {
107 :     StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
108 :     switch (sts) {
109 :     case DIDEROT_STABILIZE:
110 :     wrld->status[i] = DIDEROT_STABILIZE;
111 :     existsStabilizing = true;
112 :     break;
113 :     case DIDEROT_DIE:
114 :     wrld->status[i] = DIDEROT_DIE;
115 : jhr 1215 numDead++;
116 : jhr 1214 break;
117 :     default:
118 :     break;
119 :     }
120 :     }
121 : jhr 1215 else {
122 :     assert ((wrld->status[i] == DIDEROT_STABLE) || (wrld->status[i] == DIDEROT_DIE));
123 :     }
124 : jhr 1214 }
125 :     } while (blkSize > 0);
126 : jhr 1198
127 : jhr 1214 // barrier synchronization
128 :     pthread_mutex_lock (&wrld->lock);
129 : jhr 1215 wrld->numActive -= numDead;
130 : jhr 1214 if (wrld->numIdle+1 < wrld->numWorkers) {
131 :     wrld->numIdle++;
132 :     pthread_cond_wait (&wrld->barrier, &wrld->lock);
133 :     }
134 :     else {
135 :     // all other workers are idle, so we can proceed
136 :     wrld->numIdle = 0;
137 :     pthread_cond_broadcast (&wrld->barrier);
138 : jhr 1215 wrld->nSteps++;
139 : jhr 1214 }
140 :     pthread_mutex_unlock (&wrld->lock);
141 :    
142 :     /**** If there is a global computation phase, it goes here ****/
143 :    
144 :     // stabilize any threads that need stabilization. Each worker is responsible for
145 :     // a contiguous region of the strands
146 :     if (existsStabilizing) {
147 :     int nStrandsPerWorker = wrld->numStrands / wrld->numWorkers;
148 :     int start = myArg->id * nStrandsPerWorker;
149 :     int limit = start + nStrandsPerWorker;
150 :     if (limit > wrld->numStrands) limit = wrld->numStrands;
151 : jhr 1215 int numStabilized = 0;
152 : jhr 1214 for (int i = start; i < limit; i++) {
153 :     if (wrld->status[i] == DIDEROT_STABILIZE) {
154 :     // copy out to in so that both copies are the stable state
155 :     memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);
156 :     wrld->status[i] = DIDEROT_STABLE;
157 : jhr 1215 numStabilized++;
158 : jhr 1198 }
159 :     }
160 : jhr 1215 // adjust the numActive count
161 : jhr 1216 #if defined(HAVE_BUILTIN_ATOMIC_OPS)
162 :     __sync_fetch_and_sub(&wrld->numActive, numStabilized);
163 :     #else
164 : jhr 1215 pthread_mutex_lock (&wrld->lock);
165 :     wrld->numActive -= numStabilized;
166 :     pthread_mutex_unlock (&wrld->lock);
167 : jhr 1216 #endif
168 : jhr 1198 }
169 : jhr 1215 } // end while(true)
170 : jhr 1214
171 : jhr 1198 }
172 :    
173 :    
174 :     int main (int argc, const char **argv)
175 :     {
176 : jhr 1214 fprintf (stderr, "initializing globals ...\n");
177 :     Diderot_InitGlobals (argc, argv);
178 : jhr 1198
179 : jhr 1214 fprintf (stderr, "initializing strands ...\n");
180 : jhr 1198 Diderot_World_t *wrld = Diderot_Initially ();
181 :     for (int i = 0; i < wrld->numStrands; i++) {
182 :     // hack to make the invariant part of the state the same in both copies
183 :     memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
184 :     }
185 :    
186 :     CPUInfo_t cpuInfo;
187 :     if (! GetNumCPUs (&cpuInfo)) {
188 :     fprintf(stderr, "unable to get number of processors\n");
189 :     exit (1);
190 :     }
191 : jhr 1214
192 :     // Start worker threads
193 : jhr 1198 int nWorkers = cpuInfo.numHWCores;
194 : jhr 1214 WorkerArg_t *args = (WorkerArg_t *) malloc (nWorkers * sizeof(WorkerArg_t));
195 : jhr 1209 printf ("initializing %d workers ...\n", nWorkers);
196 : jhr 1214 wrld->numWorkers = nWorkers;
197 :     wrld->numIdle = 0;
198 : jhr 1198 for (int i = 0; i < nWorkers; i++) {
199 : jhr 1214 pthread_t pid;
200 :     args[i].wrld = wrld;
201 :     args[i].id = i;
202 :     if (pthread_create (&pid, NULL, Worker, (void *)&(args[i])) != 0) {
203 :     fprintf (stderr, "unable to create worker thread\n");
204 :     exit (1);
205 :     }
206 :     pthread_detach (pid);
207 : jhr 1198 }
208 :    
209 : jhr 1214 // wait for the computation to finish
210 :     pthread_mutex_lock (&wrld->lock);
211 :     pthread_cond_wait (&wrld->mainWait, &wrld->lock);
212 :     pthread_mutex_unlock (&wrld->lock);
213 : jhr 1198
214 : jhr 1214 fprintf (stderr, "done: %d steps\n", wrld->nSteps);
215 : jhr 1198 // here we have the final state of all of the strands in the "in" buffer
216 :     FILE *outS = fopen("mip.txt", "w");
217 :     if (outS == NULL) {
218 :     fprintf (stderr, "Cannot open output file\n");
219 :     exit (8);
220 :     }
221 :    
222 :     for (int i = 0; i < wrld->numStrands; i++) {
223 :     if (wrld->status[i] == DIDEROT_STABLE)
224 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
225 :     }
226 :     fclose (outS);
227 :    
228 : jhr 1214 Diderot_Shutdown (wrld);
229 :    
230 : jhr 1198 return 0;
231 :    
232 :     }
233 :    
234 :    
235 :     // this should be the part of the scheduler
236 :     void *Diderot_AllocStrand (Strand_t *strand)
237 :     {
238 :     return malloc(strand->stateSzb);
239 :     }
240 :    
241 :     // block allocation of an initial collection of strands
242 :     Diderot_World_t *Diderot_AllocInitially (
243 : jhr 1209 Strand_t *strand, // the type of strands being allocated
244 : jhr 1198 bool isArray, // is the initialization an array or collection?
245 :     uint32_t nDims, // depth of iteration nesting
246 :     int32_t *base, // nDims array of base indices
247 :     uint32_t *size) // nDims array of iteration sizes
248 :     {
249 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
250 :     if (wrld == 0) {
251 :     fprintf (stderr, "unable to allocate world\n");
252 :     exit (1);
253 :     }
254 :    
255 :     wrld->isArray = isArray;
256 :     wrld->nDims = nDims;
257 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
258 :     wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
259 :     size_t numStrands = 1;
260 :     for (int i = 0; i < wrld->nDims; i++) {
261 :     numStrands *= size[i];
262 :     wrld->base[i] = base[i];
263 :     wrld->size[i] = size[i];
264 :     }
265 :    
266 : jhr 1214 fprintf(stderr, "AllocInitially: %d", size[0]);
267 :     for (int i = 1; i < nDims; i++)
268 :     fprintf(stderr, " x %d", size[i]);
269 :     fprintf(stderr, "\n");
270 : jhr 1198
271 :     // allocate the strand state pointers
272 :     wrld->numStrands = numStrands;
273 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
274 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
275 :     wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
276 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
277 :     fprintf (stderr, "unable to allocate strand states\n");
278 :     exit (1);
279 :     }
280 : jhr 1214 wrld->numActive = wrld->numStrands;
281 :     wrld->nSteps = 0;
282 :     wrld->numWorkers = 0;
283 : jhr 1198
284 :     // initialize strand state pointers etc.
285 :     for (size_t i = 0; i < numStrands; i++) {
286 :     wrld->inState[i] = Diderot_AllocStrand (strand);
287 :     wrld->outState[i] = Diderot_AllocStrand (strand);
288 :     wrld->status[i] = DIDEROT_ACTIVE;
289 :     }
290 :    
291 : jhr 1214 pthread_mutex_init (&wrld->lock, NULL);
292 :     pthread_cond_init (&wrld->barrier, NULL);
293 :     pthread_cond_init (&wrld->mainWait, NULL);
294 : jhr 1198
295 :     return wrld;
296 :    
297 :     }
298 :    
299 :     // get strand state pointers
300 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
301 :     {
302 :     assert (i < wrld->numStrands);
303 :     return wrld->inState[i];
304 :     }
305 :    
306 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
307 :     {
308 :     assert (i < wrld->numStrands);
309 :     return wrld->outState[i];
310 :     }
311 :    
312 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
313 :     {
314 :     assert (i < wrld->numStrands);
315 :     return !wrld->status[i];
316 :     }

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0