Repository navigation
Expand file tree
/
Copy pathmain.c
More file actions
553 lines (493 loc) · 19.4 KB
/
Copy pathmain.c
File metadata and controls
553 lines (493 loc) · 19.4 KB
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
* Copyright 2019 IBM
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <getopt.h>
#include "kernels_api.h"
#include "sim_environs.h"
#define TIME
#ifdef INT_TIME
extern uint64_t bitrev_sec;
extern uint64_t bitrev_usec;
extern uint64_t fft_sec;
extern uint64_t fft_usec;
extern uint64_t cdfmcw_sec;
extern uint64_t cdfmcw_usec;
extern uint64_t dodec_sec;
extern uint64_t dodec_usec;
extern uint64_t depunc_sec;
extern uint64_t depunc_usec;
#endif
char h264_dict[256];
char cv_dict[256];
char rad_dict[256];
char vit_dict[256];
bool_t bypass_h264_functions = false; // This is a global-disable of executing H264 execution functions...
bool_t all_obstacle_lanes_mode = false;
unsigned time_step;
void print_usage(char * pname) {
printf("Usage: %s <OPTIONS>\n", pname);
printf(" OPTIONS:\n");
printf(" -h : print this helpful usage info\n");
printf(" -o : print the Visualizer output traace information during the run\n");
printf(" -R <file> : defines the input Radar dictionary file <file> to use\n");
printf(" -V <file> : defines the input Viterbi dictionary file <file> to use\n");
printf(" -C <file> : defines the input CV/CNN dictionary file <file> to use\n");
printf(" -H <file> : defines the input H264 dictionary file <file> to use\n");
printf(" -b : Bypass (do not use) the H264 functions in this run.\n");
#ifdef USE_SIM_ENVIRON
printf(" -s <N> : Sets the max number of time steps to simulate\n");
printf(" -r <N> : Sets the rand random number seed to N\n");
printf(" -A : Allow obstacle vehciles in All lanes (otherwise not in left or right hazard lanes)\n");
printf(" -W <wfile> : defines the world environment parameters description file <wfile> to use\n");
#else
printf(" -t <trace> : defines the input trace file <trace> to use\n");
#endif
printf(" -f <N> : defines which Radar Dictionary Set is used for Critical FFT Tasks\n");
printf(" : Each Set of Radar Dictionary Entries Can use a different sample size, etc.\n");
printf(" -n <N> : defines number of Viterbi messages per time step behavior:\n");
printf(" : 0 = One message per time step\n");
printf(" : 1 = One message per obstacle per time step\n");
printf(" : 2 = One msg per obstacle + 1 per time step\n");
printf(" -v <N> : defines Viterbi message size:\n");
printf(" : 0 = Short messages (4 characters)\n");
printf(" : 1 = Medium messages (500 characters)\n");
printf(" : 2 = Long messages (1000 characters)\n");
printf(" : 3 = Max-sized messages (1500 characters)\n");
}
int main(int argc, char *argv[])
{
vehicle_state_t vehicle_state;
label_t label;
distance_t distance;
message_t message;
#ifdef USE_SIM_ENVIRON
char* world_desc_file_name = "default_world.desc";
#else
char* trace_file = "";
#endif
int opt;
rad_dict[0] = '\0';
vit_dict[0] = '\0';
cv_dict[0] = '\0';
h264_dict[0] = '\0';
// put ':' in the starting of the
// string so that program can
// distinguish between '?' and ':'
while((opt = getopt(argc, argv, ":hAbot:v:n:s:r:W:R:V:C:H:f:")) != -1) {
switch(opt) {
case 'h':
print_usage(argv[0]);
exit(0);
case 'A':
all_obstacle_lanes_mode = true;
break;
case 'o':
output_viz_trace = true;
break;
case 'R':
snprintf(rad_dict, 255, "%s", optarg);
break;
case 'C':
snprintf(cv_dict, 255, "%s", optarg);
break;
case 'H':
snprintf(h264_dict, 255, "%s", optarg);
break;
case 'V':
snprintf(vit_dict, 255, "%s", optarg);
break;
case 'b':
bypass_h264_functions = true;
break;
case 's':
#ifdef USE_SIM_ENVIRON
max_time_steps = atoi(optarg);
printf("Using %u maximum time steps (simulation)\n", max_time_steps);
#endif
break;
case 'f':
crit_fft_samples_set = atoi(optarg);
printf("Using Radar Dictionary samples set %u for the critical FFT tasks\n", crit_fft_samples_set);
break;
case 'r':
#ifdef USE_SIM_ENVIRON
rand_seed = atoi(optarg);
#endif
break;
case 't':
#ifndef USE_SIM_ENVIRON
trace_file = optarg;
printf("Using trace file: %s\n", trace_file);
#endif
break;
case 'v':
vit_msgs_size = atoi(optarg);
if (vit_msgs_size >= VITERBI_MSG_LENGTHS) {
printf("ERROR: Specified viterbi message size (%u) is larger than max (%u) : from the -v option\n", vit_msgs_size, VITERBI_MSG_LENGTHS);
exit(-1);
} else {
printf("Using viterbi message size %u = %s\n", vit_msgs_size, vit_msgs_size_str[vit_msgs_size]);
}
break;
case 'n':
vit_msgs_per_step = atoi(optarg);
if (vit_msgs_size >= VITERBI_MSG_LENGTHS) {
printf("ERROR: Specified viterbi messages per time step behavior (%u) is larger than max (%u) : from the -n option\n", vit_msgs_size, VITERBI_MSG_LENGTHS);
exit(-1);
} else {
printf("Using viterbi messages per step behavior %u = %s\n", vit_msgs_per_step, vit_msgs_per_step_str[vit_msgs_per_step]);
}
break;
case 'W':
#ifdef USE_SIM_ENVIRON
world_desc_file_name = optarg;
printf("Using world description file: %s\n", world_desc_file_name);
#endif
break;
case ':':
printf("option needs a value\n");
break;
case '?':
printf("unknown option: %c\n", optopt);
break;
}
}
// optind is for the extra arguments
// which are not parsed
for(; optind < argc; optind++){
printf("extra arguments: %s\n", argv[optind]);
}
if (rad_dict[0] == '\0') {
sprintf(rad_dict, "traces/norm_radar_16k_dictionary.dfn");
}
if (vit_dict[0] == '\0') {
sprintf(vit_dict, "traces/vit_dictionary.dfn");
}
if (cv_dict[0] == '\0') {
sprintf(cv_dict, "traces/objects_dictionary.dfn");
}
if (h264_dict[0] == '\0') {
sprintf(h264_dict, "traces/h264_dictionary.dfn");
}
printf("\nDictionaries:\n");
printf(" CV/CNN : %s\n", cv_dict);
printf(" Radar : %s\n", rad_dict);
printf(" Viterbi: %s\n", vit_dict);
/* We plan to use three separate trace files to drive the three different kernels
* that are part of mini-ERA (CV, radar, Viterbi). All these three trace files
* are required to have the same base name, using the file extension to indicate
* which kernel the trace corresponds to (cv, rad, vit).
*/
/* if (argc != 2) */
/* { */
/* printf("Usage: %s <trace_basename>\n\n", argv[0]); */
/* printf("Where <trace_basename> is the basename of the trace files to load:\n"); */
/* printf(" <trace_basename>.cv : trace to feed the computer vision kernel\n"); */
/* printf(" <trace_basename>.rad : trace to feed the radar (FFT-1
8000
D) kernel\n"); */
/* printf(" <trace_basename>.vit : trace to feed the Viterbi decoding kernel\n"); */
/* return 1; */
/* } */
char cv_py_file[] = "../cv/keras_cnn/lenet.py";
printf("Doing initialization tasks...\n");
#ifndef USE_SIM_ENVIRON
/* Trace filename construction */
/* char * trace_file = argv[1]; */
//printf("Input trace file: %s\n", trace_file);
/* Trace Reader initialization */
if (!init_trace_reader(trace_file))
{
printf("Error: the trace reader couldn't be initialized properly.\n");
return 1;
}
#endif
/* Kernels initialization */
if (bypass_h264_functions) {
printf("Bypassing the H264 Functionality in this run...\n");
} else {
printf("Initializing the H264 kernel...\n");
if (!init_h264_kernel(h264_dict))
{
printf("Error: the H264 decoding kernel couldn't be initialized properly.\n");
return 1;
}
}
printf("Initializing the CV kernel...\n");
if (!init_cv_kernel(cv_py_file, cv_dict))
{
printf("Error: the computer vision kernel couldn't be initialized properly.\n");
return 1;
}
printf("Initializing the Radar kernel...\n");
if (!init_rad_kernel(rad_dict))
{
printf("Error: the radar kernel couldn't be initialized properly.\n");
return 1;
}
printf("Initializing the Viterbi kernel...\n");
if (!init_vit_kernel(vit_dict))
{
printf("Error: the Viterbi decoding kernel couldn't be initialized properly.\n");
return 1;
}
if (crit_fft_samples_set >= num_radar_samples_sets) {
printf("ERROR : Selected FFT Tasks from Radar Dictionary Set %u but there are only %u sets in the dictionary %s\n", crit_fft_samples_set, num_radar_samples_sets, rad_dict);
exit(-1);
}
/* We assume the vehicle starts in the following state:
* - Lane: center
* - Speed: 50 mph
*/
vehicle_state.active = true;
vehicle_state.lane = center;
vehicle_state.speed = 50;
DEBUG(printf("Vehicle starts with the following state: active: %u lane %u speed %.1f\n", vehicle_state.active, vehicle_state.lane, vehicle_state.speed));
#ifdef USE_SIM_ENVIRON
// In simulation mode, we could start the main car is a different state (lane, speed)
init_sim_environs(world_desc_file_name, &vehicle_state);
#endif
/*** MAIN LOOP -- iterates until all the traces are fully consumed ***/
time_step = 0;
#ifdef TIME
struct timeval stop_prog, start_prog;
struct timeval stop_iter_rad, start_iter_rad;
struct timeval stop_iter_vit, start_iter_vit;
struct timeval stop_iter_cv , start_iter_cv;
struct timeval stop_iter_h264 , start_iter_h264;
uint64_t iter_rad_sec = 0LL;
uint64_t iter_vit_sec = 0LL;
uint64_t iter_cv_sec = 0LL;
uint64_t iter_h264_sec = 0LL;
uint64_t iter_rad_usec = 0LL;
uint64_t iter_vit_usec = 0LL;
uint64_t iter_cv_usec = 0LL;
uint64_t iter_h264_usec = 0LL;
struct timeval stop_exec_rad, start_exec_rad;
struct timeval stop_exec_vit, start_exec_vit;
struct timeval stop_exec_cv , start_exec_cv;
struct timeval stop_exec_h264 , start_exec_h264;
uint64_t exec_rad_sec = 0LL;
uint64_t exec_vit_sec = 0LL;
uint64_t exec_cv_sec = 0LL;
uint64_t exec_h264_sec = 0LL;
uint64_t exec_rad_usec = 0LL;
uint64_t exec_vit_usec = 0LL;
uint64_t exec_cv_usec = 0LL;
uint64_t exec_h264_usec = 0LL;
//printf("Program run time in milliseconds %f\n", (double) (stop.tv_sec - start.tv_sec) * 1000 + (double) (stop.tv_usec - start.tv_usec) / 1000);
#endif // TIME
printf("Starting the main loop...\n");
/* The input trace contains the per-epoch (time-step) input data */
#ifdef TIME
gettimeofday(&start_prog, NULL);
#endif
#ifdef USE_SIM_ENVIRON
DEBUG(printf("\n\nTime Step %d\n", time_step));
while (iterate_sim_environs(vehicle_state))
#else //TRACE DRIVEN MODE
read_next_trace_record(vehicle_state);
while (!eof_trace_reader())
#endif
{
DEBUG(printf("Vehicle_State: Lane %u %s Speed %.1f\n", vehicle_state.lane, lane_names[vehicle_state.lane], vehicle_state.speed));
/* The computer vision kernel performs object recognition on the
* next image, and returns the corresponding label.
* This process takes place locally (i.e. within this car).
*/
#ifdef TIME
gettimeofday(&start_iter_h264, NULL);
#endif
h264_dict_entry_t* hdep = 0x0;
if (!bypass_h264_functions) {
hdep = iterate_h264_kernel(vehicle_state);
}
#ifdef TIME
gettimeofday(&stop_iter_h264, NULL);
iter_h264_sec += stop_iter_h264.tv_sec - start_iter_h264.tv_sec;
iter_h264_usec += stop_iter_h264.tv_usec - start_iter_h264.tv_usec;
#endif
/* The computer vision kernel performs object recognition on the
* next image, and returns the corresponding label.
* This process takes place locally (i.e. within this car).
*/
#ifdef TIME
gettimeofday(&start_iter_cv, NULL);
#endif
label_t cv_tr_label = iterate_cv_kernel(vehicle_state);
#ifdef TIME
gettimeofday(&stop_iter_cv, NULL);
iter_cv_sec += stop_iter_cv.tv_sec - start_iter_cv.tv_sec;
iter_cv_usec += stop_iter_cv.tv_usec - start_iter_cv.tv_usec;
#endif
/* The radar kernel performs distance estimation on the next radar
* data, and returns the estimated distance to the object.
*/
#ifdef TIME
gettimeofday(&start_iter_rad, NULL);
#endif
radar_dict_entry_t* rdentry_p = iterate_rad_kernel(vehicle_state);
#ifdef TIME
gettimeofday(&stop_iter_rad, NULL);
iter_rad_sec += stop_iter_rad.tv_sec - start_iter_rad.tv_sec;
iter_rad_usec += stop_iter_rad.tv_usec - start_iter_rad.tv_usec;
#endif
distance_t rdict_dist = rdentry_p->distance;
float * ref_in = rdentry_p->return_data;
float radar_inputs[2*RADAR_N];
for (int ii = 0; ii < 2*RADAR_N; ii++) {
radar_inputs[ii] = ref_in[ii];
}
/* The Viterbi decoding kernel performs Viterbi decoding on the next
* OFDM symbol (message), and returns the extracted message.
* This message can come from another car (including, for example,
* its 'pose') or from the infrastructure (like speed violation or
* road construction warnings). For simplicity, we define a fix set
* of message classes (e.g. car on the right, car on the left, etc.)
*/
#ifdef TIME
gettimeofday(&start_iter_vit, NULL);
#endif
vit_dict_entry_t* vdentry_p = iterate_vit_kernel(vehicle_state);
#ifdef TIME
gettimeofday(&stop_iter_vit, NULL);
iter_vit_sec += stop_iter_vit.tv_sec - start_iter_vit.tv_sec;
iter_vit_usec += stop_iter_vit.tv_usec - start_iter_vit.tv_usec;
#endif
// Here we will simulate multiple cases, based on global vit_msgs_behavior
int num_vit_msgs = 1; // the number of messages to send this time step (1 is default)
switch(vit_msgs_per_step) {
case 1: num_vit_msgs = total_obj; break;
case 2: num_vit_msgs = total_obj + 1; break;
}
// EXECUTE the kernels using the now known inputs
#ifdef TIME
gettimeofday(&start_exec_h264, NULL);
#endif
char* found_frame_ptr = 0x0;
if (!bypass_h264_functions) {
execute_h264_kernel(hdep, found_frame_ptr);
} else {
found_frame_ptr = (char*)0xAD065BED;
}
#ifdef TIME
gettimeofday(&stop_exec_h264, NULL);
exec_h264_sec += stop_exec_h264.tv_sec - start_exec_h264.tv_sec;
exec_h264_usec += stop_exec_h264.tv_usec - start_exec_h264.tv_usec;
gettimeofday(&start_exec_cv, NULL);
#endif
label = execute_cv_kernel(cv_tr_label, found_frame_ptr);
#ifdef TIME
gettimeofday(&stop_exec_cv, NULL);
exec_cv_sec += stop_exec_cv.tv_sec - start_exec_cv.tv_sec;
exec_cv_usec += stop_exec_cv.tv_usec - start_exec_cv.tv_usec;
gettimeofday(&start_exec_rad, NULL);
#endif
distance = execute_rad_kernel(radar_inputs);
#ifdef TIME
gettimeofday(&stop_exec_rad, NULL);
exec_rad_sec += stop_exec_rad.tv_sec - start_exec_rad.tv_sec;
exec_rad_usec += stop_exec_rad.tv_usec - start_exec_rad.tv_usec;
gettimeofday(&start_exec_vit, NULL);
#endif
message = execute_vit_kernel(vdentry_p, num_vit_msgs);
#ifdef TIME
gettimeofday(&stop_exec_vit, NULL);
exec_vit_sec += stop_exec_vit.tv_sec - start_exec_vit.tv_sec;
exec_vit_usec += stop_exec_vit.tv_usec - start_exec_vit.tv_usec;
#endif
// POST-EXECUTE each kernels to gather stats, etc.
if (!bypass_h264_functions) {
post_execute_h264_kernel();
}
post_execute_cv_kernel(cv_tr_label, label);
post_execute_rad_kernel(rdentry_p->set, rdentry_p->index_in_set, rdict_dist, distance);
for (int mi = 0; mi < num_vit_msgs; mi++) {
post_execute_vit_kernel(vdentry_p->msg_id, message);
}
/* The plan_and_control() function makes planning and control decisions
* based on the currently perceived information. It returns the new
* vehicle state.
*/
DEBUG(printf("Time Step %3u : Calling Plan and Control with message %u and distance %.1f\n", time_step, message, distance));
vehicle_state = plan_and_control(label, distance, message, vehicle_state);
DEBUG(printf("New vehicle state: lane %u speed %.1f\n\n", vehicle_state.lane, vehicle_state.speed));
#ifdef TIME
time_step++;
/* // This is done here, becuase of peculiarities in the CV/CNN software where the first */
/* // invocation does a bunch of initialization work (which other kernels do in the */
/* // dictionar-read time/init time-frame). */
/* if (time_step == 1) { */
/* gettimeofday(&start_prog, NULL); */
/* } */
#endif
#ifndef USE_SIM_ENVIRON
read_next_trace_record(vehicle_state);
#endif
}
#ifdef TIME
gettimeofday(&stop_prog, NULL);
#endif
/* All the traces have been fully consumed. Quitting... */
if (!bypass_h264_functions) {
closeout_h264_kernel();
}
closeout_cv_kernel();
closeout_rad_kernel();
closeout_vit_kernel();
#ifdef TIME
{
uint64_t total_exec = (uint64_t) (stop_prog.tv_sec - start_prog.tv_sec) * 1000000 + (uint64_t) (stop_prog.tv_usec - start_prog.tv_usec);
uint64_t iter_rad = (uint64_t) (iter_rad_sec) * 1000000 + (uint64_t) (iter_rad_usec);
uint64_t iter_vit = (uint64_t) (iter_vit_sec) * 1000000 + (uint64_t) (iter_vit_usec);
uint64_t iter_cv = (uint64_t) (iter_cv_sec) * 1000000 + (uint64_t) (iter_cv_usec);
uint64_t iter_h264 = (uint64_t) (iter_h264_sec) * 1000000 + (uint64_t) (iter_h264_usec);
uint64_t exec_rad = (uint64_t) (exec_rad_sec) * 1000000 + (uint64_t) (exec_rad_usec);
uint64_t exec_vit = (uint64_t) (exec_vit_sec) * 1000000 + (uint64_t) (exec_vit_usec);
uint64_t exec_cv = (uint64_t) (exec_cv_sec) * 1000000 + (uint64_t) (exec_cv_usec);
uint64_t exec_h264 = (uint64_t) (exec_h264_sec) * 1000000 + (uint64_t) (exec_h264_usec);
printf("\nProgram total execution time %lu usec\n", total_exec);
printf(" iterate_rad_kernel run time %lu usec\n", iter_rad);
printf(" iterate_vit_kernel run time %lu usec\n", iter_vit);
printf(" iterate_h264_kernel run time %lu usec\n", iter_h264);
printf(" iterate_cv_kernel run time %lu usec\n", iter_cv);
printf(" execute_rad_kernel run time %lu usec\n", exec_rad);
printf(" execute_vit_kernel run time %lu usec\n", exec_vit);
printf(" execute_h264_kernel run time %lu usec\n", exec_h264);
printf(" execute_cv_kernel run time %lu usec\n", exec_cv);
}
#endif // TIME
#ifdef INT_TIME
// These are timings taken from called routines...
printf("\n");
uint64_t fft = (uint64_t) (fft_sec) * 1000000 + (uint64_t) (fft_usec);
printf(" fft-total run time %lu usec\n", fft);
uint64_t bitrev = (uint64_t) (bitrev_sec) * 1000000 + (uint64_t) (bitrev_usec);
printf(" bit-reverse run time %lu usec\n", bitrev);
uint64_t cdfmcw = (uint64_t) (cdfmcw_sec) * 1000000 + (uint64_t) (cdfmcw_usec);
printf(" calc-dist run time %lu usec\n", cdfmcw);
printf("\n");
uint64_t depunc = (uint64_t) (depunc_sec) * 1000000 + (uint64_t) (depunc_usec);
printf(" depuncture run time %lu usec\n", depunc);
uint64_t dodec = (uint64_t) (dodec_sec) * 1000000 + (uint64_t) (dodec_usec);
printf(" do-decoding run time %lu usec\n", dodec);
#endif // INT_TIME
printf("\nDone.\n");
return 0;
}