]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - examples/pybind/tidl_app_utils.py
Added Python 3 bindings for TIDL API
[tidl/tidl-api.git] / examples / pybind / tidl_app_utils.py
1 #!/usr/bin/python3
3 # Copyright (c) 2018 Texas Instruments Incorporated - http://www.ti.com/
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions are met:
8 # * Redistributions of source code must retain the above copyright
9 # notice, this list of conditions and the following disclaimer.
10 # * Redistributions in binary form must reproduce the above copyright
11 # notice, this list of conditions and the following disclaimer in the
12 # documentation and/or other materials provided with the distribution.
13 # * Neither the name of Texas Instruments Incorporated nor the
14 # names of its contributors may be used to endorse or promote products
15 # derived from this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27 # THE POSSIBILITY OF SUCH DAMAGE.
30 from tidl import DeviceId
31 from tidl import DeviceType
32 from tidl import Configuration
33 from tidl import Executor
34 from tidl import TidlError
36 import tidl
38 def read_frame(eo, frame_index, c, f):
39     """Read a frame into the ExecutionObject input buffer"""
41     if (frame_index >= c.num_frames):
42         return False
44     # Read into the EO's input buffer
45     arg_info = eo.get_input_buffer()
46     bytes_read = f.readinto(arg_info)
48     if (bytes_read != arg_info.size()):
49         print("Expected {} bytes, read {}".format(size, bytes_read))
50         return False
52     if (len(f.peek(1)) == 0):
53         f.seek(0)
55     eo.set_frame_index(frame_index)
57     return True
59 def write_output(eo, f):
60     """Write the output buffer to file"""
62     arg_info = eo.get_output_buffer()
63     f.write(arg_info)
65 def report_time(eo):
66     """Report execution time on host and device"""
68     elapsed_host   = eo.get_host_process_time_in_ms()
69     elapsed_device = eo.get_process_time_in_ms()
70     overhead = elapsed_host - elapsed_device
72     # https://pyformat.info/
73     print('frame{:3d}: Time on {}: {:4.2f} ms, host: {:4.2f} ms '
74           'API overhead: {:2.2f} ms'.format(eo.get_frame_index(),
75                                            eo.get_device_name(),
76                                            elapsed_device,
77                                            elapsed_host,
78                                            overhead))