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

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