This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfpga_app.py
266 lines (213 loc) · 8.79 KB
/
fpga_app.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# pylint: disable=C0301
"""
Accelize Getting Started Example Designs
Python binding
********************************************************************
* Description: *
********************************************************************
"""
import sys
import os
import argparse
import time
import ctypes
import pyopencl as cl
import numpy as np
import xmltodict
import json
from threading import Thread
# Accelize DRM Library
from accelize_drm import DrmManager as _DrmManager
from accelize_drm.fpga_drivers import get_driver as _get_driver
class fpgaApp:
def __init__(self, xclbin=None, drmbypass=False, data_size=4096,
buffIn=None, buffOut=None, board=None, reset=False):
self.xclbin=xclbin
self.drmbypass=drmbypass
self.drm_base_address=self.get_drmbaseaddr_from_xclbin(xclbin)
self.data_size=data_size
self.board=board
self.board_reset_cmd=''
self.ocl_ctx = None
self.ocl_prg = None
self.ocl_q = None
self.ocl_krnl_scramb_stage = None
self.ocl_krnl_input_stage = None
self.ocl_krnl_output_stage = None
self.fpga_driver_name = None
self.fpga_driver = None
self.drm_manager = None
self.drm_activated=False
self.thread_drm_activation = None
self.init_board(board, reset)
self.init_hal(buffIn, buffOut)
self.thread_drm_activation = Thread(target=self.drm_activation_thread)
self.init_drm()
def __del__(self):
try:
self.release_drm()
self.drm_manager = None
except:
pass
def __str__(self):
s = "\n"
s += f"fpgaApp:\n"
s += f"\tboard = {self.board}\n"
s += f"\tdrm_base_address = {hex(self.drm_base_address)}\n"
s += f"\tboard_reset_cmd = {self.board_reset_cmd}\n"
s += f"\tfpga_driver_name = {self.fpga_driver_name}\n"
s += f"\txclbin = {self.xclbin}\n"
s += f"\tdata_size = {self.data_size}\n"
s += f"\tdrm_activated = {self.drm_activated}\n"
s += "\n"
return s
def reset_board(self):
print('Forcing Reset of FPGA Chip...')
os.system(self.board_reset_cmd)
def init_board(self, board, reset=False):
if board=='aws':
self.fpga_driver_name='aws_f1'
self.board_reset_cmd='fpga-clear-local-image -S 0 -H'
if board=='u200':
self.fpga_driver_name='xilinx_xrt'
self.board_reset_cmd='xbutil reset -d 0'
if reset:
self.reset_board()
def get_drmbaseaddr_from_xclbin(self, xclbin):
pattern_start = '<project name="'
pattern_end = '</project>'
start=None
end=None
xmltxt=None
with open(xclbin, "rb") as f:
for line in f:
try:
l = line.decode('utf-8')
if(pattern_start in l):
start = f.tell() - len(line)
if(pattern_end in l):
end = f.tell()-1
break
except UnicodeDecodeError:
continue
with open(xclbin, "r") as f:
f.seek(start, 0)
xmltxt = f.read(end-start)
xmld = xmltodict.parse(xmltxt)
for krnl in xmld['project']['platform']['device']['core']['kernel']:
if(krnl['@name']=='drm_controller_axi4st'):
return int(krnl['instance']['addrRemap']['@base'], 16)
return None
def init_hal(self, buffIn, buffOut):
# Get platform/device information
clPlatform = cl.get_platforms()[0]
clDevices = clPlatform.get_devices()
clDevice = clDevices[0]
self.ocl_ctx = cl.Context(devices=clDevices)
with open(self.xclbin, "rb") as binary_file:
binary = binary_file.read()
self.ocl_prg = cl.Program(self.ocl_ctx, clDevices, [binary])
# Init Command Queue
qprops = cl.command_queue_properties.OUT_OF_ORDER_EXEC_MODE_ENABLE|\
cl.command_queue_properties.PROFILING_ENABLE
self.ocl_q = cl.CommandQueue(context=self.ocl_ctx, device=clDevice,
properties=qprops)
# Create Kernels
self.ocl_krnl_scramb_stage = cl.Kernel(self.ocl_prg, "krnl_scrambler_stage_rtl")
self.ocl_krnl_input_stage = cl.Kernel(self.ocl_prg, "krnl_input_stage_rtl")
self.ocl_krnl_output_stage = cl.Kernel(self.ocl_prg, "krnl_output_stage_rtl")
# Create Buffers
self.buffer_input = cl.Buffer(self.ocl_ctx,
cl.mem_flags.USE_HOST_PTR|cl.mem_flags.READ_ONLY,
size=0, hostbuf=buffIn)
self.buffer_output = cl.Buffer(self.ocl_ctx,
cl.mem_flags.USE_HOST_PTR|cl.mem_flags.WRITE_ONLY,
size=0, hostbuf=buffOut)
def send(self):
# Set the Kernel Arguments
npSize = np.int32(self.data_size/4)
self.ocl_krnl_input_stage.set_args(self.buffer_input, npSize)
# Copy input data to device global memory
cl.enqueue_migrate_mem_objects(self.ocl_q, [self.buffer_input],
flags=0)
# Launch the Kernel
cl.enqueue_nd_range_kernel(self.ocl_q, self.ocl_krnl_input_stage,
[1], [1])
def recv(self):
# Set the Kernel Arguments
npSize = np.int32(self.data_size/4)
npIncr = np.int32(0)
self.ocl_krnl_scramb_stage.set_args(npIncr, npSize)
self.ocl_krnl_output_stage.set_args(self.buffer_output, npSize)
# Launch the Kernel
cl.enqueue_nd_range_kernel(self.ocl_q, self.ocl_krnl_scramb_stage,
[1], [1])
cl.enqueue_nd_range_kernel(self.ocl_q, self.ocl_krnl_output_stage,
[1], [1])
self.ocl_q.finish()
# Copy Result from Device Global Memory to Host Local Memory
cl.enqueue_migrate_mem_objects(self.ocl_q, [self.buffer_output],
flags=cl.mem_migration_flags.HOST)
self.ocl_q.finish()
def init_drm(self, fpga_slot_id=0):
if not self.drmbypass:
# Get FPGA driver
self.fpga_driver = _get_driver(name=self.fpga_driver_name)(
fpga_slot_id=fpga_slot_id,
#fpga_image=fpga_image,
drm_ctrl_base_addr=self.drm_base_address)
#log_dir=self.LOG_DIR)
self.drm_manager = _DrmManager(
conf_file_path="./conf.json",
cred_file_path="./cred.json",
read_register=self.fpga_driver.read_register_callback,
write_register=self.fpga_driver.write_register_callback,
async_error=self.async_error_callback
#self.drm_read_callback,
#self.drm_write_callback,
)
self.thread_drm_activation.start()
time.sleep(2)
def drm_activation_thread(self):
while True:
if self.drm_activated==False:
try:
self.drm_manager.activate()
self.drm_activated=True
print(f"[DRMLIB] Session ID: {self.drm_manager.get('session_id')}")
except:
self.drm_activated=False
time.sleep(1)
def async_error_callback(self, err_mess):
self.drm_activated=False
return None
def release_drm(self):
if not self.drmbypass:
self.drm_manager.deactivate()
def run(xclbinpath=None, frame_size=None, bypassDRM=False):
DATA_SIZE=4096
INCR_VALUE=10
source_input = np.arange(DATA_SIZE, dtype=np.uint32)
source_sw_results = np.arange(INCR_VALUE, DATA_SIZE + INCR_VALUE,
dtype=np.uint32)
source_hw_results = np.zeros(DATA_SIZE, np.uint32)
fapp = fpgaApp(xclbinpath)
fapp.send(source_input)
fapp.recv(source_hw_results)
fapp.release_drm()
diff = source_hw_results != source_sw_results
if diff.any():
print(f"Error: Result mismatch")
fapp.printDiff(source_sw_results, source_hw_results)
raise
print("TEST PASSED")
if __name__ == '__main__':
# Parse the arguments
option = argparse.ArgumentParser()
option.add_argument('-x', '--xclbin', dest="xclbin_path",
type=str, default=None,
required=True, help="Path to .xclbin file")
args = option.parse_args()
run(args.xclbin_path)