comparison src/parallel_execution/main.c @ 90:4b5bf5b40970

put queue
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 06:47:35 +0900
parents 9e139a340bd1
children 1e074c3878c7
comparison
equal deleted inserted replaced
89:00d4c6fa4969 90:4b5bf5b40970
3 3
4 #include "context.h" 4 #include "context.h"
5 #include "origin_cs.h" 5 #include "origin_cs.h"
6 6
7 extern __code initContext(struct Context* context); 7 extern __code initContext(struct Context* context);
8 8 extern void allocator(struct Context* context);
9 int a; 9
10 int length;
11 int* array_ptr;
12
13 void print_queue(struct Element* element) {
14 while (element) {
15 printf("%d\n", element->task->key);
16 element = element->next;
17 }
18 }
19
20 void print_tree(struct Node* node) {
21 if (node != 0) {
22 printf("%d\n", node->value->array.index);
23 print_tree(node->left);
24 print_tree(node->right);
25 }
26 }
10 27
11 __code code1(struct Context* context) { 28 __code code1(struct Context* context) {
12 if(__sync_bool_compare_and_swap(&a, 10, 0)) { 29 print_queue(context->activeQueue->first);
13 puts("success"); 30 puts("");
14 a = 10; 31 print_tree(context->tree->root);
15 } else { 32
16 puts("failure"); 33 goto meta(context, Exit);
17 goto meta(context, Code1);
18 }
19 } 34 }
20 35
21 __code code1_stub(struct Context* context) { 36 __code code1_stub(struct Context* context) {
22 goto code1(context); 37 goto code1(context);
38 }
39
40 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
41 int i = loopCounter->i;
42
43 if (i < length) {
44 allocate->size = sizeof(struct Array);
45 allocator(context);
46
47 goto meta(context, CreateData2);
48 }
49
50 goto meta(context, Code1);
51 }
52
53 __code createData1_stub(struct Context* context) {
54 goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter);
55 }
56
57 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) {
58 int i = loopCounter->i;
59
60 array->index = i;
61 array->array = array_ptr;
62
63 node->key = i;
64 node->value = (union Data*)array;
65
66 context->next = CreateTask1;
67
68 goto meta(context, PutTree);
69 }
70
71 __code createData2_stub(struct Context* context) {
72 goto createData2(context,
73 &context->data[LoopCounter]->loopCounter,
74 &context->data[context->dataNum]->array,
75 &context->data[Node]->node);
76 }
77
78 __code createTask1(struct Context* context, struct Allocate* allocate) {
79 allocate->size = sizeof(struct Task);
80 allocator(context);
81
82 goto meta(context, CreateTask2);
83 }
84
85 __code createTask1_stub(struct Context* context) {
86 goto createTask1(context, &context->data[Allocate]->allocate);
87 }
88
89 __code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
90 int i = loopCounter->i;
91
92 task->code = Code1;
93 task->key = i;
94
95 element->task = task;
96
97 context->next = CreateData1;
98 loopCounter->i++;
99
100 goto meta(context, PutQueue1);
101 }
102
103 __code createTask2_stub(struct Context* context) {
104 goto createTask2(context,
105 &context->data[LoopCounter]->loopCounter,
106 &context->data[context->dataNum]->task,
107 &context->data[Element]->element);
108 }
109
110 __code putQueue1(struct Context* context, struct Allocate* allocate) {
111 allocate->size = sizeof(struct Element);
112 allocator(context);
113
114 goto meta(context, PutQueue2);
115 }
116
117 __code putQueue1_stub(struct Context* context) {
118 goto putQueue1(context, &context->data[Allocate]->allocate);
119 }
120
121 __code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) {
122 new_element->task = element->task;
123
124 if (queue->first)
125 goto meta(context, PutQueue3);
126 else
127 goto meta(context, PutQueue4);
128 }
129
130 __code putQueue2_stub(struct Context* context) {
131 goto putQueue2(context,
132 &context->data[context->dataNum]->element,
133 &context->data[Element]->element,
134 &context->data[ActiveQueue]->queue);
135 }
136
137 __code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) {
138 struct Element* last = queue->last;
139
140 if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) {
141 last->next = new_element;
142 queue->count++;
143
144 goto meta(context, context->next);
145 } else {
146 goto meta(context, PutQueue3);
147 }
148 }
149
150 __code putQueue3_stub(struct Context* context) {
151 goto putQueue3(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
152 }
153
154 __code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) {
155 if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) {
156 queue->last = new_element;
157 queue->count++;
158
159 goto meta(context, context->next);
160 } else {
161 goto meta(context, PutQueue3);
162 }
163 }
164
165 __code putQueue4_stub(struct Context* context) {
166 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
23 } 167 }
24 168
25 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { 169 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
26 int i = loopCounter->i; 170 int i = loopCounter->i;
27 171
59 __code taskManager_stub(struct Context* context) { 203 __code taskManager_stub(struct Context* context) {
60 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); 204 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
61 } 205 }
62 206
63 int main(int argc, char** argv) { 207 int main(int argc, char** argv) {
64 a = 10;
65 int cpu_num = (int)atoi(argv[1]); 208 int cpu_num = (int)atoi(argv[1]);
209 length = (int)atoi(argv[2]);
210
211 array_ptr = (int*)malloc(sizeof(int)*length);
212
213 for(int i=0; i<length; i++)
214 array_ptr[i]=i;
66 215
67 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context)); 216 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
68 initContext(main_context); 217 initContext(main_context);
69 main_context->next = CreateWorker; 218 //main_context->next = CreateWorker;
219 main_context->next = CreateData1;
70 220
71 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num); 221 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
72 222
73 struct Worker* worker = &main_context->data[Worker]->worker; 223 struct Worker* worker = &main_context->data[Worker]->worker;
74 worker->num = cpu_num; 224 worker->num = cpu_num;