Mercurial > hg > GearsTemplate
annotate src/parallel_execution/context.h @ 304:9755206813cb
helper_string.h for ANSI C
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 14 Feb 2017 11:36:41 +0900 |
parents | 8e7926f3e271 |
children | ec0a5b4fba05 |
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;\ |
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 | |
288 | 48 #define GET_META(dseg) ((struct Meta*)(((void*)dseg) - sizeof(struct Meta))) |
49 #define GET_TYPE(dseg) (GET_META(dseg)->type) | |
50 #define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait) | |
132 | 51 |
149 | 52 #define Gearef(context, t) (&(context)->data[D_##t]->t) |
186 | 53 |
232 | 54 // (struct SingleLinkedStack *)context->data[D_Stack]->Stack.stack->Stack.stack |
55 | |
56 #define GearImpl(context, intf, name) (Gearef(context, intf)->name->intf.name) | |
57 | |
186 | 58 #include "c/enumCode.h" |
86 | 59 |
60 enum Relational { | |
61 EQ, | |
62 GT, | |
63 LT, | |
64 }; | |
65 | |
186 | 66 #include "c/enumData.h" |
86 | 67 |
68 struct Context { | |
69 enum Code next; | |
230 | 70 struct Worker* worker; |
71 struct TaskManager* taskManager; | |
86 | 72 int codeNum; |
73 __code (**code) (struct Context*); | |
74 void* heapStart; | |
75 void* heap; | |
76 long heapLimit; | |
87 | 77 int dataNum; |
236 | 78 int idgCount; //number of waiting dataGear |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
79 int odg; |
288 | 80 int maxOdg; |
242 | 81 int workerId; |
86 | 82 union Data **data; |
83 }; | |
84 | |
85 union Data { | |
133 | 86 struct Meta { |
87 enum DataType type; | |
236 | 88 long size; |
176 | 89 struct Queue* wait; // tasks waiting this dataGear |
133 | 90 } meta; |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
91 struct Context Context; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
92 struct Time { |
102 | 93 enum Code next; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
94 double time; |
149 | 95 } Time; |
86 | 96 struct LoopCounter { |
97 int i; | |
166 | 98 struct Tree* tree; |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
99 } LoopCounter; |
176 | 100 struct TaskManager { |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
101 union Data* taskManager; |
234 | 102 enum Code createTask; // create NEW contexts for execution & argument |
103 enum Code spawn; // start NEW context on the worker | |
176 | 104 enum Code shutdown; |
233 | 105 |
184 | 106 enum Code next; |
233 | 107 enum Code task; |
108 struct Context* context; | |
109 int worker; | |
110 int cpu; | |
111 int gpu; | |
112 int io; | |
237 | 113 int maxCPU; |
184 | 114 } TaskManager; |
182 | 115 struct TaskManagerImpl { |
233 | 116 int numWorker; |
242 | 117 int sendWorkerIndex; |
239 | 118 pthread_mutex_t mutex; |
184 | 119 struct Queue* activeQueue; |
120 struct Queue* taskQueue; | |
244 | 121 struct Worker** workers; |
184 | 122 } TaskManagerImpl; |
86 | 123 struct Worker { |
232 | 124 union Data* worker; |
222
77faa28128b4
Add taskSend for TaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
218
diff
changeset
|
125 enum Code taskReceive; |
176 | 126 enum Code shutdown; |
230 | 127 enum Code next; |
244 | 128 struct Queue* tasks; |
288 | 129 struct TaskManager* taskManager; |
184 | 130 } Worker; |
232 | 131 struct CPUWorker { |
233 | 132 pthread_t thread; |
242 | 133 pthread_mutex_t mutex; |
134 pthread_cond_t cond; | |
232 | 135 struct Context* context; |
136 int id; | |
137 } CPUWorker; | |
302 | 138 #ifdef USE_CUDAWorker |
139 struct CUDAWorker { | |
233 | 140 pthread_t thread; |
232 | 141 struct Context* context; |
142 int id; | |
143 struct Queue* tasks; | |
144 int runFlag; | |
145 enum Code next; | |
98 | 146 CUdevice device; |
147 CUcontext cuCtx; | |
148 CUfunction code; | |
149 CUdeviceptr* deviceptr; | |
150 CUstream stream; | |
232 | 151 } CudaWorker; |
187 | 152 #else |
302 | 153 struct CUDAWorker { |
232 | 154 } CudaWorker; |
101 | 155 #endif |
177 | 156 struct Main { |
157 enum Code code; | |
158 enum Code next; | |
159 struct Queue* args; | |
185 | 160 } Main; |
90 | 161 struct Task { |
162 enum Code code; | |
176 | 163 struct Queue* dataGears; |
112 | 164 int idsCount; |
166 | 165 } Task; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
166 // Queue Interface |
90 | 167 struct Queue { |
136 | 168 union Data* queue; |
167 | 169 union Data* data; |
170 enum Code whenEmpty; | |
171 enum Code clear; | |
172 enum Code put; | |
173 enum Code take; | |
174 enum Code isEmpty; | |
130 | 175 enum Code next; |
184 | 176 } Queue; |
167 | 177 struct SingleLinkedQueue { |
178 struct Element* top; | |
179 struct Element* last; | |
184 | 180 } SingleLinkedQueue; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
181 struct SynchronizedQueue { |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
182 struct Element* top; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
183 struct Element* last; |
287
6b099d73949c
Add queueCount sem to SynchronizedQueue
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
286
diff
changeset
|
184 struct Semaphore* queueCount; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
185 } SynchronizedQueue; |
132 | 186 // Stack Interface |
130 | 187 struct Stack { |
132 | 188 union Data* stack; |
130 | 189 union Data* data; |
133 | 190 union Data* data1; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
191 enum Code whenEmpty; |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
192 enum Code clear; |
130 | 193 enum Code push; |
194 enum Code pop; | |
133 | 195 enum Code pop2; |
130 | 196 enum Code isEmpty; |
134
2eccf4564efe
fix stack call in rb_tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
197 enum Code get; |
133 | 198 enum Code get2; |
130 | 199 enum Code next; |
194 | 200 } Stack; |
132 | 201 // Stack implementations |
202 struct SingleLinkedStack { | |
203 struct Element* top; | |
194 | 204 } SingleLinkedStack; |
132 | 205 struct ArrayStack { |
206 int size; | |
207 int limit; | |
208 struct Element* array; | |
194 | 209 } ArrayStack; |
132 | 210 // Stack implementation end |
90 | 211 struct Element { |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
212 union Data* data; |
90 | 213 struct Element* next; |
194 | 214 } Element; |
86 | 215 struct Array { |
90 | 216 int index; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
217 int prefix; |
86 | 218 int* array; |
194 | 219 } Array; |
86 | 220 struct Tree { |
166 | 221 union Data* tree; |
222 struct Node* node; | |
152 | 223 enum Code put; |
224 enum Code get; | |
225 enum Code remove; | |
171 | 226 enum Code clear; |
152 | 227 enum Code next; |
166 | 228 } Tree; |
172 | 229 struct RedBlackTree { |
152 | 230 struct Node* root; |
122 | 231 struct Node* current; // reading node of original tree |
138 | 232 struct Node* previous; // parent of reading node of original tree |
124 | 233 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
|
234 struct Node* parent; |
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
235 struct Node* grandparent; |
132 | 236 struct Stack* nodeStack; |
86 | 237 int result; |
172 | 238 } RedBlackTree; |
147 | 239 struct RotateTree { |
240 enum Code next; | |
172 | 241 struct RedBlackTree* traverse; |
147 | 242 struct Tree* tree; |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
243 } RotateTree; |
86 | 244 struct Node { |
245 int key; // comparable data segment | |
90 | 246 union Data* value; |
86 | 247 struct Node* left; |
248 struct Node* right; | |
249 // need to balancing | |
250 enum Color { | |
251 Red, | |
252 Black, | |
253 } color; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
254 } Node; |
286 | 255 struct Semaphore { |
256 union Data* semaphore; | |
257 enum Code p; | |
258 enum Code v; | |
259 enum Code next; | |
260 } Semaphore; | |
261 struct SemaphoreImpl { | |
262 int value; | |
263 pthread_mutex_t mutex; | |
264 pthread_cond_t cond; | |
265 } SemaphoreImpl; | |
86 | 266 struct Allocate { |
267 enum Code next; | |
268 long size; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
269 } Allocate; |
112 | 270 struct OutPutDataSegments { |
271 union Data **data; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
272 } Ods; |
254 | 273 }; // 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
|
274 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
275 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
|
276 |
186 | 277 #include "c/typedefData.h" |
278 | 278 |
279 #include "c/extern.h" | |
280 | |
281 extern __code start_code(struct Context* context); | |
282 extern __code exit_code(struct Context* context); | |
283 extern __code meta(struct Context* context, enum Code next); | |
284 extern void initContext(struct Context* context); | |
285 | |
132 | 286 #endif |