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