-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_cpynqapi.py
More file actions
97 lines (76 loc) · 3.24 KB
/
files_cpynqapi.py
File metadata and controls
97 lines (76 loc) · 3.24 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
cpynqapi = '''#include <stdio.h>
#include <pynq_api.h>
#define BATCH_SIZE 16
#define BM_INPUT {{ items[0].bminputs }}
#define BM_OUTPUT {{ items[0].bmoutputs }}
#define PRECISION 32
#define TOTIN BATCH_SIZE*BM_INPUT
#define TOTOUT BATCH_SIZE*BM_OUTPUT
int main(int argc, char *argv[])
{
FILE *fp;
char *line = NULL;
size_t len = 0;
ssize_t read;
int i = 0;
int total = 0;
int offset = 0;
int threshold = 0;
PYNQ_loadBitstream("firmware.bit");
PYNQ_SHARED_MEMORY shared_memory_1, shared_memory_2;
PYNQ_allocatedSharedMemory(&shared_memory_1, sizeof(float) * TOTIN, 1);
PYNQ_allocatedSharedMemory(&shared_memory_2, sizeof(float) * TOTOUT, 1);
float *d1 = (float *)shared_memory_1.pointer;
float *d2 = (float *)shared_memory_2.pointer;
PYNQ_AXI_DMA dma;
PYNQ_openDMA(&dma, 0x40400000);
if (argc == 3) // argv[1] is the input file, argv[2] is the output file
{
fp = fopen(argv[1], "r");
if (fp == NULL)
printf("Input file %s not found\\n", argv[1]), exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1)
i++;
if (i % BM_INPUT != 0)
printf("Input file %s has wrong number of elements\\n", argv[1]), exit(EXIT_FAILURE);
rewind(fp);
float *data = (float *)malloc(sizeof(float) * i);
i = 0;
while ((read = getline(&line, &len, fp)) != -1)
{
data[i] = atof(line);
//printf("%f\\n", data[i]);
i++;
}
fclose(fp);
total = i;
fp = fopen(argv[2], "w");
if (fp == NULL)
printf("Output file %s cannot be opened\\n", argv[2]), exit(EXIT_FAILURE);
threshold = BATCH_SIZE;
for (offset = 0; offset < total; offset += TOTIN)
{
for (int i = 0; i < TOTIN; i++)
if (i + offset < total)
d1[i] = data[i + offset];
else
{
if (threshold == BATCH_SIZE)
threshold = i/BM_INPUT;
d1[i] = 0;
}
PYNQ_writeDMA(&dma, &shared_memory_1, 0, sizeof(float) * TOTIN);
PYNQ_readDMA(&dma, &shared_memory_2, 0, sizeof(float) * TOTOUT);
PYNQ_waitForDMAComplete(&dma, AXI_DMA_WRITE);
PYNQ_waitForDMAComplete(&dma, AXI_DMA_READ);
for (int i = 0; i < TOTOUT; i++)
if (i < threshold * BM_OUTPUT)
fprintf(fp, "%.12f\\n", d2[i]);
}
fclose(fp);
}
PYNQ_closeDMA(&dma);
PYNQ_freeSharedMemory(&shared_memory_1);
PYNQ_freeSharedMemory(&shared_memory_2);
return 0;
}'''