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