269
|
1 #include "../context.h"
|
278
|
2
|
269
|
3 #include <stdio.h>
|
|
4
|
|
5 void createWorkers(struct Context* context, TaskManager* taskManeger, TaskManagerImpl* taskManagerImpl);
|
|
6
|
279
|
7 TaskManager* createTaskManager(struct Context* context, int numCPU, int numGPU, int numIO) {
|
269
|
8 struct TaskManager* taskManager = new TaskManager();
|
|
9 // 0...numIO-1 IOProcessor
|
|
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;
|
|
18 taskManager->shutdown = C_shutdownTaskManager;
|
|
19 struct TaskManagerImpl* taskManagerImpl = new TaskManagerImpl();
|
|
20 taskManager->taskManager = (union Data*)taskManagerImpl;
|
|
21 taskManagerImpl -> activeQueue = &createSingleLinkedQueue(context)->Queue;
|
|
22 taskManagerImpl -> taskQueue = &createSingleLinkedQueue(context)->Queue;
|
|
23 taskManagerImpl -> numWorker = taskManager->maxCPU;
|
|
24 createWorkers(context, taskManager, taskManagerImpl);
|
|
25 return taskManager;
|
|
26 }
|
|
27
|
|
28 void createWorkers(struct Context* context, TaskManager* taskManager, TaskManagerImpl* taskManagerImpl) {
|
|
29 int i = 0;
|
|
30 taskManagerImpl->workers = (Worker**)ALLOC_ARRAY(context, Worker, taskManager->maxCPU);
|
|
31 for (;i<taskManager->gpu;i++) {
|
|
32 Queue* queue = &createSynchronizedQueue(context)->Queue;
|
|
33 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
|
|
34 }
|
|
35 for (;i<taskManager->cpu;i++) {
|
|
36 #ifdef USE_CUDA
|
|
37 #else
|
|
38 Queue* queue = &createSynchronizedQueue(context)->Queue;
|
|
39 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
|
|
40 #endif
|
|
41 }
|
|
42 for (;i<taskManager->maxCPU;i++) {
|
|
43 Queue* queue = &createSynchronizedQueue(context)->Queue;
|
|
44 taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context, i, queue);
|
|
45 }
|
|
46 }
|
|
47
|
|
48 __code createTask(struct TaskManager* taskManager) {
|
|
49 taskManager->context = NEW(struct Context);
|
|
50 initContext(taskManager->context);
|
|
51 goto C_setWorker(...);
|
|
52 }
|
|
53
|
|
54 __code setWorker(struct TaskManagerImpl* taskManager, struct Context* task, __code next(...)) {
|
|
55 task->workerId = taskManager->sendWorkerIndex;
|
|
56 if(++taskManager->sendWorkerIndex >= taskManager->numWorker) {
|
|
57 taskManager->sendWorkerIndex = 0;
|
|
58 }
|
|
59 goto next(...);
|
|
60 }
|
|
61
|
|
62 __code spawnTaskManager(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
|
|
63 if (task->idgCount == 0) {
|
|
64 // enqueue activeQueue
|
|
65 queue->queue = (union Data*)taskManager->activeQueue;
|
|
66 } else {
|
|
67 // enqueue waitQueue
|
|
68 queue->queue = (union Data*)taskManager->taskQueue;
|
|
69 }
|
|
70 queue->data = (union Data*)task;
|
|
71 queue->next = C_spawnTaskManager1;
|
|
72 goto meta(context, queue->queue->Queue.put);
|
|
73 }
|
|
74
|
|
75 __code spawnTaskManager_stub(struct Context* context) {
|
|
76 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
|
|
77 pthread_mutex_lock(&taskManager->mutex);
|
|
78 goto spawnTaskManager(context,
|
|
79 taskManager,
|
|
80 Gearef(context, Queue),
|
|
81 Gearef(context, TaskManager)->context,
|
|
82 Gearef(context, TaskManager)->next);
|
|
83 }
|
|
84
|
|
85
|
|
86 __code spawnTaskManager1(struct TaskManagerImpl* taskManager) {
|
|
87 pthread_mutex_unlock(&taskManager->mutex);
|
|
88 goto C_taskSend(...);
|
|
89 }
|
|
90
|
|
91 __code spawnTaskManager1_stub(struct Context* context) {
|
|
92 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
|
|
93 goto spawnTaskManager1(context,
|
|
94 taskManager);
|
|
95 }
|
|
96
|
|
97 __code taskSend(struct TaskManagerImpl* taskManager, struct Queue* queue) {
|
|
98 queue->queue = (union Data*)taskManager->activeQueue;
|
|
99 queue->next = C_taskSend1;
|
|
100 goto meta(context, taskManager->activeQueue->take);
|
|
101 }
|
|
102
|
|
103 __code taskSend1(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
|
|
104 struct Queue* tasks = taskManager->workers[task->workerId]->tasks;
|
|
105 queue->queue = (union Data*)tasks;
|
|
106 queue->data = (union Data*)task;
|
|
107 queue->next = next;
|
|
108 pthread_cond_signal(&taskManager->workers[task->workerId]->worker->CPUWorker.cond);
|
|
109 goto meta(context, tasks->put);
|
|
110 }
|
|
111
|
|
112 __code taskSend1_stub(struct Context* context) {
|
|
113 TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
|
|
114 goto taskSend1(context, taskManager, Gearef(context, Queue), Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next);
|
|
115 }
|
|
116
|
|
117 __code shutdownTaskManager(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Queue* queue, __code next(...)) {
|
|
118 int i = loopCounter->i;
|
|
119 if (taskManager->cpu <= i && i < taskManager->maxCPU) {
|
|
120 struct Queue* tasks = taskManagerImpl->workers[i]->tasks;
|
|
121 queue->queue = (union Data*)tasks;
|
|
122 queue->data = NULL;
|
|
123 queue->next = next;
|
|
124 goto meta(context, tasks->put);
|
|
125 pthread_join(taskManagerImpl->workers[i]->worker->CPUWorker.thread, NULL);
|
|
126 loopCounter->i++;
|
|
127 goto C_shutdownTaskManager(...);
|
|
128 }
|
|
129
|
|
130 loopCounter->i = 0;
|
|
131 goto meta(context, taskManager->next);
|
|
132 }
|
|
133
|
|
134 __code shutdownTaskManager_stub(struct Context* context) {
|
|
135 TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
|
|
136 goto shutdownTaskManager(context, Gearef(context, LoopCounter), Gearef(context, TaskManager), taskManagerImpl, Gearef(context, Queue), Gearef(context, TaskManager)->next);
|
|
137 }
|