Skip to content

Commit 3276ac8

Browse files
committed
[SYCL] Guard access to the cache of device lib programs
We cache device library programs (like fallback assertion) in the context. In multi-threaded applications simultaneous access to the cache of device lib programs is possible in program_manager::build(). That's why access to the this cache needs to be guarded to avoid data race.
1 parent 584e890 commit 3276ac8

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

sycl/source/detail/context_impl.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ class context_impl {
140140
/// reference.
141141
const std::vector<device> &getDevices() const { return MDevices; }
142142

143+
using CachedLibProgramsT =
144+
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>;
145+
143146
/// In contrast to user programs, which are compiled from user code, library
144147
/// programs come from the SYCL runtime. They are identified by the
145148
/// corresponding extension:
@@ -151,10 +154,10 @@ class context_impl {
151154
/// See `doc/design/DeviceLibExtensions.rst' for
152155
/// more details.
153156
///
154-
/// \returns a map with device library programs.
155-
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram> &
156-
getCachedLibPrograms() {
157-
return MCachedLibPrograms;
157+
/// \returns an instance of sycl::detail::Locked which wraps a map with device
158+
/// library programs and the corresponding lock for synchronized access.
159+
Locked<CachedLibProgramsT> acquireCachedLibPrograms() {
160+
return {MCachedLibPrograms, MCachedLibProgramsMutex};
158161
}
159162

160163
KernelProgramCache &getKernelProgramCache() const;
@@ -179,8 +182,8 @@ class context_impl {
179182
PlatformImplPtr MPlatform;
180183
property_list MPropList;
181184
bool MHostContext;
182-
std::map<std::pair<DeviceLibExt, RT::PiDevice>, RT::PiProgram>
183-
MCachedLibPrograms;
185+
CachedLibProgramsT MCachedLibPrograms;
186+
std::mutex MCachedLibProgramsMutex;
184187
mutable KernelProgramCache MKernelProgramCache;
185188
mutable PropertySupport MSupportBufferLocationByDevices;
186189
};

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ RT::PiProgram ProgramManager::getBuiltPIProgram(
568568
ProgramPtr BuiltProgram =
569569
build(std::move(ProgramManaged), ContextImpl, CompileOpts, LinkOpts,
570570
getRawSyclObjImpl(Device)->getHandleRef(),
571-
ContextImpl->getCachedLibPrograms(), DeviceLibReqMask);
571+
ContextImpl->acquireCachedLibPrograms().get(), DeviceLibReqMask);
572572

573573
emitBuiltProgramInfo(BuiltProgram.get(), ContextImpl);
574574

@@ -1900,7 +1900,7 @@ device_image_plain ProgramManager::build(const device_image_plain &DeviceImage,
19001900
ProgramPtr BuiltProgram =
19011901
build(std::move(ProgramManaged), ContextImpl, CompileOpts, LinkOpts,
19021902
getRawSyclObjImpl(Devs[0])->getHandleRef(),
1903-
ContextImpl->getCachedLibPrograms(), DeviceLibReqMask);
1903+
ContextImpl->acquireCachedLibPrograms().get(), DeviceLibReqMask);
19041904

19051905
emitBuiltProgramInfo(BuiltProgram.get(), ContextImpl);
19061906

0 commit comments

Comments
 (0)