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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
278
23767f714f4a fix generate_stub
mir3636
parents: 277
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
3 #interface "Worker.h"
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
4 #interface "Iterator.h"
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
5 #interface "Queue.h"
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
mir3636
parents:
diff changeset
8
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 278
diff changeset
9 Worker* createCPUWorker(struct Context* context, int id, Queue* queue) {
272
mir3636
parents:
diff changeset
10 struct Worker* worker = new Worker();
mir3636
parents:
diff changeset
11 struct CPUWorker* cpuWorker = new CPUWorker();
mir3636
parents:
diff changeset
12 worker->worker = (union Data*)cpuWorker;
mir3636
parents:
diff changeset
13 worker->tasks = queue;
mir3636
parents:
diff changeset
14 cpuWorker->id = id;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
15 cpuWorker->loopCounter = 0;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
16 worker->taskReceive = C_taskReceiveCPUWorker;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
mir3636
parents:
diff changeset
19 return worker;
mir3636
parents:
diff changeset
20 }
mir3636
parents:
diff changeset
21
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
22 static void startWorker(struct Worker* worker) {
272
mir3636
parents:
diff changeset
23 CPUWorker* cpuWorker = (CPUWorker*)worker->worker;
mir3636
parents:
diff changeset
24 cpuWorker->context = NEW(struct Context);
mir3636
parents:
diff changeset
25 initContext(cpuWorker->context);
mir3636
parents:
diff changeset
26 Gearef(cpuWorker->context, Worker)->worker = (union Data*)worker;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
mir3636
parents:
diff changeset
29 }
mir3636
parents:
diff changeset
30
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
31 __code taskReceiveCPUWorker(struct CPUWorker* worker, struct Queue* tasks) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
32 goto tasks->take(getTaskCPUWorker);
272
mir3636
parents:
diff changeset
33 }
mir3636
parents:
diff changeset
34
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
35 __code getTaskCPUWorker(struct CPUWorker* cpuWorker, struct Context* task, struct Worker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
36 if (!task) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
37 goto worker->shutdown(); // end thread
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
38 }
272
mir3636
parents:
diff changeset
39 task->worker = worker;
289
3d70e21a3902 Delete createData
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 288
diff changeset
40 enum Code taskCg = task->next;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
41 task->next = C_odgCommitCPUWorker; // set CG after task exec
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
42 goto meta(task, taskCg); // switch task context
272
mir3636
parents:
diff changeset
43 }
mir3636
parents:
diff changeset
44
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
45 __code getTaskCPUWorker_stub(struct Context* context) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
46 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(context, Worker, worker);
272
mir3636
parents:
diff changeset
47 Worker* worker = &Gearef(context,Worker)->worker->Worker;
mir3636
parents:
diff changeset
48 struct Context* task = &Gearef(context, Queue)->data->Context;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
49 goto getTaskCPUWorker(context, cpuWorker, task, worker);
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
50 }
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
51
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
52 __code iterateCommitCPUWorker(struct CPUWorker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
53 struct Iterator* iterator = context->iterator;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
54 goto iterator->barrier(context, odgCommitCPUWorker, iterateCommitCPUWorker1);
272
mir3636
parents:
diff changeset
55 }
mir3636
parents:
diff changeset
56
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
57 __code iterateCommitCPUWorker1(struct CPUWorker* worker, struct Context* task) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
58 struct Worker* taskWorker = task->worker;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
65 CPUWorker* cpuWorker = &context->worker->worker->CPUWorker;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
66 goto iterateCommitCPUWorker1(workerContext,
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
67 cpuWorker,
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
71 __code odgCommitCPUWorker(struct CPUWorker* worker, struct Context* task) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
74 goto odgCommitCPUWorker1();
288
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
75 }
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
76 worker->loopCounter = 0;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
77 struct TaskManager* taskManager = task->taskManager;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
78 goto taskManager->decrementTaskCount(taskReceiveCPUWorker);
288
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
79 }
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
80
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
83 struct Context* workerContext = context->worker->worker->CPUWorker.context;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
84 CPUWorker* cpuWorker = &context->worker->worker->CPUWorker;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
85 goto odgCommitCPUWorker(workerContext,
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
86 cpuWorker,
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
90 __code odgCommitCPUWorker1(struct CPUWorker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
91 int i = worker->loopCounter;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
92 struct Queue* queue = GET_WAIT_LIST(context->data[context->odg+i]);
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
96 __code odgCommitCPUWorker2(struct CPUWorker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
97 int i = worker->loopCounter;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
98 struct Queue* queue = GET_WAIT_LIST(context->data[context->odg+i]);
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
102 __code odgCommitCPUWorker3(struct CPUWorker* worker, struct Context* task) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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)
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
104 struct TaskManager* taskManager = task->taskManager;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
105 taskManager->task = task;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
106 goto taskManager->spawn(task, odgCommitCPUWorker1);
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
107 }
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
111 __code odgCommitCPUWorker3_stub(struct Context* context) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
112 CPUWorker* cpuWorker = (CPUWorker*)GearImpl(context, Worker, worker);
288
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
113 struct Context* task = &Gearef(context, Queue)->data->Context;
473
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
114 goto odgCommitCPUWorker3(context,
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
119 __code odgCommitCPUWorker4(struct CPUWorker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
120 worker->loopCounter++;
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
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
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
124 __code shutdownCPUWorker(struct CPUWorker* worker) {
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents: 468
diff changeset
125 goto exit_code();
288
f1b0cc555b6e Add odgCommit
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 282
diff changeset
126 }