comparison tableau.cbc @ 0:d4bc23cb728b

Import from CVS (CVS_DB/member/atsuki/cbc/DPP)
author Yasutaka Higa <e115763@ie.u-ryukyu.ac.jp>
date Wed, 16 Dec 2015 15:16:11 +0900
parents
children 6bf69a0f2e24
comparison
equal deleted inserted replaced
-1:000000000000 0:d4bc23cb728b
1 /*
2 ** Dining Philosophers Problem's scheduler
3 ** with state developper as a tableau method
4
5 ** 連絡先: 琉球大学情報工学科 河野 真治
6 ** (E-Mail Address: kono@ie.u-ryukyu.ac.jp)
7 **
8 ** このソースのいかなる複写,改変,修正も許諾します。ただし、
9 ** その際には、誰が貢献したを示すこの部分を残すこと。
10 ** 再配布や雑誌の付録などの問い合わせも必要ありません。
11 ** 営利利用も上記に反しない範囲で許可します。
12 ** バイナリの配布の際にはversion messageを保存することを条件とします。
13 ** このプログラムについては特に何の保証もしない、悪しからず。
14 **
15 ** Everyone is permitted to do anything on this program
16 ** including copying, modifying, improving,
17 ** as long as you don't try to pretend that you wrote it.
18 ** i.e., the above copyright notice has to appear in all copies.
19 ** Binary distribution requires original version messages.
20 ** You don't have to ask before copying, redistribution or publishing.
21 ** THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE.
22
23 */
24 #include <stdlib.h>
25 #include <time.h>
26 #include "dpp2.h"
27 #include "queue.h"
28 #include "memory.h"
29 #include "state_db.h"
30
31 int NUM_PHILOSOPHER = 5; /* A number of philosophers must be more than 2. */
32
33 static code (*ret)(int);
34 static void *env;
35
36 static PhilsPtr phils_list = NULL;
37
38 static int max_step = 100;
39
40 static StateDB state_db;
41 static MemoryPtr mem;
42 static StateNode st;
43
44 int
45 list_length(TaskPtr list)
46 {
47 int length;
48 TaskPtr t;
49
50 if (!list) return 0;
51 t = list->next;
52
53 for (length = 1; t && t != list; length++) {
54 t = t->next;
55 }
56 return length;
57 }
58
59 TaskPtr
60 get_task(int num, TaskPtr list)
61 {
62 while (num-- > 0) {
63 list = list->next;
64 }
65 return list;
66 }
67
68
69 static TaskIteratorPtr task_iter;
70 static int depth,count;
71
72 /*
73 Performe depth frist search
74 Possible task iterleave is generated by TaskIterator
75 (using task ring)
76 State are recorded in StateDB
77 all memory fragments are regsitered by add_memory_range()
78 including task queue
79 */
80
81
82 code tableau(TaskPtr list)
83 {
84 StateDB out;
85
86 st.hash = get_memory_hash(mem,0);
87 if (lookup_StateDB(&st, &state_db, &out)) {
88 // found in the state database
89 //printf("found %d\n",count);
90 while(!(list = next_task_iterator(task_iter))) {
91 // no more branch, go back to the previous one
92 TaskIteratorPtr prev_iter = task_iter->prev;
93 if (!prev_iter) {
94 printf("All done count %d\n",count);
95 memory_usage();
96 goto ret(0),env;
97 }
98 //printf("no more branch %d\n",count);
99 depth--;
100 free_task_iterator(task_iter);
101 task_iter = prev_iter;
102 }
103 // return to previous state
104 // here we assume task list is fixed, we don't have to
105 // recover task list itself
106 restore_memory(task_iter->state->memory);
107 //printf("restore list %x next %x\n",(int)list,(int)(list->next));
108 } else {
109 // one step further
110 depth++;
111 task_iter = create_task_iterator(list,out,task_iter);
112 }
113 //printf("depth %d count %d\n", depth, count++);
114 count++;
115 goto list->phils->next(list->phils,list);
116 }
117
118 code get_next_task_fifo(TaskPtr list)
119 {
120 TaskPtr t = list;
121 TaskPtr e;
122
123 if (max_step--<0) goto die("Simuration end.");
124
125 list = list->next;
126 goto list->phils->next(list->phils,list);
127 }
128
129 code scheduler(PhilsPtr phils, TaskPtr list)
130 {
131 goto tableau(list);
132 // goto next_next_task_fifo(list);
133 }
134
135 code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last);
136
137 code task_entry2(int count,PhilsPtr self, TaskPtr list,TaskPtr last, TaskPtr q)
138 {
139 if (!q) {
140 goto die("Can't allocate Task\n");
141 } else {
142 add_memory_range(q,sizeof(Task),&mem);
143 goto enqueue(count, self, list, last, q, task_entry1);
144 }
145 }
146
147 code task_entry1(int count, PhilsPtr self, TaskPtr list, TaskPtr last)
148 {
149 StateDB out;
150 /*
151 printf("int count %d, PhilsPtr self %x, TaskPtr list %x, TaskPtr last %x\n",
152 count, self, list, last);
153 */
154
155 if (count++ < NUM_PHILOSOPHER) {
156 self = self->left;
157 goto create_queue(count,self,list,last,task_entry2);
158 } else {
159 // make circular task list
160 last->next = list;
161 st.memory = mem;
162 st.hash = get_memory_hash(mem,0);
163 lookup_StateDB(&st, &state_db, &out);
164 task_iter = create_task_iterator(list,out,0);
165 // start first task
166 goto list->phils->next(list->phils,list);
167 }
168 }
169
170 code task_entry0(int count, PhilsPtr self, TaskPtr list, TaskPtr last, TaskPtr q)
171 {
172 add_memory_range(q,sizeof(Task),&mem);
173 goto task_entry1(count, self, q, q);
174 }
175
176 code init_final(PhilsPtr self)
177 {
178 self->right = phils_list;
179 self->right_fork = phils_list->left_fork;
180 //printf("init all\n");
181
182 goto create_queue(1, self, 0, 0, task_entry0);
183 }
184
185 code init_phils2(PhilsPtr self, int count, int id)
186 {
187 PhilsPtr tmp_self;
188
189 tmp_self = (PhilsPtr)malloc(sizeof(Phils));
190 if (!tmp_self) {
191 goto die("Can't allocate Phils\n");
192 }
193 self->right = tmp_self;
194 tmp_self->id = id;
195 tmp_self->right_fork = NULL;
196 tmp_self->left_fork = self->right_fork;
197 tmp_self->right = NULL;
198 tmp_self->left = self;
199 tmp_self->next = thinking;
200 add_memory_range(tmp_self,sizeof(Phils),&mem);
201
202 count--;
203 id++;
204
205 if (count == 0) {
206 goto init_final(tmp_self);
207 } else {
208 goto init_fork2(tmp_self, count, id);
209 }
210 }
211
212 code init_fork2(PhilsPtr self, int count, int id)
213 {
214 ForkPtr tmp_fork;
215
216 tmp_fork = (ForkPtr)malloc(sizeof(Fork));
217 if (!tmp_fork) {
218 goto die("Can't allocate Fork\n");
219 }
220 tmp_fork->id = id;
221 tmp_fork->owner = NULL;
222 self->right_fork = tmp_fork;
223 add_memory_range(tmp_fork,sizeof(Fork),&mem);
224
225 goto init_phils2(self, count, id);
226 }
227
228 code init_phils1(ForkPtr fork, int count, int id)
229 {
230 PhilsPtr self;
231
232 self = (PhilsPtr)malloc(sizeof(Phils));
233 if (!self) {
234 goto die("Can't allocate Phils\n");
235 }
236 phils_list = self;
237 self->id = id;
238 self->right_fork = NULL;
239 self->left_fork = fork;
240 self->right = NULL;
241 self->left = NULL;
242 self->next = thinking;
243 add_memory_range(self,sizeof(Phils),&mem);
244
245 count--;
246 id++;
247
248 goto init_fork2(self, count, id);
249 }
250
251 code init_fork1(int count)
252 {
253 ForkPtr fork;
254 int id = 1;
255
256 fork = (ForkPtr)malloc(sizeof(Fork));
257 if (!fork) {
258 goto die("Can't allocate Fork\n");
259 }
260 fork->id = id;
261 fork->owner = NULL;
262 add_memory_range(fork,sizeof(Fork),&mem);
263
264 goto init_phils1(fork, count, id);
265 }
266
267 code die(char *err)
268 {
269 printf("%s\n", err);
270 goto ret(1), env;
271 }
272
273 int main(int ac, char *av[])
274 {
275 ret = return;
276 env = environment;
277 // srand((unsigned)time(NULL));
278 // srandom((unsigned long)time(NULL));
279 srandom(555);
280
281 if (ac==2) {
282 NUM_PHILOSOPHER = atoi(av[1]);
283 if (NUM_PHILOSOPHER >10 ||NUM_PHILOSOPHER < 2) {
284 printf("illegal number of philosopher = %d\n", NUM_PHILOSOPHER );
285 return 1;
286 }
287 printf("number of philosopher = %d\n", NUM_PHILOSOPHER );
288 }
289
290 goto init_fork1(NUM_PHILOSOPHER);
291 }
292
293 /* end */