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 1131 - (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 :     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 1131 uint8_t *status; // array of strand status flags
25 : jhr 1115 };
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 1131 bool existsStabilizing = false;
51 : jhr 1115 for (int i = 0; i < wrld->numStrands; i++) {
52 : jhr 1131 if (! wrld->status[i]) {
53 : jhr 1115 nUpdates++;
54 :     StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]);
55 :     switch (sts) {
56 :     case DIDEROT_STABILIZE:
57 : jhr 1131 existsStabilizing = true;
58 :     wrld->status[i] = DIDEROT_STABILIZE;
59 : jhr 1115 break;
60 :     case DIDEROT_DIE:
61 : jhr 1131 wrld->status[i] = DIDEROT_DIE;
62 : jhr 1115 nActive--;
63 :     break;
64 :     }
65 :     }
66 :     }
67 : jhr 1131 if (existsStabilizing) {
68 :     for (int i = 0; i < wrld->numStrands; i++) {
69 :     // NOTE: we may want to compact the array of strands
70 :     if (wrld->status[i] == DIDEROT_STABILIZE) {
71 :     // copy out to in so that both copies are the stable state
72 :     memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb);
73 :     wrld->status[i] = DIDEROT_STABLE;
74 :     nActive--;
75 :     }
76 :     }
77 :     }
78 : jhr 1115 // swap in and out
79 :     void **tmp = wrld->inState;
80 :     wrld->inState = wrld->outState;
81 :     wrld->outState = tmp;
82 :     }
83 :    
84 :     printf("done: %d updates, %d steps\n", nUpdates, nSteps);
85 :     // here we have the final state of all of the strands in the "in" buffer
86 :     FILE *outS = fopen("mip.txt", "w");
87 :     if (outS == NULL) {
88 :     fprintf (stderr, "Cannot open output file\n");
89 :     exit (8);
90 :     }
91 :    
92 :     for (int i = 0; i < wrld->numStrands; i++) {
93 : jhr 1131 if (wrld->status[i] == DIDEROT_STABLE)
94 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
95 : jhr 1115 }
96 :     fclose (outS);
97 :    
98 :     return 0;
99 :    
100 :     }
101 :    
102 :    
103 :     // this should be the part of the scheduler
104 :     void *Diderot_AllocStrand (Strand_t *strand)
105 :     {
106 :     return malloc(strand->stateSzb);
107 :     }
108 :    
109 :     // block allocation of an initial collection of strands
110 :     Diderot_World_t *Diderot_AllocInitially (
111 :     Strand_t *strand, // the type of strands being allocated
112 :     bool isArray, // is the initialization an array or collection?
113 :     uint32_t nDims, // depth of iteration nesting
114 :     int32_t *base, // nDims array of base indices
115 :     uint32_t *size) // nDims array of iteration sizes
116 :     {
117 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
118 :     if (wrld == 0) {
119 :     fprintf (stderr, "unable to allocate world\n");
120 :     exit (1);
121 :     }
122 :    
123 :     wrld->isArray = isArray;
124 :     wrld->nDims = nDims;
125 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
126 :     wrld->size = (int32_t *) malloc (nDims * sizeof(int32_t));
127 :     size_t numStrands = 1;
128 :     for (int i = 0; i < wrld->nDims; i++) {
129 :     numStrands *= size[i];
130 :     wrld->base[i] = base[i];
131 :     wrld->size[i] = size[i];
132 :     }
133 :    
134 :     printf("AllocInitially: %d", size[0]);
135 :     for (int i = 1; i < nDims; i++) printf(" x %d", size[i]);
136 :     printf("\n");
137 :    
138 :     // allocate the strand state pointers
139 :     wrld->numStrands = numStrands;
140 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
141 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
142 : jhr 1131 wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
143 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
144 : jhr 1115 fprintf (stderr, "unable to allocate strand states\n");
145 :     exit (1);
146 :     }
147 :    
148 :     // initialize strand state pointers etc.
149 :     for (size_t i = 0; i < numStrands; i++) {
150 :     wrld->inState[i] = Diderot_AllocStrand (strand);
151 :     wrld->outState[i] = Diderot_AllocStrand (strand);
152 : jhr 1131 wrld->status[i] = DIDEROT_ACTIVE;
153 : jhr 1115 }
154 :    
155 :     return wrld;
156 :    
157 :     }
158 :    
159 :     // get strand state pointers
160 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
161 :     {
162 :     assert (i < wrld->numStrands);
163 :     return wrld->inState[i];
164 :     }
165 :    
166 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
167 :     {
168 :     assert (i < wrld->numStrands);
169 :     return wrld->outState[i];
170 :     }
171 :    
172 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
173 :     {
174 :     assert (i < wrld->numStrands);
175 : jhr 1131 return !wrld->status[i];
176 : jhr 1115 }

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