DPC++ Runtime
Runtime libraries for oneAPI DPC++
util.hpp
Go to the documentation of this file.
1 //===-- util.hpp - Shared SYCL runtime utilities interface -----*- C++ -*--===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #ifndef __SYCL_DEVICE_ONLY
12 
13 #include <sycl/detail/defines.hpp>
14 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
15 #include <sycl/detail/string.hpp>
16 #endif
17 #include <cstring>
18 #include <mutex>
19 #include <vector>
20 
21 namespace sycl {
22 inline namespace _V1 {
23 namespace detail {
24 
26 class Sync {
27 public:
30  static std::mutex &getGlobalLock() { return getInstance().GlobalLock; }
31 
32 private:
33  static Sync &getInstance();
34  std::mutex GlobalLock;
35 };
36 
37 // TempAssignGuard is the class for a guard object that will assign some OTHER
38 // variable to a temporary value but restore it when the guard itself goes out
39 // of scope.
40 template <typename T> struct TempAssignGuard {
41  T &field;
43  TempAssignGuard(T &fld, T tempVal) : field(fld), restoreValue(fld) {
44  field = tempVal;
45  }
47 };
48 
49 // const char* key hash for STL maps
50 struct HashCStr {
51  size_t operator()(const char *S) const {
52  constexpr size_t Prime = 31;
53  size_t Res = 0;
54  char Ch = 0;
55 
56  for (; (Ch = *S); S++) {
57  Res += Ch + (Prime * Res);
58  }
59  return Res;
60  }
61 };
62 
63 // const char* key comparison for STL maps
64 struct CmpCStr {
65  bool operator()(const char *A, const char *B) const {
66  return std::strcmp(A, B) == 0;
67  }
68 };
69 
70 using SerializedObj = std::vector<unsigned char>;
71 
72 #ifdef __INTEL_PREVIEW_BREAKING_CHANGES
73 template <typename T> struct ABINeutralT { using type = T; };
74 // We need special handling of std::string to handle ABI incompatibility
75 // for get_info<>() when it returns std::string and vector<std::string>.
76 // For this purpose, get_info_impl<>() is created to handle special
77 // cases, and it is only called internally and not exposed to the user.
78 // The following ReturnType structure is intended for general return type,
79 // and special return types (std::string and vector of it).
80 
81 template <> struct ABINeutralT<std::string> { using type = detail::string; };
82 
83 template <> struct ABINeutralT<std::vector<std::string>> {
84  using type = std::vector<detail::string>;
85 };
86 
87 template <typename T> using ABINeutralT_t = typename ABINeutralT<T>::type;
88 #else
89 template <typename T> using ABINeutralT_t = T;
90 #endif
91 
92 } // namespace detail
93 } // namespace _V1
94 } // namespace sycl
95 
96 #endif //__SYCL_DEVICE_ONLY
Groups and provides access to all the locks used the SYCL runtime.
Definition: util.hpp:26
static std::mutex & getGlobalLock()
Retuns a reference to the global lock.
Definition: util.hpp:30
std::string string
Definition: handler.hpp:426
std::vector< unsigned char > SerializedObj
Definition: util.hpp:70
Definition: access.hpp:18
bool operator()(const char *A, const char *B) const
Definition: util.hpp:65
size_t operator()(const char *S) const
Definition: util.hpp:51
TempAssignGuard(T &fld, T tempVal)
Definition: util.hpp:43