]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - tidl/tidl-api.git/blob - tidl_api/inc/executor.h
Added an example to illustrate 1 EO per frame
[tidl/tidl-api.git] / tidl_api / inc / executor.h
1 /******************************************************************************
2  * Copyright (c) 2017-2018 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"
41 #include "custom.h"
43 namespace tidl {
45 //! Enumerates types of devices available to offload the network.
46 enum class DeviceType { DSP, /**< Offload to C66x DSP */
47                         EVE  /**< Offload to TI EVE */
48                       };
50 //! Enumerates IDs for devices of a given type.
51 enum class DeviceId : int { ID0=0, /**< DSP1 or EVE1 */
52                             ID1,   /**< DSP2 or EVE2 */
53                             ID2,   /**< EVE3 */
54                             ID3    /**< EVE4 */
55                           };
57 //! Used to specify the set of devices available to an Executor
58 typedef std::set<DeviceId> DeviceIds;
60 class ExecutorImpl;
61 class ExecutionObject;
63 //! Defines the return type for Executor::GetExecutionObjects
64 typedef std::vector<std::unique_ptr<ExecutionObject>> ExecutionObjects;
66 /*! @class Executor
67     @brief Manages the overall execution of a layersGroup in a network using the
68     specified configuration and the set of devices available to the
69     executor.
70 */
71 class Executor
72 {
73     public:
74         //! @brief Create an Executor object.
75         //!
76         //! The Executor will create the required ExecutionObject's and
77         //! initialize them with the specified TI DL network. E.g.
78         //! @code
79         //!   Configuration configuration;
80         //!   configuration.ReadFromFile("path to configuration file");
81         //!   DeviceIds ids = {DeviceId::ID2, DeviceId::ID3};
82         //!   Executor executor(DeviceType::EVE, ids, configuration);
83         //! @endcode
84         //!
85         //! @param device_type DSP or EVE device
86         //! @param ids Set of devices uses by this instance of the Executor
87         //! @param configuration Configuration used to initialize the Executor
88         //! @param layers_group_id Layers group that this Executor should run
89         Executor(DeviceType device_type, const DeviceIds& ids,
90                  const Configuration& configuration,
91                  int layers_group_id = OCL_TIDL_DEFAULT_LAYERS_GROUP_ID);
93         //! @brief Tear down an Executor and free resources used by the
94         //! Executor object
95         ~Executor();
97         //! Returns a vector of unique_ptr's to execution objects
98         //! available on this instance of the Executor
99         const ExecutionObjects& GetExecutionObjects() const;
101         //! Returns a single execution object at index
102         ExecutionObject* operator[](uint32_t index) const;
104         //! Returns the number of ExecutionObjects associated with the
105         //! Executor
106         uint32_t GetNumExecutionObjects() const;
108         //! @brief Returns the number of devices of the specified type
109         //! available for TI DL.
110         //! @param  device_type DSP or EVE/EVE device
111         //! @return number of devices available
112         static uint32_t GetNumDevices(DeviceType device_type);
114         //! @brief Returns a string corresponding to the API version
115         //!
116         //! @return \<major_ver>.\<minor_ver>.\<patch_ver>.\<git_sha>
117         static std::string GetAPIVersion();
119         Executor(const Executor&) = delete;
120         Executor& operator= (const Executor&) = delete;
123     private:
124         std::unique_ptr<ExecutorImpl> pimpl_m;
125 };
127 /*! @class ArgInfo
128  *  @brief Describe input and output buffers required by ExecutionObjects
129  */
130 class ArgInfo
132     public:
133         enum class DeviceAccess { R_ONLY=0, W_ONLY, RW };
135         //! Construct an ArgInfo object from a pointer to a chunk of memory
136         //! and its size.
137         ArgInfo(void *p, size_t size) :
138             ptr_m(p), size_m(size), access_m(DeviceAccess::RW) {}
140         ArgInfo(const ArgInfo& arg) = default;
141         ArgInfo& operator=(const ArgInfo& arg) = default;
143         //! @return Pointer to the buffer or scalar represented by ArgInfo
144         void  *ptr()  const { return ptr_m; }
146         //! @return The size of the buffer or scalar represented by ArgInfo
147         size_t size() const { return size_m; }
149     protected:
150         void*        ptr_m;
151         size_t       size_m;
152         DeviceAccess access_m;
153 };
156 extern "C" void  __free_ddr(void *ptr);
157 extern "C" void* __malloc_ddr(size_t s);
159 //! template typedef for unique_ptr with __free_ddr deleter
160 template<class T>
161 using up_malloc_ddr = std::unique_ptr<T, decltype(&__free_ddr)>;
163 //! __malloc_ddr wrapper - Bytes allocated determined by sizeof(T)
164 template<class T>
165 inline T* malloc_ddr()
167     assert (std::is_pointer<T>::value == false);
168     T* val =  reinterpret_cast<T *>(__malloc_ddr(sizeof(T)));
169     assert (val != nullptr);
170     return val;
173 //! __malloc_ddr wrapper - Bytes allocated passed as argument
174 template<class T>
175 inline T* malloc_ddr(size_t size)
177     assert (std::is_pointer<T>::value == false);
178     T* val = reinterpret_cast<T *>(__malloc_ddr(size));
179     assert (val != nullptr);
180     return val;
183 /*! @class Exception
184  *  @brief Used to error reporting
185  */
186 class Exception : public std::exception
188     public:
189         Exception() {}
190         Exception(const std::string& error, const std::string& file,
191                   const std::string& func, uint32_t line_no);
192         Exception(int32_t errorCode, const std::string& file,
193                   const std::string& func, uint32_t line_no);
195         virtual ~Exception() {}
197         //! @return String describing the error message and its location
198         virtual const char* what() const noexcept;
200     private:
201         std::string message_m;
202 };
204 } // namespace tidl