-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_hjsolver_dram.c
352 lines (280 loc) · 10.5 KB
/
test_hjsolver_dram.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
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
/*
* Amazon FPGA Hardware Development Kit
*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Amazon Software License (the "License"). You may not use
* this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/asl/
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
* implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <poll.h>
#include <time.h>
#include <math.h>
//#include <vector>
#include "fpga_pci.h"
#include "fpga_mgmt.h"
#include "fpga_dma.h"
#include "utils/lcd.h"
#include "test_dram_dma_common.h"
#define MEM_16G (1ULL << 34)
#define USER_INTERRUPTS_MAX (16)
/* use the standard out logger */
static const struct logger *logger = &logger_stdout;
void usage(const char* program_name);
int dma_example(int slot_id, size_t buffer_size);
int axi_mstr_example(int slot_id);
int axi_mstr_ddr_access(int slot_id, pci_bar_handle_t pci_bar_handle, uint32_t ddr_hi_addr, uint32_t ddr_lo_addr, uint32_t ddr_data);
int dandelion_access(int slot_id);
int hjsolver_test(int slot_id);
float int_to_float(int a);
int float_to_int(float a);
void my_linspace(float lb, float mb, float A[], int dim);
void Initialize_V(int* A);
int main(int argc, char **argv) {
int rc;
int slot_id = 0;
switch (argc) {
case 1:
break;
case 3:
sscanf(argv[2], "%x", &slot_id);
break;
default:
usage(argv[0]);
return 1;
}
/* setup logging to print to stdout */
rc = log_init("test_dram_dma");
fail_on(rc, out, "Unable to initialize the log.");
rc = log_attach(logger, NULL, 0);
fail_on(rc, out, "%s", "Unable to attach to the log.");
/* initialize the fpga_plat library */
rc = fpga_mgmt_init();
fail_on(rc, out, "Unable to initialize the fpga_mgmt library");
/* check that the AFI is loaded */
log_info("Checking to see if the right AFI is loaded...");
#ifndef SV_TEST
rc = check_slot_config(slot_id);
fail_on(rc, out, "slot config is not correct");
#endif
//run my own example
rc = hjsolver_test(slot_id);
fail_on(rc, out, "Dandelion example failed\n");
out:
log_info("TEST %s", (rc == 0) ? "PASSED" : "FAILED");
return rc;
}
void usage(const char* program_name) {
printf("usage: %s [--slot <slot>]\n", program_name);
}
int hjsolver_test(int slot_id) {
int rc_control;
int rc_memory;
pci_bar_handle_t pci_bar_handle = PCI_BAR_HANDLE_INIT;
int pf_id = 0;
int bar_id = 0;
int fpga_attach_flags = 0;
uint32_t in_hi_addr, in_lo_addr;
uint32_t out_hi_addr, out_lo_addr;
uint32_t cycle_count = 0;
uint32_t control_reg = 0;
rc_control = fpga_pci_attach(slot_id, pf_id, bar_id, fpga_attach_flags, &pci_bar_handle);
fail_on(rc_control, out, "Unable to attach to the AFI on slot id %d", slot_id);
//Variables for read/write data through DMA
int write_fd, read_fd;
read_fd = -1;
write_fd = -1;
// Test input
int buffer_size = 60 * 60 * 20 * 36;
int *write_buffer = (int *) malloc(buffer_size * sizeof(uint32_t));
Initialize_V((int *)write_buffer);
int *read_buffer = (int *) malloc(buffer_size * sizeof(uint32_t));
if (write_buffer == NULL || read_buffer == NULL) {
rc_memory = -ENOMEM;
goto out;
}
// Open the channel
read_fd = fpga_dma_open_queue(FPGA_DMA_XDMA, slot_id,
/*channel*/ 0, /*is_read*/ true);
fail_on((rc_memory = (read_fd < 0) ? -1 : 0), out, "unable to open read dma queue");
write_fd = fpga_dma_open_queue(FPGA_DMA_XDMA, slot_id,
/*channel*/ 0, /*is_read*/ false);
fail_on((rc_memory = (write_fd < 0) ? -1 : 0), out, "unable to open write dma queue");
fail_on(rc_memory, out, "unabled to initialize buffer");
// Write to dram through DMA
rc_memory = fpga_dma_burst_write(write_fd, (uint8_t *) write_buffer, buffer_size * sizeof(int), 0);
fail_on(rc_memory, out, "DMA write failed");
// write at the output address
rc_memory = fill_buffer_zeros((uint8_t *) read_buffer, buffer_size * sizeof(int));
rc_memory = fpga_dma_burst_write(write_fd, (uint8_t *) read_buffer, buffer_size * sizeof(int), 0x10000000);
fail_on(rc_memory, out, "DMA write failed");
log_info("Starting AXI Master to DDR test");
/* Initializing control registers
* Register File.
* Six 32-bit register file.
*
* -------------------------------
* Register description | addr
* ------------------------|-----
* Control status register | 0x500
* Ecnt1 | 0x504
* Ecnt2 | 0x508
* Input addr lsb | 0x50c
* Input addr msb | 0x510
* Output addr lsb | 0x514
* Output addr msb | 0x518
* -------------------------------
*
* ------------------------------
* Control status register | bit
* ------------------------------
* Launch | 0
* Finish | 1
* ------------------------------
*/
printf("i = %d\n", i);
static uint64_t ccr_control = 0x500;
static uint64_t ccr_cycle = 0x504;
//static uint64_t ccr_ptime = 0x508;
static uint64_t ccr_in_lsb = 0x50C;
static uint64_t ccr_in_msb = 0x510;
static uint64_t ccr_out_lsb = 0x514;
static uint64_t ccr_out_msb = 0x518;
in_hi_addr = 0x00000000;
in_lo_addr = 0x00000000;
out_hi_addr = 0x00000000;
out_lo_addr = 0x10000000;
/* write a value into the mapped address space */
printf("Initializing dandelion accelerator:\n");
printf("Writing 0x%08x to Dandelion in lsb register (0x%016lx)\n\n", in_lo_addr, ccr_in_lsb);
rc_control = fpga_pci_poke(pci_bar_handle, ccr_in_lsb, in_lo_addr);
fail_on(rc_control, out, "Unable to write to the fpga !");
printf("Writing 0x%08x to Dandelion in msb register (0x%016lx)\n\n", in_hi_addr, ccr_in_msb);
rc_control = fpga_pci_poke(pci_bar_handle, ccr_in_msb, in_hi_addr);
fail_on(rc_control, out, "Unable to write to the fpga !");
printf("Writing 0x%08x to Dandelion out lsb register (0x%016lx)\n\n", out_lo_addr, ccr_out_lsb);
rc_control = fpga_pci_poke(pci_bar_handle, ccr_out_lsb, out_lo_addr);
fail_on(rc_control, out, "Unable to write to the fpga !");
printf("Writing 0x%08x to Dandelion out msb register (0x%016lx)\n\n", out_hi_addr, ccr_out_msb);
rc_control = fpga_pci_poke(pci_bar_handle, ccr_out_msb, out_hi_addr);
fail_on(rc_control, out, "Unable to write to the fpga !");
printf("Launching -- Writing 0x1 to dandelion ctrl register:\n");
rc_control = fpga_pci_poke(pci_bar_handle, ccr_control, 0x1);
fail_on(rc_control, out, "Unable to write to the fpga !");
clock_t begin = clock();
do{
rc_control = fpga_pci_peek(pci_bar_handle, ccr_control, &control_reg);
fail_on(rc_control, out, "Unable to read read from the fpga !");
} while(control_reg != 2);
clock_t diff = clock() - begin;
double time_spent = (double ) diff/ CLOCKS_PER_SEC;
/* read it back and print it out; you should expect the byte order to be
* reversed (That's what this CL does) */
rc_control = fpga_pci_peek(pci_bar_handle, ccr_cycle, &cycle_count);
fail_on(rc_memory, out, "Unable to read read from the fpga !");
printf("Execution finished in [ %d ] cycle\n", cycle_count);
printf("time taken %f\n", time_spent);
// Read from dram
rc_memory = fpga_dma_burst_read(read_fd, (uint8_t *) read_buffer, buffer_size * sizeof(int), out_lo_addr);
fail_on(rc_memory, out, "DMA read failed on reading form out buffer.");
//Checking memory output
bool flag = false;
int count_nz = 0;
// Write into file
FILE *fp;
if((fp=fopen("result_V.txt", "wb"))==NULL) {
printf("Cannot open file.\n");
}
if(fwrite(read_buffer, sizeof(int), buffer_size, fp) != buffer_size)
printf("File read error.");
fclose(fp);
for(uint32_t i = 0; i < buffer_size; i++){
if(read_buffer[i] <= 0) {
flag = true;
count_nz++;
}
}
if(!flag) printf("\nsth might worng\n");
printf("count_nz is %d\n", count_nz);
out:
if (write_buffer != NULL) {
free(write_buffer);
}
if (read_buffer != NULL) {
free(read_buffer);
}
if (write_fd >= 0) {
close(write_fd);
}
if (read_fd >= 0) {
close(read_fd);
}
/* if there is an error code, exit with status 1 */
return (rc_control != 0 ? (rc_memory != 0 ? 1 : 0) : 0);
}
float int_to_float(int a){
int mantissa = 27;
int multiplier = pow(2, mantissa);
float result = (float) a / multiplier;
return result;
}
int float_to_int(float a){
int mantissa = 27;
int result = (int) round(a * pow(2, mantissa));
return result;
}
void my_linspace(float lb, float mb, float A[], int dim){
float dx;
dx = (mb - lb) / (dim - 1);
for(int i = 0; i < dim; i++){
A[i] = dx * i + lb;
}
return;
}
void Initialize_V(int* A){
float x_lb = -3.0, x_mb = 3.0;
int y_lb = -1.0, y_mb = 4.0;
int v_lb = 0.0, v_mb = 4.0;
int t_lb = -M_PI, t_mb = M_PI;
int x_dim = 60, y_dim = 60, v_dim = 20, t_dim = 36;
float x_arr[60], y_arr[60], v_arr[20], t_arr[36];
my_linspace(x_lb, x_mb, x_arr, x_dim);
my_linspace(y_lb, y_mb, y_arr, y_dim);
my_linspace(v_lb, v_mb, v_arr, v_dim);
my_linspace(t_lb, t_mb, t_arr, t_dim);
float x_list[3] = {1.4, 0.0, -1.9};
float y_list[3] = {2.15, 1.0, 1.55};
float r_list[3] = {0.7, 0.7, 0.7};
int count_nz = 0;
for(int i = 0; i < x_dim; i++){
for(int j = 0; j < y_dim; j++){
for(int k = 0; k < v_dim; k++){
for(int l = 0; l < t_dim; l++){
float min_val = 1000;
for(int m = 0; m < 3; m++){
float val_func = sqrt((y_arr[j] - y_list[m]) * (y_arr[j] - y_list[m]) + (x_arr[i] - x_list[m]) * (x_arr[i] - x_list[m]))
- r_list[m];
min_val = min(val_func, min_val);
}
//int convert_val = float_to_int(sqrt(( y_arr[j] -1.0 ) * (y_arr[j] - 1.0) + x_arr[i]*x_arr[i]) - radius);
int convert_val = float_to_int(min_val);
A[i * y_dim * v_dim * t_dim + j * v_dim * t_dim + k * t_dim + l] = convert_val;
if(min_val < 0) count_nz++;
}
}
}
}
}