Mercurial > hg > GearsTemplate
annotate src/parallel_execution/context.h @ 358:98c6e13d8ec7
add sort.cbc
author | Nozomi Teruya <e125769@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 24 Jun 2017 20:07:27 +0900 |
parents | b8be1d51f002 |
children | e68080586c15 |
rev | line source |
---|---|
86 | 1 /* Context definition for llrb example */ |
132 | 2 #ifndef CONTEXT_H |
3 #define CONTEXT_H | |
207
7470b8382672
Fix compile error to stack_test
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
194
diff
changeset
|
4 #include <stdlib.h> |
86 | 5 #include <pthread.h> |
304
9755206813cb
helper_string.h for ANSI C
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
302
diff
changeset
|
6 #ifdef USE_CUDAWorker |
98 | 7 #include <cuda.h> |
101 | 8 #endif |
86 | 9 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
10 #define ALLOCATE_SIZE 20000000 |
101 | 11 #define NEW(type) (type*)(calloc(1, sizeof(type))) |
12 #define NEWN(n, type) (type*)(calloc(n, sizeof(type))) | |
13 | |
133 | 14 #define ALLOC_DATA(context, dseg) ({\ |
15 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
|
16 meta->type = D_##dseg;\ |
236 | 17 meta->size = 1;\ |
133 | 18 context->heap += sizeof(struct Meta);\ |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
19 context->data[D_##dseg] = context->heap; context->heap += sizeof(struct dseg); (struct dseg *)context->data[D_##dseg]; }) |
101 | 20 |
133 | 21 #define ALLOC_DATA_TYPE(context, dseg, t) ({\ |
22 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
|
23 meta->type = D_##t;\ |
236 | 24 meta->size = 1;\ |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
25 context->heap += sizeof(struct Meta);\ |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
26 context->data[D_##dseg] = context->heap; context->heap += sizeof(struct t); (struct t *)context->data[D_##dseg]; }) |
86 | 27 |
132 | 28 #define ALLOCATE(context, t) ({ \ |
133 | 29 struct Meta* meta = (struct Meta*)context->heap;\ |
30 context->heap += sizeof(struct Meta);\ | |
31 union Data* data = context->heap; \ | |
32 context->heap += sizeof(struct t); \ | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
147
diff
changeset
|
33 meta->type = D_##t; \ |
236 | 34 meta->size = 1; \ |
133 | 35 data; }) |
132 | 36 |
233 | 37 #define ALLOC(context, t) (&ALLOCATE(context, t)->t) |
38 | |
237 | 39 #define ALLOC_ARRAY(context, dseg, len) ({\ |
236 | 40 struct Meta* meta = (struct Meta*)context->heap;\ |
41 context->heap += sizeof(struct Meta);\ | |
42 union Data* data = context->heap; \ | |
244 | 43 context->heap += sizeof(struct dseg *)*len; \ |
236 | 44 meta->type = D_##dseg; \ |
237 | 45 meta->size = len; \ |
236 | 46 data; }) |
47 | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
48 #define ALLOCATE_DATA_GEAR(context, t) ({ \ |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
49 union Data* data = ALLOCATE(context, t); \ |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
50 struct Meta* meta = GET_META(data); \ |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
51 meta->wait = createSingleLinkedQueue(context); \ |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
52 data; }) |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
53 |
288 | 54 #define GET_META(dseg) ((struct Meta*)(((void*)dseg) - sizeof(struct Meta))) |
55 #define GET_TYPE(dseg) (GET_META(dseg)->type) | |
56 #define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait) | |
132 | 57 |
149 | 58 #define Gearef(context, t) (&(context)->data[D_##t]->t) |
186 | 59 |
232 | 60 // (struct SingleLinkedStack *)context->data[D_Stack]->Stack.stack->Stack.stack |
61 | |
62 #define GearImpl(context, intf, name) (Gearef(context, intf)->name->intf.name) | |
63 | |
186 | 64 #include "c/enumCode.h" |
86 | 65 |
66 enum Relational { | |
67 EQ, | |
68 GT, | |
69 LT, | |
70 }; | |
71 | |
186 | 72 #include "c/enumData.h" |
86 | 73 |
74 struct Context { | |
75 enum Code next; | |
230 | 76 struct Worker* worker; |
77 struct TaskManager* taskManager; | |
86 | 78 int codeNum; |
79 __code (**code) (struct Context*); | |
80 void* heapStart; | |
81 void* heap; | |
82 long heapLimit; | |
87 | 83 int dataNum; |
236 | 84 int idgCount; //number of waiting dataGear |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
85 int idg; |
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
86 int maxIdg; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
87 int odg; |
288 | 88 int maxOdg; |
242 | 89 int workerId; |
308 | 90 #ifdef USE_CUDAWorker |
91 int num_exec; | |
92 CUmodule module; | |
93 CUfunction function; | |
309 | 94 #endif |
86 | 95 union Data **data; |
96 }; | |
97 | |
98 union Data { | |
133 | 99 struct Meta { |
100 enum DataType type; | |
236 | 101 long size; |
176 | 102 struct Queue* wait; // tasks waiting this dataGear |
133 | 103 } meta; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
104 struct Context Context; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
105 struct Time { |
102 | 106 enum Code next; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
107 double time; |
149 | 108 } Time; |
86 | 109 struct LoopCounter { |
110 int i; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
111 } LoopCounter; |
344
b8be1d51f002
Add CodeGear interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
342
diff
changeset
|
112 struct CodeGear { |
b8be1d51f002
Add CodeGear interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
342
diff
changeset
|
113 union Data* CodeGear; |
342 | 114 enum Code code; |
344
b8be1d51f002
Add CodeGear interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
342
diff
changeset
|
115 union Data* dataGears[10]; |
b8be1d51f002
Add CodeGear interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
342
diff
changeset
|
116 enum Code next; |
b8be1d51f002
Add CodeGear interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
342
diff
changeset
|
117 } CodeGear; |
176 | 118 struct TaskManager { |
320
f730761bb044
non CUDA case clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
316
diff
changeset
|
119 #ifdef USE_CUDA_MAIN_THREAD |
f730761bb044
non CUDA case clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
316
diff
changeset
|
120 volatile |
f730761bb044
non CUDA case clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
316
diff
changeset
|
121 #endif |
f730761bb044
non CUDA case clean up
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
316
diff
changeset
|
122 union Data* taskManager; |
234 | 123 enum Code createTask; // create NEW contexts for execution & argument |
124 enum Code spawn; // start NEW context on the worker | |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
125 enum Code setWaitTask; |
176 | 126 enum Code shutdown; |
233 | 127 |
184 | 128 enum Code next; |
233 | 129 enum Code task; |
130 struct Context* context; | |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
131 union Data* data; |
233 | 132 int worker; |
133 int cpu; | |
134 int gpu; | |
135 int io; | |
237 | 136 int maxCPU; |
184 | 137 } TaskManager; |
182 | 138 struct TaskManagerImpl { |
233 | 139 int numWorker; |
242 | 140 int sendWorkerIndex; |
239 | 141 pthread_mutex_t mutex; |
184 | 142 struct Queue* activeQueue; |
143 struct Queue* taskQueue; | |
244 | 144 struct Worker** workers; |
184 | 145 } TaskManagerImpl; |
86 | 146 struct Worker { |
232 | 147 union Data* worker; |
222
77faa28128b4
Add taskSend for TaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
218
diff
changeset
|
148 enum Code taskReceive; |
176 | 149 enum Code shutdown; |
230 | 150 enum Code next; |
244 | 151 struct Queue* tasks; |
288 | 152 struct TaskManager* taskManager; |
184 | 153 } Worker; |
232 | 154 struct CPUWorker { |
233 | 155 pthread_t thread; |
242 | 156 pthread_mutex_t mutex; |
157 pthread_cond_t cond; | |
232 | 158 struct Context* context; |
159 int id; | |
160 } CPUWorker; | |
302 | 161 #ifdef USE_CUDAWorker |
162 struct CUDAWorker { | |
316
54d203daf06b
CUDAtwice.cbc is called.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
309
diff
changeset
|
163 CUdevice device; |
54d203daf06b
CUDAtwice.cbc is called.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
309
diff
changeset
|
164 CUcontext cuCtx; |
233 | 165 pthread_t thread; |
232 | 166 struct Context* context; |
167 int id; | |
168 struct Queue* tasks; | |
169 int runFlag; | |
170 enum Code next; | |
305 | 171 int num_stream; |
172 CUstream *stream; | |
173 } CUDAWorker; | |
187 | 174 #else |
302 | 175 struct CUDAWorker { |
305 | 176 } CUDAWorker; |
101 | 177 #endif |
177 | 178 struct Main { |
179 enum Code code; | |
180 enum Code next; | |
181 struct Queue* args; | |
185 | 182 } Main; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
183 // Queue Interface |
90 | 184 struct Queue { |
136 | 185 union Data* queue; |
167 | 186 union Data* data; |
187 enum Code whenEmpty; | |
188 enum Code clear; | |
189 enum Code put; | |
190 enum Code take; | |
191 enum Code isEmpty; | |
130 | 192 enum Code next; |
184 | 193 } Queue; |
167 | 194 struct SingleLinkedQueue { |
195 struct Element* top; | |
196 struct Element* last; | |
184 | 197 } SingleLinkedQueue; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
198 struct SynchronizedQueue { |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
199 struct Element* top; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
200 struct Element* last; |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
286
diff
changeset
|
201 struct Semaphore* queueCount; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
202 } SynchronizedQueue; |
132 | 203 // Stack Interface |
130 | 204 struct Stack { |
132 | 205 union Data* stack; |
130 | 206 union Data* data; |
133 | 207 union Data* data1; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
208 enum Code whenEmpty; |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
209 enum Code clear; |
130 | 210 enum Code push; |
211 enum Code pop; | |
133 | 212 enum Code pop2; |
130 | 213 enum Code isEmpty; |
134
2eccf4564efe
fix stack call in rb_tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
214 enum Code get; |
133 | 215 enum Code get2; |
130 | 216 enum Code next; |
194 | 217 } Stack; |
132 | 218 // Stack implementations |
219 struct SingleLinkedStack { | |
220 struct Element* top; | |
194 | 221 } SingleLinkedStack; |
132 | 222 struct ArrayStack { |
223 int size; | |
224 int limit; | |
225 struct Element* array; | |
194 | 226 } ArrayStack; |
132 | 227 // Stack implementation end |
90 | 228 struct Element { |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
229 union Data* data; |
90 | 230 struct Element* next; |
194 | 231 } Element; |
86 | 232 struct Array { |
308 | 233 int size; |
90 | 234 int index; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
235 int prefix; |
86 | 236 int* array; |
194 | 237 } Array; |
86 | 238 struct Tree { |
166 | 239 union Data* tree; |
240 struct Node* node; | |
152 | 241 enum Code put; |
242 enum Code get; | |
243 enum Code remove; | |
171 | 244 enum Code clear; |
152 | 245 enum Code next; |
166 | 246 } Tree; |
172 | 247 struct RedBlackTree { |
152 | 248 struct Node* root; |
122 | 249 struct Node* current; // reading node of original tree |
138 | 250 struct Node* previous; // parent of reading node of original tree |
124 | 251 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
|
252 struct Node* parent; |
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
253 struct Node* grandparent; |
132 | 254 struct Stack* nodeStack; |
86 | 255 int result; |
172 | 256 } RedBlackTree; |
147 | 257 struct RotateTree { |
258 enum Code next; | |
172 | 259 struct RedBlackTree* traverse; |
147 | 260 struct Tree* tree; |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
261 } RotateTree; |
86 | 262 struct Node { |
263 int key; // comparable data segment | |
90 | 264 union Data* value; |
86 | 265 struct Node* left; |
266 struct Node* right; | |
267 // need to balancing | |
268 enum Color { | |
269 Red, | |
270 Black, | |
271 } color; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
272 } Node; |
286 | 273 struct Semaphore { |
274 union Data* semaphore; | |
275 enum Code p; | |
276 enum Code v; | |
277 enum Code next; | |
278 } Semaphore; | |
279 struct SemaphoreImpl { | |
280 int value; | |
281 pthread_mutex_t mutex; | |
282 pthread_cond_t cond; | |
283 } SemaphoreImpl; | |
86 | 284 struct Allocate { |
285 enum Code next; | |
286 long size; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
287 } Allocate; |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
288 struct Integer { |
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
289 int value; |
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
320
diff
changeset
|
290 } Integer; |
358 | 291 struct SortArray {//そもそもこれは必要なのか? |
292 struct Integer **array;//Array arrayじゃできない? | |
293 int loop_counter; | |
294 enum Code make_array; | |
295 enum Code print; | |
296 }SortArray; | |
254 | 297 }; // union Data end this is necessary for context generator |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
298 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
299 typedef union Data Data; |
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
300 |
186 | 301 #include "c/typedefData.h" |
278 | 302 |
303 #include "c/extern.h" | |
304 | |
305 extern __code start_code(struct Context* context); | |
306 extern __code exit_code(struct Context* context); | |
307 extern __code meta(struct Context* context, enum Code next); | |
308 extern void initContext(struct Context* context); | |
309 | |
132 | 310 #endif |