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 3349 - (view) (download) (as text)

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

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