2 // Copyright (c) 2016 Intel Corporation
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ///////////////////////////////////////////////////////////////////////////////////////////////////
19 #include "cldnn_defs.h"
24 /// @addtogroup cpp_api C++ API
27 /// @defgroup cpp_engine Execution Engine
30 /// @brief Defines available engine types
31 enum class engine_types : int32_t
33 ocl = cldnn_engine_ocl
36 /// @brief Defines available priority mode types
37 enum class priority_mode_types : int16_t
39 disabled = cldnn_priority_disabled,
40 low = cldnn_priority_low,
41 med = cldnn_priority_med,
42 high = cldnn_priority_high
45 /// @brief Defines available priority mode types
46 enum class throttle_mode_types : int16_t
48 disabled = cldnn_throttle_disabled,
49 low = cldnn_throttle_low,
50 med = cldnn_throttle_med,
51 high = cldnn_throttle_high
54 /// @brief Configuration parameters for created engine.
55 struct engine_configuration
57 const bool enable_profiling; ///< Enable per-primitive profiling.
58 const bool meaningful_kernels_names; ///< Generate meaniful names fo OpenCL kernels.
59 const bool dump_custom_program; ///< Dump the user OpenCL programs to files
60 const std::string compiler_options; ///< OpenCL compiler options string.
61 const std::string single_kernel_name; ///< If provided, runs specific layer.
62 const bool enable_parallelisation; ///< Enables parallel execution of primitives which don't depend on each other. Disabled by default.
63 const std::string engine_log; ///< Specifies a file to which engine log should be dumped. Empty by default (means no logging).
64 const std::string sources_dumps_dir; ///< Specifies a directory where sources of cldnn::program objects should be dumped. Empty by default (means no dumping).
65 const priority_mode_types priority_mode; ///< Priority mode (support of priority hints in command queue). If cl_khr_priority_hints extension is not supported by current OpenCL implementation, the value must be set to cldnn_priority_disabled.
67 const throttle_mode_types throttle_mode; ///< Throttle mode (support of throttle hints in command queue). If cl_khr_throttle_hints extension is not supported by current OpenCL implementation, the value must be set to cldnn_throttle_disabled.
69 bool enable_memory_pool; ///< Enables memory usage optimization. memory objects will be reused when possible (switched off for older drivers then NEO).
70 void* context; ///< Pointer to user context
71 const std::string tuning_cache_path; ///< Path to tuning kernel cache
73 /// @brief Constructs engine configuration with specified options.
74 /// @param profiling Enable per-primitive profiling.
75 /// @param decorate_kernel_names Generate meaniful names fo OpenCL kernels.
76 /// @param dump_custom_program Dump the custom OpenCL programs to files
77 /// @param options OpenCL compiler options string.
78 /// @param single_kernel If provided, runs specific layer.
80 bool profiling = false,
81 bool decorate_kernel_names = false,
82 bool dump_custom_program = false,
83 const std::string& options = std::string(),
84 const std::string& single_kernel = std::string(),
85 bool primitives_parallelisation = true,
86 const std::string& engine_log = std::string(),
87 const std::string& sources_dumps_dir = std::string(),
88 priority_mode_types priority_mode = priority_mode_types::disabled,
89 throttle_mode_types throttle_mode = throttle_mode_types::disabled,
90 bool memory_pool = true,
91 void* context = nullptr,
92 const std::string& tuning_cache_path = "cache.json")
93 : enable_profiling(profiling)
94 , meaningful_kernels_names(decorate_kernel_names)
95 , dump_custom_program(dump_custom_program)
96 , compiler_options(options)
97 , single_kernel_name(single_kernel)
98 , enable_parallelisation(primitives_parallelisation)
99 , engine_log(engine_log)
100 , sources_dumps_dir(sources_dumps_dir)
101 , priority_mode(priority_mode)
102 , throttle_mode(throttle_mode)
103 , enable_memory_pool(memory_pool)
105 , tuning_cache_path(tuning_cache_path)
108 engine_configuration(const cldnn_engine_configuration& c_conf)
109 : enable_profiling(c_conf.enable_profiling != 0)
110 , meaningful_kernels_names(c_conf.meaningful_kernels_names != 0)
111 , dump_custom_program(c_conf.dump_custom_program != 0)
112 , compiler_options(c_conf.compiler_options)
113 , single_kernel_name(c_conf.single_kernel_name)
114 , enable_parallelisation(c_conf.enable_parallelisation != 0)
115 , engine_log(c_conf.engine_log)
116 , sources_dumps_dir(c_conf.sources_dumps_dir)
117 , priority_mode(static_cast<priority_mode_types>(c_conf.priority_mode))
118 , throttle_mode(static_cast<throttle_mode_types>(c_conf.throttle_mode))
119 , enable_memory_pool(c_conf.enable_memory_pool != 0)
120 , context(c_conf.context)
121 , tuning_cache_path(c_conf.tuning_cache_path)
124 /// @brief Implicit conversion to C API @ref ::cldnn_engine_configuration
125 operator ::cldnn_engine_configuration() const
129 meaningful_kernels_names,
131 compiler_options.c_str(),
132 single_kernel_name.c_str(),
133 enable_parallelisation,
135 sources_dumps_dir.c_str(),
136 static_cast<int16_t>(priority_mode),
137 static_cast<int16_t>(throttle_mode),
140 tuning_cache_path.c_str()
145 /// @brief Information about the engine properties and capabilities.
146 /// @details Look into @ref ::cldnn_engine_info for details.
147 using engine_info = ::cldnn_engine_info;
149 /// @brief Represents clDNN engine object.
152 /// @brief Constructs @p OpenCL engine
153 engine(const engine_configuration& configuration = engine_configuration())
154 :engine(engine_types::ocl, 0, configuration)
157 /// @brief Construct engine of the specified @p type, @p engine_num, and @p configuration options.
158 /// @param[in] type Engine type @ref cldnn_engine_type. Only OCL engine is supported.
159 /// @param[in] engine_num Engine index. Should be 0.
160 /// @param[in] configuration Pointer to engine configuration options.
161 engine(engine_types type, uint32_t engine_num, const engine_configuration& configuration = engine_configuration())
162 :_impl(check_status<::cldnn_engine>("failed to create engine", [&](status_t* status)
164 cldnn_engine_configuration conf = configuration;
165 return cldnn_create_engine(static_cast<int32_t>(type), engine_num, &conf, status);
169 // TODO add move construction/assignment
170 engine(const engine& other) :_impl(other._impl)
175 engine& operator=(const engine& other)
177 if (_impl == other._impl) return *this;
189 friend bool operator==(const engine& lhs, const engine& rhs) { return lhs._impl == rhs._impl; }
190 friend bool operator!=(const engine& lhs, const engine& rhs) { return !(lhs == rhs); }
192 /// @brief Returns number of available engines of the particular @p type.
193 static uint32_t engine_count(engine_types type)
195 return check_status<uint32_t>("engine_count failed", [=](status_t* status)
197 return cldnn_get_engine_count(static_cast<int32_t>(type), status);
202 /// @brief Release pending memory allocated in OpenCL context.
203 void release_pending_memory() const
205 check_status<void>("flush_memory failed", [=](status_t* status)
207 return cldnn_release_pending_memory(_impl, status);
212 /// @brief Returns information about properties and capabilities for the engine.
213 engine_info get_info() const
215 return check_status<engine_info>("engine_count failed", [=](status_t* status)
217 return cldnn_get_engine_info(_impl, status);
221 /// @brief Returns total size of all resources allocated using given engine
222 uint64_t get_max_used_device_memory_size() const
224 return check_status<uint64_t>("get total device memory failed", [=](status_t* status)
226 return cldnn_get_max_used_device_memory_size(_impl, status);
230 /// @brief Returns total size of currently resources allocated using given engine
231 uint64_t get_temp_used_device_memory_size() const
233 return check_status<uint64_t>("get device memory failed", [=](status_t* status)
235 return cldnn_get_temp_used_device_memory_size(_impl, status);
239 /// @brief Returns type of the engine.
240 engine_types get_type() const
242 return check_status<engine_types>("engine_count failed", [=](status_t* status)
244 return static_cast<engine_types>(cldnn_get_engine_type(_impl, status));
248 /// @brief get C API engine handler.
249 ::cldnn_engine get() const { return _impl; }
252 friend struct network;
253 friend struct memory;
255 engine(::cldnn_engine impl) : _impl(impl)
257 if (_impl == nullptr) throw std::invalid_argument("implementation pointer should not be null");
259 ::cldnn_engine _impl;
263 check_status<void>("retain engine failed", [=](status_t* status) { cldnn_retain_engine(_impl, status); });
267 check_status<void>("release engine failed", [=](status_t* status) { cldnn_release_engine(_impl, status); });
270 CLDNN_API_CLASS(engine)