Mercurial > hg > Gears > GearsAgda
annotate src/parallel_execution/CPUWorker.cbc @ 473:71b634a5ed65
Merge
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 28 Dec 2017 11:55:59 +0900 |
parents | ac244346c85d |
children | b92898d3a630 |
rev | line source |
---|---|
278 | 1 #include "../context.h" |
468
ac244346c85d
Change used interface syntax from #include to #interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
462
diff
changeset
|
2 #interface "TaskManager.h" |
473 | 3 #interface "Worker.h" |
4 #interface "Iterator.h" | |
5 #interface "Queue.h" | |
6 | |
410
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
405
diff
changeset
|
7 static void startWorker(Worker* worker); |
272 | 8 |
280 | 9 Worker* createCPUWorker(struct Context* context, int id, Queue* queue) { |
272 | 10 struct Worker* worker = new Worker(); |
11 struct CPUWorker* cpuWorker = new CPUWorker(); | |
12 worker->worker = (union Data*)cpuWorker; | |
13 worker->tasks = queue; | |
14 cpuWorker->id = id; | |
473 | 15 cpuWorker->loopCounter = 0; |
16 worker->taskReceive = C_taskReceiveCPUWorker; | |
17 worker->shutdown = C_shutdownCPUWorker; | |
411
0eba9a04633f
Work CUDAtwice
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
410
diff
changeset
|
18 pthread_create(&worker->thread, NULL, (void*)&startWorker, worker); |
272 | 19 return worker; |
20 } | |
21 | |
473 | 22 static void startWorker(struct Worker* worker) { |
272 | 23 CPUWorker* cpuWorker = (CPUWorker*)worker->worker; |
24 cpuWorker->context = NEW(struct Context); | |
25 initContext(cpuWorker->context); | |
26 Gearef(cpuWorker->context, Worker)->worker = (union Data*)worker; | |
473 | 27 Gearef(cpuWorker->context, Worker)->tasks = worker->tasks; |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
28 goto meta(cpuWorker->context, worker->taskReceive); |
272 | 29 } |
30 | |
473 | 31 __code taskReceiveCPUWorker(struct CPUWorker* worker, struct Queue* tasks) { |
32 goto tasks->take(getTaskCPUWorker); | |
272 | 33 } |
34 | |
473 | 35 __code getTaskCPUWorker(struct CPUWorker* cpuWorker, struct Context* task, struct Worker* worker) { |
36 if (!task) { | |
37 goto worker->shutdown(); // end thread | |
38 } | |
272 | 39 task->worker = worker; |
289 | 40 enum Code taskCg = task->next; |
473 | 41 task->next = C_odgCommitCPUWorker; // set CG after task exec |
42 goto meta(task, taskCg); // switch task context | |
272 | 43 } |
44 | |
473 | 45 __code getTaskCPUWorker_stub(struct Context* context) { |
46 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(context, Worker, worker); | |
272 | 47 Worker* worker = &Gearef(context,Worker)->worker->Worker; |
48 struct Context* task = &Gearef(context, Queue)->data->Context; | |
473 | 49 goto getTaskCPUWorker(context, cpuWorker, task, worker); |
50 } | |
51 | |
52 __code iterateCommitCPUWorker(struct CPUWorker* worker) { | |
53 struct Iterator* iterator = context->iterator; | |
54 goto iterator->barrier(context, odgCommitCPUWorker, iterateCommitCPUWorker1); | |
272 | 55 } |
56 | |
473 | 57 __code iterateCommitCPUWorker1(struct CPUWorker* worker, struct Context* task) { |
58 struct Worker* taskWorker = task->worker; | |
59 goto taskWorker->taskReceive(taskWorker->tasks); | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
60 } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
61 |
473 | 62 __code iterateCommitCPUWorker1_stub(struct Context* context) { |
450
d3d7a7d6a117
Delete USE_CUDA_MAIN_THREAD
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
440
diff
changeset
|
63 // switch worker context |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
64 struct Context* workerContext = context->worker->worker->CPUWorker.context; |
473 | 65 CPUWorker* cpuWorker = &context->worker->worker->CPUWorker; |
66 goto iterateCommitCPUWorker1(workerContext, | |
67 cpuWorker, | |
68 context); | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
69 } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
70 |
473 | 71 __code odgCommitCPUWorker(struct CPUWorker* worker, struct Context* task) { |
72 int i = worker->loopCounter; | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
73 if (task->odg+i < task->maxOdg) { |
473 | 74 goto odgCommitCPUWorker1(); |
288 | 75 } |
473 | 76 worker->loopCounter = 0; |
77 struct TaskManager* taskManager = task->taskManager; | |
78 goto taskManager->decrementTaskCount(taskReceiveCPUWorker); | |
288 | 79 } |
80 | |
473 | 81 __code odgCommitCPUWorker_stub(struct Context* context) { |
450
d3d7a7d6a117
Delete USE_CUDA_MAIN_THREAD
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
440
diff
changeset
|
82 // switch worker context |
288 | 83 struct Context* workerContext = context->worker->worker->CPUWorker.context; |
473 | 84 CPUWorker* cpuWorker = &context->worker->worker->CPUWorker; |
85 goto odgCommitCPUWorker(workerContext, | |
86 cpuWorker, | |
87 context); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
88 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
89 |
473 | 90 __code odgCommitCPUWorker1(struct CPUWorker* worker) { |
91 int i = worker->loopCounter; | |
92 struct Queue* queue = GET_WAIT_LIST(context->data[context->odg+i]); | |
93 goto queue->isEmpty(odgCommitCPUWorker2, odgCommitCPUWorker4); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
94 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
95 |
473 | 96 __code odgCommitCPUWorker2(struct CPUWorker* worker) { |
97 int i = worker->loopCounter; | |
98 struct Queue* queue = GET_WAIT_LIST(context->data[context->odg+i]); | |
99 goto queue->take(odgCommitCPUWorker3); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
100 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
101 |
473 | 102 __code odgCommitCPUWorker3(struct CPUWorker* worker, struct Context* task) { |
103 if (__sync_fetch_and_sub(&task->idgCount, 1) == 1) { // atomic decrement idg counter(__sync_fetch_and_sub function return initial value of task->idgCount point) | |
104 struct TaskManager* taskManager = task->taskManager; | |
105 taskManager->task = task; | |
106 goto taskManager->spawn(task, odgCommitCPUWorker1); | |
107 } | |
108 goto odgCommitCPUWorker1(); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
109 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
110 |
473 | 111 __code odgCommitCPUWorker3_stub(struct Context* context) { |
112 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(context, Worker, worker); | |
288 | 113 struct Context* task = &Gearef(context, Queue)->data->Context; |
473 | 114 goto odgCommitCPUWorker3(context, |
115 cpuWorker, | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
116 task); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
117 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
118 |
473 | 119 __code odgCommitCPUWorker4(struct CPUWorker* worker) { |
120 worker->loopCounter++; | |
121 goto odgCommitCPUWorker(); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
122 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
123 |
473 | 124 __code shutdownCPUWorker(struct CPUWorker* worker) { |
125 goto exit_code(); | |
288 | 126 } |