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 #ifndef __INTEL_PREVIEW_BREAKING_CHANGES
27 class device_filter_list;
28 #endif
29 class ods_target_list;
30 class XPTIRegistry;
31 class ThreadPool;
32 
33 using PlatformImplPtr = std::shared_ptr<platform_impl>;
34 using ContextImplPtr = std::shared_ptr<context_impl>;
35 using PluginPtr = std::shared_ptr<plugin>;
36 
50 public:
55  static GlobalHandler &instance();
56 
57  GlobalHandler(const GlobalHandler &) = delete;
60 
61  void registerSchedulerUsage(bool ModifyCounter = true);
63  bool isSchedulerAlive() const;
65  Sync &getSync();
66  std::vector<PlatformImplPtr> &getPlatformCache();
67 
68  std::unordered_map<PlatformImplPtr, ContextImplPtr> &
70 
72  std::mutex &getPlatformMapMutex();
73  std::mutex &getFilterMutex();
74  std::vector<PluginPtr> &getPlugins();
75 #ifndef __INTEL_PREVIEW_BREAKING_CHANGES
76  device_filter_list &getDeviceFilterList(const std::string &InitValue);
77 #endif
78  ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
81 
83 
84  void unloadPlugins();
86  void drainThreadPool();
87  void prepareSchedulerToRelease(bool Blocking);
88 
89  void InitXPTI();
90  void TraceEventXPTI(const char *Message);
91 
92  // For testing purposes only
94 
95 private:
96 #ifdef XPTI_ENABLE_INSTRUMENTATION
97  void *GSYCLCallEvent = nullptr;
98 #endif
99 
100  friend void shutdown();
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 #ifndef __INTEL_PREVIEW_BREAKING_CHANGES
129  InstWithLock<device_filter_list> MDeviceFilterList;
130 #endif
131  InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
132  InstWithLock<XPTIRegistry> MXPTIRegistry;
133  // Thread pool for host task and event callbacks execution
134  InstWithLock<ThreadPool> MHostTaskThreadPool;
135 };
136 } // namespace detail
137 } // namespace _V1
138 } // namespace sycl
Wrapper class for global data structures with non-trivial destructors.
GlobalHandler(const GlobalHandler &)=delete
ods_target_list & getOneapiDeviceSelectorTargets(const std::string &InitValue)
device_filter_list & getDeviceFilterList(const std::string &InitValue)
static void registerDefaultContextReleaseHandler()
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:363
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:24
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:33
std::shared_ptr< plugin > PluginPtr
Definition: pi.hpp:48
std::shared_ptr< detail::platform_impl > PlatformImplPtr
Definition: access.hpp:18