Mercurial > hg > GearsTemplate
annotate src/parallel_execution/CPUWorker.cbc @ 538:c0b6ce2ed820
Add comment
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Feb 2018 04:35:17 +0900 |
parents | a517b11c37f7 |
children |
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) { |
481
a517b11c37f7
Refactoring CUDAWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
474
diff
changeset
|
23 struct CPUWorker* cpuWorker = &worker->worker->CPUWorker; |
272 | 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) { |
481
a517b11c37f7
Refactoring CUDAWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
474
diff
changeset
|
36 if (!task) { |
a517b11c37f7
Refactoring CUDAWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
474
diff
changeset
|
37 goto worker->shutdown(); // end thread |
473 | 38 } |
272 | 39 task->worker = worker; |
289 | 40 enum Code taskCg = task->next; |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
41 task->next = C_odgCommitCPUWorker; // commit outputDG after task exec |
473 | 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 odgCommitCPUWorker(struct CPUWorker* worker, struct Context* task) { | |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
53 if (task->iterate) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
54 struct Iterator* iterator = task->iterator; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
55 goto iterator->barrier(task, odgCommitCPUWorker1, odgCommitCPUWorker6); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
56 } else { |
473 | 57 goto odgCommitCPUWorker1(); |
288 | 58 } |
59 } | |
60 | |
473 | 61 __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
|
62 // switch worker context |
288 | 63 struct Context* workerContext = context->worker->worker->CPUWorker.context; |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
64 Gearef(workerContext, Worker)->worker = (union Data*)context->worker; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
65 Gearef(workerContext, Worker)->task = context; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
66 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(workerContext, Worker, worker); |
473 | 67 goto odgCommitCPUWorker(workerContext, |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
68 cpuWorker, |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
69 context); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
70 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
71 |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
72 __code odgCommitCPUWorker1(struct CPUWorker* worker, struct Context* task) { |
473 | 73 int i = worker->loopCounter; |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
74 if (task->odg+i < task->maxOdg) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
75 goto odgCommitCPUWorker2(); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
76 } |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
77 worker->loopCounter = 0; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
78 struct TaskManager* taskManager = task->taskManager; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
79 goto taskManager->decrementTaskCount(odgCommitCPUWorker6); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
80 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
81 |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
82 __code odgCommitCPUWorker2(struct CPUWorker* worker, struct Context* task) { |
473 | 83 int i = worker->loopCounter; |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
84 struct Queue* queue = GET_WAIT_LIST(task->data[task->odg+i]); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
85 goto queue->isEmpty(odgCommitCPUWorker3, odgCommitCPUWorker5); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
86 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
87 |
473 | 88 __code odgCommitCPUWorker3(struct CPUWorker* worker, struct Context* task) { |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
89 int i = worker->loopCounter; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
90 struct Queue* queue = GET_WAIT_LIST(task->data[task->odg+i]); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
91 goto queue->take(odgCommitCPUWorker4); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
92 } |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
93 |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
94 __code odgCommitCPUWorker4(struct CPUWorker* worker, struct Context* task, struct Context* waitTask) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
95 if (__sync_fetch_and_sub(&waitTask->idgCount, 1) == 1) { // atomic decrement idg counter(__sync_fetch_and_sub function return initial value of waitTask->idgCount point) |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
96 struct TaskManager* taskManager = waitTask->taskManager; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
97 goto taskManager->spawn(waitTask, odgCommitCPUWorker2); |
473 | 98 } |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
99 goto odgCommitCPUWorker2(); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
100 } |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
101 |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
102 __code odgCommitCPUWorker4_stub(struct Context* context) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
103 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(context, Worker, worker); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
104 struct Context* task = Gearef(context, Worker)->task; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
105 struct Context* waitTask = &Gearef(context, Queue)->data->Context; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
106 goto odgCommitCPUWorker4(context, |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
107 cpuWorker, |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
108 task, |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
109 waitTask); |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
110 } |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
111 |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
112 __code odgCommitCPUWorker5(struct CPUWorker* worker, struct Context* task) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
113 worker->loopCounter++; |
473 | 114 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
|
115 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
116 |
474
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
117 __code odgCommitCPUWorker6(struct CPUWorker* worker, struct Context* task) { |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
118 struct Worker* taskWorker = task->worker; |
b92898d3a630
Refactoring CPUWorker.cbc
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
473
diff
changeset
|
119 goto taskWorker->taskReceive(taskWorker->tasks); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
120 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
121 |
473 | 122 __code shutdownCPUWorker(struct CPUWorker* worker) { |
123 goto exit_code(); | |
288 | 124 } |