Mercurial > hg > GearsTemplate
annotate src/parallel_execution/context.h @ 282:a3448b0f0a56
Add input data gear
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 05 Feb 2017 04:08:30 +0900 |
parents | 23767f714f4a |
children | fd470e090403 |
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; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
278
diff
changeset
|
182 } SynchronizedQueue; |
132 | 183 // Stack Interface |
130 | 184 struct Stack { |
132 | 185 union Data* stack; |
130 | 186 union Data* data; |
133 | 187 union Data* data1; |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
168
diff
changeset
|
188 enum Code whenEmpty; |
145
cc071cf1ba85
add stack clear interface
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
189 enum Code clear; |
130 | 190 enum Code push; |
191 enum Code pop; | |
133 | 192 enum Code pop2; |
130 | 193 enum Code isEmpty; |
134
2eccf4564efe
fix stack call in rb_tree
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
133
diff
changeset
|
194 enum Code get; |
133 | 195 enum Code get2; |
130 | 196 enum Code next; |
194 | 197 } Stack; |
132 | 198 // Stack implementations |
199 struct SingleLinkedStack { | |
200 struct Element* top; | |
194 | 201 } SingleLinkedStack; |
132 | 202 struct ArrayStack { |
203 int size; | |
204 int limit; | |
205 struct Element* array; | |
194 | 206 } ArrayStack; |
132 | 207 // Stack implementation end |
90 | 208 struct Element { |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
209 union Data* data; |
90 | 210 struct Element* next; |
194 | 211 } Element; |
86 | 212 struct Array { |
90 | 213 int index; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
214 int prefix; |
86 | 215 int* array; |
194 | 216 } Array; |
86 | 217 struct Tree { |
166 | 218 union Data* tree; |
219 struct Node* node; | |
152 | 220 enum Code put; |
221 enum Code get; | |
222 enum Code remove; | |
171 | 223 enum Code clear; |
152 | 224 enum Code next; |
166 | 225 } Tree; |
172 | 226 struct RedBlackTree { |
152 | 227 struct Node* root; |
122 | 228 struct Node* current; // reading node of original tree |
138 | 229 struct Node* previous; // parent of reading node of original tree |
124 | 230 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
|
231 struct Node* parent; |
34a7a21edc36
recude stack get using traverse field
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
138
diff
changeset
|
232 struct Node* grandparent; |
132 | 233 struct Stack* nodeStack; |
86 | 234 int result; |
172 | 235 } RedBlackTree; |
147 | 236 struct RotateTree { |
237 enum Code next; | |
172 | 238 struct RedBlackTree* traverse; |
147 | 239 struct Tree* tree; |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
240 } RotateTree; |
86 | 241 struct Node { |
242 int key; // comparable data segment | |
90 | 243 union Data* value; |
86 | 244 struct Node* left; |
245 struct Node* right; | |
246 // need to balancing | |
247 enum Color { | |
248 Red, | |
249 Black, | |
250 } color; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
251 } Node; |
86 | 252 struct Allocate { |
253 enum Code next; | |
254 long size; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
255 } Allocate; |
112 | 256 struct OutPutDataSegments { |
257 union Data **data; | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
207
diff
changeset
|
258 } Ods; |
254 | 259 }; // 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
|
260 |
259
195518ab62fc
fix type gathering pattern match in generate_stub.pl
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
254
diff
changeset
|
261 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
|
262 |
186 | 263 #include "c/typedefData.h" |
278 | 264 |
265 #include "c/extern.h" | |
266 | |
267 extern __code start_code(struct Context* context); | |
268 extern __code exit_code(struct Context* context); | |
269 extern __code meta(struct Context* context, enum Code next); | |
270 extern void initContext(struct Context* context); | |
271 | |
132 | 272 #endif |