-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_hw_task.c
113 lines (89 loc) · 2.71 KB
/
demo_hw_task.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
/*
* Fred for Linux. Experimental support.
*
* Copyright (C) 2018-2021, Marco Pagani, ReTiS Lab.
* <marco.pag(at)outlook.com>
*
* 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.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "fred_lib.h"
#include "demo_hw_task.h"
#define ARRAY_SIZE 1024
int demo_hw_task_init(struct demo_hw_task *self, uint32_t hw_id,
data_t (*op)(data_t, data_t))
{
int retval;
assert(self);
assert(op);
self->hw_id = hw_id;
self->op = op;
retval = fred_init(&self->fred_data);
if (retval) {
printf("fred_init failed for simple hw-task %u\n", self->hw_id);
return -1;
}
printf("fred_init done for simple hw-task %u\n", self->hw_id);
retval = fred_bind(self->fred_data, &self->fred_hw_task, self->hw_id);
if (retval) {
printf("fred_bind failed for simple hw-task %u\n", self->hw_id);
return -1;
}
printf("fred_init done for simple hw-task %u\n", self->hw_id);
self->out = fred_map_buff(self->fred_data, self->fred_hw_task, 0);
if (!self->out) {
printf("fred_map_buff failed on buff 0 for simple hw-task %u\n", self->hw_id);
return -1;
}
self->in_a = fred_map_buff(self->fred_data, self->fred_hw_task, 1);
if (!self->in_a) {
printf("fred_map_buff failed on buff 1 for simple hw-task %u\n", self->hw_id);
return -1;
}
self->in_b = fred_map_buff(self->fred_data, self->fred_hw_task, 2);
if (!self->in_b) {
printf("fred_map_buff failed on buff 2 for simple hw-task %u\n", self->hw_id);
return -1;
}
return 0;
}
int demo_hw_task_accel(struct demo_hw_task *self)
{
int retval;
data_t out_sw;
printf("Requesting acceleration for simple hw-task %u\n", self->hw_id);
// Initialize random input data
for (int i = 0; i < ARRAY_SIZE; ++i) {
self->in_a[i] = rand() % 1024;
self->in_b[i] = rand() % 1024;
}
retval = fred_accel(self->fred_data, self->fred_hw_task);
if (retval) {
printf("Acceleration request failed for simple hw-task %u\n", self->hw_id);
return -1;
}
printf("Acceleration done for hw-task %u\n", self->hw_id);
// Check output data
for (int i = 0; i < ARRAY_SIZE; ++i) {
out_sw = self->op(self->in_a[i], self->in_b[i]);
if (self->out[i] != out_sw) {
printf("Result mismatch for simple hw-task %u: SW:%u, HW:%u)\n",
self->hw_id, out_sw, self->out[i]);
return -1;
} else if (i == 0) {
printf("Result sample for simple hw-task %u: SW:%u, HW:%u)\n",
self->hw_id, out_sw, self->out[i]);
}
}
return 0;
}
void demo_hw_task_free(struct demo_hw_task *self)
{
if (self->fred_data)
fred_free(self->fred_data);
}