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 1232 - (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 : jhr 1232 Diderot_InitGlobals (argc, argv);
34 : jhr 1115
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 : jhr 1232 default:
65 :     break;
66 : jhr 1115 }
67 :     }
68 :     }
69 : jhr 1131 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 1115 // 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 1131 if (wrld->status[i] == DIDEROT_STABLE)
96 :     Diderot_Strands[0]->print (outS, wrld->inState[i]);
97 : jhr 1115 }
98 :     fclose (outS);
99 :    
100 : jhr 1232 Diderot_Shutdown (wrld);
101 :    
102 : jhr 1115 return 0;
103 :    
104 :     }
105 :    
106 :    
107 :     // this should be the part of the scheduler
108 :     void *Diderot_AllocStrand (Strand_t *strand)
109 :     {
110 :     return malloc(strand->stateSzb);
111 :     }
112 :    
113 :     // block allocation of an initial collection of strands
114 :     Diderot_World_t *Diderot_AllocInitially (
115 :     Strand_t *strand, // the type of strands being allocated
116 :     bool isArray, // is the initialization an array or collection?
117 :     uint32_t nDims, // depth of iteration nesting
118 :     int32_t *base, // nDims array of base indices
119 :     uint32_t *size) // nDims array of iteration sizes
120 :     {
121 :     Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t));
122 :     if (wrld == 0) {
123 :     fprintf (stderr, "unable to allocate world\n");
124 :     exit (1);
125 :     }
126 :    
127 :     wrld->isArray = isArray;
128 :     wrld->nDims = nDims;
129 :     wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t));
130 : jhr 1232 wrld->size = (uint32_t *) malloc (nDims * sizeof(uint32_t));
131 : jhr 1115 size_t numStrands = 1;
132 :     for (int i = 0; i < wrld->nDims; i++) {
133 :     numStrands *= size[i];
134 :     wrld->base[i] = base[i];
135 :     wrld->size[i] = size[i];
136 :     }
137 :    
138 :     printf("AllocInitially: %d", size[0]);
139 :     for (int i = 1; i < nDims; i++) printf(" x %d", size[i]);
140 :     printf("\n");
141 :    
142 :     // allocate the strand state pointers
143 :     wrld->numStrands = numStrands;
144 :     wrld->inState = (void **) malloc (numStrands * sizeof(void *));
145 :     wrld->outState = (void **) malloc (numStrands * sizeof(void *));
146 : jhr 1131 wrld->status = (uint8_t *) malloc (numStrands * sizeof(uint8_t));
147 :     if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->status == 0)) {
148 : jhr 1115 fprintf (stderr, "unable to allocate strand states\n");
149 :     exit (1);
150 :     }
151 :    
152 :     // initialize strand state pointers etc.
153 :     for (size_t i = 0; i < numStrands; i++) {
154 :     wrld->inState[i] = Diderot_AllocStrand (strand);
155 :     wrld->outState[i] = Diderot_AllocStrand (strand);
156 : jhr 1131 wrld->status[i] = DIDEROT_ACTIVE;
157 : jhr 1115 }
158 :    
159 :     return wrld;
160 :    
161 :     }
162 :    
163 :     // get strand state pointers
164 :     void *Diderot_InState (Diderot_World_t *wrld, uint32_t i)
165 :     {
166 :     assert (i < wrld->numStrands);
167 :     return wrld->inState[i];
168 :     }
169 :    
170 :     void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i)
171 :     {
172 :     assert (i < wrld->numStrands);
173 :     return wrld->outState[i];
174 :     }
175 :    
176 :     bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i)
177 :     {
178 :     assert (i < wrld->numStrands);
179 : jhr 1131 return !wrld->status[i];
180 : jhr 1115 }

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