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

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