SCM Repository
Annotation of /branches/pure-cfg/src/lib/main.c
Parent Directory
|
Revision Log
Revision 654 - (view) (download) (as text)
1 : | jhr | 571 | /*! \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 : | jhr | 572 | #include <stdio.h> |
13 : | jhr | 622 | #include <assert.h> |
14 : | jhr | 571 | #include <Diderot/diderot.h> |
15 : | |||
16 : | jhr | 622 | 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 : | jhr | 572 | extern float getOutf (void *self); |
28 : | |||
29 : | jhr | 571 | int main (int argc, const char **argv) |
30 : | { | ||
31 : | // | ||
32 : | jhr | 572 | printf("initializing globals ...\n"); |
33 : | jhr | 571 | Diderot_InitGlobals (); |
34 : | |||
35 : | // FIXME: we need to figure out how initialization should be handled. | ||
36 : | jhr | 572 | printf("initializing strands ...\n"); |
37 : | jhr | 622 | Diderot_World_t *wrld = Diderot_Initially (); |
38 : | jhr | 571 | |
39 : | // iterate until all strands are stable | ||
40 : | jhr | 633 | printf("run with %d strands ...\n", wrld->numStrands); |
41 : | jhr | 572 | int nSteps = 0, nUpdates = 0; |
42 : | jhr | 622 | int nActive = wrld->numStrands; |
43 : | jhr | 571 | while (nActive > 0) { |
44 : | jhr | 572 | nSteps++; |
45 : | jhr | 571 | // update strands |
46 : | jhr | 622 | for (int i = 0; i < wrld->numStrands; i++) { |
47 : | if (wrld->isActive[i]) { | ||
48 : | jhr | 572 | nUpdates++; |
49 : | jhr | 624 | StrandStatus_t sts = Diderot_Strands[0]->update(wrld->inState[i], wrld->outState[i]); |
50 : | jhr | 571 | if (sts == DIDEROT_STABILIZE) { |
51 : | jhr | 589 | // copy out to in so that both copies are the stable state |
52 : | jhr | 622 | // FIXME: there is a race condition here, since other strands might query this strand |
53 : | jhr | 633 | memcpy (wrld->inState[i], wrld->outState[i], Diderot_Strands[0]->stateSzb); |
54 : | jhr | 622 | wrld->isActive[i] = false; |
55 : | jhr | 571 | nActive--; |
56 : | } | ||
57 : | } | ||
58 : | } | ||
59 : | // swap in and out | ||
60 : | jhr | 622 | void **tmp = wrld->inState; |
61 : | wrld->inState = wrld->outState; | ||
62 : | wrld->outState = tmp; | ||
63 : | jhr | 571 | } |
64 : | |||
65 : | jhr | 572 | printf("done: %d updates, %d steps\n", nUpdates, nSteps); |
66 : | jhr | 571 | // here we have the final state of all of the strands in the "in" buffer |
67 : | jhr | 572 | FILE *outS = fopen("mip.txt", "w"); |
68 : | if (outS == NULL) { | ||
69 : | fprintf (stderr, "Cannot open output file\n"); | ||
70 : | exit (8); | ||
71 : | } | ||
72 : | jhr | 571 | |
73 : | jhr | 622 | for (int i = 0; i < wrld->numStrands; i++) { |
74 : | jhr | 654 | Diderot_Strands[0]->print (outS, wrld->inState[i]); |
75 : | jhr | 572 | } |
76 : | fclose (outS); | ||
77 : | |||
78 : | jhr | 571 | return 0; |
79 : | |||
80 : | } | ||
81 : | |||
82 : | |||
83 : | // this should be the part of the scheduler | ||
84 : | void *Diderot_AllocStrand (Strand_t *strand) | ||
85 : | { | ||
86 : | return malloc(strand->stateSzb); | ||
87 : | } | ||
88 : | jhr | 622 | |
89 : | // block allocation of an initial collection of strands | ||
90 : | jhr | 624 | Diderot_World_t *Diderot_AllocInitially ( |
91 : | Strand_t *strand, // the type of strands being allocated | ||
92 : | bool isArray, // is the initialization an array or collection? | ||
93 : | uint32_t nDims, // depth of iteration nesting | ||
94 : | int32_t *base, // nDims array of base indices | ||
95 : | uint32_t *size) // nDims array of iteration sizes | ||
96 : | jhr | 622 | { |
97 : | Diderot_World_t *wrld = (Diderot_World_t *) malloc (sizeof(Diderot_World_t)); | ||
98 : | if (wrld == 0) { | ||
99 : | fprintf (stderr, "unable to allocate world\n"); | ||
100 : | exit (1); | ||
101 : | } | ||
102 : | |||
103 : | jhr | 624 | wrld->isArray = isArray; |
104 : | wrld->nDims = nDims; | ||
105 : | wrld->base = (int32_t *) malloc (nDims * sizeof(int32_t)); | ||
106 : | wrld->size = (int32_t *) malloc (nDims * sizeof(int32_t)); | ||
107 : | jhr | 622 | size_t numStrands = 1; |
108 : | for (int i = 0; i < wrld->nDims; i++) { | ||
109 : | jhr | 624 | numStrands *= size[i]; |
110 : | wrld->base[i] = base[i]; | ||
111 : | wrld->size[i] = size[i]; | ||
112 : | jhr | 622 | } |
113 : | |||
114 : | jhr | 633 | printf("AllocInitially: %d", size[0]); |
115 : | for (int i = 1; i < nDims; i++) printf(" x %d", size[i]); | ||
116 : | printf("\n"); | ||
117 : | |||
118 : | jhr | 622 | // allocate the strand state pointers |
119 : | wrld->numStrands = numStrands; | ||
120 : | wrld->inState = (void **) malloc (numStrands * sizeof(void *)); | ||
121 : | wrld->outState = (void **) malloc (numStrands * sizeof(void *)); | ||
122 : | wrld->isActive = (bool *) malloc (numStrands * sizeof(bool)); | ||
123 : | if ((wrld->inState == 0) || (wrld->outState == 0) || (wrld->isActive == 0)) { | ||
124 : | fprintf (stderr, "unable to allocate strand states\n"); | ||
125 : | exit (1); | ||
126 : | } | ||
127 : | |||
128 : | // initialize strand state pointers etc. | ||
129 : | for (size_t i = 0; i < numStrands; i++) { | ||
130 : | wrld->inState[i] = Diderot_AllocStrand (strand); | ||
131 : | wrld->outState[i] = Diderot_AllocStrand (strand); | ||
132 : | wrld->isActive[i] = true; | ||
133 : | } | ||
134 : | |||
135 : | return wrld; | ||
136 : | |||
137 : | } | ||
138 : | |||
139 : | // get strand state pointers | ||
140 : | void *Diderot_InState (Diderot_World_t *wrld, uint32_t i) | ||
141 : | { | ||
142 : | assert (i < wrld->numStrands); | ||
143 : | return wrld->inState[i]; | ||
144 : | } | ||
145 : | |||
146 : | void *Diderot_OutState (Diderot_World_t *wrld, uint32_t i) | ||
147 : | { | ||
148 : | assert (i < wrld->numStrands); | ||
149 : | return wrld->outState[i]; | ||
150 : | } | ||
151 : | |||
152 : | bool Diderot_IsActive (Diderot_World_t *wrld, uint32_t i) | ||
153 : | { | ||
154 : | assert (i < wrld->numStrands); | ||
155 : | return wrld->isActive[i]; | ||
156 : | } |
root@smlnj-gforge.cs.uchicago.edu | ViewVC Help |
Powered by ViewVC 1.0.0 |