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