annotate src/parallel_execution/TaskManagerImpl.cbc @ 280:2c2e4e597eb0

generate no compile errors
author mir3636
date Fri, 03 Feb 2017 17:47:27 +0900
parents b5e8fce400a5
children a3448b0f0a56
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
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
7 TaskManager* createTaskManagerImpl(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;
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
21 taskManagerImpl -> activeQueue = createSingleLinkedQueue(context);
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
22 taskManagerImpl -> taskQueue = createSingleLinkedQueue(context);
269
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++) {
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
32 Queue* queue = createSynchronizedQueue(context);
269
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
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
38 Queue* queue = createSynchronizedQueue(context);
269
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++) {
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
43 Queue* queue = createSynchronizedQueue(context);
269
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);
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
51 goto meta(context, C_setWorker);
269
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
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
62 __code setWorker_stub(struct Context* context) {
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
63 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
64 goto setWorker(context, taskManager, Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next);
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
65 }
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
66
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
67 __code spawnTaskManager(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
68 if (task->idgCount == 0) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
69 // enqueue activeQueue
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
70 queue->queue = (union Data*)taskManager->activeQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
71 } else {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
72 // enqueue waitQueue
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
73 queue->queue = (union Data*)taskManager->taskQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
74 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
75 queue->data = (union Data*)task;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
76 queue->next = C_spawnTaskManager1;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
77 goto meta(context, queue->queue->Queue.put);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
78 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
79
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
80 __code spawnTaskManager_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
81 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
82 pthread_mutex_lock(&taskManager->mutex);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
83 goto spawnTaskManager(context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
84 taskManager,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
85 Gearef(context, Queue),
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
86 Gearef(context, TaskManager)->context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
87 Gearef(context, TaskManager)->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
88 }
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(struct TaskManagerImpl* taskManager) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
92 pthread_mutex_unlock(&taskManager->mutex);
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
93 goto meta(context, C_taskSend);
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
94 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
95
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
96 __code spawnTaskManager1_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
97 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
98 goto spawnTaskManager1(context,
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
99 taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
100 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
101
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
102 __code taskSend(struct TaskManagerImpl* taskManager, struct Queue* queue) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
103 queue->queue = (union Data*)taskManager->activeQueue;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
104 queue->next = C_taskSend1;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
105 goto meta(context, taskManager->activeQueue->take);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
106 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
107
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
108 __code taskSend1(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
109 struct Queue* tasks = taskManager->workers[task->workerId]->tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
110 queue->queue = (union Data*)tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
111 queue->data = (union Data*)task;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
112 queue->next = next;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
113 pthread_cond_signal(&taskManager->workers[task->workerId]->worker->CPUWorker.cond);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
114 goto meta(context, tasks->put);
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 taskSend1_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
118 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
119 goto taskSend1(context, taskManager, Gearef(context, Queue), Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
120 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
121
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
122 __code shutdownTaskManager(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Queue* queue, __code next(...)) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
123 int i = loopCounter->i;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
124 if (taskManager->cpu <= i && i < taskManager->maxCPU) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
125 struct Queue* tasks = taskManagerImpl->workers[i]->tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
126 queue->queue = (union Data*)tasks;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
127 queue->data = NULL;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
128 queue->next = next;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
129 goto meta(context, tasks->put);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
130 pthread_join(taskManagerImpl->workers[i]->worker->CPUWorker.thread, NULL);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
131 loopCounter->i++;
280
2c2e4e597eb0 generate no compile errors
mir3636
parents: 279
diff changeset
132 goto meta(context, C_shutdownTaskManager);
269
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
133 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
134
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
135 loopCounter->i = 0;
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
136 goto meta(context, taskManager->next);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
137 }
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
138
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
139 __code shutdownTaskManager_stub(struct Context* context) {
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
140 TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
5170539348ec rename TaskManagerImpl.cbc
mir3636
parents:
diff changeset
141 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
142 }