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

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