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 1215 - (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 :     pthread_mutex_lock (&wrld->lock);
162 :     wrld->numActive -= numStabilized;
163 :     pthread_mutex_unlock (&wrld->lock);
164 : jhr 1198 }
165 : jhr 1215 } // end while(true)
166 : jhr 1214
167 : jhr 1198 }
168 :    
169 :    
170 :     int main (int argc, const char **argv)
171 :     {
172 : jhr 1214 fprintf (stderr, "initializing globals ...\n");
173 :     Diderot_InitGlobals (argc, argv);
174 : jhr 1198
175 : jhr 1214 fprintf (stderr, "initializing strands ...\n");
176 : jhr 1198 Diderot_World_t *wrld = Diderot_Initially ();
177 :     for (int i = 0; i < wrld->numStrands; i++) {
178 :     // hack to make the invariant part of the state the same in both copies
179 :     memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
180 :     }
181 :    
182 :     CPUInfo_t cpuInfo;
183 :     if (! GetNumCPUs (&cpuInfo)) {
184 :     fprintf(stderr, "unable to get number of processors\n");
185 :     exit (1);
186 :     }
187 : jhr 1214
188 :     // Start worker threads
189 : jhr 1198 int nWorkers = cpuInfo.numHWCores;
190 : jhr 1214 WorkerArg_t *args = (WorkerArg_t *) malloc (nWorkers * sizeof(WorkerArg_t));
191 : jhr 1209 printf ("initializing %d workers ...\n", nWorkers);
192 : jhr 1214 wrld->numWorkers = nWorkers;
193 :     wrld->numIdle = 0;
194 : jhr 1198 for (int i = 0; i < nWorkers; i++) {
195 : jhr 1214 pthread_t pid;
196 :     args[i].wrld = wrld;
197 :     args[i].id = i;
198 :     if (pthread_create (&pid, NULL, Worker, (void *)&(args[i])) != 0) {
199 :     fprintf (stderr, "unable to create worker thread\n");
200 :     exit (1);
201 :     }
202 :     pthread_detach (pid);
203 : jhr 1198 }
204 :    
205 : jhr 1214 // wait for the computation to finish
206 :     pthread_mutex_lock (&wrld->lock);
207 :     pthread_cond_wait (&wrld->mainWait, &wrld->lock);
208 :     pthread_mutex_unlock (&wrld->lock);
209 : jhr 1198
210 : jhr 1214 fprintf (stderr, "done: %d steps\n", wrld->nSteps);
211 : jhr 1198 // here we have the final state of all of the strands in the "in" buffer
212 :     FILE *outS = fopen("mip.txt", "w");
213 :     if (outS == NULL) {
214 :     fprintf (stderr, "Cannot open output file\n");
215 :     exit (8);
216 :     }
217 :    
218 :     for (int i = 0; i < wrld->numStrands; i++) {
219 :     if (wrld->status[i] == DIDEROT_STABLE)
220 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
221 :     }
222 :     fclose (outS);
223 :    
224 : jhr 1214 Diderot_Shutdown (wrld);
225 :    
226 : jhr 1198 return 0;
227 :    
228 :     }
229 :    
230 :    
231 :     // this should be the part of the scheduler
232 :     void *Diderot_AllocStrand (Strand_t *strand)
233 :     {
234 :     return malloc(strand->stateSzb);
235 :     }
236 :    
237 :     // block allocation of an initial collection of strands
238 :     Diderot_World_t *Diderot_AllocInitially (
239 : jhr 1209 Strand_t *strand, // the type of strands being allocated
240 : jhr 1198 bool isArray, // is the initialization an array or collection?
241 :     uint32_t nDims, // depth of iteration nesting
242 :     int32_t *base, // nDims array of base indices
243 :     uint32_t *size) // nDims array of iteration sizes
244 :     {
245 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
246 :     if (wrld == 0) {
247 :     fprintf (stderr, "unable to allocate world\n");
248 :     exit (1);
249 :     }
250 :    
251 :     wrld->isArray = isArray;
252 :     wrld->nDims = nDims;
253 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
254 :     wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
255 :     size_t numStrands = 1;
256 :     for (int i = 0; i < wrld->nDims; i++) {
257 :     numStrands *= size[i];
258 :     wrld->base[i] = base[i];
259 :     wrld->size[i] = size[i];
260 :     }
261 :    
262 : jhr 1214 fprintf(stderr, "AllocInitially: %d", size[0]);
263 :     for (int i = 1; i < nDims; i++)
264 :     fprintf(stderr, " x %d", size[i]);
265 :     fprintf(stderr, "\n");
266 : jhr 1198
267 :     // allocate the strand state pointers
268 :     wrld->numStrands = numStrands;
269 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
270 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
271 :     wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
272 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
273 :     fprintf (stderr, "unable to allocate strand states\n");
274 :     exit (1);
275 :     }
276 : jhr 1214 wrld->numActive = wrld->numStrands;
277 :     wrld->nSteps = 0;
278 :     wrld->numWorkers = 0;
279 : jhr 1198
280 :     // initialize strand state pointers etc.
281 :     for (size_t i = 0; i < numStrands; i++) {
282 :     wrld->inState[i] = Diderot_AllocStrand (strand);
283 :     wrld->outState[i] = Diderot_AllocStrand (strand);
284 :     wrld->status[i] = DIDEROT_ACTIVE;
285 :     }
286 :    
287 : jhr 1214 pthread_mutex_init (&wrld->lock, NULL);
288 :     pthread_cond_init (&wrld->barrier, NULL);
289 :     pthread_cond_init (&wrld->mainWait, NULL);
290 : jhr 1198
291 :     return wrld;
292 :    
293 :     }
294 :    
295 :     // get strand state pointers
296 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
297 :     {
298 :     assert (i < wrld->numStrands);
299 :     return wrld->inState[i];
300 :     }
301 :    
302 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
303 :     {
304 :     assert (i < wrld->numStrands);
305 :     return wrld->outState[i];
306 :     }
307 :    
308 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
309 :     {
310 :     assert (i < wrld->numStrands);
311 :     return !wrld->status[i];
312 :     }

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