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

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