Skip to content

Commit f41f953

Browse files
authored
[SYCL] Optimize creation of global objects in GlobalHandler (#17740)
This PR optimizes global object singleton creation by removing the `getOrCreate()` from the hot path for most of the global objects. - `getOrCreate()` method locks mutex on every call - We cannot just use the C++11 way of creating static singleton variables inside corresponding methods because we need to control the destruction order of global objects. Therefore, we use static variables to cache the result of the `getOrCreate()` method so that it calls only once per global object. - The optimization is not done for the `GlobalHandler::getPlatformToDefaultContextCache` method because there are public methods of the `GlobalHandler` that can set the `MPlatformToDefaultContextCache` back to `nullptr`.
1 parent 03f26a6 commit f41f953

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

sycl/source/detail/global_handler.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ GlobalHandler &GlobalHandler::instance() {
136136
}
137137

138138
template <typename T, typename... Types>
139-
T &GlobalHandler::getOrCreate(InstWithLock<T> &IWL, Types... Args) {
139+
T &GlobalHandler::getOrCreate(InstWithLock<T> &IWL, Types &&...Args) {
140140
const LockGuard Lock{IWL.Lock};
141141

142142
if (!IWL.Inst)
143-
IWL.Inst = std::make_unique<T>(Args...);
143+
IWL.Inst = std::make_unique<T>(std::forward<Types>(Args)...);
144144

145145
return *IWL.Inst;
146146
}
@@ -182,50 +182,67 @@ void GlobalHandler::registerSchedulerUsage(bool ModifyCounter) {
182182
}
183183

184184
ProgramManager &GlobalHandler::getProgramManager() {
185-
return getOrCreate(MProgramManager);
185+
static ProgramManager &PM = getOrCreate(MProgramManager);
186+
return PM;
186187
}
187188

188189
std::unordered_map<PlatformImplPtr, ContextImplPtr> &
189190
GlobalHandler::getPlatformToDefaultContextCache() {
191+
// The optimization with static reference is not done because
192+
// there are public methods of the GlobalHandler
193+
// that can set the MPlatformToDefaultContextCache back to nullptr.
194+
// So one time initialization is not possible and we need
195+
// to call getOrCreate on every access.
190196
return getOrCreate(MPlatformToDefaultContextCache);
191197
}
192198

193199
std::mutex &GlobalHandler::getPlatformToDefaultContextCacheMutex() {
194-
return getOrCreate(MPlatformToDefaultContextCacheMutex);
200+
static std::mutex &PlatformToDefaultContextCacheMutex =
201+
getOrCreate(MPlatformToDefaultContextCacheMutex);
202+
return PlatformToDefaultContextCacheMutex;
195203
}
196204

197-
Sync &GlobalHandler::getSync() { return getOrCreate(MSync); }
205+
Sync &GlobalHandler::getSync() {
206+
static Sync &sync = getOrCreate(MSync);
207+
return sync;
208+
}
198209

199210
std::vector<PlatformImplPtr> &GlobalHandler::getPlatformCache() {
200-
return getOrCreate(MPlatformCache);
211+
static std::vector<PlatformImplPtr> &PlatformCache =
212+
getOrCreate(MPlatformCache);
213+
return PlatformCache;
201214
}
202215

203216
std::mutex &GlobalHandler::getPlatformMapMutex() {
204-
return getOrCreate(MPlatformMapMutex);
217+
static std::mutex &PlatformMapMutex = getOrCreate(MPlatformMapMutex);
218+
return PlatformMapMutex;
205219
}
206220

207221
std::mutex &GlobalHandler::getFilterMutex() {
208-
return getOrCreate(MFilterMutex);
222+
static std::mutex &FilterMutex = getOrCreate(MFilterMutex);
223+
return FilterMutex;
209224
}
210225

211226
std::vector<AdapterPtr> &GlobalHandler::getAdapters() {
227+
static std::vector<AdapterPtr> &adapters = getOrCreate(MAdapters);
212228
enableOnCrashStackPrinting();
213-
return getOrCreate(MAdapters);
229+
return adapters;
214230
}
215231

216232
ods_target_list &
217233
GlobalHandler::getOneapiDeviceSelectorTargets(const std::string &InitValue) {
218-
return getOrCreate(MOneapiDeviceSelectorTargets, InitValue);
234+
static ods_target_list &OneapiDeviceSelectorTargets =
235+
getOrCreate(MOneapiDeviceSelectorTargets, InitValue);
236+
return OneapiDeviceSelectorTargets;
219237
}
220238

221239
XPTIRegistry &GlobalHandler::getXPTIRegistry() {
222240
return getOrCreate(MXPTIRegistry);
223241
}
224242

225243
ThreadPool &GlobalHandler::getHostTaskThreadPool() {
226-
int Size = SYCLConfig<SYCL_QUEUE_THREAD_POOL_SIZE>::get();
227-
ThreadPool &TP = getOrCreate(MHostTaskThreadPool, Size);
228-
244+
static ThreadPool &TP = getOrCreate(
245+
MHostTaskThreadPool, SYCLConfig<SYCL_QUEUE_THREAD_POOL_SIZE>::get());
229246
return TP;
230247
}
231248

sycl/source/detail/global_handler.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class GlobalHandler {
113113
};
114114

115115
template <typename T, typename... Types>
116-
T &getOrCreate(InstWithLock<T> &IWL, Types... Args);
116+
T &getOrCreate(InstWithLock<T> &IWL, Types &&...Args);
117117

118118
InstWithLock<Scheduler> MScheduler;
119119
InstWithLock<ProgramManager> MProgramManager;

0 commit comments

Comments
 (0)