clDNN
cldnn_defs.h
1 /*
2 // Copyright (c) 2016 Intel Corporation
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 */
16 
131 #pragma once
133 
134 #include <functional>
135 #include <string>
136 #include <type_traits>
137 #include <utility>
138 #include <vector>
139 #include <stdexcept>
140 
141 #include "../C/cldnn.h"
142 
143 namespace cldnn {
144  // There is no portable half precision floating point support.
145  // Using wrapped integral type with the same size and alignment restrictions.
146  class half_impl
147  {
148  public:
149  half_impl() = default;
150  template <typename T, typename = typename std::enable_if<!std::is_floating_point<T>::value>::type>
151  explicit half_impl(T data) : _data(data) {}
152 
153  operator uint16_t() const { return _data; }
154  operator float() const
155  {
156  cldnn_status status = CLDNN_SUCCESS;
157  auto value = cldnn_half_to_float(_data, &status);
158  if (status != CLDNN_SUCCESS)
159  throw std::runtime_error("Conversion from half failed");
160  return value;
161  }
162  explicit half_impl(float value)
163  {
164  cldnn_status status = CLDNN_SUCCESS;
165  _data = cldnn_float_to_half(value, &status);
166  if (status != CLDNN_SUCCESS)
167  throw std::runtime_error("Conversion to half failed");
168  }
169 
170  private:
171  uint16_t _data;
172  };
173 }
174 // Use complete implementation if necessary.
175 #if defined HALF_HALF_HPP
176 typedef half half_t;
177 #else
178 typedef cldnn::half_impl half_t;
179 #endif
180 
181 namespace cldnn {
184 
187 
188 using status_t = ::cldnn_status;
189 
191 class error : public std::runtime_error
192 {
193 public:
194  explicit error(const std::string& _Message, status_t status = CLDNN_ERROR)
195  : runtime_error(_Message)
196  , _status(status)
197  {
198  }
199 
200  explicit error(const char* _Message, status_t status = CLDNN_ERROR)
201  : runtime_error(_Message)
202  , _status(status)
203  {
204  }
205 
207  const status_t& status() const { return _status; }
208 private:
209  status_t _status;
210 };
211 
212 #define CLDNN_THROW(msg, status) throw cldnn::error(msg, status);
213 
214 template<class T>
215 T check_status(std::string err_msg, std::function<T(status_t*)> func)
216 {
217  status_t status = CLDNN_SUCCESS;
218  auto result = func(&status);
219  if (status != CLDNN_SUCCESS)
220  CLDNN_THROW(err_msg.append(": ").append(cldnn_get_last_error_message()), status);
221  return result;
222 }
223 
224 template<>
225 inline void check_status<void>(std::string err_msg, std::function<void(status_t*)> func)
226 {
227  status_t status = CLDNN_SUCCESS;
228  func(&status);
229  if (status != CLDNN_SUCCESS)
230  CLDNN_THROW(err_msg.append(": ").append(cldnn_get_last_error_message()), status);
231 }
232 
234 
237 
238 using version_t = ::cldnn_version;
239 
242 {
243  return check_status<version_t>("get_version: fetching version information failed",
244  [](status_t* status)
245  {
246  return ::cldnn_get_version(status);
247  });
248 }
249 
251 
253 
256 
257 #define CLDNN_API_CLASS(the_class) static_assert(std::is_standard_layout<the_class>::value, #the_class " has to be 'standart layout' class");
258 
259 
260 template<typename T>
261 typename std::enable_if<std::is_integral<T>::value, T>::type align_to(T size, size_t align) {
262  return static_cast<T>((size % align == 0) ? size : size - size % align + align);
263 }
264 
265 template<typename T>
266 typename std::enable_if<std::is_integral<T>::value, T>::type pad_to(T size, size_t align) {
267  return static_cast<T>((size % align == 0) ? 0 : align - size % align);
268 }
269 
270 template<typename T>
271 typename std::enable_if<std::is_integral<T>::value, bool>::type is_aligned_to(T size, size_t align)
272 {
273  return !(size % align);
274 }
275 
290 template <typename T1, typename T2>
291 constexpr auto ceil_div(T1 val, T2 divider)
292  -> typename std::enable_if<std::is_integral<T1>::value && std::is_integral<T2>::value,
293  decltype(std::declval<typename std::make_unsigned<T1>::type>() / std::declval<typename std::make_unsigned<T2>::type>())>::type
294 {
295  typedef typename std::make_unsigned<T1>::type UT1;
296  typedef typename std::make_unsigned<T2>::type UT2;
297  typedef decltype(std::declval<UT1>() / std::declval<UT2>()) RetT;
298 
299  return static_cast<RetT>((static_cast<UT1>(val) + static_cast<UT2>(divider) - 1U) / static_cast<UT2>(divider));
300 }
301 
315 template <typename T1, typename T2>
316 constexpr auto round_up_to(T1 val, T2 rounding)
317  -> typename std::enable_if<std::is_integral<T1>::value && std::is_integral<T2>::value,
318  decltype(std::declval<typename std::make_unsigned<T1>::type>() / std::declval<typename std::make_unsigned<T2>::type>())>::type
319 {
320  typedef typename std::make_unsigned<T1>::type UT1;
321  typedef typename std::make_unsigned<T2>::type UT2;
322  typedef decltype(std::declval<UT1>() / std::declval<UT2>()) RetT;
323 
324  return static_cast<RetT>(ceil_div(val, rounding) * static_cast<UT2>(rounding));
325 }
326 
330 inline std::vector<float> float_arr_to_vector(const cldnn_float_arr& arr)
331 {
332  std::vector<float> result(arr.size);
333  for (size_t i = 0; i < arr.size; i++)
334  {
335  result[i] = arr.data[i];
336  }
337  return result;
338 }
339 
343 inline std::vector<uint16_t> uint16_t_arr_to_vector(const cldnn_uint16_t_arr& arr)
344 {
345  std::vector<uint16_t> result(arr.size);
346  for (size_t i = 0; i < arr.size; i++)
347  {
348  result[i] = arr.data[i];
349  }
350  return result;
351 }
352 
353 
357 inline cldnn_float_arr float_vector_to_arr(const std::vector<float>& stor)
358 {
359  return { stor.data(), stor.size() };
360 }
361 
365 inline cldnn_uint16_t_arr uint16_t_vector_to_arr(const std::vector<uint16_t>& stor)
366 {
367  return{ stor.data(), stor.size() };
368 }
369 
373 inline cldnn_tensor_arr tensor_vector_to_arr(const std::vector<cldnn_tensor>& stor)
374 {
375  return cldnn_tensor_arr{ stor.data(), stor.size() };
376 }
377 
379 
381 
383 }
Represents reference to an array of uint16_t.
Definition: cldnn.h:315
CLDNN_API uint16_t cldnn_float_to_half(float, cldnn_status *)
converts float(32 bit) to half_t(fp16 bit)
const status_t & status() const
Returns clDNN status code.
Definition: cldnn_defs.h:207
const float * data
Pointer to float array.
Definition: cldnn.h:310
Represents reference to an array of tensor.
Definition: cldnn.h:322
Represents reference to an array of floats.
Definition: cldnn.h:308
CLDNN_API const char * cldnn_get_last_error_message()
If cldnn function returns status different than CLDNN_SUCCESS, user call this function to get more de...
version_t get_version()
Get information about version of clDNN.
Definition: cldnn_defs.h:241
size_t size
Size (in uint16_t) of the array.
Definition: cldnn.h:318
Provides input data to topology.
Definition: data.hpp:36
int32_t cldnn_status
Represents errors status for all API calls.
Definition: cldnn.h:82
const cldnn_tensor * data
Pointer to tensor array.
Definition: cldnn.h:324
clDNN specific exception type.
Definition: cldnn_defs.h:191
CLDNN_API float cldnn_half_to_float(uint16_t, cldnn_status *)
converts half_t(f16 bit) to float(32 bit)
const uint16_t * data
Pointer to uint16_t array.
Definition: cldnn.h:317