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

SCM Repository

[diderot] Annotation of /trunk/src/lib/c-target/main.c
ViewVC logotype

Annotation of /trunk/src/lib/c-target/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : jhr 1115 /*! \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 :    
16 : jhr 1301 // NOTE: we probably should put this in a file that supports runtime printing
17 :     static bool VerboseFlg = false;
18 :     static bool TimingFlg = false;
19 :    
20 : jhr 1115 struct struct_world {
21 : jhr 1301 const char *name; // the program name
22 :     bool isArray; // is the initialization an array or collection?
23 :     uint32_t nDims; // depth of iteration nesting
24 :     int32_t *base; // nDims array of base indices
25 :     uint32_t *size; // nDims array of iteration sizes
26 :     uint32_t numStrands; // number of strands in the world
27 :     void **inState;
28 :     void **outState;
29 :     uint8_t *status; // array of strand status flags
30 : jhr 1115 };
31 :    
32 :     extern float getOutf (void *self);
33 :    
34 :     int main (int argc, const char **argv)
35 :     {
36 : jhr 1301 Diderot_Options_t *opts = Diderot_OptNew ();
37 : jhr 1115
38 : jhr 1301 Diderot_OptAddFlag (opts, "verbose", "enable runtime-system messages", &VerboseFlg);
39 :     Diderot_OptAddFlag (opts, "timing", "enable execution timing", &TimingFlg);
40 :     Diderot_RegisterGlobalOpts (opts);
41 :     Diderot_OptProcess (opts, argc, argv);
42 :     Diderot_OptFree (opts);
43 :    
44 :     // run the generated global initialization code
45 :     if (VerboseFlg) fprintf (stderr, "initializing globals ...\n");
46 :     Diderot_InitGlobals ();
47 :    
48 : jhr 1115 // FIXME: we need to figure out how initialization should be handled.
49 : jhr 1301 if (VerboseFlg) fprintf (stderr, "initializing strands ...\n");
50 : jhr 1115 Diderot_World_t *wrld = Diderot_Initially ();
51 :     for (int i = 0; i < wrld->numStrands; i++) {
52 :     // hack to make the invariant part of the state the same in both copies
53 : jhr 1301 memcpy (wrld->outState[i], wrld->inState[i], Diderot_Strands[0]->stateSzb);
54 : jhr 1115 }
55 :    
56 :     // iterate until all strands are stable
57 : jhr 1301 if (VerboseFlg) fprintf(stderr, "run with %d strands ...\n", wrld->numStrands);
58 :     double t0 = GetTime();
59 : jhr 1115 int nSteps = 0, nUpdates = 0;
60 :     int nActive = wrld->numStrands;
61 :     while (nActive > 0) {
62 : jhr 1301 nSteps++;
63 : jhr 1115 // update strands
64 : jhr 1301 bool existsStabilizing = false;
65 :     for (int i = 0; i < wrld->numStrands; i++) {
66 :     if (! wrld->status[i]) {
67 :     nUpdates++;
68 :     StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
69 :     switch (sts) {
70 :     case DIDEROT_STABILIZE:
71 :     existsStabilizing = true;
72 :     wrld->status[i] = DIDEROT_STABILIZE;
73 :     break;
74 :     case DIDEROT_DIE:
75 :     wrld->status[i] = DIDEROT_DIE;
76 :     nActive--;
77 :     break;
78 :     default:
79 :     break;
80 :     }
81 :     }
82 :     }
83 :     if (existsStabilizing) {
84 :     for (int i = 0; i < wrld->numStrands; i++) {
85 : jhr 1131 // NOTE: we may want to compact the array of strands
86 : jhr 1301 if (wrld->status[i] == DIDEROT_STABILIZE) {
87 :     // copy out to in so that both copies are the stable state
88 :     memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);
89 :     wrld->status[i] = DIDEROT_STABLE;
90 :     nActive--;
91 :     }
92 :     }
93 :     }
94 : jhr 1115 // swap in and out
95 : jhr 1301 void **tmp = wrld->inState;
96 :     wrld->inState = wrld->outState;
97 :     wrld->outState = tmp;
98 : jhr 1115 }
99 : jhr 1301 double totalTime = GetTime() - t0;
100 : jhr 1115
101 : jhr 1301 if (VerboseFlg)
102 :     fprintf (stderr, "done: %d updates, %d steps, in %f seconds\n", nUpdates, nSteps, totalTime);
103 :     else if (TimingFlg)
104 :     printf ("usr=%f\n", totalTime);
105 :    
106 : jhr 1115 // here we have the final state of all of the strands in the "in" buffer
107 : jhr 1301 int outFileNameLen = strlen(wrld->name) + 5;
108 :     char *outFileName = (char *)malloc(outFileNameLen);
109 :     snprintf (outFileName, outFileNameLen, "%s.txt", wrld->name);
110 :     FILE *outS = fopen(outFileName, "w");
111 : jhr 1115 if (outS == NULL) {
112 : jhr 1301 fprintf (stderr, "Cannot open output file %s\n", outFileName);
113 : jhr 1115 exit (8);
114 :     }
115 :    
116 :     for (int i = 0; i < wrld->numStrands; i++) {
117 : jhr 1301 if (wrld->status[i] == DIDEROT_STABLE)
118 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
119 : jhr 1115 }
120 :     fclose (outS);
121 :    
122 : jhr 1232 Diderot_Shutdown (wrld);
123 :    
124 : jhr 1115 return 0;
125 :    
126 :     }
127 :    
128 :    
129 :     // this should be the part of the scheduler
130 :     void *Diderot_AllocStrand (Strand_t *strand)
131 :     {
132 :     return malloc(strand->stateSzb);
133 :     }
134 :    
135 :     // block allocation of an initial collection of strands
136 :     Diderot_World_t *Diderot_AllocInitially (
137 : jhr 1301 const char *name, // the name of the program
138 :     Strand_t *strand, // the type of strands being allocated
139 :     bool isArray, // is the initialization an array or collection?
140 :     uint32_t nDims, // depth of iteration nesting
141 :     int32_t *base, // nDims array of base indices
142 :     uint32_t *size) // nDims array of iteration sizes
143 : jhr 1115 {
144 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
145 :     if (wrld == 0) {
146 : jhr 1301 fprintf (stderr, "unable to allocate world\n");
147 :     exit (1);
148 : jhr 1115 }
149 :    
150 : jhr 1301 wrld->name = name; /* NOTE: we are assuming that name is statically allocated! */
151 : jhr 1115 wrld->isArray = isArray;
152 :     wrld->nDims = nDims;
153 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
154 : jhr 1232 wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
155 : jhr 1115 size_t numStrands = 1;
156 :     for (int i = 0; i < wrld->nDims; i++) {
157 : jhr 1301 numStrands *= size[i];
158 :     wrld->base[i] = base[i];
159 :     wrld->size[i] = size[i];
160 : jhr 1115 }
161 :    
162 : jhr 1301 if (VerboseFlg) {
163 :     fprintf(stderr, "AllocInitially: %d", size[0]);
164 :     for (int i = 1; i < nDims; i++) fprintf(stderr, " x %d", size[i]);
165 :     fprintf(stderr, "\n");
166 :     }
167 : jhr 1115
168 :     // allocate the strand state pointers
169 :     wrld->numStrands = numStrands;
170 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
171 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
172 : jhr 1131 wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
173 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
174 : jhr 1301 fprintf (stderr, "unable to allocate strand states\n");
175 :     exit (1);
176 : jhr 1115 }
177 :    
178 :     // initialize strand state pointers etc.
179 :     for (size_t i = 0; i < numStrands; i++) {
180 : jhr 1301 wrld->inState[i] = Diderot_AllocStrand (strand);
181 :     wrld->outState[i] = Diderot_AllocStrand (strand);
182 :     wrld->status[i] = DIDEROT_ACTIVE;
183 : jhr 1115 }
184 :    
185 :     return wrld;
186 :    
187 :     }
188 :    
189 :     // get strand state pointers
190 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
191 :     {
192 :     assert (i < wrld->numStrands);
193 :     return wrld->inState[i];
194 :     }
195 :    
196 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
197 :     {
198 :     assert (i < wrld->numStrands);
199 :     return wrld->outState[i];
200 :     }
201 :    
202 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
203 :     {
204 :     assert (i < wrld->numStrands);
205 : jhr 1131 return !wrld->status[i];
206 : jhr 1115 }

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