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 #include <sycl/detail/string.hpp>
15 
16 #include <cstring>
17 #include <mutex>
18 #include <vector>
19 
20 namespace sycl {
21 inline namespace _V1 {
22 namespace detail {
23 
25 class Sync {
26 public:
29  static std::mutex &getGlobalLock() { return getInstance().GlobalLock; }
30 
31 private:
32  static Sync &getInstance();
33  std::mutex GlobalLock;
34 };
35 
36 // TempAssignGuard is the class for a guard object that will assign some OTHER
37 // variable to a temporary value but restore it when the guard itself goes out
38 // of scope.
39 template <typename T> struct TempAssignGuard {
40  T &field;
42  TempAssignGuard(T &fld, T tempVal) : field(fld), restoreValue(fld) {
43  field = tempVal;
44  }
46 };
47 
48 // const char* key hash for STL maps
49 struct HashCStr {
50  size_t operator()(const char *S) const {
51  constexpr size_t Prime = 31;
52  size_t Res = 0;
53  char Ch = 0;
54 
55  for (; (Ch = *S); S++) {
56  Res += Ch + (Prime * Res);
57  }
58  return Res;
59  }
60 };
61 
62 // const char* key comparison for STL maps
63 struct CmpCStr {
64  bool operator()(const char *A, const char *B) const {
65  return std::strcmp(A, B) == 0;
66  }
67 };
68 
69 using SerializedObj = std::vector<unsigned char>;
70 
71 template <typename T> struct ABINeutralT { using type = T; };
72 // We need special handling of std::string to handle ABI incompatibility
73 // for get_info<>() when it returns std::string and vector<std::string>.
74 // For this purpose, get_info_impl<>() is created to handle special
75 // cases, and it is only called internally and not exposed to the user.
76 // The following ReturnType structure is intended for general return type,
77 // and special return types (std::string and vector of it).
78 
79 template <> struct ABINeutralT<std::string> { using type = detail::string; };
80 
81 template <> struct ABINeutralT<std::vector<std::string>> {
82  using type = std::vector<detail::string>;
83 };
84 
85 template <typename T> using ABINeutralT_t = typename ABINeutralT<T>::type;
86 } // namespace detail
87 } // namespace _V1
88 } // namespace sycl
89 
90 #endif //__SYCL_DEVICE_ONLY
Groups and provides access to all the locks used the SYCL runtime.
Definition: util.hpp:25
static std::mutex & getGlobalLock()
Retuns a reference to the global lock.
Definition: util.hpp:29
typename ABINeutralT< T >::type ABINeutralT_t
Definition: util.hpp:85
std::vector< unsigned char > SerializedObj
Definition: util.hpp:69
Definition: access.hpp:18
bool operator()(const char *A, const char *B) const
Definition: util.hpp:64
size_t operator()(const char *S) const
Definition: util.hpp:50
TempAssignGuard(T &fld, T tempVal)
Definition: util.hpp:42