Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] Annotation of /trunk/src/lib/main.c
ViewVC logotype

Annotation of /trunk/src/lib/main.c

Parent Directory Parent Directory | Revision Log Revision Log


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

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