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