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 
47 public:
52  static GlobalHandler &instance();
53 
54  GlobalHandler(const GlobalHandler &) = delete;
56 
57  Scheduler &getScheduler();
58  ProgramManager &getProgramManager();
59  Sync &getSync();
60  std::vector<PlatformImplPtr> &getPlatformCache();
61 
62  std::unordered_map<PlatformImplPtr, ContextImplPtr> &
63  getPlatformToDefaultContextCache();
64 
65  std::mutex &getPlatformToDefaultContextCacheMutex();
66  std::mutex &getPlatformMapMutex();
67  std::mutex &getFilterMutex();
68  std::vector<plugin> &getPlugins();
69  device_filter_list &getDeviceFilterList(const std::string &InitValue);
70  ods_target_list &getOneapiDeviceSelectorTargets(const std::string &InitValue);
71  XPTIRegistry &getXPTIRegistry();
72  ThreadPool &getHostTaskThreadPool();
73 
74  static void registerDefaultContextReleaseHandler();
75 
76  void unloadPlugins();
77 
78 private:
79  friend void releaseDefaultContexts();
80  friend void shutdown();
81 
82  // Constructor and destructor are declared out-of-line to allow incomplete
83  // types as template arguments to unique_ptr.
84  GlobalHandler();
85  ~GlobalHandler();
86 
87  template <typename T> struct InstWithLock {
88  std::unique_ptr<T> Inst;
89  SpinLock Lock;
90  };
91 
92  template <typename T, typename... Types>
93  T &getOrCreate(InstWithLock<T> &IWL, Types... Args);
94 
95  InstWithLock<Scheduler> MScheduler;
96  InstWithLock<ProgramManager> MProgramManager;
97  InstWithLock<Sync> MSync;
98  InstWithLock<std::vector<PlatformImplPtr>> MPlatformCache;
99  InstWithLock<std::unordered_map<PlatformImplPtr, ContextImplPtr>>
100  MPlatformToDefaultContextCache;
101  InstWithLock<std::mutex> MPlatformToDefaultContextCacheMutex;
102  InstWithLock<std::mutex> MPlatformMapMutex;
103  InstWithLock<std::mutex> MFilterMutex;
104  InstWithLock<std::vector<plugin>> MPlugins;
105  InstWithLock<device_filter_list> MDeviceFilterList;
106  InstWithLock<ods_target_list> MOneapiDeviceSelectorTargets;
107  InstWithLock<XPTIRegistry> MXPTIRegistry;
108  // Thread pool for host task and event callbacks execution
109  InstWithLock<ThreadPool> MHostTaskThreadPool;
110 };
111 } // namespace detail
112 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
113 } // namespace sycl
Wrapper class for global data structures with non-trivial destructors.
GlobalHandler(const GlobalHandler &)=delete
GlobalHandler(GlobalHandler &&)=delete
DPC++ graph scheduler class.
Definition: scheduler.hpp:358
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
#define __SYCL_INLINE_VER_NAMESPACE(X)
std::shared_ptr< sycl::detail::context_impl > ContextImplPtr
Definition: event_impl.hpp:30
std::shared_ptr< detail::platform_impl > PlatformImplPtr
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14