-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
180 lines (160 loc) · 4.65 KB
/
main.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
/* Neural network application
* Hardware accelerator that does pipelined NN
*
* Note: The DDR mem is needed to hold the config of NN layers and dataset
* => Correctly configure the ldscript to use full DDR as heap etc
*/
#include <stdio.h>
#include <stdlib.h>
#include <sleep.h>
#include <time.h>
#include "platform.h"
#include "xparameters.h"
#include "xuartps_hw.h"
#include "xtime_l.h"
#include "xil_cache.h"
#include "xil_cache_l.h"
#include "main.h"
#include "neuralnetwork.h"
#include "accregs.h"
#include "demo.h"
int main()
{
int image, neuron;
int difference_soft_hard = 0;
uint32_t results_soft[FRAMES_NB][NEU2];
uint32_t results_hard[FRAMES_NB][NEU2];
uint32_t classification_soft[FRAMES_NB];
uint32_t classification_hard[FRAMES_NB];
uint32_t success_hits_hard = 0;
uint32_t success_hits_soft = 0;
init_platform();
// Explicitely enable Rx ad Tx
XUartPs_WriteReg(XPAR_PS7_UART_1_BASEADDR, XUARTPS_CR_OFFSET,
XUARTPS_CR_RX_EN | XUARTPS_CR_TX_EN);
// No parity
XUartPs_WriteReg(XPAR_PS7_UART_1_BASEADDR, XUARTPS_MR_OFFSET,
XUARTPS_MR_CHMODE_NORM | XUARTPS_MR_PARITY_NONE);
// Disable interrupts
XUartPs_WriteReg(XPAR_PS7_UART_1_BASEADDR, XUARTPS_IDR_OFFSET,
XUARTPS_IXR_MASK);
print(" --------------------------\r\n");
print(" | MENINGE NEURAL NETWORK |\r\n");
print(" | Paul Luperini |\r\n");
print(" | Lucas Mahieu |\r\n");
print(" | Hugues de Valon |\r\n");
print(" --------------------------\r\n\n");
sleep(3);
#ifdef MNIST
/*
* On passe à la fonction un tableau à deux dimensions en lui donnant
* l'adresse de la première case du tableau.
*/
printf("hardware computation... ");
fflush(stdout);
nn_hardware(&frames[0][0], &results_hard[0][0], &w1[0][0], &w2[0][0],
b1, b2);
printf("done.\n");
printf("software computation... ");
fflush(stdout);
nn_software(&frames[0][0], &results_soft[0][0], &w1[0][0], &w2[0][0],
b1, b2);
printf("done.\n");
#else
printf("hardware computation... ");
fflush(stdout);
nn_hardware(&data_frames[0][0], &results_hard[0][0],
&config_neu1[0][0], &config_neu2[0][0],
config_recode1, config_recode2);
printf("done.\n");
printf("software computation... ");
fflush(stdout);
nn_software(&data_frames[0][0], &results_soft[0][0],
&config_neu1[0][0], &config_neu2[0][0],
config_recode1, config_recode2);
printf("done.\n");
#endif /* MNIST */
/*
* Compare hard and soft results.
*/
for (image = 0; image < FRAMES_NB; image++) {
for (neuron = 0; neuron < NEU2; neuron++) {
if (results_soft[image][neuron] !=
results_hard[image][neuron]) {
difference_soft_hard = 1;
printf("WARNING: difference soft/hard\n");
printf("soft: image n°%3d, neurone n°%3d : %10lu\n",
image, neuron,
(unsigned long)
results_soft[image][neuron]);
printf("hard: image n°%3d, neurone n°%3d : %10lu\n",
image, neuron,
(unsigned long)
results_hard[image][neuron]);
}
}
}
if (!difference_soft_hard) {
printf("no difference between software and hardware results.\n");
}
/*
* Classify the results.
*/
classify(&results_hard[0][0], classification_hard);
classify(&results_soft[0][0], classification_soft);
#ifdef MNIST
/*
* Compute hardware and software succes rates.
*/
for (image = 0; image < FRAMES_NB; image++) {
// A décommenter pour la démo n°2
//display(classification_hard[image]);
if (classification_hard[image] == labels[image]) {
success_hits_hard++;
}
if (classification_soft[image] == labels[image]) {
success_hits_soft++;
}
}
printf("hardware success rate: %.2f %%\n",
(success_hits_hard / (float)FRAMES_NB) * 100);
printf("software success rate: %.2f %%\n",
(success_hits_soft / (float)FRAMES_NB) * 100);
#endif
/*
* Compare hard and soft timing performances.
*/
printf("%u frames\n", FRAMES_NB);
printf("hardware: %4.2g seconds => %5d frames/s\n",
hard_time, (int)(FRAMES_NB/hard_time));
printf("software: %4.2g seconds => %5d frames/s\n",
soft_time, (int)(FRAMES_NB/soft_time));
printf("speedup is %d\n", (int)(soft_time / hard_time));
while(1);
cleanup_platform();
return 0;
}
double XTime_ToDouble(XTime *t)
{
return ((double)(*t)) / COUNTS_PER_SECOND;
}
double XTime_GetDiff_Double(XTime *oldtime, XTime *newtime)
{
return ((double)(*newtime - *oldtime)) / COUNTS_PER_SECOND;
}
double XTime_DiffCurrReal_Double(XTime *oldtime)
{
XTime newtime;
XTime_GetTime(&newtime);
return XTime_GetDiff_Double(oldtime, &newtime);
}
void* malloc_with_loc(unsigned size, char* file, unsigned line)
{
void* ptr = malloc(size);
if (ptr == NULL) {
abort_printf(
"From %s:%u : malloc() returned NULL for size %u\n",
file, line, size);
}
return ptr;
}