Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /branches/pure-cfg/src/lib/c-target/main.c
ViewVC logotype

Annotation of /branches/pure-cfg/src/lib/c-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1669 - (view) (download) (as text)

1 : jhr 1093 /*! \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 : jhr 1411 #include <Diderot/diderot.h>
12 : nseltzer 1450 #include <teem/nrrd.h>
13 : jhr 1093
14 :     struct struct_world {
15 : jhr 1519 STRUCT_WORLD_PREFIX
16 : jhr 1276 void **inState;
17 :     void **outState;
18 : jhr 1093 };
19 :    
20 :     extern float getOutf (void *self);
21 :    
22 :     int main (int argc, const char **argv)
23 :     {
24 : jhr 1669 // handle command-line options
25 : jhr 1262 Diderot_Options_t *opts = Diderot_OptNew ();
26 :     Diderot_RegisterGlobalOpts (opts);
27 :     Diderot_OptProcess (opts, argc, argv);
28 :     Diderot_OptFree (opts);
29 :    
30 :     // run the generated global initialization code
31 : jhr 1411 if (VerboseFlg)
32 :     fprintf (stderr, "initializing globals ...\n");
33 : jhr 1262 Diderot_InitGlobals ();
34 :    
35 : jhr 1093 // FIXME: we need to figure out how initialization should be handled.
36 : jhr 1276 if (VerboseFlg) fprintf (stderr, "initializing strands ...\n");
37 : jhr 1093 Diderot_World_t *wrld = Diderot_Initially ();
38 :     for (int i = 0; i < wrld->numStrands; i++) {
39 :     // hack to make the invariant part of the state the same in both copies
40 : jhr 1276 memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
41 : jhr 1093 }
42 :    
43 :     // iterate until all strands are stable
44 : jhr 1276 if (VerboseFlg) fprintf(stderr, "run with %d strands ...\n", wrld->numStrands);
45 : jhr 1603 double t0 = airTime();
46 : jhr 1093 int nSteps = 0, nUpdates = 0;
47 :     int nActive = wrld->numStrands;
48 :     while (nActive > 0) {
49 : jhr 1276 nSteps++;
50 : jhr 1093 // update strands
51 : jhr 1276 bool existsStabilizing = false;
52 :     for (int i = 0; i < wrld->numStrands; i++) {
53 :     if (! wrld->status[i]) {
54 :     nUpdates++;
55 :     StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
56 :     switch (sts) {
57 :     case DIDEROT_STABILIZE:
58 :     existsStabilizing = true;
59 :     wrld->status[i] = DIDEROT_STABILIZE;
60 :     break;
61 :     case DIDEROT_DIE:
62 :     wrld->status[i] = DIDEROT_DIE;
63 :     nActive--;
64 :     break;
65 :     default:
66 :     break;
67 :     }
68 :     }
69 :     }
70 :     if (existsStabilizing) {
71 :     for (int i = 0; i < wrld->numStrands; i++) {
72 : jhr 1130 // NOTE: we may want to compact the array of strands
73 : jhr 1276 if (wrld->status[i] == DIDEROT_STABILIZE) {
74 : jhr 1448 // stabilize the strand's state. Note that the outState has been set by
75 :     // the last call to update, so we make the inState be the target of the
76 : jhr 1519 // stabilize method.
77 : jhr 1448 Diderot_Strands[0]->stabilize(wrld->outState[i], wrld->inState[i]);
78 : jhr 1519 memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
79 : jhr 1276 wrld->status[i] = DIDEROT_STABLE;
80 :     nActive--;
81 :     }
82 :     }
83 :     }
84 : jhr 1093 // swap in and out
85 : jhr 1276 void **tmp = wrld->inState;
86 :     wrld->inState = wrld->outState;
87 :     wrld->outState = tmp;
88 : jhr 1093 }
89 : jhr 1603 double totalTime = airTime() - t0;
90 : jhr 1093
91 : jhr 1276 if (VerboseFlg)
92 :     fprintf (stderr, "done: %d updates, %d steps, in %f seconds\n", nUpdates, nSteps, totalTime);
93 :     else if (TimingFlg)
94 :     printf ("usr=%f\n", totalTime);
95 :    
96 : jhr 1482 // output the final strand states
97 : jhr 1481 if (NrrdOutputFlg)
98 : jhr 1519 Diderot_Output (wrld, Diderot_Strands[0]->outputSzb);
99 : jhr 1481 else
100 : jhr 1519 Diderot_Print (wrld);
101 : jhr 1093
102 : jhr 1214 Diderot_Shutdown (wrld);
103 :    
104 : jhr 1093 return 0;
105 :    
106 :     }
107 :    
108 :     // block allocation of an initial collection of strands
109 :     Diderot_World_t *Diderot_AllocInitially (
110 : jhr 1280 const char *name, // the name of the program
111 : jhr 1276 Strand_t *strand, // the type of strands being allocated
112 :     bool isArray, // is the initialization an array or collection?
113 :     uint32_t nDims, // depth of iteration nesting
114 :     int32_t *base, // nDims array of base indices
115 :     uint32_t *size) // nDims array of iteration sizes
116 : jhr 1093 {
117 : jhr 1472 Diderot_World_t *wrld = NEW(Diderot_World_t);
118 : jhr 1093 if (wrld == 0) {
119 : jhr 1276 fprintf (stderr, "unable to allocate world\n");
120 :     exit (1);
121 : jhr 1093 }
122 :    
123 : jhr 1287 wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
124 : jhr 1093 wrld->isArray = isArray;
125 :     wrld->nDims = nDims;
126 : jhr 1472 wrld->base = NEWVEC(int32_t, nDims);
127 :     wrld->size = NEWVEC(uint32_t, nDims);
128 : jhr 1093 size_t numStrands = 1;
129 :     for (int i = 0; i < wrld->nDims; i++) {
130 : jhr 1276 numStrands *= size[i];
131 :     wrld->base[i] = base[i];
132 :     wrld->size[i] = size[i];
133 : jhr 1093 }
134 :    
135 : jhr 1276 if (VerboseFlg) {
136 :     fprintf(stderr, "AllocInitially: %d", size[0]);
137 :     for (int i = 1; i < nDims; i++) fprintf(stderr, " x %d", size[i]);
138 :     fprintf(stderr, "\n");
139 :     }
140 : jhr 1093
141 :     // allocate the strand state pointers
142 :     wrld->numStrands = numStrands;
143 : jhr 1472 wrld->inState = NEWVEC(void *, numStrands);
144 :     wrld->outState = NEWVEC(void *, numStrands);
145 :     wrld->status = NEWVEC(uint8_t, numStrands);
146 : jhr 1130 if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
147 : jhr 1276 fprintf (stderr, "unable to allocate strand states\n");
148 :     exit (1);
149 : jhr 1093 }
150 :    
151 :     // initialize strand state pointers etc.
152 :     for (size_t i = 0; i < numStrands; i++) {
153 : jhr 1519 wrld->inState[i] = CheckedAlloc (strand->stateSzb);
154 :     wrld->outState[i] = CheckedAlloc (strand->stateSzb);
155 : jhr 1276 wrld->status[i] = DIDEROT_ACTIVE;
156 : jhr 1093 }
157 :    
158 :     return wrld;
159 :    
160 :     }
161 :    
162 :     // get strand state pointers
163 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
164 :     {
165 :     assert (i < wrld->numStrands);
166 :     return wrld->inState[i];
167 :     }
168 :    
169 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
170 :     {
171 :     assert (i < wrld->numStrands);
172 :     return wrld->outState[i];
173 :     }

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