Mercurial > hg > GearsTemplate
annotate src/parallel_execution/context.h @ 183:dec50dd624c7
fix
author | mir3636 |
---|---|
date | Thu, 15 Dec 2016 17:31:38 +0900 |
parents | 57a11c15ff4c |
children | 21840c7fb309 |
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, | |
182 | 114 C_QueueTest1, |
115 C_QueueTest2, | |
116 C_QueueTest3, | |
117 C_QueueTest4, | |
112 | 118 SpawnTask, |
92 | 119 Twice, |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
120 StartTime, |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
121 EndTime, |
86 | 122 Exit, |
123 }; | |
124 | |
125 enum Relational { | |
126 EQ, | |
127 GT, | |
128 LT, | |
129 }; | |
130 | |
130 | 131 enum DataType { |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
132 D_Worker, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
133 D_Allocate, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
134 D_SingleLinkedStack, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
135 D_Stack, |
171 | 136 D_RedBlackTree, |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
137 D_Tree, |
182 | 138 D_TaskManager, |
139 D_TaskManagerImpl, | |
170 | 140 D_Task, |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
141 D_Traverse, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
142 D_RotateTree, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
143 D_Node, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
144 D_LoopCounter, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
145 D_Time, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
146 D_Element, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
147 D_ActiveQueue, |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
148 D_WaitQueue, |
171 | 149 D_SingleLinkedQueue, |
150 D_SynchronizedQueue, | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
151 D_Queue |
86 | 152 }; |
153 | |
154 struct Context { | |
155 enum Code next; | |
156 int codeNum; | |
157 __code (**code) (struct Context*); | |
158 void* heapStart; | |
159 void* heap; | |
160 long heapLimit; | |
161 pthread_t thread; | |
92 | 162 int thread_num; |
87 | 163 int dataNum; |
86 | 164 union Data **data; |
165 }; | |
166 | |
167 union Data { | |
133 | 168 struct Meta { |
169 enum DataType type; | |
176 | 170 struct Queue* wait; // tasks waiting this dataGear |
133 | 171 } meta; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
172 struct Time { |
102 | 173 enum Code next; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
174 double time; |
149 | 175 } Time; |
86 | 176 struct LoopCounter { |
177 int i; | |
166 | 178 struct Tree* tree; |
86 | 179 } loopCounter; |
176 | 180 struct TaskManager { |
181 int numWorker; | |
182 enum Code spawn; | |
183 enum Code shutdown; | |
184 struct Worker* workers; | |
182 | 185 } taskManager; |
186 struct TaskManagerImpl { | |
187 struct Queue *activeQueue; | |
188 struct Queue *waitQueue; | |
189 } taskManagerImpl; | |
86 | 190 struct Worker { |
176 | 191 int id; |
86 | 192 struct Context* contexts; |
176 | 193 enum Code execute; |
194 enum Code taskSend; | |
195 enum Code taskRecive; | |
196 enum Code shutdown; | |
197 struct Queue* tasks; | |
86 | 198 } worker; |
101 | 199 #ifdef USE_CUDA |
98 | 200 struct CudaTask { |
201 CUdevice device; | |
202 CUcontext cuCtx; | |
203 CUfunction code; | |
204 CUdeviceptr* deviceptr; | |
205 CUstream stream; | |
206 } cudatask; | |
101 | 207 #endif |
177 | 208 struct Main { |
209 enum Code code; | |
210 enum Code next; | |
211 struct Queue* args; | |
182 | 212 } main; |
90 | 213 struct Task { |
214 enum Code code; | |
176 | 215 struct Queue* dataGears; |
112 | 216 int idsCount; |
166 | 217 } Task; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
218 // Queue Interface |
90 | 219 struct Queue { |
136 | 220 union Data* queue; |
167 | 221 union Data* data; |
222 enum Code whenEmpty; | |
223 enum Code clear; | |
224 enum Code put; | |
225 enum Code take; | |
226 enum Code isEmpty; | |
130 | 227 enum Code next; |
182 | 228 } queue; |
167 | 229 struct SingleLinkedQueue { |
230 struct Element* top; | |
231 struct Element* last; | |
182 | 232 } singleLinkedQueue; |
132 | 233 // Stack Interface |
130 | 234 struct Stack { |
132 | 235 union Data* stack; |
130 | 236 union Data* data; |
133 | 237 union Data* data1; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
238 enum Code whenEmpty; |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
239 enum Code clear; |
130 | 240 enum Code push; |
241 enum Code pop; | |
133 | 242 enum Code pop2; |
130 | 243 enum Code isEmpty; |
134
2eccf4564efe
fix stack call in rb_tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
244 enum Code get; |
133 | 245 enum Code get2; |
130 | 246 enum Code next; |
131
a4507906938c
Fix compile error but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
130
diff
changeset
|
247 } stack; |
132 | 248 // Stack implementations |
249 struct SingleLinkedStack { | |
250 struct Element* top; | |
135 | 251 } singleLinkedStack; |
132 | 252 struct ArrayStack { |
253 int size; | |
254 int limit; | |
255 struct Element* array; | |
256 } arrayStack; | |
257 // Stack implementation end | |
90 | 258 struct Element { |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
259 union Data* data; |
90 | 260 struct Element* next; |
261 } element; | |
86 | 262 struct Array { |
90 | 263 int index; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
264 int prefix; |
86 | 265 int* array; |
266 } array; | |
267 struct Tree { | |
166 | 268 union Data* tree; |
269 struct Node* node; | |
152 | 270 enum Code put; |
271 enum Code get; | |
272 enum Code remove; | |
171 | 273 enum Code clear; |
152 | 274 enum Code next; |
166 | 275 } Tree; |
172 | 276 struct RedBlackTree { |
152 | 277 struct Node* root; |
122 | 278 struct Node* current; // reading node of original tree |
138 | 279 struct Node* previous; // parent of reading node of original tree |
124 | 280 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
|
281 struct Node* parent; |
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
282 struct Node* grandparent; |
132 | 283 struct Stack* nodeStack; |
86 | 284 int result; |
172 | 285 } RedBlackTree; |
147 | 286 struct RotateTree { |
287 enum Code next; | |
172 | 288 struct RedBlackTree* traverse; |
147 | 289 struct Tree* tree; |
290 } rotateTree; | |
86 | 291 struct Node { |
292 int key; // comparable data segment | |
90 | 293 union Data* value; |
86 | 294 struct Node* left; |
295 struct Node* right; | |
296 // need to balancing | |
297 enum Color { | |
298 Red, | |
299 Black, | |
300 } color; | |
301 } node; | |
302 struct Allocate { | |
303 enum Code next; | |
304 long size; | |
305 } allocate; | |
112 | 306 struct OutPutDataSegments { |
307 union Data **data; | |
308 } ods; | |
86 | 309 }; |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
310 |
147 | 311 // typedef struct RotateTree D_RotateTree; |
149 | 312 typedef struct Time Time; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
313 typedef struct LoopCounter LoopCounter; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
314 typedef struct Worker Worker; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
315 typedef struct CudaTask CudaTask; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
316 typedef struct Task Task; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
317 typedef struct Queue Queue; |
167 | 318 typedef struct SingleLinkedQueue SingleLinkedQueue; |
171 | 319 typedef struct SynchronizedQueue SynchronizedQueue; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
320 typedef struct Stack Stack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
321 typedef struct SingleLinkedStack SingleLinkedStack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
322 typedef struct ArrayStack ArrayStack; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
323 typedef struct Element Element; |
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
324 typedef struct Array Array; |
171 | 325 typedef struct RedBlackTree RedBlackTree; |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
326 typedef struct Tree Tree; |
151 | 327 typedef struct RotateTree RotateTree; |
328 typedef struct Node Node; | |
329 typedef struct Allocate Allocate; | |
150
8c687b8fabf2
Add typedef to define Code Gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
149
diff
changeset
|
330 typedef struct OutputDataSegments OutputDataSegments; |
147 | 331 |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
332 union MetaData { |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
333 struct Queue waitMeTasks; |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
334 struct Queue waitI; |
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
335 }; |
132 | 336 #endif |