Mercurial > hg > GearsTemplate
annotate src/parallel_execution/context.h @ 178:5077cf9bf54e
add TaskManager.c
author | mir3636 |
---|---|
date | Tue, 29 Nov 2016 18:09:28 +0900 |
parents | bb52f7f77bf5 |
children | 57a11c15ff4c |
rev | line source |
---|---|
86 | 1 /* Context definition for llrb example */ |
132 | 2 #ifndef CONTEXT_H |
3 #define CONTEXT_H | |
86 | 4 #include <pthread.h> |
101 | 5 #ifdef USE_CUDA |
98 | 6 #include <cuda.h> |
101 | 7 #endif |
86 | 8 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
9 #define ALLOCATE_SIZE 20000000 |
101 | 10 #define NEW(type) (type*)(calloc(1, sizeof(type))) |
11 #define NEWN(n, type) (type*)(calloc(n, sizeof(type))) | |
12 | |
133 | 13 #define ALLOC_DATA(context, dseg) ({\ |
14 struct Meta* meta = (struct Meta*)context->heap;\ | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
15 meta->type = D_##dseg;\ |
133 | 16 context->heap += sizeof(struct Meta);\ |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
17 context->data[D_##dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[D_##dseg]; }) |
101 | 18 |
133 | 19 #define ALLOC_DATA_TYPE(context, dseg, t) ({\ |
20 struct Meta* meta = (struct Meta*)context->heap;\ | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
21 meta->type = D_##t;\ |
133 | 22 context->heap += sizeof(struct Meta);\ |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
23 context->data[D_##dseg] = context->heap; context->heap += sizeof(struct t); (struct t *)context->data[D_##dseg]; }) |
86 | 24 |
132 | 25 #define ALLOCATE(context, t) ({ \ |
133 | 26 struct Meta* meta = (struct Meta*)context->heap;\ |
27 context->heap += sizeof(struct Meta);\ | |
28 union Data* data = context->heap; \ | |
29 context->heap += sizeof(struct t); \ | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
30 meta->type = D_##t; \ |
133 | 31 data; }) |
132 | 32 |
133 | 33 #define GET_TYPE(dseg) ({ \ |
34 struct Meta* meta = (struct Meta*)(((void*)dseg) - sizeof(struct Meta));\ | |
35 meta->type; }) | |
132 | 36 |
149 | 37 #define Gearef(context, t) (&(context)->data[D_##t]->t) |
86 | 38 enum Code { |
144 | 39 C_code1, |
40 C_code2, | |
41 C_code3, | |
42 C_code4, | |
43 C_code5, | |
44 C_find, | |
86 | 45 Not_find, |
46 Code6, | |
47 Allocator, | |
144 | 48 C_put, |
49 C_replaceNode, | |
50 C_replaceNode1, | |
51 C_insertNode, | |
86 | 52 Compare, |
144 | 53 C_rotateLeft, |
54 C_rotateLeft1, | |
55 C_rotateRight, | |
56 C_rotateRight1, | |
86 | 57 SetTree, |
144 | 58 C_insertCase1, |
59 C_insertCase2, | |
60 C_insertCase3, | |
61 C_insertCase4, | |
62 C_insertCase5, | |
63 C_insertCase51, | |
64 C_stackClear, | |
65 C_get, | |
66 C_search, | |
86 | 67 Delete, |
68 Delete1, | |
69 Delete2, | |
70 Delete3, | |
71 Replace_d1, | |
72 Replace_d2, | |
73 FindMax1, | |
74 FindMax2, | |
75 DeleteCase1, | |
76 DeleteCase2, | |
77 DeleteCase3, | |
78 DeleteCase4, | |
79 DeleteCase5, | |
80 DeleteCase6, | |
81 CreateWorker, | |
82 TaskManager, | |
90 | 83 CreateData1, |
84 CreateData2, | |
85 CreateTask1, | |
86 CreateTask2, | |
112 | 87 CreateTask3, |
88 CreateTask4, | |
90 | 89 PutQueue1, |
90 PutQueue2, | |
91 PutQueue3, | |
92 PutQueue4, | |
170 | 93 C_getTask1, |
94 C_getTask2, | |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
95 C_clearSingleLinkedStack, |
144 | 96 C_pushSingleLinkedStack, |
97 C_popSingleLinkedStack, | |
98 C_pop2SingleLinkedStack, | |
99 C_getSingleLinkedStack, | |
100 C_get2SingleLinkedStack, | |
101 C_isEmptySingleLinkedStack, | |
168
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
102 C_takeSingleLinkedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
103 C_putSingleLinkedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
104 C_isEmptySingleLinkedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
105 C_clearSingleLinkedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
106 C_takeSynchronizedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
107 C_putSynchronizedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
108 C_isEmptySynchronizedQueue, |
fa7419e2c67c
Add synchornizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
167
diff
changeset
|
109 C_clearSynchronizedQueue, |
170 | 110 C_putRedBlackTree, |
111 C_getRedBlackTree, | |
112 C_removeRedBlackTree, | |
113 C_clearRedBlackTree, | |
112 | 114 SpawnTask, |
92 | 115 Twice, |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
116 StartTime, |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
117 EndTime, |
86 | 118 Exit, |
119 }; | |
120 | |
121 enum Relational { | |
122 EQ, | |
123 GT, | |
124 LT, | |
125 }; | |
126 | |
130 | 127 enum DataType { |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
128 D_Worker, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
129 D_Allocate, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
130 D_SingleLinkedStack, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
131 D_Stack, |
171 | 132 D_RedBlackTree, |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
133 D_Tree, |
170 | 134 D_Task, |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
135 D_Traverse, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
136 D_RotateTree, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
137 D_Node, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
138 D_LoopCounter, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
139 D_Time, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
140 D_Element, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
141 D_ActiveQueue, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
142 D_WaitQueue, |
171 | 143 D_SingleLinkedQueue, |
144 D_SynchronizedQueue, | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
145 D_Queue |
86 | 146 }; |
147 | |
148 struct Context { | |
149 enum Code next; | |
150 int codeNum; | |
151 __code (**code) (struct Context*); | |
152 void* heapStart; | |
153 void* heap; | |
154 long heapLimit; | |
155 pthread_t thread; | |
92 | 156 int thread_num; |
87 | 157 int dataNum; |
86 | 158 union Data **data; |
159 }; | |
160 | |
161 union Data { | |
133 | 162 struct Meta { |
163 enum DataType type; | |
176 | 164 struct Queue* wait; // tasks waiting this dataGear |
133 | 165 } meta; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
166 struct Time { |
102 | 167 enum Code next; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
168 double time; |
149 | 169 } Time; |
86 | 170 struct LoopCounter { |
171 int i; | |
166 | 172 struct Tree* tree; |
86 | 173 } loopCounter; |
176 | 174 struct TaskManager { |
175 int numWorker; | |
176 enum Code spawn; | |
178 | 177 enum Code taskSend; |
176 | 178 enum Code shutdown; |
179 struct Worker* workers; | |
180 } | |
86 | 181 struct Worker { |
176 | 182 int id; |
86 | 183 struct Context* contexts; |
176 | 184 enum Code execute; |
185 enum Code taskSend; | |
186 enum Code taskRecive; | |
187 enum Code shutdown; | |
188 struct Queue* tasks; | |
86 | 189 } worker; |
101 | 190 #ifdef USE_CUDA |
98 | 191 struct CudaTask { |
192 CUdevice device; | |
193 CUcontext cuCtx; | |
194 CUfunction code; | |
195 CUdeviceptr* deviceptr; | |
196 CUstream stream; | |
197 } cudatask; | |
101 | 198 #endif |
177 | 199 struct Main { |
200 enum Code code; | |
201 enum Code next; | |
202 struct Queue* args; | |
203 } | |
90 | 204 struct Task { |
205 enum Code code; | |
176 | 206 struct Queue* dataGears; |
112 | 207 int idsCount; |
166 | 208 } Task; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
209 // Queue Interface |
90 | 210 struct Queue { |
136 | 211 union Data* queue; |
167 | 212 union Data* data; |
213 enum Code whenEmpty; | |
214 enum Code clear; | |
215 enum Code put; | |
216 enum Code take; | |
217 enum Code isEmpty; | |
130 | 218 enum Code next; |
166 | 219 } Queue; |
167 | 220 struct SingleLinkedQueue { |
221 struct Element* top; | |
222 struct Element* last; | |
223 } SingleLinkedQueue; | |
132 | 224 // Stack Interface |
130 | 225 struct Stack { |
132 | 226 union Data* stack; |
130 | 227 union Data* data; |
133 | 228 union Data* data1; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
229 enum Code whenEmpty; |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
230 enum Code clear; |
130 | 231 enum Code push; |
232 enum Code pop; | |
133 | 233 enum Code pop2; |
130 | 234 enum Code isEmpty; |
134
2eccf4564efe
fix stack call in rb_tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
235 enum Code get; |
133 | 236 enum Code get2; |
130 | 237 enum Code next; |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
238 } stack; |
132 | 239 // Stack implementations |
240 struct SingleLinkedStack { | |
241 struct Element* top; | |
135 | 242 } singleLinkedStack; |
132 | 243 struct ArrayStack { |
244 int size; | |
245 int limit; | |
246 struct Element* array; | |
247 } arrayStack; | |
248 // Stack implementation end | |
90 | 249 struct Element { |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
250 union Data* data; |
90 | 251 struct Element* next; |
252 } element; | |
86 | 253 struct Array { |
90 | 254 int index; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
255 int prefix; |
86 | 256 int* array; |
257 } array; | |
258 struct Tree { | |
166 | 259 union Data* tree; |
260 struct Node* node; | |
152 | 261 enum Code put; |
262 enum Code get; | |
263 enum Code remove; | |
171 | 264 enum Code clear; |
152 | 265 enum Code next; |
166 | 266 } Tree; |
172 | 267 struct RedBlackTree { |
152 | 268 struct Node* root; |
122 | 269 struct Node* current; // reading node of original tree |
138 | 270 struct Node* previous; // parent of reading node of original tree |
124 | 271 struct Node* newNode; // writing node of new tree |
143
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
272 struct Node* parent; |
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
273 struct Node* grandparent; |
132 | 274 struct Stack* nodeStack; |
86 | 275 int result; |
172 | 276 } RedBlackTree; |
147 | 277 struct RotateTree { |
278 enum Code next; | |
172 | 279 struct RedBlackTree* traverse; |
147 | 280 struct Tree* tree; |
281 } rotateTree; | |
86 | 282 struct Node { |
283 int key; // comparable data segment | |
90 | 284 union Data* value; |
86 | 285 struct Node* left; |
286 struct Node* right; | |
287 // need to balancing | |
288 enum Color { | |
289 Red, | |
290 Black, | |
291 } color; | |
292 } node; | |
293 struct Allocate { | |
294 enum Code next; | |
295 long size; | |
296 } allocate; | |
112 | 297 struct OutPutDataSegments { |
298 union Data **data; | |
299 } ods; | |
86 | 300 }; |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
301 |
147 | 302 // typedef struct RotateTree D_RotateTree; |
149 | 303 typedef struct Time Time; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
304 typedef struct LoopCounter LoopCounter; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
305 typedef struct Worker Worker; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
306 typedef struct CudaTask CudaTask; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
307 typedef struct Task Task; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
308 typedef struct Queue Queue; |
167 | 309 typedef struct SingleLinkedQueue SingleLinkedQueue; |
171 | 310 typedef struct SynchronizedQueue SynchronizedQueue; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
311 typedef struct Stack Stack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
312 typedef struct SingleLinkedStack SingleLinkedStack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
313 typedef struct ArrayStack ArrayStack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
314 typedef struct Element Element; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
315 typedef struct Array Array; |
171 | 316 typedef struct RedBlackTree RedBlackTree; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
317 typedef struct Tree Tree; |
151 | 318 typedef struct RotateTree RotateTree; |
319 typedef struct Node Node; | |
320 typedef struct Allocate Allocate; | |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
321 typedef struct OutputDataSegments OutputDataSegments; |
147 | 322 |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
323 union MetaData { |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
324 struct Queue waitMeTasks; |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
325 struct Queue waitI; |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
326 }; |
132 | 327 #endif |