Mercurial > hg > GearsTemplate
annotate src/parallel_execution/TaskManagerImpl.cbc @ 348:c03159481cb6
Par goto exchange inline expansion
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 06 Jun 2017 17:08:55 +0900 |
parents | 534601ed8c50 |
children | 01e0fa598ce3 |
rev | line source |
---|---|
269 | 1 #include "../context.h" |
278 | 2 |
269 | 3 #include <stdio.h> |
4 | |
5 void createWorkers(struct Context* context, TaskManager* taskManeger, TaskManagerImpl* taskManagerImpl); | |
6 | |
280 | 7 TaskManager* createTaskManagerImpl(struct Context* context, int numCPU, int numGPU, int numIO) { |
269 | 8 struct TaskManager* taskManager = new TaskManager(); |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
319
diff
changeset
|
9 // 0...numIO-1 IOProcessor |
269 | 10 // numIO...numIO+numGPU-1 GPUProcessor |
11 // numIO+numGPU...numIO+numGPU+numCPU-1 CPUProcessor | |
12 taskManager->io = 0; | |
13 taskManager->gpu = numIO; | |
14 taskManager->cpu = numIO+numGPU; | |
15 taskManager->maxCPU = numIO+numGPU+numCPU; | |
16 taskManager->createTask = C_createTask; | |
17 taskManager->spawn = C_spawnTaskManager; | |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
319
diff
changeset
|
18 taskManager->setWaitTask = C_setWaitTask; |
269 | 19 taskManager->shutdown = C_shutdownTaskManager; |
20 struct TaskManagerImpl* taskManagerImpl = new TaskManagerImpl(); | |
280 | 21 taskManagerImpl -> activeQueue = createSingleLinkedQueue(context); |
22 taskManagerImpl -> taskQueue = createSingleLinkedQueue(context); | |
269 | 23 taskManagerImpl -> numWorker = taskManager->maxCPU; |
24 createWorkers(context, taskManager, taskManagerImpl); | |
316
54d203daf06b
CUDAtwice.cbc is called.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
312
diff
changeset
|
25 taskManager->taskManager = (union Data*)taskManagerImpl; |
269 | 26 return taskManager; |
27 } | |
28 | |
29 void createWorkers(struct Context* context, TaskManager* taskManager, TaskManagerImpl* taskManagerImpl) { | |
30 int i = 0; | |
31 taskManagerImpl->workers = (Worker**)ALLOC_ARRAY(context, Worker, taskManager->maxCPU); | |
32 for (;i<taskManager->gpu;i++) { | |
280 | 33 Queue* queue = createSynchronizedQueue(context); |
269 | 34 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue); |
35 } | |
36 for (;i<taskManager->cpu;i++) { | |
312
7dd5a7d52a67
USE_CUDAWorker flag only for CUDAtwice
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
288
diff
changeset
|
37 #ifdef USE_CUDAWorker |
7dd5a7d52a67
USE_CUDAWorker flag only for CUDAtwice
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
288
diff
changeset
|
38 Queue* queue = createSynchronizedQueue(context); |
319
a15511b1a6e0
separate cuda.c, and USE_CUDA_MAIN_THREAD flag
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
317
diff
changeset
|
39 #ifndef USE_CUDA_MAIN_THREAD |
a15511b1a6e0
separate cuda.c, and USE_CUDA_MAIN_THREAD flag
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
317
diff
changeset
|
40 taskManagerImpl->workers[i] = (Worker*)createCUDAWorker(context, i, queue,0); |
a15511b1a6e0
separate cuda.c, and USE_CUDA_MAIN_THREAD flag
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
317
diff
changeset
|
41 #else |
316
54d203daf06b
CUDAtwice.cbc is called.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
312
diff
changeset
|
42 taskManagerImpl->workers[i] = (Worker*)queue; |
319
a15511b1a6e0
separate cuda.c, and USE_CUDA_MAIN_THREAD flag
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
317
diff
changeset
|
43 #endif |
269 | 44 #else |
280 | 45 Queue* queue = createSynchronizedQueue(context); |
269 | 46 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue); |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
319
diff
changeset
|
47 #endif |
269 | 48 } |
49 for (;i<taskManager->maxCPU;i++) { | |
280 | 50 Queue* queue = createSynchronizedQueue(context); |
269 | 51 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue); |
52 } | |
53 } | |
54 | |
55 __code createTask(struct TaskManager* taskManager) { | |
56 taskManager->context = NEW(struct Context); | |
57 initContext(taskManager->context); | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
58 taskManager->context->taskManager = (struct TaskManager*)taskManager->taskManager; |
326
f23f6d0aa4e9
Add examples/calc.cbc and build but not work
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
319
diff
changeset
|
59 taskManager->context->idg = taskManager->context->dataNum; |
280 | 60 goto meta(context, C_setWorker); |
269 | 61 } |
62 | |
63 __code setWorker(struct TaskManagerImpl* taskManager, struct Context* task, __code next(...)) { | |
64 task->workerId = taskManager->sendWorkerIndex; | |
65 if(++taskManager->sendWorkerIndex >= taskManager->numWorker) { | |
66 taskManager->sendWorkerIndex = 0; | |
67 } | |
68 goto next(...); | |
69 } | |
70 | |
280 | 71 __code setWorker_stub(struct Context* context) { |
72 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager); | |
73 goto setWorker(context, taskManager, Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next); | |
74 } | |
75 | |
348
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
76 __code setWaitTask(struct TaskManager* taskManager, struct Queue* queue, struct Context* task, struct LoopCounter* loopCounter, __code next(...)) { |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
77 int i = loopCounter->i; |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
78 if(task->idg + i < task->maxidg) { |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
79 loopCounter->i++; |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
80 queue->queue = (Data *)GET_WAIT_LIST(task->data[task->idg + i]); |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
81 queue->next = C_setWaitTask; |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
82 queue->data = (Data *)task; |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
83 goto meta(context, queue->queue->Queue.put); |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
84 } |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
85 goto meta(context, taskManager->taskManager->spawn); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
86 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
87 |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
88 __code setWaitTask_stub(struct Context* context) { |
348
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
89 TaskManager* taskManager = Gearef(context, TaskManager); |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
90 struct Context* task = Gearef(context, TaskManager)->context; |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
91 goto setWaitTask(context, taskManager, Gearef(context, Queue), task, Gearef(task, LoopCounter), taskGearef(context, TaskManager)->next); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
92 } |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
93 |
269 | 94 __code spawnTaskManager(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) { |
95 if (task->idgCount == 0) { | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
96 goto meta(context, C_taskSend); |
269 | 97 } else { |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
98 pthread_mutex_unlock(&taskManager->mutex); |
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
99 goto next(...); |
269 | 100 } |
101 } | |
102 | |
103 __code spawnTaskManager_stub(struct Context* context) { | |
104 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager); | |
105 pthread_mutex_lock(&taskManager->mutex); | |
106 goto spawnTaskManager(context, | |
348
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
107 taskManager, |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
108 Gearef(context, Queue), |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
109 Gearef(context, TaskManager)->context, |
c03159481cb6
Par goto exchange inline expansion
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
327
diff
changeset
|
110 Gearef(context, TaskManager)->next); |
269 | 111 } |
112 | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
113 __code taskSend(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) { |
269 | 114 struct Queue* tasks = taskManager->workers[task->workerId]->tasks; |
115 queue->queue = (union Data*)tasks; | |
116 queue->data = (union Data*)task; | |
117 queue->next = next; | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
118 pthread_mutex_unlock(&taskManager->mutex); |
269 | 119 goto meta(context, tasks->put); |
120 } | |
121 | |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
122 __code taskSend_stub(struct Context* context) { |
269 | 123 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager); |
327
534601ed8c50
Running dependency example for single thread and single task
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
326
diff
changeset
|
124 goto taskSend(context, taskManager, Gearef(context, Queue), Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next); |
269 | 125 } |
126 | |
127 __code shutdownTaskManager(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Queue* queue, __code next(...)) { | |
128 int i = loopCounter->i; | |
129 if (taskManager->cpu <= i && i < taskManager->maxCPU) { | |
130 struct Queue* tasks = taskManagerImpl->workers[i]->tasks; | |
131 queue->queue = (union Data*)tasks; | |
132 queue->data = NULL; | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
133 queue->next = C_shutdownTaskManager1; |
269 | 134 goto meta(context, tasks->put); |
135 } | |
136 | |
137 loopCounter->i = 0; | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
138 goto meta(context, next); |
269 | 139 } |
140 | |
141 __code shutdownTaskManager_stub(struct Context* context) { | |
142 TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager); | |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
143 goto shutdownTaskManager(context, Gearef(context, LoopCounter), &Gearef(context, TaskManager)->taskManager->TaskManager, taskManagerImpl, Gearef(context, Queue), Gearef(context, TaskManager)->next); |
269 | 144 } |
282
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
145 |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
146 __code shutdownTaskManager1(struct LoopCounter* loopCounter, TaskManagerImpl* taskManagerImpl) { |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
147 int i = loopCounter->i; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
148 pthread_join(taskManagerImpl->workers[i]->worker->CPUWorker.thread, NULL); |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
149 loopCounter->i++; |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
150 goto meta(context, C_shutdownTaskManager); |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
151 } |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
152 |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
153 __code shutdownTaskManager1_stub(struct Context* context) { |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
154 TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager); |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
155 goto shutdownTaskManager1(context, Gearef(context, LoopCounter), taskManagerImpl); |
a3448b0f0a56
Add input data gear
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
280
diff
changeset
|
156 } |