DPC++ Runtime
Runtime libraries for oneAPI DPC++
global_handler.hpp
Go to the documentation of this file.
1 //==--------- global_handler.hpp --- Global objects handler ----------------==//
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 #include <sycl/detail/spinlock.hpp>
12 #include <sycl/detail/util.hpp>
13 
14 #include <memory>
15 #include <unordered_map>
16 
17 namespace sycl {
19 namespace detail {
20 class platform_impl;
21 class context_impl;
22 class Scheduler;
23 class ProgramManager;
24 class Sync;
25 class plugin;
26 class device_filter_list;
27 class ods_target_list;
28 class XPTIRegistry;
29 class ThreadPool;
30 
31 using PlatformImplPtr = std::shared_ptr<platform_impl>;
32 using ContextImplPtr = std::shared_ptr<context_impl>;
33 using PluginPtr = std::shared_ptr<plugin>;
34 
48 public:
53  static GlobalHandler &instance();
54 
55  GlobalHandler(const GlobalHandler &) = delete;
56  GlobalHandler(GlobalHandler &&) = delete;
57 
58  void registerSchedulerUsage(bool ModifyCounter = true);
59  Scheduler &getScheduler();
60  ProgramManager &getProgramManager();
61  Sync &getSync();
62  std::vector<PlatformImplPtr> &getPlatformCache();
63 
64  std::unordered_map<PlatformImplPtr, ContextImplPtr> &
65  getPlatformToDefaultContextCache();
66 
67  std::mutex &getPlatformToDefaultContextCacheMutex();
68  std::mutex &getPlatformMapMutex();
69  std::mutex &getFilterMutex();
70  std::vector<PluginPtr> &getPlugins();
71  device_filter_list &getDeviceFilterList(const std::string &InitValue);
72  ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
73  XPTIRegistry &getXPTIRegistry();
74  ThreadPool &getHostTaskThreadPool();
75 
76  static void registerDefaultContextReleaseHandler();
77 
78  void unloadPlugins();
79  void releaseDefaultContexts();
80  void drainThreadPool();
81  void prepareSchedulerToRelease();
82 
83  void InitXPTI();
84  void TraceEventXPTI(const char *Message);
85 
86  // For testing purposes only
87  void attachScheduler(Scheduler *Scheduler);
88 
89 private:
90 #ifdef XPTI_ENABLE_INSTRUMENTATION
91  void *GSYCLCallEvent = nullptr;
92 #endif
93 
94  friend void shutdown();
95  friend class ObjectUsageCounter;
96  static GlobalHandler *&getInstancePtr();
97  static SpinLock MSyclGlobalHandlerProtector;
98 
99  // Constructor and destructor are declared out-of-line to allow incomplete
100  // types as template arguments to unique_ptr.
101  GlobalHandler();
102  ~GlobalHandler();
103 
104  template <typename T> struct InstWithLock {
105  std::unique_ptr<T> Inst;
106  SpinLock Lock;
107  };
108 
109  template <typename T, typename... Types>
110  T &getOrCreate(InstWithLock<T> &IWL, Types... Args);
111 
112  InstWithLock<Scheduler> MScheduler;
113  InstWithLock<ProgramManager> MProgramManager;
114  InstWithLock<Sync> MSync;
115  InstWithLock<std::vector<PlatformImplPtr>> MPlatformCache;
116  InstWithLock<std::unordered_map<PlatformImplPtr, ContextImplPtr>>
117  MPlatformToDefaultContextCache;
118  InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
119  InstWithLock<std::mutex> MPlatformMapMutex;
120  InstWithLock<std::mutex> MFilterMutex;
121  InstWithLock<std::vector<PluginPtr>> MPlugins;
122  InstWithLock<device_filter_list> MDeviceFilterList;
123  InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
124  InstWithLock<XPTIRegistry> MXPTIRegistry;
125  // Thread pool for host task and event callbacks execution
126  InstWithLock<ThreadPool> MHostTaskThreadPool;
127 };
128 } // namespace detail
129 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
130 } // namespace sycl
sycl::_V1::detail::ContextImplPtr
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
sycl::_V1::detail::PlatformImplPtr
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: context_impl.hpp:32
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::detail::XPTIRegistry
Definition: xpti_registry.hpp:59
sycl::_V1::detail::SpinLock
SpinLock is a synchronization primitive, that uses atomic variable and causes thread trying acquire l...
Definition: spinlock.hpp:27
sycl::_V1::detail::ObjectUsageCounter
Definition: global_handler.cpp:45
sycl::_V1::detail::shutdown
void shutdown()
Definition: global_handler.cpp:299
spinlock.hpp
sycl::_V1::detail::Sync
Groups and provides access to all the locks used the SYCL runtime.
Definition: util.hpp:24
sycl::_V1::detail::device_filter_list
Definition: device_filter.hpp:84
sycl::_V1::detail::ods_target_list
Definition: device_filter.hpp:55
sycl::_V1::detail::GlobalHandler
Wrapper class for global data structures with non-trivial destructors.
Definition: global_handler.hpp:47
sycl::_V1::detail::ThreadPool
Definition: thread_pool.hpp:25
util.hpp
sycl::_V1::detail::ProgramManager
Definition: program_manager.hpp:84
sycl::_V1::detail::Scheduler
DPC++ graph scheduler class.
Definition: scheduler.hpp:363
sycl::_V1::detail::PluginPtr
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:47