]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - tinn_api/inc/executor.h
e2ce077280f1c26ea1e260972923bebd9edf6a2c
[tidl/tidl-api.git] / tinn_api / inc / executor.h
1 /******************************************************************************
2  * Copyright (c) 2017-18 Texas Instruments Incorporated - http://www.ti.com/
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *      * Redistributions of source code must retain the above copyright
8  *        notice, this list of conditions and the following disclaimer.
9  *      * Redistributions in binary form must reproduce the above copyright
10  *        notice, this list of conditions and the following disclaimer in the
11  *        documentation and/or other materials provided with the distribution.
12  *      * Neither the name of Texas Instruments Incorporated nor the
13  *        names of its contributors may be used to endorse or promote products
14  *        derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  *  THE POSSIBILITY OF SUCH DAMAGE.
27  *****************************************************************************/
29 //! @file executor.h
31 #pragma once
32 #include <string>
33 #include <vector>
34 #include <memory>
35 #include <cstdint>
36 #include <cassert>
37 #include <set>
38 #include <exception>
40 #include "configuration.h"
42 namespace tinn {
44 //! Enumerates types of devices available to offload the network.
45 enum class DeviceType { DSP, /**< Offload to C66x DSP */
46                         DLA  /**< Offload to TI DLA */
47                       };
49 //! Enumerates IDs for devices of a given type.
50 enum class DeviceId : int { ID0=0, /**< DSP1 or DLA1 */
51                             ID1,   /**< DSP2 or DLA2 */
52                             ID2,   /**< DLA3 */
53                             ID3    /**< DLA4 */
54                           };
56 //! Used to specify the set of devices available to an Executor
57 typedef std::set<DeviceId> DeviceIds;
59 class ExecutorImpl;
60 class ExecutionObject;
62 //! Defines the return type for Executor::GetExecutionObjects
63 typedef std::vector<std::unique_ptr<ExecutionObject>> ExecutionObjects;
65 /*! @class Executor
66     @brief Manages the overall execution of a network using the
67     specified configuration and the set of devices available to the
68     executor.
69 */
70 class Executor
71 {
72     public:
73         //! @brief Create an Executor object.
74         //!
75         //! The Executor will create the required ExecutionObject's and
76         //! initialize them with the specified TI DL network. E.g.
77         //! @code
78         //!   Configuration configuration;
79         //!   configuration.ReadFromFile("path to configuration file");
80         //!   DeviceIds ids1 = {DeviceId::ID2, DeviceId::ID3};
81         //!   Executor executor(DeviceType::DLA, ids, configuration);
82         //! @endcode
83         //!
84         //! @param device_type DSP or EVE/DLA device
85         //! @param ids Set of devices uses by this instance of the Executor
86         //! @param configuration Configuration used to initialize the Executor
87         Executor(DeviceType device_type, const DeviceIds& ids,
88                  const Configuration& configuration);
90         //! @brief Tear down an Executor and free resources used by the
91         //! Executor object
92         ~Executor();
94         //! Returns a vector of unique_ptr's to execution objects
95         //! available on this instance of the Executor
96         const ExecutionObjects& GetExecutionObjects() const;
98         //! @brief Returns the number of devices of the specified type
99         //! available for TI DL.
100         //! @param  device_type DSP or EVE/DLA device
101         //! @return number of devices available
102         static uint32_t GetNumDevices(DeviceType device_type);
104         //! @brief Returns a string corresponding to the API version
105         //!
106         //! @return <major_ver>.<minor_ver>.<patch_ver>.<git_sha>
107         static std::string GetAPIVersion();
109         Executor(const Executor&) = delete;
110         Executor& operator= (const Executor&) = delete;
113     private:
114         std::unique_ptr<ExecutorImpl> pimpl_m;
115 };
118 /*! @class ArgInfo
119  *  @brief Describe input and output buffers required by ExecutionObjects
120  */
121 class ArgInfo
123     public:
124         enum class DeviceAccess { R_ONLY=0, W_ONLY, RW };
126         //! Enumerates the types of arguments represented by ArgInfo
127         enum class Kind { BUFFER=0, SCALAR };
129         //! Construct an ArgInfo object from a pointer to a chunk of memory
130         //! and its size.
131         ArgInfo(void *p, size_t size) :
132             ptr_m(p), size_m(size),
133             access_m(DeviceAccess::RW), kind_m(Kind::BUFFER) {}
135         //! Construct an ArgInfo object from a pointer to a chunk of memory
136         //! its size and kind
137         ArgInfo(void *p, size_t size, Kind kind) :
138             ptr_m(p), size_m(size), access_m(DeviceAccess::RW), kind_m(kind) {}
140         //! @return Pointer to the buffer or scalar represented by ArgInfo
141         void  *ptr()  const { return ptr_m; }
143         //! @return The size of the buffer or scalar represented by ArgInfo
144         size_t size() const { return size_m; }
146         // Only used by tinn::Device
147         Kind   kind() const { return kind_m; }
148         bool   isLocal() const { return (ptr_m == nullptr); }
150     private:
151         void*        ptr_m;
152         size_t       size_m;
153         DeviceAccess access_m;
154         Kind         kind_m;
155 };
158 extern "C" void  __free_ddr(void *ptr);
159 extern "C" void* __malloc_ddr(size_t s);
161 //! template typedef for unique_ptr with __free_ddr deleter
162 template<class T>
163 using up_malloc_ddr = std::unique_ptr<T, decltype(&__free_ddr)>;
165 //! __malloc_ddr wrapper - Bytes allocated determined by sizeof(T)
166 template<class T>
167 inline T* malloc_ddr()
169     assert (std::is_pointer<T>::value == false);
170     T* val =  reinterpret_cast<T *>(__malloc_ddr(sizeof(T)));
171     assert (val != nullptr);
172     return val;
175 //! __malloc_ddr wrapper - Bytes allocated passed as argument
176 template<class T>
177 inline T* malloc_ddr(size_t size)
179     assert (std::is_pointer<T>::value == false);
180     T* val = reinterpret_cast<T *>(__malloc_ddr(size));
181     assert (val != nullptr);
182     return val;
185 /*! @class Exception
186  *  @brief Used to error reporting
187  */
188 class Exception : public std::exception
190     public:
191         Exception() {}
192         Exception(const std::string& error, const std::string& file,
193                   const std::string& func, uint32_t line_no);
194         Exception(int32_t errorCode, const std::string& file,
195                   const std::string& func, uint32_t line_no);
197         virtual ~Exception() {}
199         //! @return String describing the error message and its location
200         virtual const char* what() const noexcept;
202     private:
203         std::string message_m;
204 };
206 } // namespace tinn