Mercurial > hg > GearsTemplate
annotate src/parallel_execution/CPUWorker.cbc @ 450:d3d7a7d6a117
Delete USE_CUDA_MAIN_THREAD
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 04 Dec 2017 04:24:30 +0900 |
parents | 55db2a339958 |
children | 57c715bd6283 |
rev | line source |
---|---|
278 | 1 #include "../context.h" |
272 | 2 |
410
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
405
diff
changeset
|
3 static void startWorker(Worker* worker); |
272 | 4 |
280 | 5 Worker* createCPUWorker(struct Context* context, int id, Queue* queue) { |
272 | 6 struct Worker* worker = new Worker(); |
7 struct CPUWorker* cpuWorker = new CPUWorker(); | |
8 worker->worker = (union Data*)cpuWorker; | |
9 worker->tasks = queue; | |
10 cpuWorker->id = id; | |
11 worker->taskReceive = C_taskReceiveWorker; | |
12 worker->shutdown = C_shutdownWorker; | |
411
0eba9a04633f
Work CUDAtwice
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
410
diff
changeset
|
13 pthread_create(&worker->thread, NULL, (void*)&startWorker, worker); |
272 | 14 return worker; |
15 } | |
16 | |
410
85b0ddbf458e
Fix CudaWorker
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
405
diff
changeset
|
17 static void startWorker(Worker* worker) { |
272 | 18 CPUWorker* cpuWorker = (CPUWorker*)worker->worker; |
19 cpuWorker->context = NEW(struct Context); | |
20 initContext(cpuWorker->context); | |
21 Gearef(cpuWorker->context, Worker)->worker = (union Data*)worker; | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
22 goto meta(cpuWorker->context, worker->taskReceive); |
272 | 23 } |
24 | |
25 __code taskReceiveWorker(struct Worker* worker,struct Queue* queue) { | |
26 queue->queue = (union Data*)worker->tasks; | |
27 queue->next = C_getTask; | |
28 goto meta(context, worker->tasks->take); | |
29 } | |
30 | |
31 __code taskReceiveWorker_stub(struct Context* context) { | |
32 goto taskReceiveWorker(context, &Gearef(context, Worker)->worker->Worker, Gearef(context, Queue)); | |
33 } | |
34 | |
35 __code getTask(struct Worker* worker, struct Context* task) { | |
36 if (!task) | |
411
0eba9a04633f
Work CUDAtwice
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
410
diff
changeset
|
37 goto meta(context, worker->shutdown); // end thread |
272 | 38 task->worker = worker; |
289 | 39 enum Code taskCg = task->next; |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
40 if (task->iterate) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
41 task->next = C_iterateCommit; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
42 } else { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
43 task->next = C_odgCommit; // set CG after task exec |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
44 } |
289 | 45 goto meta(task, taskCg); |
272 | 46 } |
47 | |
48 __code getTask_stub(struct Context* context) { | |
49 Worker* worker = &Gearef(context,Worker)->worker->Worker; | |
50 struct Context* task = &Gearef(context, Queue)->data->Context; | |
51 goto getTask(context, worker, task); | |
52 } | |
53 | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
54 __code iterateCommit(struct Iterator* iterator) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
55 iterator->iterator = (union Data*)context->iterator; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
56 iterator->task = context; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
57 iterator->next = C_odgCommit; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
58 iterator->whenWait = C_iterateCommit1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
59 goto meta(context, context->iterator->barrier); |
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 |
375
ad44fdb11433
Fix compile error but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
374
diff
changeset
|
62 __code iterateCommit1(struct Context* task) { |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
63 goto meta(context, C_taskReceiveWorker); |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
64 } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
65 |
375
ad44fdb11433
Fix compile error but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
374
diff
changeset
|
66 __code iterateCommit1_stub(struct Context* context) { |
450
d3d7a7d6a117
Delete USE_CUDA_MAIN_THREAD
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
440
diff
changeset
|
67 // switch worker context |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
68 struct Context* workerContext = context->worker->worker->CPUWorker.context; |
375
ad44fdb11433
Fix compile error but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
374
diff
changeset
|
69 goto iterateCommit1(workerContext, context); |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
70 } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
71 |
405
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
72 __code odgCommit(struct LoopCounter* loopCounter, struct Context* task, struct TaskManager* taskManager) { |
288 | 73 int i = loopCounter->i ; |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
74 if (task->odg+i < task->maxOdg) { |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
75 goto meta(task, C_odgCommit1); |
288 | 76 } |
77 loopCounter->i = 0; | |
405
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
78 taskManager->taskManager = (union Data*)task->taskManager; |
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
79 taskManager->next = C_taskReceiveWorker; |
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
80 goto meta(context, task->taskManager->decrementTaskCount); |
288 | 81 } |
82 | |
83 __code odgCommit_stub(struct Context* context) { | |
450
d3d7a7d6a117
Delete USE_CUDA_MAIN_THREAD
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
440
diff
changeset
|
84 // switch worker context |
288 | 85 struct Context* workerContext = context->worker->worker->CPUWorker.context; |
86 goto odgCommit(workerContext, | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
87 Gearef(context, LoopCounter), |
405
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
88 context, |
8915fce522b3
Fix shutdown TaskManager
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
375
diff
changeset
|
89 Gearef(workerContext, TaskManager)); |
288 | 90 } |
91 | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
92 __code odgCommit1(struct LoopCounter* loopCounter, struct Queue* queue) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
93 int i = loopCounter->i ; |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
94 queue->queue = (union Data*)GET_WAIT_LIST(context->data[context->odg+i]); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
95 queue->whenEmpty = C_odgCommit4; |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
96 queue->next = C_odgCommit2; |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
97 goto meta(context, queue->queue->Queue.isEmpty); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
98 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
99 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
100 __code odgCommit1_stub(struct Context* context) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
101 goto odgCommit1(context, |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
102 Gearef(context, LoopCounter), |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
103 Gearef(context, Queue)); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
104 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
105 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
106 __code odgCommit2(struct Queue* queue) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
107 queue->next = C_odgCommit3; |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
108 goto meta(context, queue->queue->Queue.take); |
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 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
111 __code odgCommit2_stub(struct Context* context) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
112 goto odgCommit2(context, |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
113 Gearef(context, Queue)); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
114 } |
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 __code odgCommit3(struct TaskManager* taskManager, struct Context* task) { |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
117 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) |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
118 taskManager->taskManager = (union Data*)task->taskManager; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
119 taskManager->context = task; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
120 taskManager->next = C_odgCommit1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
121 goto meta(context, task->taskManager->spawn); |
288 | 122 } |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
123 goto meta(context, C_odgCommit1); |
288 | 124 } |
125 | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
126 __code odgCommit3_stub(struct Context* context) { |
288 | 127 struct Context* task = &Gearef(context, Queue)->data->Context; |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
128 goto odgCommit3(context, |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
129 Gearef(context, TaskManager), |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
130 task); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
131 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
132 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
133 __code odgCommit4(struct LoopCounter* loopCounter) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
134 loopCounter->i++; |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
135 goto meta(context, C_odgCommit); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
136 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
137 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
138 __code odgCommit4_stub(struct Context* context) { |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
325
diff
changeset
|
139 goto odgCommit4(context, |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
372
diff
changeset
|
140 Gearef(context, LoopCounter)); |
288 | 141 } |
142 | |
272 | 143 __code shutdownWorker(struct CPUWorker* worker) { |
440
55db2a339958
Add worker shutdown
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
411
diff
changeset
|
144 goto meta(context, C_exit_code); |
272 | 145 } |