-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
97 lines (77 loc) · 2.02 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#define SIZE 64
#define TOTAL_SIZE (SIZE+1) * SIZE
typedef union {
float f;
unsigned int i;
} foo;
int main(int argc, char **argv) {
int i, j, k;
foo container;
float flat[TOTAL_SIZE];
float global_mem[SIZE];
float local_mem[SIZE][SIZE];
float output_cpu[SIZE];
float *output_fpga;
// initialization
FILE *fd;
fd = fopen("./input.txt", "r");
unsigned int a;
i = 0;
j = 0;
k = -1;
while (!feof(fd)) {
if (fscanf(fd, "%X", &a) != EOF) {
container.i = a;
if (i < SIZE)
global_mem[i] = container.f;
else {
if (j == SIZE) {
j = 0;
k++;
}
local_mem[k][j] = container.f;
}
flat[i] = container.f;
i++;
j++;
}
}
fclose(fd);
// output_cpu = 0.0f;
// computation
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE; j++) {
output_cpu[i] += global_mem[j] * local_mem[i][j];
}
}
// FPGA offloading
// memory load
int foo;
foo = open("/dev/mem", O_RDWR | O_NONBLOCK);
float *fpga_bram = mmap(NULL, TOTAL_SIZE * sizeof(float), PROT_WRITE, MAP_SHARED, foo, 0x40000000);
for (i = 0; i < TOTAL_SIZE; i++) {
*(fpga_bram + i) = flat[i];
}
// run
unsigned int *fpga_ip = mmap(NULL, sizeof(float), PROT_WRITE, MAP_SHARED, foo, 0x43c00000);
*fpga_ip = 0x5555;
// wait
while (*fpga_ip == 0x5555);
// result
// output_fpga
// output_fpga = fpga_bram;//*fpga_bram?
// display
printf("%-10s%-20s%-20s%-20s\n", "index", "CPU", "FPGA", "FPGA(hex)");
// container.f = output_fpga;
for (i = 0; i < SIZE; i++) {
container.f = fpga_bram[i];
printf("%-10d%-20f%-20f%-20x\n", i, output_cpu[i], fpga_bram[i], container.i);
}
close(foo);
return 0;
}