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