TIOVX User Guide
kernel.py
1 #
2 # Copyright (c) 2017 Texas Instruments Incorporated
3 #
4 # All rights reserved not granted herein.
5 #
6 # Limited License.
7 #
8 # Texas Instruments Incorporated grants a world-wide, royalty-free, non-exclusive
9 # license under copyrights and patents it now or hereafter owns or controls to make,
10 # have made, use, import, offer to sell and sell ("Utilize") this software subject to the
11 # terms herein. With respect to the foregoing patent license, such license is granted
12 # solely to the extent that any such patent is necessary to Utilize the software alone.
13 # The patent license shall not apply to any combinations which include this software,
14 # other than combinations with devices manufactured by or for TI ("TI Devices").
15 # No hardware patent is licensed hereunder.
16 #
17 # Redistributions must preserve existing copyright notices and reproduce this license
18 # (including the above copyright notice and the disclaimer and (if applicable) source
19 # code license limitations below) in the documentation and/or other materials provided
20 # with the distribution
21 #
22 # Redistribution and use in binary form, without modification, are permitted provided
23 # that the following conditions are met:
24 #
25 # No reverse engineering, decompilation, or disassembly of this software is
26 # permitted with respect to any software provided in binary form.
27 #
28 # any redistribution and use are licensed by TI for use only with TI Devices.
29 #
30 # Nothing shall obligate TI to provide you with source code for the software
31 # licensed and provided to you in object code.
32 #
33 # If software source code is provided to you, modification and redistribution of the
34 # source code are permitted provided that the following conditions are met:
35 #
36 # any redistribution and use of the source code, including any resulting derivative
37 # works, are licensed by TI for use only with TI Devices.
38 #
39 # any redistribution and use of any object code compiled from the source code
40 # and any resulting derivative works, are licensed by TI for use only with TI Devices.
41 #
42 # Neither the name of Texas Instruments Incorporated nor the names of its suppliers
43 #
44 # may be used to endorse or promote products derived from this software without
45 # specific prior written permission.
46 #
47 # DISCLAIMER.
48 #
49 # THIS SOFTWARE IS PROVIDED BY TI AND TI'S LICENSORS "AS IS" AND ANY EXPRESS
50 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52 # IN NO EVENT SHALL TI AND TI'S LICENSORS BE LIABLE FOR ANY DIRECT, INDIRECT,
53 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
54 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
56 # OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
57 # OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
58 # OF THE POSSIBILITY OF SUCH DAMAGE.
59 #
60 #
61 
62 from . import *
63 
64 class KernelParams :
65  def __init__(self, index, type, direction, state, name, data_types=[], do_map=True, do_unmap=True, do_map_unmap_all_planes=False):
66  self.index = index
67  self.type = type
68  self.direction = direction
69  self.state = state
70  self.name_upper = name.upper()
71  self.name_lower = name.lower()
72  self.name_camel = toCamelCase(name)
73  self.data_types = data_types
74  self.do_map = do_map
75  self.do_unmap = do_unmap
76  self.do_map_unmap_all_planes = do_map_unmap_all_planes
77  if Type.is_scalar_type(type) :
78  self.do_map = False;
79  self.do_unmap = False;
80 
81  def __str__(self):
82  return "Param " + str(self.index) + ": " + self.name_upper + " " + Type.get_vx_enum_name(self.type) + " " + Direction.get_vx_enum_name(self.direction) + " " + ParamState.get_vx_enum_name(self.state)
83 
84 class KernelParamRelationship :
85  def __init__(self, prm_list, attribute_list, type, state):
86  self.attribute_list = attribute_list
87  self.prm_list = prm_list
88  self.type = type
89  self.state = state
90 
91  def __str__(self) :
92  rel_str = "Param Relationship:\n"
93  for param in self.prm_list :
94  rel_str += '\t' + str(param) + "\n"
95  for attr in self.attribute_list :
96  rel_str += '\t' + str(attr) + "\n"
97  return rel_str
98 
99 class KernelLocalMem :
100  def __init__(self, prm, attribute_list, name, state):
101  self.attribute_list = attribute_list
102  self.prm = prm
103  self.state = state
104  self.name = name
105 
106  def __str__(self):
107  mem_str = "Local Mem:\n\t" + self.name + "\n"
108  mem_str += '\t' + str(self.prm) + "\n"
109  mem_str += '\t' + str(self.state) + "\n"
110  for attr in self.attribute_list :
111  mem_str += '\t' + str(attr) + "\n"
112  return mem_str
113 
114 
126 class Kernel :
127 
130  def __init__(self, name="default") :
131  self.name_lower = name.lower()
132  self.name_upper = name.upper()
133  self.name_camel = toCamelCase(name)
134  self.index = 0;
135  self.params = []
136  self.name_str_prefix = "com.ti."
137  self.enum_str_prefix = "TIVX_KERNEL_"
138  self.targets = []
139  self.relationship_list = []
140  self.relationship_list_index = 0
141  self.local_mem_list = []
142  self.local_mem_list_index = 0
143  self.localMem = False
144  self.local_mem_name_list = []
145  self.parameter_name_list = []
146 
147  def setKernelPrefix(self, name_str_prefix, enum_str_prefix) :
148  self.name_str_prefix = name_str_prefix
149  self.enum_str_prefix = enum_str_prefix
150 
151 
160  def setTarget(self, target) :
161  self.targets.append(target)
162 
163  def __str__(self) :
164  kernel_str = "Kernel: " + self.name_lower + " "+ self.name_upper + " "+ self.name_camel + "\n"
165  kernel_str += "Targets: "
166  for target in self.targets :
167  if type(target) is Target :
168  kernel_str += Target.get_vx_enum_name(target) + "(CPU: " + Cpu.get_vx_enum_name(Target.get_cpu(target)) + ") "
169  else :
170  sys.exit("target arguments have invalid type.")
171  kernel_str += "\n"
172  for prm in self.params :
173  kernel_str += str(prm) + "\n"
174  return kernel_str
175 
176 
192  def setParameter(self, type, direction, state, name, data_types=[], do_map=True, do_unmap=True, do_map_unmap_all_planes=False):
193  in_list = False
194 
195  if name in self.parameter_name_list :
196  in_list = True
197  else :
198  self.parameter_name_list.append(name)
199 
200  assert in_list == False, "'%s' was already used as local memory name" % name
201 
202  params = KernelParams(self.index, type, direction, state, name, data_types, do_map, do_unmap, do_map_unmap_all_planes);
203  self.params.append(params)
204  self.index = self.index + 1
205 
206 
297  def allocateLocalMemory(self, local_mem_name, attribute_list, parameter_name=""):
298  self.localMem = True
299 
300  local_prm = Null()
301  required = 0
302  optional = 0
303  first_required = 0
304  in_list = False
305 
306  if local_mem_name in self.local_mem_name_list :
307  in_list = True
308  else :
309  self.local_mem_name_list.append(local_mem_name)
310 
311  assert in_list == False, "'%s' was already used as local memory name" % local_mem_name
312 
313  # Get params from names
314  if parameter_name :
315  found = False
316  for prm in self.params :
317  if parameter_name.lower() == prm.name_lower :
318  if KernelExportCode.is_supported_type(self, prm.type):
319  local_prm = prm
320  found = True
321  break
322  assert found == True, "'%s' was not found in image parameter list" % parameter_name
323 
324  if parameter_name :
325  localmem = KernelLocalMem(local_prm, attribute_list, local_mem_name, local_prm.state)
326  else :
327  localmem = KernelLocalMem(local_prm, attribute_list, local_mem_name, ParamState.REQUIRED)
328  self.local_mem_list.append(localmem)
330 
331  def getNumImages(self) :
332  num_images = 0
333  for prm in self.params :
334  if prm.type == Type.IMAGE :
335  num_images += 1
336  return num_images
337 
338  def getNumRequiredImages(self) :
339  num_images = 0
340  for prm in self.params :
341  if prm.type == Type.IMAGE and prm.state == ParamState.REQUIRED :
342  num_images += 1
343  return num_images
344 
345  def getNumOptionalImages(self) :
346  num_images = 0
347  for prm in self.params :
348  if prm.type == Type.IMAGE and prm.state == ParamState.OPTIONAL :
349  num_images += 1
350  return num_images
351 
352  def getNumScalars(self) :
353  num_scalars = 0
354  for prm in self.params :
355  if Type.is_scalar_type(prm.type) is True :
356  num_scalars += 1
357  return num_scalars
358 
359  def getNumInputImages(self) :
360  num_input_images = 0
361  for prm in self.params :
362  if prm.type == Type.IMAGE and prm.direction == Direction.INPUT:
363  num_input_images += 1
364  return num_input_images
365 
366  def getNumRequiredInputImages(self) :
367  num_input_images = 0
368  for prm in self.params :
369  if prm.type == Type.IMAGE and prm.direction == Direction.INPUT and prm.state == ParamState.REQUIRED :
370  num_input_images += 1
371  return num_input_images
372 
373  def getNumOutputImages(self) :
374  num_output_images = 0
375  for prm in self.params :
376  if prm.type == Type.IMAGE and prm.direction == Direction.OUTPUT:
377  num_output_images += 1
378  return num_output_images
379 
380  def getNumRequiredOutputImages(self) :
381  num_output_images = 0
382  for prm in self.params :
383  if prm.type == Type.IMAGE and prm.direction == Direction.OUTPUT and prm.state == ParamState.REQUIRED :
384  num_output_images += 1
385  return num_output_images
386 
387 
400  def setParameterRelationship(self, name_list=[], attribute_list=["all"], type="equal") :
401  assert len(name_list) > 1, "There should be more than 1 parameter in name_list"
402  prm_list = []
403  required = 0
404  optional = 0
405  first_required = 0
406 
407  # Get params from names
408  for name in name_list :
409  found = False
410  for prm in self.params :
411  if name.lower() == prm.name_lower :
412  prm_list.append(prm)
413  if prm.state == ParamState.REQUIRED :
414  if first_required == 0 :
415  first_required = prm
416  required += 1
417  elif prm.state == ParamState.OPTIONAL :
418  optional += 1
419  found = True
420  break
421  assert found == True, "'%s' was not found in parameter list" % name
422 
423  # Divide list into multiple lists depending on if some of the parameters are optional or not
424  if required > 1 :
425  if optional == 0 :
426  relationship = KernelParamRelationship(prm_list, attribute_list, type, ParamState.REQUIRED)
427  else :
428  sub_prm_list = []
429  for prm in prm_list :
430  if prm.state == ParamState.REQUIRED :
431  sub_prm_list.append(prm)
432  prm_list.remove(prm)
433  relationship = KernelParamRelationship(sub_prm_list, attribute_list, type, ParamState.REQUIRED)
434  self.relationship_list.append(relationship)
436  if required > 0 :
437  while optional > 0 :
438  optional -= 1
439  sub_prm_list = []
440  for prm in prm_list :
441  if prm.state == ParamState.OPTIONAL :
442  sub_prm_list.append(prm)
443  sub_prm_list.append(first_required)
444  relationship = KernelParamRelationship(sub_prm_list, attribute_list, type, ParamState.OPTIONAL)
445  self.relationship_list.append(relationship)
447  prm_list.remove(prm)
448  break
449  else :
450  relationship = KernelParamRelationship(prm_list, attribute_list, type, ParamState.OPTIONAL)
451  self.relationship_list.append(relationship)
Kernel class containing parameter information.
Definition: kernel.py:126
def __init__(self, name="default")
Constructor used to create this object.
Definition: kernel.py:130
Null object.
Definition: null.py:76
def setTarget(self, target)
Method used to set a possible target of the kernel.
Definition: kernel.py:160
def setParameter(self, type, direction, state, name, data_types=[], do_map=True, do_unmap=True, do_map_unmap_all_planes=False)
Method used to set a parameter of the kernel; called for as many parameters as necessary.
Definition: kernel.py:192
def __init__(self, df_image, image_patch_address, name="default")
Constructor used to create this object.
Definition: image.py:178
def setParameterRelationship(self, name_list=[], attribute_list=["all"], type="equal")
Method used to set the relationship between two parameters of a kernels Information from this method ...
Definition: kernel.py:400
def allocateLocalMemory(self, local_mem_name, attribute_list, parameter_name="")
Method used to allocated local memory; called per instance of memory allocation.
Definition: kernel.py:297