Mercurial > hg > Members > menikon > CbC_xv6
annotate src/context.h @ 94:0956648d24e5
impl cmake on macos
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 23 Oct 2019 17:33:25 +0900 |
parents | bc5bcfd2f6d6 |
children | 0ddcd561d975 |
rev | line source |
---|---|
41 | 1 /* Context definition for llrb example */ |
52 | 2 // #ifdef CBC_CONTEXT_H does not work well |
3 #define CBC_CONTEXT_H | |
4 // #include <stdlib.h> | |
5 // #include <pthread.h> | |
41 | 6 #ifdef USE_CUDAWorker |
7 #include <cuda.h> | |
8 #include <driver_types.h> | |
9 #include <cuda_runtime.h> | |
10 #include "helper_cuda.h" | |
11 #endif | |
12 | |
94
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
13 #ifndef NULL |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
14 # if defined __GNUG__ && \ |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
15 (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)) |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
16 # define NULL (__null) |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
17 # else |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
18 # if !defined(__cplusplus) |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
19 # define NULL ((void*)0) |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
20 # else |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
21 # define NULL (0) |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
22 # endif |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
23 # endif |
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
24 #endif |
53 | 25 |
62 | 26 #ifdef XV6KERNEL |
53 | 27 #define calloc(a,b) kmalloc((a)*(b)) |
28 #define free(a) kfree(a) | |
62 | 29 #else |
30 #define calloc(a,b) malloc((a)*(b)) | |
31 #define free(a) free(a) | |
32 #endif | |
51 | 33 |
41 | 34 #define ALLOCATE_SIZE 20000000 |
35 #define NEW(type) (type*)(calloc(1, sizeof(type))) | |
36 #define NEWN(n, type) (type*)(calloc(n, sizeof(type))) | |
37 | |
52 | 38 #define ALLOC_DATA(cbc_context, dseg) ({\ |
39 Meta* meta = (Meta*)cbc_context->heap;\ | |
41 | 40 meta->type = D_##dseg;\ |
41 meta->size = sizeof(dseg);\ | |
42 meta->len = 1;\ | |
52 | 43 cbc_context->heap += sizeof(Meta);\ |
44 cbc_context->data[D_##dseg] = cbc_context->heap; cbc_context->heap += sizeof(dseg); (dseg *)cbc_context->data[D_##dseg]; }) | |
41 | 45 |
52 | 46 #define ALLOC_DATA_TYPE(cbc_context, dseg, t) ({\ |
47 Meta* meta = (Meta*)cbc_context->heap;\ | |
41 | 48 meta->type = D_##t;\ |
49 meta->size = sizeof(t);\ | |
50 meta->len = 1;\ | |
52 | 51 cbc_context->heap += sizeof(Meta);\ |
52 cbc_context->data[D_##dseg] = cbc_context->heap; cbc_context->heap += sizeof(t); (t *)cbc_context->data[D_##dseg]; }) | |
41 | 53 |
52 | 54 #define ALLOCATE(cbc_context, t) ({ \ |
55 Meta* meta = (Meta*)cbc_context->heap;\ | |
56 cbc_context->heap += sizeof(Meta);\ | |
57 union Data* data = cbc_context->heap; \ | |
58 cbc_context->heap += sizeof(t); \ | |
41 | 59 meta->type = D_##t; \ |
60 meta->size = sizeof(t); \ | |
61 meta->len = 1;\ | |
62 data; }) | |
63 | |
52 | 64 #define ALLOCATE_ARRAY(cbc_context, t, length) ({ \ |
65 Meta* meta = (Meta*)cbc_context->heap;\ | |
66 cbc_context->heap += sizeof(Meta);\ | |
67 union Data* data = cbc_context->heap; \ | |
68 cbc_context->heap += sizeof(t)*length; \ | |
41 | 69 meta->type = D_##t; \ |
70 meta->size = sizeof(t)*length; \ | |
71 meta->len = length; \ | |
72 data; }) | |
73 | |
52 | 74 #define ALLOCATE_PTR_ARRAY(cbc_context, dseg, length) ({\ |
75 Meta* meta = (Meta*)cbc_context->heap;\ | |
76 cbc_context->heap += sizeof(Meta);\ | |
77 union Data* data = cbc_context->heap; \ | |
78 cbc_context->heap += sizeof(dseg *)*length; \ | |
41 | 79 meta->type = D_##dseg; \ |
80 meta->size = sizeof(dseg *)*length; \ | |
81 meta->len = length; \ | |
82 data; }) | |
83 | |
52 | 84 #define ALLOCATE_DATA_GEAR(cbc_context, t) ({ \ |
85 union Data* data = ALLOCATE(cbc_context, t); \ | |
41 | 86 Meta* meta = GET_META(data); \ |
52 | 87 meta->wait = createSynchronizedQueue(cbc_context); \ |
41 | 88 data; }) |
89 | |
52 | 90 #define ALLOC(cbc_context, t) (&ALLOCATE(cbc_context, t)->t) |
41 | 91 |
92 #define GET_META(dseg) ((Meta*)(((void*)dseg) - sizeof(Meta))) | |
93 #define GET_TYPE(dseg) (GET_META(dseg)->type) | |
94 #define GET_SIZE(dseg) (GET_META(dseg)->size) | |
95 #define GET_LEN(dseg) (GET_META(dseg)->len) | |
96 #define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait) | |
97 | |
52 | 98 #define Gearef(cbc_context, t) (&(cbc_context)->data[D_##t]->t) |
41 | 99 |
52 | 100 // (SingleLinkedStack *)cbc_context->data[D_Stack]->Stack.stack->Stack.stack |
41 | 101 |
52 | 102 #define GearImpl(cbc_context, intf, name) (Gearef(cbc_context, intf)->name->intf.name) |
41 | 103 |
104 #include "c/enumCode.h" | |
105 | |
106 enum Relational { | |
107 EQ, | |
108 GT, | |
109 LT, | |
110 }; | |
111 | |
112 #include "c/enumData.h" | |
51 | 113 #define NDIRECT 12 //fs.h |
41 | 114 |
115 struct Context { | |
116 enum Code next; | |
117 struct Worker* worker; | |
118 struct TaskManager* taskManager; | |
119 int codeNum; | |
120 __code (**code) (struct Context*); | |
121 union Data **data; | |
122 void* heapStart; | |
123 void* heap; | |
124 long heapLimit; | |
125 int dataNum; | |
126 | |
127 // task parameter | |
128 int idgCount; //number of waiting dataGear | |
129 int idg; | |
130 int maxIdg; | |
131 int odg; | |
132 int maxOdg; | |
133 int gpu; // GPU task | |
134 struct Context* task; | |
135 struct Element* taskList; | |
136 #ifdef USE_CUDAWorker | |
137 int num_exec; | |
138 CUmodule module; | |
139 CUfunction function; | |
140 #endif | |
141 /* multi dimension parameter */ | |
142 int iterate; | |
143 struct Iterator* iterator; | |
144 enum Code before; | |
145 }; | |
146 | |
147 typedef int Int; | |
148 #ifndef USE_CUDAWorker | |
149 typedef unsigned long long CUdeviceptr; | |
150 #endif | |
151 union Data { | |
152 struct Meta { | |
153 enum DataType type; | |
154 long size; | |
155 long len; | |
156 struct Queue* wait; // tasks waiting this dataGear | |
157 } Meta; | |
158 struct Context Context; | |
159 struct Timer { | |
160 union Data* timer; | |
161 enum Code start; | |
162 enum Code end; | |
163 enum Code next; | |
164 } Timer; | |
165 struct TimerImpl { | |
166 double time; | |
167 } TimerImpl; | |
168 struct LoopCounter { | |
169 int i; | |
170 } LoopCounter; | |
171 struct TaskManager { | |
172 union Data* taskManager; | |
52 | 173 enum Code spawn; // start NEW cbc_context on the worker |
41 | 174 enum Code spawnTasks; // start NEW tasks on the worker |
175 enum Code shutdown; | |
176 enum Code incrementTaskCount; | |
177 enum Code decrementTaskCount; | |
178 enum Code next; | |
179 enum Code next1; | |
180 enum Code setWaitTask; | |
181 struct Context* task; | |
182 struct Element* taskList; | |
183 union Data* data; | |
184 } TaskManager; | |
185 struct TaskManagerImpl { | |
186 enum Code next; | |
187 int numWorker; | |
188 int sendCPUWorkerIndex; | |
189 int sendGPUWorkerIndex; | |
190 int taskCount; | |
52 | 191 // pthread_mutex_t mutex; |
41 | 192 struct Queue* activeQueue; |
193 struct Worker** workers; | |
194 struct Element* taskList; | |
195 int loopCounter; | |
196 int cpu; | |
197 int gpu; | |
198 int io; | |
199 int maxCPU; | |
200 } TaskManagerImpl; | |
201 struct Worker { | |
202 union Data* worker; | |
203 enum Code taskReceive; | |
204 enum Code shutdown; | |
205 enum Code next; | |
206 struct Queue* tasks; | |
52 | 207 // pthread_t thread; |
41 | 208 struct TaskManager* taskManager; |
209 struct Context* task; | |
210 } Worker; | |
211 struct CPUWorker { | |
52 | 212 // pthread_mutex_t mutex; |
213 // pthread_cond_t cond; | |
214 struct Context* cbc_context; | |
41 | 215 int id; |
216 int loopCounter; | |
217 } CPUWorker; | |
218 #ifdef USE_CUDAWorker | |
219 struct CUDAWorker { | |
220 CUdevice device; | |
52 | 221 CUcbc_context cuCtx; |
222 struct Context* cbc_context; | |
41 | 223 int id; |
224 int loopCounter; | |
225 int deviceNum; | |
226 struct Queue* tasks; | |
227 int runFlag; | |
228 enum Code next; | |
229 int numStream; | |
230 struct Executor* executor; | |
231 CUstream *stream; | |
232 } CUDAWorker; | |
233 #else | |
234 struct CUDAWorker { | |
235 } CUDAWorker; | |
236 #endif | |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
237 struct CbCSysFile { |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
238 struct file *f; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
239 int n; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
240 char *p; |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
241 struct UInteger* num; |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
242 enum Code next; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
243 enum Code read; |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
244 enum Code cbc_file_ret; |
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
245 } CbCSysFile; |
41 | 246 struct Main { |
247 enum Code code; | |
248 enum Code next; | |
249 struct Queue* args; | |
250 } Main; | |
251 // Queue Interface | |
252 struct Queue { | |
253 union Data* queue; | |
254 union Data* data; | |
255 enum Code whenEmpty; | |
256 enum Code clear; | |
257 enum Code put; | |
258 enum Code take; | |
259 enum Code isEmpty; | |
260 enum Code next; | |
261 } Queue; | |
262 struct SingleLinkedQueue { | |
263 struct Element* top; | |
264 struct Element* last; | |
265 } SingleLinkedQueue; | |
266 struct SynchronizedQueue { | |
267 struct Element* top; | |
268 struct Element* last; | |
269 struct Atomic* atomic; | |
270 } SynchronizedQueue; | |
271 // Stack Interface | |
272 struct Stack { | |
273 union Data* stack; | |
274 union Data* data; | |
275 union Data* data1; | |
276 enum Code whenEmpty; | |
277 enum Code clear; | |
278 enum Code push; | |
279 enum Code pop; | |
280 enum Code pop2; | |
281 enum Code isEmpty; | |
282 enum Code get; | |
283 enum Code get2; | |
284 enum Code next; | |
285 } Stack; | |
286 // Stack implementations | |
287 struct SingleLinkedStack { | |
288 struct Element* top; | |
289 } SingleLinkedStack; | |
290 struct ArrayStack { | |
291 int size; | |
292 int limit; | |
293 struct Element* array; | |
294 } ArrayStack; | |
295 // Stack implementation end | |
296 struct Element { | |
297 union Data* data; | |
298 struct Element* next; | |
299 } Element; | |
300 struct Array { | |
301 int prefix; | |
302 Int* array; | |
303 } Array; | |
304 struct Tree { | |
305 union Data* tree; | |
306 struct Node* node; | |
307 enum Code put; | |
308 enum Code get; | |
309 enum Code remove; | |
310 enum Code clear; | |
311 enum Code next; | |
312 } Tree; | |
313 struct RedBlackTree { | |
314 struct Node* root; | |
315 struct Node* current; // reading node of original tree | |
316 struct Node* previous; // parent of reading node of original tree | |
317 struct Node* newNode; // writing node of new tree | |
318 struct Node* parent; | |
319 struct Node* grandparent; | |
320 struct Stack* nodeStack; | |
321 enum Code findNodeNext; | |
322 int result; | |
323 } RedBlackTree; | |
324 struct RotateTree { | |
325 enum Code next; | |
326 struct RedBlackTree* traverse; | |
327 struct Tree* tree; | |
328 } RotateTree; | |
329 struct Node { | |
330 int key; // comparable data segment | |
331 union Data* value; | |
332 struct Node* left; | |
333 struct Node* right; | |
334 // need to balancing | |
335 enum Color { | |
336 Red, | |
337 Black, | |
338 // Red eq 0,Black eq 1. enum name convert intager. | |
339 } color; | |
340 } Node; | |
341 struct Atomic { | |
342 union Data* atomic; | |
343 union Data** ptr; | |
344 union Data* oldData; | |
345 union Data* newData; | |
346 enum Code checkAndSet; | |
347 enum Code next; | |
348 enum Code fail; | |
349 } Atomic; | |
350 struct AtomicReference { | |
351 } AtomicReference; | |
352 struct Semaphore { | |
353 union Data* semaphore; | |
354 enum Code p; | |
355 enum Code v; | |
356 enum Code next; | |
357 } Semaphore; | |
358 struct SemaphoreImpl { | |
359 int value; | |
360 struct Lock* lock; | |
361 struct Queue* waitThreadQueue; | |
362 } SemaphoreImpl; | |
363 struct Allocate { | |
364 enum Code next; | |
365 long size; | |
366 } Allocate; | |
367 struct Integer { | |
368 int value; | |
369 } Integer; | |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
370 struct UInteger { |
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
371 unsigned int value; |
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
372 } UInteger; |
41 | 373 struct SortArray { |
374 struct Integer *array; //Array arrayじゃできない? | |
375 int loopCounter; | |
376 int block; | |
377 int first; | |
378 int prefix; | |
379 } SortArray; | |
380 struct Iterator { | |
381 union Data* iterator; | |
382 struct Context* task; | |
383 int numGPU; | |
384 enum Code exec; | |
385 enum Code barrier; | |
386 enum Code whenWait; | |
387 enum Code next; | |
388 } Iterator; | |
389 struct MultiDimIterator { | |
390 int x; | |
391 int y; | |
392 int z; | |
393 int count; | |
394 int counterX; | |
395 int counterY; | |
396 int counterZ; | |
397 } MultiDimIterator; | |
398 struct MultiDim { | |
399 int x; | |
400 int y; | |
401 int z; | |
402 } MultiDim; | |
403 struct Executor { | |
404 union Data* executor; | |
405 struct Context* task; | |
406 enum Code read; | |
407 enum Code exec; | |
408 enum Code write; | |
409 enum Code next; | |
410 } Executor; | |
411 #ifdef USE_CUDAWorker | |
412 struct CUDAExecutor { | |
413 CUdeviceptr** kernelParams; | |
414 struct CUDABuffer* buffer; | |
415 int maxThreadPerBlock; | |
416 int maxThreadPerBlockX; | |
417 int maxThreadPerBlockY; | |
418 int maxThreadPerBlockZ; | |
419 struct Timer* timer; | |
420 } CUDAExecutor; | |
421 struct CUDABuffer { | |
422 int inputLen; | |
423 int outputLen; | |
424 union Data** inputData; | |
425 union Data** outputData; | |
426 } CUDABuffer; | |
427 CUdeviceptr CUdeviceptr; | |
428 #else | |
429 struct CUDAExecutor { | |
430 } CUDAExecutor; | |
431 struct CUDABuffer { | |
432 } CUDABuffer; | |
433 CUdeviceptr CUdeviceptr; | |
434 #endif | |
435 Int Int; | |
436 struct Memory { | |
437 union Data* adr; | |
438 int length; | |
439 union Data* body; | |
440 int hash; | |
441 } Memory; | |
442 struct Buffer { | |
443 union Data* buffer; | |
444 union Data* data; | |
445 enum Code put; | |
446 enum Code take; | |
447 enum Code next; | |
448 } Buffer; | |
449 struct BoundedBuffer { | |
450 struct Element* top; | |
451 struct Element* last; | |
452 struct Semaphore* fullCount; | |
453 struct Semaphore* emptyCount; | |
454 struct Semaphore* lock; | |
455 } BoundedBuffer; | |
456 struct Lock { | |
457 union Data* lock; | |
458 enum Code doLock; | |
459 enum Code doUnlock; | |
460 enum Code next; | |
461 } Lock; | |
462 struct LockImpl { | |
463 Int* lock; | |
464 struct Queue* waitThreadQueue; | |
465 struct Atomic* atomic; | |
466 struct Context* lockContext; | |
467 } LockImpl; | |
468 struct SpinLock { | |
469 volatile Int* lock; | |
470 struct Atomic* atomic; | |
471 struct Context* lockContext; | |
472 } SpinLock; | |
52 | 473 /* CbCxv6 cbc_context*/ |
51 | 474 struct Uinteger { |
94
0956648d24e5
impl cmake on macos
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
475 unsigned int value; |
51 | 476 } Uinteger; |
477 struct Short { | |
478 short value; | |
479 } Short; | |
480 struct String { | |
481 char* string; | |
482 } String; | |
52 | 483 }; // union Data end this is necessary for cbc_context generator |
51 | 484 |
41 | 485 typedef union Data Data; |
486 | |
487 #include "c/typedefData.h" | |
488 | |
489 #include "c/extern.h" | |
490 | |
52 | 491 extern __code start_code(struct Context* cbc_context); |
492 extern __code exit_code(struct Context* cbc_context); | |
493 extern __code meta(struct Context* cbc_context, enum Code next); | |
494 //extern __code par_meta(struct Context* cbc_context, enum Code spawns, enum Code next); | |
495 extern __code parGotoMeta(struct Context* cbc_context, enum Code next); | |
496 extern void initContext(struct Context* cbc_context); | |
41 | 497 |
52 | 498 // #endif |