-
Notifications
You must be signed in to change notification settings - Fork 13
/
flow.cu
338 lines (297 loc) · 11 KB
/
flow.cu
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#include <cuda_runtime.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <fcntl.h>
#include <unistd.h>
#include <sched.h>
#include <pthread.h>
#include <sys/time.h>
#include "dsp.h"
#include "flow.h"
#include "errorhandler.h"
#define FLOW_THREAD_PRIORITY 26
// Note that, in cuCheckHelper, the second parameter "failStatement" is run as failStatement;
// Since this is a macro, the statements "KeepRunning=false; break" are inserted into "failStatement"
// allowing these two lines of code to be executed.
#define cuCheckF(stmt) cuCheckHelper(stmt, KeepRunning=false; break)
dsp::Flow::~Flow(){} // implementing pure virtual destructor 21109417
int dsp::Flow::LoadFlow(const char *filename) {
return -1;
}
int dsp::Flow::Start() {
int i,j;
std::clog << "[FLOW] Starting." << std::endl;
cuCheck(cudaStreamCreate(&cuStream));
for (i=0; i<(int)Mods.size(); i++) {
j = Mods[i]->Start((void*)&cuStream);
if (j) {
std::cerr << "[Flow] Unable to start module "
<< Mods[i]->GetModuleName() << std::endl;
for (j=0; j<i; j++)
Mods[i]->Stop();
return -1;
}
}
cuCheck(cudaEventCreate(&cuEvent));
pthread_attr_t attr;
struct sched_param param;
if (Mods.size() < 1) {
std::cerr << "[Flow] Flow not loaded." << std::endl;
return -1;
}
i = pthread_attr_init(&attr);
if (i) {
std::cerr << "[Flow] Unable to init thread attr."
<< std::endl;
return -1;
}
int minPrio = sched_get_priority_min(SCHED_RR);
int maxPrio = sched_get_priority_max(SCHED_RR);
/* Posix min requirements is 32 priority levels, Although linux provides
99 levels, good to check for min/max available levels. */
param.sched_priority = (maxPrio - minPrio) *
FLOW_THREAD_PRIORITY / 32 + minPrio;
i = pthread_attr_setschedpolicy(&attr, SCHED_RR);
if (i) {
std::cerr << "[Flow] Unable to set sample thread real-time."
<< std::endl;
return -1;
}
i = pthread_attr_setschedparam(&attr, ¶m);
if (i) {
std::cerr << "[Flow] Unable to set sample thread priority."
<< std::endl;
return -1;
}
i = pthread_create(&thread, &attr, FlowThreadEntry, this);
if (i) {
std::cerr << "[Flow] Unable to create thread."
<< std::endl;
return -1;
}
std::clog << "[FLOW] Started." << std::endl;
return 0;
}
int dsp::Flow::Stop(void) {
if (KeepRunning || FlowDone) {
std::clog << "[Flow] Stopping Flow." << std::endl;
KeepRunning = false;
return pthread_join(thread, NULL);
} else {
std::clog << "[Flow] Stop: Flow wasn't running." << std::endl;
return 0;
}
}
void * dsp::Flow::FlowThreadEntry(void * thisObj) {
((dsp::Flow *)thisObj)->FlowThread();
return NULL;
}
void dsp::Flow::FlowThread() {
int i, j;
unsigned long runCount = 0;
struct timeval start, stop, iterStart, iterStop;
unsigned long long totalIterDuration = 0; //microseconds
unsigned int avgIterDuration = 0; //microseconds/
unsigned char numMax = 40;
unsigned int maxIterDuration[numMax];
unsigned long maxCount[numMax];
unsigned int minCount;
unsigned int minIterDuration = (unsigned int)-1;
unsigned int iterDuration;
unsigned long microsecs;
double seconds;
for (i=0; i<numMax; i++) maxIterDuration[i] = 0;
j = gettimeofday(&start, NULL);
KeepRunning = true;
while (KeepRunning) {
for (i=0; i<(int)Mods.size(); i++) {
j = Mods[i]->Update((void*)&cuStream);
if (j) {
std::cerr << "[Flow] " << Mods[i]->GetModuleName()
<< "->Update() Failed. \nStopping Flow."
<< std::endl;
KeepRunning = false;
break;
}
if (i == 0){
// start timer after SampleBlock returned
j = gettimeofday(&iterStart, NULL);
}
}
cuCheckF(cudaStreamSynchronize(cuStream));
j = gettimeofday(&iterStop, NULL);
if ((iterStop.tv_sec - iterStart.tv_sec) > 0) {
iterDuration = 1000000 + iterStop.tv_usec - iterStart.tv_usec
+ (iterStop.tv_sec - iterStart.tv_sec - 1) * 1000000;
} else {
iterDuration = (iterStop.tv_usec - iterStart.tv_usec);
}
runCount++;
for (i=0; i<numMax; i++) {
if (iterDuration > maxIterDuration[i]) {
for (j=numMax-1; j>i; j--){
maxIterDuration[j] = maxIterDuration[j-1];
maxCount[j] = maxCount[j-1];
}
maxIterDuration[i] = iterDuration;
maxCount[i] = runCount;
break;
}
}
if (iterDuration < minIterDuration) {
minIterDuration = iterDuration;
minCount = runCount;
}
totalIterDuration += iterDuration;
//if ((runCount % 2000) == 0)
// std::clog << "[Flow] runCount = " << runCount << std::endl;
}
std::clog << "[Flow] Flow Stopped." << std::endl;
std::clog << "[Flow] Stopping Modules." << std::endl;
for (i=0; i<(int)Mods.size(); i++) {
Mods[i]->Stop();
}
std::clog << "[Flow] runCount = " << runCount << std::endl;
avgIterDuration = totalIterDuration / runCount;
std::clog << "[Flow] Average 1ms block duration = " << avgIterDuration
<< " us" << std::endl;
for (i=0; i<1; i++) //numMax; i++)
std::clog << "[Flow] Max[" << i << "] 1ms block duration = "
<< maxIterDuration[i]
<< " us, run count = " << maxCount[i] << std::endl;
std::clog << "[Flow] Min 1ms block duration = " << minIterDuration
<< " us, run count = " << minCount << std::endl;
j = gettimeofday(&stop, NULL);
if (stop.tv_usec < start.tv_usec) {
seconds = (double)(stop.tv_sec - start.tv_sec - 1);
microsecs = 1000000 + stop.tv_usec - start.tv_usec;
} else {
seconds = (double)(stop.tv_sec - start.tv_sec);
microsecs = stop.tv_usec - start.tv_usec;
}
seconds += (double)microsecs / 1000000.0;
std::clog << "[Flow] Total time = " << seconds << " seconds." << std::endl;
cuCheckV(cudaEventDestroy(cuEvent));
cuCheckV(cudaStreamDestroy(cuStream));
FlowDone = true;
}
bool dsp::Flow::CheckFlowState(void) {
return FlowDone;
}
int dsp::Flow::GetModID(const std::string &ModName)const {
for (int i=0; i<(int)Mods.size(); i++){
if (ModName.compare(Mods[i]->GetModuleName()) == 0)
return i;
}
std::cerr << "[FLOW] GetModID: Invalid Module Name: " << ModName << std::endl;
return -1;
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const int val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, val);
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const char val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, val);
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const float val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, val);
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const double val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, val);
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const bool val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, val);
}
int dsp::Flow::SetModParam(const std::string &ModName, const std::string key, const char *str) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->SetParam(key, str);
}
int dsp::Flow::GetModParam(const std::string &ModName, const std::string key, int *val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->GetParam(key, val);
}
int dsp::Flow::GetModParam(const std::string &ModName, const std::string key, float *val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->GetParam(key, val);
}
int dsp::Flow::GetModParam(const std::string &ModName, const std::string key, double *val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->GetParam(key, val);
}
int dsp::Flow::GetModParam(const std::string &ModName, const std::string key, bool *val) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->GetParam(key, val);
}
int dsp::Flow::GetModParam(const std::string &ModName, const std::string key, char *str,
const unsigned int capacity) {
int ModID = GetModID(ModName);
if (ModID < 0) return -1;
return Mods[ModID]->GetParam(key, str, capacity);
}
int dsp::Flow::ConnectPort(const std::string &srcModName, const char *srcPortName,
const std::string &dstModName, const char *dstPortName)
{
int srcModID, srcPortID, dstModID, dstPortID;
srcModID = GetModID(srcModName);
if (srcModID < 0) return -1;
srcPortID = Mods[srcModID]->GetOutputID(srcPortName);
if (srcPortID < 0) return -1;
dstModID = GetModID(dstModName);
if (dstModID < 0) return -1;
dstPortID = Mods[dstModID]->GetInputID(dstPortName);
if (dstPortID < 0) return -1;
return ConnectPort(srcModID, srcPortID, dstModID, dstPortID);
}
int dsp::Flow::ConnectPort(int srcModID, int srcPortID,
int dstModID, int dstPortID)
{
int i;
dsp::Port *temp;
if ((srcModID >= (int)Mods.size()) || (dstModID >= (int)Mods.size())) {
std::cerr << "[Flow] Module IDs do not exist" << std::endl;
return -1;
}
i = Mods[srcModID]->GetOutput(srcPortID, &temp);
if (i) {
std::cerr << "[Flow] Unable to get output from ["
<< Mods[srcModID]->GetModuleName() << "]" << std::endl;
return -1;
}
i = Mods[dstModID]->SetInput(dstPortID, temp);
if (i) {
std::cerr << "[Flow] Unable to set input to ["
<< Mods[dstModID]->GetModuleName() << "]" << std::endl;
return -1;
}
/** \todo Implement some kind of graph data structure to save connections
* to allow for generating a graphical block diagram in future.
*/
return 0;
}
int
dsp::Flow::GetOutput (const std::string &modName,const std::string &portName,dsp::Port **out)const{
const int modID = GetModID(modName);
if (modID < 0) return -1;
char* portname_c = new char[portName.size()+1];
strcpy(portname_c,portName.c_str());
const int portID = Mods[modID]->GetOutputID(portname_c);
delete[] portname_c;
if (portID < 0) return -1;
return Mods[modID]->GetOutput(portID,out);
}