Mercurial > hg > Members > anatofuz > CbC_xv6
annotate src/context.h @ 93:7d0ec88cdd22
fix read interface
author | anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 19 Oct 2019 18:44:57 +0900 |
parents | bc5bcfd2f6d6 |
children | d876c9a65239 |
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 | |
53 | 13 #include "types.h" |
14 #include "use_context_numbers.h" | |
15 #include "fs.h" | |
16 #include "defs.h" | |
17 | |
62 | 18 #ifdef XV6KERNEL |
53 | 19 #define calloc(a,b) kmalloc((a)*(b)) |
20 #define free(a) kfree(a) | |
62 | 21 #else |
22 #define calloc(a,b) malloc((a)*(b)) | |
23 #define free(a) free(a) | |
24 #endif | |
51 | 25 |
41 | 26 #define ALLOCATE_SIZE 20000000 |
27 #define NEW(type) (type*)(calloc(1, sizeof(type))) | |
28 #define NEWN(n, type) (type*)(calloc(n, sizeof(type))) | |
29 | |
52 | 30 #define ALLOC_DATA(cbc_context, dseg) ({\ |
31 Meta* meta = (Meta*)cbc_context->heap;\ | |
41 | 32 meta->type = D_##dseg;\ |
33 meta->size = sizeof(dseg);\ | |
34 meta->len = 1;\ | |
52 | 35 cbc_context->heap += sizeof(Meta);\ |
36 cbc_context->data[D_##dseg] = cbc_context->heap; cbc_context->heap += sizeof(dseg); (dseg *)cbc_context->data[D_##dseg]; }) | |
41 | 37 |
52 | 38 #define ALLOC_DATA_TYPE(cbc_context, dseg, t) ({\ |
39 Meta* meta = (Meta*)cbc_context->heap;\ | |
41 | 40 meta->type = D_##t;\ |
41 meta->size = sizeof(t);\ | |
42 meta->len = 1;\ | |
52 | 43 cbc_context->heap += sizeof(Meta);\ |
44 cbc_context->data[D_##dseg] = cbc_context->heap; cbc_context->heap += sizeof(t); (t *)cbc_context->data[D_##dseg]; }) | |
41 | 45 |
52 | 46 #define ALLOCATE(cbc_context, t) ({ \ |
47 Meta* meta = (Meta*)cbc_context->heap;\ | |
48 cbc_context->heap += sizeof(Meta);\ | |
49 union Data* data = cbc_context->heap; \ | |
50 cbc_context->heap += sizeof(t); \ | |
41 | 51 meta->type = D_##t; \ |
52 meta->size = sizeof(t); \ | |
53 meta->len = 1;\ | |
54 data; }) | |
55 | |
52 | 56 #define ALLOCATE_ARRAY(cbc_context, t, length) ({ \ |
57 Meta* meta = (Meta*)cbc_context->heap;\ | |
58 cbc_context->heap += sizeof(Meta);\ | |
59 union Data* data = cbc_context->heap; \ | |
60 cbc_context->heap += sizeof(t)*length; \ | |
41 | 61 meta->type = D_##t; \ |
62 meta->size = sizeof(t)*length; \ | |
63 meta->len = length; \ | |
64 data; }) | |
65 | |
52 | 66 #define ALLOCATE_PTR_ARRAY(cbc_context, dseg, length) ({\ |
67 Meta* meta = (Meta*)cbc_context->heap;\ | |
68 cbc_context->heap += sizeof(Meta);\ | |
69 union Data* data = cbc_context->heap; \ | |
70 cbc_context->heap += sizeof(dseg *)*length; \ | |
41 | 71 meta->type = D_##dseg; \ |
72 meta->size = sizeof(dseg *)*length; \ | |
73 meta->len = length; \ | |
74 data; }) | |
75 | |
52 | 76 #define ALLOCATE_DATA_GEAR(cbc_context, t) ({ \ |
77 union Data* data = ALLOCATE(cbc_context, t); \ | |
41 | 78 Meta* meta = GET_META(data); \ |
52 | 79 meta->wait = createSynchronizedQueue(cbc_context); \ |
41 | 80 data; }) |
81 | |
52 | 82 #define ALLOC(cbc_context, t) (&ALLOCATE(cbc_context, t)->t) |
41 | 83 |
84 #define GET_META(dseg) ((Meta*)(((void*)dseg) - sizeof(Meta))) | |
85 #define GET_TYPE(dseg) (GET_META(dseg)->type) | |
86 #define GET_SIZE(dseg) (GET_META(dseg)->size) | |
87 #define GET_LEN(dseg) (GET_META(dseg)->len) | |
88 #define GET_WAIT_LIST(dseg) (GET_META(dseg)->wait) | |
89 | |
52 | 90 #define Gearef(cbc_context, t) (&(cbc_context)->data[D_##t]->t) |
41 | 91 |
52 | 92 // (SingleLinkedStack *)cbc_context->data[D_Stack]->Stack.stack->Stack.stack |
41 | 93 |
52 | 94 #define GearImpl(cbc_context, intf, name) (Gearef(cbc_context, intf)->name->intf.name) |
41 | 95 |
96 #include "c/enumCode.h" | |
97 | |
98 enum Relational { | |
99 EQ, | |
100 GT, | |
101 LT, | |
102 }; | |
103 | |
104 #include "c/enumData.h" | |
51 | 105 #define NDIRECT 12 //fs.h |
41 | 106 |
107 struct Context { | |
108 enum Code next; | |
109 struct Worker* worker; | |
110 struct TaskManager* taskManager; | |
111 int codeNum; | |
112 __code (**code) (struct Context*); | |
113 union Data **data; | |
114 void* heapStart; | |
115 void* heap; | |
116 long heapLimit; | |
117 int dataNum; | |
118 | |
119 // task parameter | |
120 int idgCount; //number of waiting dataGear | |
121 int idg; | |
122 int maxIdg; | |
123 int odg; | |
124 int maxOdg; | |
125 int gpu; // GPU task | |
126 struct Context* task; | |
127 struct Element* taskList; | |
128 #ifdef USE_CUDAWorker | |
129 int num_exec; | |
130 CUmodule module; | |
131 CUfunction function; | |
132 #endif | |
133 /* multi dimension parameter */ | |
134 int iterate; | |
135 struct Iterator* iterator; | |
136 enum Code before; | |
137 }; | |
138 | |
139 typedef int Int; | |
140 #ifndef USE_CUDAWorker | |
141 typedef unsigned long long CUdeviceptr; | |
142 #endif | |
143 union Data { | |
144 struct Meta { | |
145 enum DataType type; | |
146 long size; | |
147 long len; | |
148 struct Queue* wait; // tasks waiting this dataGear | |
149 } Meta; | |
150 struct Context Context; | |
151 struct Timer { | |
152 union Data* timer; | |
153 enum Code start; | |
154 enum Code end; | |
155 enum Code next; | |
156 } Timer; | |
157 struct TimerImpl { | |
158 double time; | |
159 } TimerImpl; | |
160 struct LoopCounter { | |
161 int i; | |
162 } LoopCounter; | |
163 struct TaskManager { | |
164 union Data* taskManager; | |
52 | 165 enum Code spawn; // start NEW cbc_context on the worker |
41 | 166 enum Code spawnTasks; // start NEW tasks on the worker |
167 enum Code shutdown; | |
168 enum Code incrementTaskCount; | |
169 enum Code decrementTaskCount; | |
170 enum Code next; | |
171 enum Code next1; | |
172 enum Code setWaitTask; | |
173 struct Context* task; | |
174 struct Element* taskList; | |
175 union Data* data; | |
176 } TaskManager; | |
177 struct TaskManagerImpl { | |
178 enum Code next; | |
179 int numWorker; | |
180 int sendCPUWorkerIndex; | |
181 int sendGPUWorkerIndex; | |
182 int taskCount; | |
52 | 183 // pthread_mutex_t mutex; |
41 | 184 struct Queue* activeQueue; |
185 struct Worker** workers; | |
186 struct Element* taskList; | |
187 int loopCounter; | |
188 int cpu; | |
189 int gpu; | |
190 int io; | |
191 int maxCPU; | |
192 } TaskManagerImpl; | |
193 struct Worker { | |
194 union Data* worker; | |
195 enum Code taskReceive; | |
196 enum Code shutdown; | |
197 enum Code next; | |
198 struct Queue* tasks; | |
52 | 199 // pthread_t thread; |
41 | 200 struct TaskManager* taskManager; |
201 struct Context* task; | |
202 } Worker; | |
203 struct CPUWorker { | |
52 | 204 // pthread_mutex_t mutex; |
205 // pthread_cond_t cond; | |
206 struct Context* cbc_context; | |
41 | 207 int id; |
208 int loopCounter; | |
209 } CPUWorker; | |
210 #ifdef USE_CUDAWorker | |
211 struct CUDAWorker { | |
212 CUdevice device; | |
52 | 213 CUcbc_context cuCtx; |
214 struct Context* cbc_context; | |
41 | 215 int id; |
216 int loopCounter; | |
217 int deviceNum; | |
218 struct Queue* tasks; | |
219 int runFlag; | |
220 enum Code next; | |
221 int numStream; | |
222 struct Executor* executor; | |
223 CUstream *stream; | |
224 } CUDAWorker; | |
225 #else | |
226 struct CUDAWorker { | |
227 } CUDAWorker; | |
228 #endif | |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
229 struct CbCSysFile { |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
230 struct file *f; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
231 int n; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
232 char *p; |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
233 struct UInteger* num; |
91
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
234 enum Code next; |
b5ddf6fb0a6d
use CbCFile instead of File struct Interface
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
87
diff
changeset
|
235 enum Code read; |
92
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
236 enum Code cbc_file_ret; |
bc5bcfd2f6d6
rename CbCFile to CbCSysFile
anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
parents:
91
diff
changeset
|
237 } CbCSysFile; |
41 | 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 Inode { |
467 uint dev; // Device number | |
468 uint inum; // Inode number | |
469 int ref; // Reference count | |
470 int flags; // I_BUSY, I_VALID | |
471 | |
472 short type; // copy of disk inode | |
473 short major; | |
474 short minor; | |
475 short nlink; | |
476 uint size; | |
477 uint addrs[NDIRECT+1]; | |
478 } Inode; | |
479 struct Uinteger { | |
480 uint value; | |
481 } Uinteger; | |
482 struct Short { | |
483 short value; | |
484 } Short; | |
485 struct String { | |
486 char* string; | |
487 } String; | |
488 // fs.h --- | |
489 struct SuperBlock { | |
490 uint size; // Size of file system image (blocks) | |
491 uint nblocks; // Number of data blocks | |
492 uint ninodes; // Number of inodes. | |
493 uint nlog; // Number of log blocks | |
494 } SuperBlock; | |
495 struct Dinode { | |
496 short type; // copy of disk inode | |
497 short major; | |
498 short minor; | |
499 short nlink; | |
500 uint size; | |
501 uint addrs[NDIRECT+1]; | |
502 } Dinode; | |
503 struct Dirent { | |
504 ushort inum; | |
505 char name[DIRSIZ]; | |
506 } Dirent; | |
507 // --- fs.h | |
52 | 508 }; // union Data end this is necessary for cbc_context generator |
51 | 509 |
41 | 510 typedef union Data Data; |
511 | |
512 #include "c/typedefData.h" | |
513 | |
514 #include "c/extern.h" | |
515 | |
52 | 516 extern __code start_code(struct Context* cbc_context); |
517 extern __code exit_code(struct Context* cbc_context); | |
518 extern __code meta(struct Context* cbc_context, enum Code next); | |
519 //extern __code par_meta(struct Context* cbc_context, enum Code spawns, enum Code next); | |
520 extern __code parGotoMeta(struct Context* cbc_context, enum Code next); | |
521 extern void initContext(struct Context* cbc_context); | |
41 | 522 |
52 | 523 // #endif |