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 1277 - (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 1268 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 : jhr 1214 // synchronization state
45 : jhr 1268 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 : jhr 1268 int id;
56 :     Diderot_World_t *wrld;
57 : jhr 1277 } WorkerArg_t __attribute__((aligned(64)));
58 : jhr 1198
59 : jhr 1262 // NOTE: we probably should put this in a file that supports runtime printing
60 : jhr 1268 static bool VerboseFlg = false;
61 : jhr 1276 static bool TimingFlg = false;
62 : jhr 1262
63 : jhr 1198 /* Function which processes active strands. */
64 : jhr 1214 static void *Worker (void *arg)
65 : jhr 1198 {
66 : jhr 1268 WorkerArg_t *myArg = (WorkerArg_t *)arg;
67 :     Diderot_World_t *wrld = myArg->wrld;
68 : jhr 1198
69 : jhr 1277 int nStrandsPerWorker = wrld->numStrands / wrld->numWorkers;
70 :     int start = myArg->id * nStrandsPerWorker;
71 :     int limit;
72 :     if (wrld->numWorkers-1 == myArg->id)
73 :     limit = wrld->numStrands;
74 :     else
75 :     limit = start + nStrandsPerWorker;
76 :    
77 : jhr 1198 while (true) {
78 : jhr 1214 // barrier synchronization at start of super step
79 : jhr 1268 pthread_mutex_lock (&wrld->lock);
80 :     if (wrld->numIdle+1 < wrld->numWorkers) {
81 :     wrld->numIdle++;
82 :     pthread_cond_wait (&wrld->barrier, &wrld->lock);
83 :     }
84 :     else {
85 :     // all other workers are idle, so we can proceed after some initialization
86 :     wrld->numIdle = 0;
87 :     wrld->numAvail = wrld->numStrands; // includes inactive strands
88 :     wrld->nextStrand = 0;
89 :     // swap in and out
90 :     void **tmp = wrld->inState;
91 :     wrld->inState = wrld->outState;
92 :     wrld->outState = tmp;
93 :     pthread_cond_broadcast (&wrld->barrier);
94 :     }
95 :     pthread_mutex_unlock (&wrld->lock);
96 : jhr 1198
97 : jhr 1214 // if there are no active strands left, then we're done
98 : jhr 1268 if (wrld->numActive == 0) {
99 :     pthread_cond_signal (&wrld->mainWait);
100 :     pthread_exit (0);
101 :     }
102 : jhr 1198
103 : jhr 1214 // iterate until there is no more work to do
104 : jhr 1268 int blkStart, blkSize;
105 :     int numDead = 0;
106 :     do {
107 :     // grab some work
108 :     pthread_mutex_lock (&wrld->lock);
109 :     blkStart = wrld->nextStrand;
110 :     blkSize = (wrld->numAvail >= BLOCK_SIZE) ? BLOCK_SIZE : wrld->numAvail;
111 :     wrld->numAvail -= blkSize;
112 :     wrld->nextStrand += blkSize;
113 :     pthread_mutex_unlock (&wrld->lock);
114 :     // update the strands
115 :     for (int i = blkStart; i < blkStart+blkSize; i++) {
116 :     if (! wrld->status[i]) {
117 :     StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
118 :     switch (sts) {
119 :     case DIDEROT_STABILIZE:
120 :     wrld->status[i] = DIDEROT_STABILIZE;
121 :     break;
122 :     case DIDEROT_DIE:
123 :     wrld->status[i] = DIDEROT_DIE;
124 :     numDead++;
125 :     break;
126 :     default:
127 :     break;
128 :     }
129 :     }
130 :     else {
131 :     assert ((wrld->status[i] == DIDEROT_STABLE) || (wrld->status[i] == DIDEROT_DIE));
132 :     }
133 :     }
134 :     } while (blkSize > 0);
135 : jhr 1198
136 : jhr 1214 // barrier synchronization
137 : jhr 1268 pthread_mutex_lock (&wrld->lock);
138 :     wrld->numActive -= numDead;
139 :     if (wrld->numIdle+1 < wrld->numWorkers) {
140 :     wrld->numIdle++;
141 :     pthread_cond_wait (&wrld->barrier, &wrld->lock);
142 :     }
143 :     else {
144 :     // all other workers are idle, so we can proceed
145 :     wrld->numIdle = 0;
146 :     pthread_cond_broadcast (&wrld->barrier);
147 :     wrld->nSteps++;
148 :     }
149 :     pthread_mutex_unlock (&wrld->lock);
150 : jhr 1214
151 :     /**** If there is a global computation phase, it goes here ****/
152 :    
153 :     // stabilize any threads that need stabilization. Each worker is responsible for
154 :     // a contiguous region of the strands
155 : jhr 1277 // FIXME: once we switch to dynamic lists of strand blocks, then we use finer-grain
156 :     // tracking
157 :     int numStabilized = 0;
158 :     for (int i = start; i < limit; i++) {
159 :     if (wrld->status[i] == DIDEROT_STABILIZE) {
160 :     // copy out to in so that both copies are the stable state
161 :     memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);
162 :     wrld->status[i] = DIDEROT_STABLE;
163 :     numStabilized++;
164 :     }
165 :     }
166 :     // adjust the numActive count
167 : jhr 1216 #if defined(HAVE_BUILTIN_ATOMIC_OPS)
168 : jhr 1277 __sync_fetch_and_sub(&wrld->numActive, numStabilized);
169 : jhr 1216 #else
170 : jhr 1277 pthread_mutex_lock (&wrld->lock);
171 :     wrld->numActive -= numStabilized;
172 :     pthread_mutex_unlock (&wrld->lock);
173 : jhr 1216 #endif
174 : jhr 1215 } // end while(true)
175 : jhr 1214
176 : jhr 1198 }
177 :    
178 :    
179 :     int main (int argc, const char **argv)
180 :     {
181 : jhr 1262 CPUInfo_t cpuInfo;
182 :     if (! GetNumCPUs (&cpuInfo)) {
183 : jhr 1268 fprintf(stderr, "unable to get number of processors\n");
184 :     exit (1);
185 : jhr 1262 }
186 :    
187 :     Diderot_int_t np = cpuInfo.numHWCores;
188 :    
189 :     Diderot_Options_t *opts = Diderot_OptNew ();
190 :    
191 : jhr 1263 Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
192 : jhr 1276 Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);
193 : jhr 1262 Diderot_OptAddInt (opts, "np", "specify number of threads", &np, true);
194 :     Diderot_RegisterGlobalOpts (opts);
195 :     Diderot_OptProcess (opts, argc, argv);
196 :     Diderot_OptFree (opts);
197 :    
198 :     if (VerboseFlg) fprintf (stderr, "initializing globals ...\n");
199 : jhr 1276 Diderot_InitGlobals ();
200 : jhr 1198
201 : jhr 1262 if (VerboseFlg) fprintf (stderr, "initializing strands ...\n");
202 : jhr 1198 Diderot_World_t *wrld = Diderot_Initially ();
203 :     for (int i = 0; i < wrld->numStrands; i++) {
204 :     // hack to make the invariant part of the state the same in both copies
205 : jhr 1268 memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
206 : jhr 1198 }
207 :    
208 : jhr 1214 // Start worker threads
209 : jhr 1262 int nWorkers = np;
210 : jhr 1214 WorkerArg_t *args = (WorkerArg_t *) malloc (nWorkers * sizeof(WorkerArg_t));
211 : jhr 1262 if (VerboseFlg) printf ("initializing %d workers ...\n", nWorkers);
212 : jhr 1276 double t0 = GetTime();
213 : jhr 1214 wrld->numWorkers = nWorkers;
214 :     wrld->numIdle = 0;
215 : jhr 1198 for (int i = 0; i < nWorkers; i++) {
216 : jhr 1268 pthread_t pid;
217 :     args[i].wrld = wrld;
218 :     args[i].id = i;
219 :     if (pthread_create (&pid, NULL, Worker, (void *)&(args[i])) != 0) {
220 :     fprintf (stderr, "unable to create worker thread\n");
221 :     exit (1);
222 :     }
223 :     pthread_detach (pid);
224 : jhr 1198 }
225 :    
226 : jhr 1214 // wait for the computation to finish
227 :     pthread_mutex_lock (&wrld->lock);
228 : jhr 1268 pthread_cond_wait (&wrld->mainWait, &wrld->lock);
229 : jhr 1214 pthread_mutex_unlock (&wrld->lock);
230 : jhr 1198
231 : jhr 1276 double totalTime = GetTime() - t0;
232 :    
233 :     if (VerboseFlg)
234 :     fprintf (stderr, "done: %d steps, in %f seconds\n", wrld->nSteps, totalTime);
235 :     else if (TimingFlg)
236 :     printf ("np=%d usr=%f\n", nWorkers, totalTime);
237 :    
238 : jhr 1198 // here we have the final state of all of the strands in the "in" buffer
239 :     FILE *outS = fopen("mip.txt", "w");
240 :     if (outS == NULL) {
241 : jhr 1268 fprintf (stderr, "Cannot open output file\n");
242 :     exit (8);
243 : jhr 1198 }
244 :    
245 :     for (int i = 0; i < wrld->numStrands; i++) {
246 : jhr 1268 if (wrld->status[i] == DIDEROT_STABLE)
247 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
248 : jhr 1198 }
249 :     fclose (outS);
250 :    
251 : jhr 1214 Diderot_Shutdown (wrld);
252 :    
253 : jhr 1198 return 0;
254 :    
255 :     }
256 :    
257 :    
258 :     // this should be the part of the scheduler
259 :     void *Diderot_AllocStrand (Strand_t *strand)
260 :     {
261 :     return malloc(strand->stateSzb);
262 :     }
263 :    
264 :     // block allocation of an initial collection of strands
265 :     Diderot_World_t *Diderot_AllocInitially (
266 : jhr 1276 Strand_t *strand, // the type of strands being allocated
267 :     bool isArray, // is the initialization an array or collection?
268 :     uint32_t nDims, // depth of iteration nesting
269 :     int32_t *base, // nDims array of base indices
270 :     uint32_t *size) // nDims array of iteration sizes
271 : jhr 1198 {
272 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
273 :     if (wrld == 0) {
274 : jhr 1268 fprintf (stderr, "unable to allocate world\n");
275 :     exit (1);
276 : jhr 1198 }
277 :    
278 :     wrld->isArray = isArray;
279 :     wrld->nDims = nDims;
280 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
281 :     wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
282 :     size_t numStrands = 1;
283 :     for (int i = 0; i < wrld->nDims; i++) {
284 : jhr 1268 numStrands *= size[i];
285 :     wrld->base[i] = base[i];
286 :     wrld->size[i] = size[i];
287 : jhr 1198 }
288 :    
289 : jhr 1276 if (VerboseFlg) {
290 :     fprintf(stderr, "AllocInitially: %d", size[0]);
291 :     for (int i = 1; i < nDims; i++) fprintf(stderr, " x %d", size[i]);
292 :     fprintf(stderr, "\n");
293 :     }
294 : jhr 1198
295 :     // allocate the strand state pointers
296 :     wrld->numStrands = numStrands;
297 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
298 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
299 :     wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
300 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
301 : jhr 1268 fprintf (stderr, "unable to allocate strand states\n");
302 :     exit (1);
303 : jhr 1198 }
304 : jhr 1214 wrld->numActive = wrld->numStrands;
305 :     wrld->nSteps = 0;
306 :     wrld->numWorkers = 0;
307 : jhr 1198
308 :     // initialize strand state pointers etc.
309 : jhr 1268 for (size_t i = 0; i < numStrands; i++) {
310 :     wrld->inState[i] = Diderot_AllocStrand (strand);
311 :     wrld->outState[i] = Diderot_AllocStrand (strand);
312 :     wrld->status[i] = DIDEROT_ACTIVE;
313 : jhr 1198 }
314 :    
315 : jhr 1214 pthread_mutex_init (&wrld->lock, NULL);
316 :     pthread_cond_init (&wrld->barrier, NULL);
317 :     pthread_cond_init (&wrld->mainWait, NULL);
318 : jhr 1198
319 :     return wrld;
320 :    
321 :     }
322 :    
323 :     // get strand state pointers
324 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
325 :     {
326 :     assert (i < wrld->numStrands);
327 :     return wrld->inState[i];
328 :     }
329 :    
330 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
331 :     {
332 :     assert (i < wrld->numStrands);
333 :     return wrld->outState[i];
334 :     }
335 :    
336 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
337 :     {
338 :     assert (i < wrld->numStrands);
339 :     return !wrld->status[i];
340 :     }

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