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