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 {
18 inline namespace _V1 {
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 ods_target_list;
27 class XPTIRegistry;
28 class ThreadPool;
29 
30 using PlatformImplPtr = std::shared_ptr<platform_impl>;
31 using ContextImplPtr = std::shared_ptr<context_impl>;
32 using PluginPtr = std::shared_ptr<plugin>;
33 
47 public:
52  static GlobalHandler &instance();
53 
54  GlobalHandler(const GlobalHandler &) = delete;
57 
58  void registerSchedulerUsage(bool ModifyCounter = true);
60  bool isSchedulerAlive() const;
62  Sync &getSync();
63  std::vector<PlatformImplPtr> &getPlatformCache();
64 
65  std::unordered_map<PlatformImplPtr, ContextImplPtr> &
67 
69  std::mutex &getPlatformMapMutex();
70  std::mutex &getFilterMutex();
71  std::vector<PluginPtr> &getPlugins();
72  ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
75 
76  static void registerEarlyShutdownHandler();
77 
78  bool isOkToDefer() const;
79  void endDeferredRelease();
80  void unloadPlugins();
82  void drainThreadPool();
83  void prepareSchedulerToRelease(bool Blocking);
84 
85  void InitXPTI();
86  void TraceEventXPTI(const char *Message);
87 
88  // For testing purposes only
90 
91 private:
92 #ifdef XPTI_ENABLE_INSTRUMENTATION
93  void *GSYCLCallEvent = nullptr;
94 #endif
95 
96  bool OkToDefer = true;
97 
98  friend void shutdown_win();
99  friend void shutdown_early();
100  friend void shutdown_late();
101  friend class ObjectUsageCounter;
102  static GlobalHandler *&getInstancePtr();
103  static SpinLock MSyclGlobalHandlerProtector;
104 
105  // Constructor and destructor are declared out-of-line to allow incomplete
106  // types as template arguments to unique_ptr.
107  GlobalHandler();
108  ~GlobalHandler();
109 
110  template <typename T> struct InstWithLock {
111  std::unique_ptr<T> Inst;
112  SpinLock Lock;
113  };
114 
115  template <typename T, typename... Types>
116  T &getOrCreate(InstWithLock<T> &IWL, Types... Args);
117 
118  InstWithLock<Scheduler> MScheduler;
119  InstWithLock<ProgramManager> MProgramManager;
120  InstWithLock<Sync> MSync;
121  InstWithLock<std::vector<PlatformImplPtr>> MPlatformCache;
122  InstWithLock<std::unordered_map<PlatformImplPtr, ContextImplPtr>>
123  MPlatformToDefaultContextCache;
124  InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
125  InstWithLock<std::mutex> MPlatformMapMutex;
126  InstWithLock<std::mutex> MFilterMutex;
127  InstWithLock<std::vector<PluginPtr>> MPlugins;
128  InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
129  InstWithLock<XPTIRegistry> MXPTIRegistry;
130  // Thread pool for host task and event callbacks execution
131  InstWithLock<ThreadPool> MHostTaskThreadPool;
132 };
133 } // namespace detail
134 } // namespace _V1
135 } // namespace sycl
Wrapper class for global data structures with non-trivial destructors.
GlobalHandler(const GlobalHandler &)=delete
ods_target_list & getOneapiDeviceSelectorTargets(const std::string &InitValue)
GlobalHandler & operator=(const GlobalHandler &)=delete
std::vector< PlatformImplPtr > & getPlatformCache()
std::unordered_map< PlatformImplPtr, ContextImplPtr > & getPlatformToDefaultContextCache()
std::vector< PluginPtr > & getPlugins()
GlobalHandler(GlobalHandler &&)=delete
void prepareSchedulerToRelease(bool Blocking)
void TraceEventXPTI(const char *Message)
void attachScheduler(Scheduler *Scheduler)
void registerSchedulerUsage(bool ModifyCounter=true)
std::mutex & getPlatformToDefaultContextCacheMutex()
static GlobalHandler & instance()
DPC++ graph scheduler class.
Definition: scheduler.hpp:366
SpinLock is a synchronization primitive, that uses atomic variable and causes thread trying acquire l...
Definition: spinlock.hpp:27
Groups and provides access to all the locks used the SYCL runtime.
Definition: util.hpp:25
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:32
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:47
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: access.hpp:18