cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
select_cfpzoo_kernel.py
Go to the documentation of this file.
1 # *******************************************************************************
2 # * Copyright 2016 -- 2022 IBM Corporation
3 # *
4 # * Licensed under the Apache License, Version 2.0 (the "License");
5 # * you may not use this file except in compliance with the License.
6 # * You may obtain a copy of the License at
7 # *
8 # * http://www.apache.org/licenses/LICENSE-2.0
9 # *
10 # * Unless required by applicable law or agreed to in writing, software
11 # * distributed under the License is distributed on an "AS IS" BASIS,
12 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # * See the License for the specific language governing permissions and
14 # * limitations under the License.
15 # ********************************************************************************
16 
17 # @brief A script for editing the files needed to select the cFp_Zoo kernel to be implemented
18 # @author DID
19 # @date Nov. 2020
20 
21 import pathlib
22 import os
23 import shutil
24 from re import search
25 import sys
26 
27 
28 
29 def edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr):
30  f1 = open(full_file, 'r')
31  f2 = open(full_file+"_new", 'w')
32 
33  replaced = 0
34  uaf_start_detected = taf_start_detected = 0
35  ddr_component_start_detected = ddr_component_end_detected = 0
36  ddr_uaf_start_detected = ddr_uaf_end_detected = 0
37  ddr_taf_start_detected = ddr_taf_end_detected = 0
38  ddr_Mp2_dummy_start_detected = ddr_Mp2_dummy_end_detected = 0
39 
40  for s in f1:
41  new_s = s2 = s
42  if search("vhdl", full_file):
43  search_str = " component "
44  if search(search_str, s):
45  s2 = " component "+new_kernel+"Application is\n"
46  replaced = replaced + 1
47 
48  search_str = " end component "
49  if search(search_str, s):
50  s2 = " end component "+new_kernel+"Application;\n"
51  replaced = replaced + 1
52 
53 
55 
56  # Check component start
57  search_str = "SHELL / Mem / Mp0 Interface / Start Component"
58  if search(search_str, s):
59  s2 = s
60  ddr_component_start_detected = 1
61 
62  # Check component stop
63  search_str = "SHELL / Mem / Mp1 Interface / End Component"
64  if search(search_str, s):
65  s2 = s
66  ddr_component_end_detected = 1
67 
68  # Replace ddr pattern in component declaration
69  if (ddr_component_start_detected):
70  excluding_pattern = "-- auto excluding component Mp0-Mp1 "
71  if (search(excluding_pattern, s2)):
72  if (ddr):
73  s2 = s2.replace(str(excluding_pattern), str(''))
74  replaced = replaced + 1
75  else:
76  if (not ddr):
77  s2 = excluding_pattern + s2
78  replaced = replaced + 1
79  if (ddr_component_end_detected):
80  ddr_component_start_detected = ddr_component_end_detected = 0;
81 
82 
83  # Check DDR in UAF start
84  search_str = "SHELL / Mem / Mp0 Interface / Start in UAF"
85  if search(search_str, s):
86  s2 = s
87  ddr_uaf_start_detected = 1
88 
89  # Check DDR in UAF stop
90  search_str = "SHELL / Mem / Mp1 Interface / End in UAF"
91  if search(search_str, s):
92  s2 = s
93  ddr_uaf_end_detected = 1
94 
95  # Replace ddr pattern in component declaration
96  if (ddr_uaf_start_detected):
97  excluding_pattern = "-- auto excluding Mp0-Mp1 in UAF "
98  if (search(excluding_pattern, s2)):
99  if (ddr):
100  s2 = s2.replace(str(excluding_pattern), str(''))
101  replaced = replaced + 1
102  else:
103  if (not ddr):
104  s2 = excluding_pattern + s2
105  replaced = replaced + 1
106  if (ddr_uaf_end_detected):
107  ddr_uaf_start_detected = ddr_uaf_end_detected = 0;
108 
109 
110  # Check DDR in TAF start
111  search_str = "SHELL / Mem / Mp0 Interface / Start in TAF"
112  if search(search_str, s):
113  s2 = s
114  ddr_taf_start_detected = 1
115 
116  # Check DDR in TAF stop
117  search_str = "SHELL / Mem / Mp1 Interface / End in TAF"
118  if search(search_str, s):
119  s2 = s
120  ddr_taf_end_detected = 1
121 
122  # Replace ddr pattern in component declaration
123  if (ddr_taf_start_detected):
124  excluding_pattern = "-- auto excluding Mp0-Mp1 in TAF "
125  if (search(excluding_pattern, s2)):
126  if (ddr):
127  s2 = s2.replace(str(excluding_pattern), str(''))
128  replaced = replaced + 1
129  else:
130  if (not ddr):
131  s2 = excluding_pattern + s2
132  replaced = replaced + 1
133  if (ddr_taf_end_detected):
134  ddr_taf_start_detected = ddr_taf_end_detected = 0;
135 
136 
137  # Check DDR in dummy start
138  search_str = "2nd Memory Port dummy connections Start"
139  if search(search_str, s):
140  s2 = s
141  ddr_Mp2_dummy_start_detected = 1
142 
143  # Check DDR in dummy stop
144  search_str = "2nd Memory Port dummy connections End"
145  if search(search_str, s):
146  s2 = s
147  ddr_Mp2_dummy_end_detected = 1
148 
149  # Replace ddr pattern in component declaration
150  if (ddr_Mp2_dummy_start_detected):
151  excluding_pattern = "-- auto excluding Mp2 open connections "
152  if (search(excluding_pattern, s2)):
153  if (not ddr):
154  s2 = s2.replace(str(excluding_pattern), str(''))
155  replaced = replaced + 1
156  else:
157  if (ddr):
158  s2 = excluding_pattern + s2
159  replaced = replaced + 1
160  if (ddr_Mp2_dummy_end_detected):
161  ddr_Mp2_dummy_start_detected = ddr_Mp2_dummy_end_detected = 0;
162 
163 
164  search_str = " UAF: "
165  if search(search_str, s):
166  s2 = " UAF: "+new_kernel+"Application\n"
167  uaf_start_detected = 1
168  replaced = replaced + 1
169 
170  search_str = " TAF: "
171  if search(search_str, s):
172  s2 = " TAF: "+new_kernel+"Application\n"
173  taf_start_detected = 1
174  replaced = replaced + 1
175 
176  # Checking if we have to comment out UAF
177  if (uaf_start_detected):
178  excluding_pattern = "-- auto excluding UAF "
179  if (search(excluding_pattern, s2)):
180  if (udp):
181  s2 = s2.replace(str(excluding_pattern), str(''))
182  replaced = replaced + 1
183  else:
184  if (not udp):
185  s2 = excluding_pattern + s2
186  replaced = replaced + 1
187  if (search(" \‍);", s)):
188  uaf_start_detected = 0;
189 
190  # Checking if we have to comment out TAF
191  if (taf_start_detected):
192  excluding_pattern = "-- auto excluding TAF "
193  if (search(excluding_pattern, s2)):
194  if (tcp):
195  s2 = s2.replace(str(excluding_pattern), str(''))
196  replaced = replaced + 1
197  else:
198  if (not tcp):
199  s2 = excluding_pattern + s2
200  replaced = replaced + 1
201  if (search(" \‍);", s)):
202  taf_start_detected = 0;
203 
204  new_s = s.replace(str(s), str(s2))
205  f2.write(new_s)
206 
207  if search("tcl", full_file):
208  search_str = "# IBM-HSL-IP : "
209  if search(search_str, s):
210  s2 = "# IBM-HSL-IP : "+new_kernel+" Application Flash\n"
211  replaced = replaced + 1
212 
213  search_str = "set ipModName"
214  if search(search_str, s):
215  s2 = "set ipModName \""+new_kernel+"Application\"\n"
216  replaced = replaced + 1
217 
218  search_str = "set ipName"
219  if search(search_str, s):
220  s2 = "set ipName \""+new_kernel.lower()+"\"\n"
221  replaced = replaced + 1
222 
223  new_s = s.replace(str(s), str(s2))
224  f2.write(new_s)
225 
226  if search("Makefile", full_file):
227  search_str = ".PHONY: all clean mem_test_flash "
228  if search(search_str, s):
229  s2 = ".PHONY: all clean mem_test_flash "+new_kernel.lower()+"\n"
230  replaced = replaced + 1
231 
232  search_str = "all: mem_test_flash "
233  if search(search_str, s):
234  s2 = "all: mem_test_flash "+new_kernel.lower()+"\n"
235  replaced = replaced + 1
236 
237  new_s = s.replace(str(s), str(s2))
238  f2.write(new_s)
239 
240  if search("config.h", full_file):
241  search_str1 = "#define NET_TYPE "
242  search_str2 = "#define PACK_SIZE "
243  if search(search_str1, s):
244  if (udp and tcp):
245  s2 = search_str1 + "udp //tcp\n"
246  elif (udp):
247  s2 = search_str1 + "udp\n"
248  elif (tcp):
249  s2 = search_str1 + "tcp\n"
250  replaced = replaced + 1
251  elif search(search_str2, s):
252  s2 = search_str2 + mtu + "\n"
253  replaced = replaced + 1
254  new_s = s.replace(str(s), str(s2))
255  f2.write(new_s)
256 
257 
258  if search(".hpp", full_file):
259  search_str1 = "#define DEFAULT_TX_PORT "
260  search_str2 = "#define DEFAULT_RX_PORT "
261  if search(search_str1, s):
262  s2 = search_str1 + port + "\n"
263  replaced = replaced + 1
264  elif search(search_str2, s):
265  s2 = search_str2 + port + "\n"
266  replaced = replaced + 1
267  new_s = s.replace(str(s), str(s2))
268 
269  search_str3 = "#define ENABLE_DDR"
270  if search(search_str3, s):
271  if (ddr):
272  s2 = search_str3 + "\n"
273  else:
274  s2 = "// " + search_str3 + "\n"
275  replaced = replaced + 1
276  new_s = new_s.replace(str(s), str(s2))
277  f2.write(new_s)
278 
279 
280  print("INFO: Edits of file " + full_file + " : "+ str(replaced))
281  print("#################")
282  f1.close()
283  f2.close()
284  if (replaced != 0):
285  shutil.copyfile(full_file, full_file+"_backup")
286  shutil.move(full_file+"_new", full_file)
287 
288 
289 
292 
293 
294 kernels = ["Harris", "Median_Blur", "MCEuropeanEngine", "Uppercase", "Memtest", "Warp_Transform"]
295 #print("Available kernels:")
296 #print('\n'.join(kernels))
297 
298 # Count the arguments
299 arguments = len(sys.argv) - 1
300 
301 if arguments != 6:
302  print("ERROR: Invalid number of arguments. Expected 6 but provided " + str(arguments) + ". Aborting...")
303  exit(-1)
304 
305 kernel_id = -1
306 for i in range(len(kernels)):
307  if (kernels[i] == sys.argv[3]):
308  kernel_id = i
309  break
310 
311 if (kernel_id == -1):
312  print("ERROR: Kernel " + sys.argv[3] + " not found. Aborting...")
313  exit(-1)
314 else:
315  print("INFO: Kernel " + sys.argv[3] + " found at index " + str(kernel_id) + ". Continuing...")
316 
317 # select tcp/udp role
318 udp = tcp = 0
319 if search('udp', sys.argv[1]):
320  udp = 1
321 if search('tcp', sys.argv[1]):
322  tcp = 1
323 
324 mtu=sys.argv[4]
325 port=sys.argv[5]
326 
327 # select ddr in role
328 if search('ddr_enabled', sys.argv[6]):
329  ddr = 1
330 elif search('ddr_disabled', sys.argv[6]):
331  ddr = 0
332 else:
333  print("ERROR: Invalid DDR option. Aborting...")
334  exit(-1)
335 
336 if ((kernel_id < 0 ) or kernel_id >= len(kernels)):
337  print("ERROR: Invalid kernel id. Aborting...")
338  exit(-1)
339 
340 new_kernel = kernels[kernel_id]
341 
342 role = os.getenv('roleName1')
343 
344 file = "ROLE/"+role+"/hdl/Role.vhdl"
345 full_file = str(pathlib.Path().absolute()) + '/' + str(file)
346 print("#################\n"+full_file+"\n----------------")
347 edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
348 
349 file = "ROLE/"+role+"/tcl/create_ip_cores.tcl"
350 full_file = str(pathlib.Path().absolute()) + '/' + str(file)
351 print("#################\n"+full_file+"\n----------------")
352 edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
353 
354 file = "ROLE/"+role+"/hls/Makefile"
355 full_file = str(pathlib.Path().absolute()) + '/' + str(file)
356 print("#################\n"+full_file+"\n----------------")
357 edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
358 
359 file = "HOST/"+role+"/"+new_kernel.lower()+"/languages/cplusplus/include/config.h"
360 full_file = str(pathlib.Path().absolute()) + '/' + str(file)
361 print("#################\n"+full_file+"\n----------------")
362 edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
363 
364 file = "ROLE/"+role+"/hls/"+new_kernel.lower()+"/include/"+new_kernel.lower()+".hpp"
365 full_file = str(pathlib.Path().absolute()) + '/' + str(file)
366 print("#################\n"+full_file+"\n----------------")
367 edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)
368 
369 
def edit_file(full_file, new_kernel, udp, tcp, mtu, port, ddr)