-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCPUControllerThread.c
284 lines (231 loc) · 10.8 KB
/
CPUControllerThread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
//
// CPUControllerThread.c
//
// mica
//
// Copyright (C) 2013, HKU
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
///////////////////////////////////////////////////////////////////////////////////////////////
#include "CPUControllerThread.h"
void CPUCTWriteStatus(CPUControllerThread * thread, char *status) {
strcpy(thread->healthString,status);
}
void CPUCTSetSRA(CPUControllerThread * thread, int alignmentModel, SRAArguments * cpuSraArgTemplate) {
thread->alignmentModel = alignmentModel;
thread->cpuSraArgTemplate = cpuSraArgTemplate;
}
void CPUCTSetPE(CPUControllerThread * thread, PEArguments * cpuPeArgTemplate) {
thread->cpuPeArgTemplate = cpuPeArgTemplate;
}
void CPUCTSetReadThread(CPUControllerThread * thread, PEReadThread * peReadThread) {
thread->peReadThread = peReadThread;
}
void CPUCTSetWriteThread(CPUControllerThread * thread, PEWriteThread * peWriteThread) {
thread->writeThread = peWriteThread;
}
CPUControllerThread * CPUCTCreate(uint8_t threadId, char * charMap,
unsigned int cpuMaxBatchSize) {
CPUControllerThread * ret = (CPUControllerThread *) MEMManMalloc(sizeof(CPUControllerThread), MEMORY_TYPE_CPU);
// Initialize some fields in CPUControllerThread
ret->threadId = threadId;
ret->charMap = charMap;
ret->cpuMaxBatchSize = cpuMaxBatchSize;
ret->t = MEMManMalloc(sizeof(pthread_t), MEMORY_TYPE_CPU);
ret->writeThread = NULL;
ret->statsTotalAlignmentTime = 0;
ret->statsTotalReadLoadTime = 0;
ret->statsTotalModelBuildTime = 0;
ret->statsTotalWaitTime = 0;
ret->statsQueryHandled = 0;
#ifdef MICA_PE_ENABLE_HEALTH_CHECK
strcpy(ret->healthString,"INIT");
#else
strcpy(ret->healthString,"DISABLED");
#endif
MICCTPollHealthString(ret);
return ret;
}
static void * start(void * cpuControllerThread) {
CPUControllerThread * thread = (CPUControllerThread *) cpuControllerThread;
uint8_t threadId = thread->threadId;
PEReadThread * peReadThread = thread->peReadThread;
// Timestamp'ed -----------
double startTime = setStartTime();
double lastEventTime = 0;
// double timestamp = getElapsedTime(startTime);
// printf("CPUC#%d - Elapsed time (Index Loading) : %9.4f seconds\n\n", timestamp - lastEventTime);
// lastEventTime = timestamp;
// ------------------------
////////////////////////////////////////////////////////////
// CPU Allocation of Memory Frame
// Fake pointer for memory straight copy.
////////////////////////////////////////////////////////////
unsigned int numReadPerCore = thread->cpuMaxBatchSize;
char * readNameBufferFrame;
unsigned char * readBodyBufferFrame;
uint16_t * readLengthBufferFrame;
char * mateNameBufferFrame;
unsigned char * mateBodyBufferFrame;
uint16_t * mateLengthBufferFrame;
PEInputParam * peInputParamBufferFrame;
PEReadOutput * peReadOutputBufferFrame;
char * readQualityBufferFrame;
char * mateQualityBufferFrame;
int algnFuncReasonFlag;
// Timestamp'ed -----------
double timestamp = getElapsedTime(startTime);
printf("CPUC#%d - Elapsed time (CPU Memory Allocation) : %9.4f seconds\n\n", thread->threadId, timestamp - lastEventTime);
lastEventTime = timestamp;
// ------------------------
// Write Thread
PEWriteThread * writeThread = thread->writeThread;
double processingTime = 0;
double alignmentTime = 0;
double readLoadTime = 0;
double modelTime = 0;
double waitTime = 0;
unsigned int queryInBatch = 1;
unsigned int readInBatch = 1;
unsigned int mateInBatch = 1;
unsigned long long queryHandled = 0;
#ifdef MIC_DEBUG_PRINT_OUTPUT_COUNTS
unsigned int numOfClosed = 0;
unsigned int numOfSaReq = 0;
unsigned int numOfSaContent = 0;
unsigned int numOfOccContent = 0;
#endif
char batchHandled = 0;
while ( queryInBatch != 0 && ( batchHandled < CPU_CONTROL_MAX_BATCH || CPU_CONTROL_MAX_BATCH == -1 ) ) {
#ifdef MICA_PE_ENABLE_HEALTH_CHECK
// Update status to WAIT
CPUCTWriteStatus(thread,"WAIT");
#endif
// Read in the query for the buckets for all threads
// The current stamp is being retrieved
PEReadBatch * peReadBatch = PEReadThread_wait(peReadThread);
#ifdef MICA_PE_ENABLE_HEALTH_CHECK
// Update status to WAIT
CPUCTWriteStatus(thread,"START");
#endif
// Timestamp'ed -----------
timestamp = getElapsedTime(startTime);
waitTime += timestamp - lastEventTime;
processingTime += timestamp - lastEventTime;
lastEventTime = timestamp;
// ------------------------
readInBatch = peReadBatch->readSize;
mateInBatch = peReadBatch->mateSize;
readNameBufferFrame = peReadBatch->readNames;
mateNameBufferFrame = peReadBatch->mateNames;
readBodyBufferFrame = peReadBatch->readPatterns;
mateBodyBufferFrame = peReadBatch->matePatterns;
readLengthBufferFrame = peReadBatch->readLengths;
mateLengthBufferFrame = peReadBatch->mateLengths;
peInputParamBufferFrame = peReadBatch->batchParam;
peReadOutputBufferFrame = peReadBatch->batchOutput;
readQualityBufferFrame = peReadBatch->readQuality;
mateQualityBufferFrame = peReadBatch->mateQuality;
algnFuncReasonFlag = peReadBatch->algnFuncReasonFlag;
queryInBatch = readInBatch > mateInBatch ? mateInBatch : readInBatch;
if (readInBatch != mateInBatch) {
printf("CPUC#%d - [ERROR] Number of reads are different between READ(%u) and MATE(%u). Only processing %u..\n", thread->threadId,readInBatch,mateInBatch,queryInBatch);
}
if (queryInBatch == 0) {
#ifdef MICA_PE_ENABLE_HEALTH_CHECK
// Update status to WAIT
CPUCTWriteStatus(thread,"EXITING");
#endif
PEReadThread_signal(peReadThread);
break;
}
// Timestamp'ed -----------
timestamp = getElapsedTime(startTime);
//printf("CPUC#%d - Elapsed time (Read Loading) : %9.4f seconds\n\n", thread->threadId, timestamp - lastEventTime);
readLoadTime += timestamp - lastEventTime;
processingTime += timestamp - lastEventTime;
lastEventTime = timestamp;
// ------------------------
////////////////////////////////////////////////////////////
// Handling Output
// This following pick results from MIC and output with CPU
////////////////////////////////////////////////////////////
#ifdef MICA_PE_ENABLE_HEALTH_CHECK
// Update status to WAIT
CPUCTWriteStatus(thread,"ALGNMT");
#endif
#ifndef MICA_PE_DISCARD_OUTPUT_FROM_MIC
PEWTCloneBuffer(writeThread,
1,queryInBatch,queryHandled,queryInBatch,
readNameBufferFrame,readBodyBufferFrame,readLengthBufferFrame,
mateNameBufferFrame,mateBodyBufferFrame,mateLengthBufferFrame,
readQualityBufferFrame, mateQualityBufferFrame,
peInputParamBufferFrame, peReadOutputBufferFrame,
MICA_PE_WRITE_THREAD_PAYLOAD_OUTPUT_TYPE_SKIP_MIC,
NULL,NULL,NULL,
NULL,NULL,NULL,
algnFuncReasonFlag);
#endif
// We do a direct copy from Read Thread into Write Thread
// Therefore the read completed signal cannot be fired too early
PEReadThread_signal(peReadThread);
queryHandled+=queryInBatch;
// Timestamp'ed -----------
timestamp = getElapsedTime(startTime);
//printf("CPUC#%d - Elapsed time (Output) : %9.4f seconds\n\n", thread->threadId, timestamp - lastEventTime);
processingTime += timestamp - lastEventTime;
lastEventTime = timestamp;
// ------------------------
batchHandled++;
printf("CPUC#%d - Number of Query Handled = %llu\n", thread->threadId,queryHandled);
printf("\n");
}
////////////////////////////////////////////////////////////
// Statistics
////////////////////////////////////////////////////////////
thread->statsTotalAlignmentTime = alignmentTime;
thread->statsTotalReadLoadTime = readLoadTime;
thread->statsTotalModelBuildTime = modelTime;
thread->statsTotalWaitTime = waitTime;
thread->statsQueryHandled = queryHandled;
thread->writeThread = NULL;
return NULL;
}
void CPUCTPrintStats (CPUControllerThread * thread, Logging * logger) {
LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Number of Query Handled = %llu\n", thread->threadId,thread->statsQueryHandled);
LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Read Loading Time = %9.4f seconds\n", thread->threadId,thread->statsTotalReadLoadTime);
LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Model Building Time = %9.4f seconds\n", thread->threadId,thread->statsTotalModelBuildTime);
LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Wait Time = %9.4f seconds\n", thread->threadId,thread->statsTotalWaitTime);
// LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Alignment Time = %9.4f seconds\n", thread->threadId,alignmentTime);
// LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "CPUC#%d - Processing (Total) Time = %9.4f seconds\n", thread->threadId,processingTime);
LOGWriteLine(logger, LOGGING_MSG_TYPE_ALL, "\n");
}
void CPUCTStart(CPUControllerThread * thread) {
pthread_create(thread->t, NULL, start, thread);
}
void CPUCTJoin(CPUControllerThread * thread) {
pthread_join(*thread->t, NULL);
}
void CPUCTFree(CPUControllerThread * thread) {
free(thread->t);
free(thread);
}
void CPUCTPollHealthString(CPUControllerThread * thread) {
sprintf(thread->consolidHealth,"%s",thread->healthString);
}
char * CPUCTGetHealthText(CPUControllerThread * thread) {
return thread->consolidHealth;
}