annotate src/parallel_execution/TaskManagerImpl.cbc @ 279:b5e8fce400a5

Fix
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Fri, 03 Feb 2017 03:25:57 +0900
parents 23767f714f4a
children 2c2e4e597eb0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
1 #include "../context.h"
278
23767f714f4a fix generate_stub
mir3636
parents: 277
diff changeset
2
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
3 #include <stdio.h>
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
4
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
5 void createWorkers(struct Context* context, TaskManager* taskManeger, TaskManagerImpl* taskManagerImpl);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
6
279
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents: 278
diff changeset
7 TaskManager* createTaskManager(struct Context* context, int numCPU, int numGPU, int numIO) {
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
8 struct TaskManager* taskManager = new TaskManager();
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
9 // 0...numIO-1 IOProcessor
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
10 // numIO...numIO+numGPU-1 GPUProcessor
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
11 // numIO+numGPU...numIO+numGPU+numCPU-1 CPUProcessor
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
12 taskManager->io = 0;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
13 taskManager->gpu = numIO;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
14 taskManager->cpu = numIO+numGPU;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
15 taskManager->maxCPU = numIO+numGPU+numCPU;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
16 taskManager->createTask = C_createTask;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
17 taskManager->spawn = C_spawnTaskManager;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
18 taskManager->shutdown = C_shutdownTaskManager;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
19 struct TaskManagerImpl* taskManagerImpl = new TaskManagerImpl();
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
20 taskManager->taskManager = (union Data*)taskManagerImpl;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
21 taskManagerImpl -> activeQueue = &createSingleLinkedQueue(context)->Queue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
22 taskManagerImpl -> taskQueue = &createSingleLinkedQueue(context)->Queue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
23 taskManagerImpl -> numWorker = taskManager->maxCPU;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
24 createWorkers(context, taskManager, taskManagerImpl);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
25 return taskManager;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
26 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
27
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
28 void createWorkers(struct Context* context, TaskManager* taskManager, TaskManagerImpl* taskManagerImpl) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
29 int i = 0;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
30 taskManagerImpl->workers = (Worker**)ALLOC_ARRAY(context, Worker, taskManager->maxCPU);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
31 for (;i<taskManager->gpu;i++) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
32 Queue* queue = &createSynchronizedQueue(context)->Queue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
33 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
34 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
35 for (;i<taskManager->cpu;i++) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
36 #ifdef USE_CUDA
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
37 #else
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
38 Queue* queue = &createSynchronizedQueue(context)->Queue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
39 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
40 #endif
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
41 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
42 for (;i<taskManager->maxCPU;i++) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
43 Queue* queue = &createSynchronizedQueue(context)->Queue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
44 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
45 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
46 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
47
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
48 __code createTask(struct TaskManager* taskManager) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
49 taskManager->context = NEW(struct Context);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
50 initContext(taskManager->context);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
51 goto C_setWorker(...);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
52 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
53
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
54 __code setWorker(struct TaskManagerImpl* taskManager, struct Context* task, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
55 task->workerId = taskManager->sendWorkerIndex;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
56 if(++taskManager->sendWorkerIndex >= taskManager->numWorker) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
57 taskManager->sendWorkerIndex = 0;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
58 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
59 goto next(...);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
60 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
61
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
62 __code spawnTaskManager(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
63 if (task->idgCount == 0) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
64 // enqueue activeQueue
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
65 queue->queue = (union Data*)taskManager->activeQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
66 } else {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
67 // enqueue waitQueue
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
68 queue->queue = (union Data*)taskManager->taskQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
69 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
70 queue->data = (union Data*)task;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
71 queue->next = C_spawnTaskManager1;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
72 goto meta(context, queue->queue->Queue.put);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
73 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
74
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
75 __code spawnTaskManager_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
76 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
77 pthread_mutex_lock(&taskManager->mutex);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
78 goto spawnTaskManager(context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
79 taskManager,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
80 Gearef(context, Queue),
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
81 Gearef(context, TaskManager)->context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
82 Gearef(context, TaskManager)->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
83 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
84
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
85
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
86 __code spawnTaskManager1(struct TaskManagerImpl* taskManager) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
87 pthread_mutex_unlock(&taskManager->mutex);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
88 goto C_taskSend(...);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
89 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
90
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
91 __code spawnTaskManager1_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
92 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
93 goto spawnTaskManager1(context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
94 taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
95 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
96
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
97 __code taskSend(struct TaskManagerImpl* taskManager, struct Queue* queue) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
98 queue->queue = (union Data*)taskManager->activeQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
99 queue->next = C_taskSend1;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
100 goto meta(context, taskManager->activeQueue->take);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
101 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
102
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
103 __code taskSend1(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
104 struct Queue* tasks = taskManager->workers[task->workerId]->tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
105 queue->queue = (union Data*)tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
106 queue->data = (union Data*)task;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
107 queue->next = next;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
108 pthread_cond_signal(&taskManager->workers[task->workerId]->worker->CPUWorker.cond);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
109 goto meta(context, tasks->put);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
110 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
111
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
112 __code taskSend1_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
113 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
114 goto taskSend1(context, taskManager, Gearef(context, Queue), Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
115 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
116
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
117 __code shutdownTaskManager(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Queue* queue, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
118 int i = loopCounter->i;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
119 if (taskManager->cpu <= i && i < taskManager->maxCPU) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
120 struct Queue* tasks = taskManagerImpl->workers[i]->tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
121 queue->queue = (union Data*)tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
122 queue->data = NULL;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
123 queue->next = next;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
124 goto meta(context, tasks->put);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
125 pthread_join(taskManagerImpl->workers[i]->worker->CPUWorker.thread, NULL);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
126 loopCounter->i++;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
127 goto C_shutdownTaskManager(...);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
128 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
129
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
130 loopCounter->i = 0;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
131 goto meta(context, taskManager->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
132 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
133
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
134 __code shutdownTaskManager_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
135 TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
136 goto shutdownTaskManager(context, Gearef(context, LoopCounter), Gearef(context, TaskManager), taskManagerImpl, Gearef(context, Queue), Gearef(context, TaskManager)->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
137 }