Skip to content

Commit c3ec15a

Browse files
committed
[SYCL] optimize enqueueImpKernel by making trace faster
1 parent ceae49b commit c3ec15a

File tree

6 files changed

+91
-95
lines changed

6 files changed

+91
-95
lines changed

sycl/source/detail/config.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,34 @@ const std::array<std::pair<std::string, backend>, 8> &getSyclBeMap() {
180180
{"*", backend::all}}};
181181
return SyclBeMap;
182182
}
183+
namespace {
184+
185+
unsigned int parseLevel(const char *ValStr) {
186+
unsigned int intVal = 0;
187+
188+
if (ValStr) {
189+
try {
190+
intVal = std::stoul(ValStr);
191+
} catch (...) {
192+
// If the value is not null and not a number, it is considered
193+
// to enable disk cache tracing. This is the legacy behavior.
194+
intVal = 1;
195+
}
196+
}
197+
198+
// Legacy behavior.
199+
if (intVal > 7)
200+
intVal = 1;
201+
202+
return intVal;
203+
}
204+
205+
} // namespace
206+
207+
void SYCLConfigTrace::reset() { Level = parseLevel(BaseT::getRawValue()); }
208+
209+
unsigned int SYCLConfigTrace::Level =
210+
parseLevel(SYCLConfigTrace::BaseT::getRawValue());
183211

184212
} // namespace detail
185213
} // namespace _V1

sycl/source/detail/config.hpp

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -709,52 +709,19 @@ template <> class SYCLConfig<SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES> {
709709
// tracing of the corresponding caches. If the input value is not null and
710710
// not a valid number, the disk cache tracing will be enabled (depreciated
711711
// behavior). The default value is 0 and no tracing is enabled.
712-
template <> class SYCLConfig<SYCL_CACHE_TRACE> {
712+
class SYCLConfigTrace {
713713
using BaseT = SYCLConfigBase<SYCL_CACHE_TRACE>;
714714
enum TraceBitmask { DiskCache = 1, InMemCache = 2, KernelCompiler = 4 };
715715

716716
public:
717-
static unsigned int get() { return getCachedValue(); }
718-
static void reset() { (void)getCachedValue(true); }
719-
static bool isTraceDiskCache() {
720-
return getCachedValue() & TraceBitmask::DiskCache;
721-
}
722-
static bool isTraceInMemCache() {
723-
return getCachedValue() & TraceBitmask::InMemCache;
724-
}
725-
static bool isTraceKernelCompiler() {
726-
return getCachedValue() & TraceBitmask::KernelCompiler;
727-
}
717+
static unsigned int get() { return Level; }
718+
static void reset();
719+
static bool isTraceDiskCache() { return Level & DiskCache; }
720+
static bool isTraceInMemCache() { return Level & InMemCache; }
721+
static bool isTraceKernelCompiler() { return Level & KernelCompiler; }
728722

729723
private:
730-
static unsigned int getCachedValue(bool ResetCache = false) {
731-
const auto Parser = []() {
732-
const char *ValStr = BaseT::getRawValue();
733-
int intVal = 0;
734-
735-
if (ValStr) {
736-
try {
737-
intVal = std::stoi(ValStr);
738-
} catch (...) {
739-
// If the value is not null and not a number, it is considered
740-
// to enable disk cache tracing. This is the legacy behavior.
741-
intVal = 1;
742-
}
743-
}
744-
745-
// Legacy behavior.
746-
if (intVal > 7)
747-
intVal = 1;
748-
749-
return intVal;
750-
};
751-
752-
static unsigned int Level = Parser();
753-
if (ResetCache)
754-
Level = Parser();
755-
756-
return Level;
757-
}
724+
static unsigned int Level;
758725
};
759726

760727
// SYCL_IN_MEM_CACHE_EVICTION_THRESHOLD accepts an integer that specifies

sycl/source/detail/kernel_program_cache.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
namespace sycl {
1313
inline namespace _V1 {
1414
namespace detail {
15+
16+
void KernelProgramCache::traceKernelImpl(const char *Msg,
17+
KernelNameStrRefT KernelName,
18+
bool IsFastKernelCache) {
19+
std::string Identifier =
20+
"[IsFastCache: " + std::to_string(IsFastKernelCache) +
21+
"][Key:{Name = " + KernelName.data() + "}]: ";
22+
23+
std::cerr << "[In-Memory Cache][Thread Id:" << std::this_thread::get_id()
24+
<< "][Kernel Cache]" << Identifier << Msg << std::endl;
25+
}
26+
1527
adapter_impl &KernelProgramCache::getAdapter() {
1628
return MParentContext.getAdapter();
1729
}

sycl/source/detail/kernel_program_cache.hpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ class KernelProgramCache {
331331
template <typename MsgType>
332332
static inline void traceProgram(const MsgType &Msg,
333333
const ProgramCacheKeyT &CacheKey) {
334-
if (!SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache())
334+
if (!SYCLConfigTrace::isTraceInMemCache())
335335
return;
336336

337337
int ImageId = CacheKey.first.second;
@@ -361,21 +361,15 @@ class KernelProgramCache {
361361
<< "][Program Cache]" << Identifier << Msg << std::endl;
362362
}
363363

364+
static void traceKernelImpl(const char *Msg, KernelNameStrRefT KernelName,
365+
bool IsFastKernelCache);
366+
364367
// Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is
365368
// set.
366-
template <typename MsgType>
367-
static inline void traceKernel(const MsgType &Msg,
368-
KernelNameStrRefT KernelName,
369-
bool IsFastKernelCache = false) {
370-
if (!SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache())
371-
return;
372-
373-
std::string Identifier =
374-
"[IsFastCache: " + std::to_string(IsFastKernelCache) +
375-
"][Key:{Name = " + KernelName.data() + "}]: ";
376-
377-
std::cerr << "[In-Memory Cache][Thread Id:" << std::this_thread::get_id()
378-
<< "][Kernel Cache]" << Identifier << Msg << std::endl;
369+
static void traceKernel(const char *Msg, KernelNameStrRefT KernelName,
370+
bool isFastKernelCache = false) {
371+
if (__builtin_expect(SYCLConfigTrace::isTraceInMemCache(), false))
372+
traceKernelImpl(Msg, KernelName, isFastKernelCache);
379373
}
380374

381375
Locked<ProgramCache> acquireCachedPrograms() {
@@ -513,7 +507,7 @@ class KernelProgramCache {
513507
auto LockedCacheKP = acquireKernelsPerProgramCache();
514508
// List kernels that are to be removed from the cache, if tracing is
515509
// enabled.
516-
if (SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache()) {
510+
if (SYCLConfigTrace::isTraceInMemCache()) {
517511
for (const auto &Kernel : LockedCacheKP.get()[NativePrg])
518512
traceKernel("Kernel evicted.", Kernel.first);
519513
}

sycl/source/detail/persistent_device_code_cache.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,7 @@ class PersistentDeviceCodeCache {
222222

223223
/* Sends message to std:cerr stream when SYCL_CACHE_TRACE environemnt is set*/
224224
static void trace(const std::string &msg, const std::string &path = "") {
225-
static const bool traceEnabled =
226-
SYCLConfig<SYCL_CACHE_TRACE>::isTraceDiskCache();
225+
static const bool traceEnabled = SYCLConfigTrace::isTraceDiskCache();
227226
if (traceEnabled) {
228227
auto outputPath = path;
229228
std::replace(outputPath.begin(), outputPath.end(), '\\', '/');
@@ -232,8 +231,7 @@ class PersistentDeviceCodeCache {
232231
}
233232
static void trace_KernelCompiler(const std::string &msg,
234233
const std::string &path = "") {
235-
static const bool traceEnabled =
236-
SYCLConfig<SYCL_CACHE_TRACE>::isTraceKernelCompiler();
234+
static const bool traceEnabled = SYCLConfigTrace::isTraceKernelCompiler();
237235
if (traceEnabled) {
238236
auto outputPath = path;
239237
std::replace(outputPath.begin(), outputPath.end(), '\\', '/');

sycl/unittests/config/ConfigTests.cpp

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
2828
File.close();
2929
}
3030
try {
31-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
32-
throw std::logic_error("sycl::exception didn't throw");
31+
sycl::detail::readConfig(true);
32+
throw std::logic_error("sycl::exception didn't throw 1");
3333
} catch (sycl::exception &e) {
3434
EXPECT_EQ(
3535
std::string(
@@ -46,8 +46,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
4646
File.close();
4747
}
4848
try {
49-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
50-
throw std::logic_error("sycl::exception didn't throw");
49+
sycl::detail::readConfig(true);
50+
throw std::logic_error("sycl::exception didn't throw 2");
5151
} catch (sycl::exception &e) {
5252
EXPECT_EQ(
5353
std::string(
@@ -64,8 +64,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
6464
File.close();
6565
}
6666
try {
67-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
68-
throw std::logic_error("sycl::exception didn't throw");
67+
sycl::detail::readConfig(true);
68+
throw std::logic_error("sycl::exception didn't throw 3");
6969
} catch (sycl::exception &e) {
7070
EXPECT_EQ(
7171
std::string(
@@ -82,8 +82,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
8282
File.close();
8383
}
8484
try {
85-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
86-
throw std::logic_error("sycl::exception didn't throw");
85+
sycl::detail::readConfig(true);
86+
throw std::logic_error("sycl::exception didn't throw 4");
8787
} catch (sycl::exception &e) {
8888
EXPECT_EQ(
8989
std::string(
@@ -103,8 +103,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
103103
File.close();
104104
}
105105
try {
106-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
107-
throw std::logic_error("sycl::exception didn't throw");
106+
sycl::detail::readConfig(true);
107+
throw std::logic_error("sycl::exception didn't throw 5");
108108
} catch (sycl::exception &e) {
109109
EXPECT_TRUE(std::regex_match(
110110
e.what(),
@@ -121,8 +121,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
121121
File.close();
122122
}
123123
try {
124-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
125-
throw std::logic_error("sycl::exception didn't throw");
124+
sycl::detail::readConfig(true);
125+
throw std::logic_error("sycl::exception didn't throw 6");
126126
} catch (sycl::exception &e) {
127127
EXPECT_TRUE(std::regex_match(
128128
e.what(), std::regex("Variable name is more than ([\\d]+) or less "
@@ -142,8 +142,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
142142
File.close();
143143
}
144144
try {
145-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
146-
throw std::logic_error("sycl::exception didn't throw");
145+
sycl::detail::readConfig(true);
146+
throw std::logic_error("sycl::exception didn't throw 7");
147147
} catch (sycl::exception &e) {
148148
EXPECT_TRUE(std::regex_match(
149149
e.what(), std::regex("The value contains more than ([\\d]+) characters "
@@ -159,8 +159,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
159159
File.close();
160160
}
161161
try {
162-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
163-
throw std::logic_error("sycl::exception didn't throw");
162+
sycl::detail::readConfig(true);
163+
throw std::logic_error("sycl::exception didn't throw 8");
164164
} catch (sycl::exception &e) {
165165
EXPECT_TRUE(std::regex_match(
166166
e.what(), std::regex("The value contains more than ([\\d]+) characters "
@@ -176,8 +176,8 @@ TEST(ConfigTests, CheckConfigProcessing) {
176176
File.close();
177177
}
178178
try {
179-
sycl::detail::SYCLConfig<sycl::detail::SYCL_DEVICE_ALLOWLIST>::get();
180-
throw std::logic_error("sycl::exception didn't throw");
179+
sycl::detail::readConfig(true);
180+
throw std::logic_error("sycl::exception didn't throw 9");
181181
} catch (sycl::exception &e) {
182182
EXPECT_TRUE(std::regex_match(
183183
e.what(), std::regex("The value contains more than ([\\d]+) characters "
@@ -249,20 +249,17 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) {
249249
// Lambda to test parsing of SYCL_CACHE_TRACE
250250
auto TestConfig = [](int expectedValue, int expectedDiskCache,
251251
int expectedInMemCache, int expectedKernelCompiler) {
252-
EXPECT_EQ(static_cast<unsigned int>(expectedValue),
253-
SYCLConfig<SYCL_CACHE_TRACE>::get());
252+
EXPECT_EQ(static_cast<unsigned int>(expectedValue), SYCLConfigTrace::get());
254253

255254
EXPECT_EQ(
256255
expectedDiskCache,
257-
static_cast<int>(
258-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::isTraceDiskCache()));
256+
static_cast<int>(sycl::detail::SYCLConfigTrace::isTraceDiskCache()));
259257
EXPECT_EQ(
260258
expectedInMemCache,
261-
static_cast<int>(
262-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::isTraceInMemCache()));
259+
static_cast<int>(sycl::detail::SYCLConfigTrace::isTraceInMemCache()));
263260
EXPECT_EQ(expectedKernelCompiler,
264-
static_cast<int>(sycl::detail::SYCLConfig<
265-
SYCL_CACHE_TRACE>::isTraceKernelCompiler()));
261+
static_cast<int>(
262+
sycl::detail::SYCLConfigTrace::isTraceKernelCompiler()));
266263
};
267264

268265
// Lambda to set SYCL_CACHE_TRACE
@@ -279,40 +276,40 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) {
279276
TestConfig(0, 0, 0, 0);
280277

281278
SetSyclCacheTraceEnv("1");
282-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
279+
sycl::detail::SYCLConfigTrace::reset();
283280
TestConfig(1, 1, 0, 0);
284281

285282
SetSyclCacheTraceEnv("2");
286-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
283+
sycl::detail::SYCLConfigTrace::reset();
287284
TestConfig(2, 0, 1, 0);
288285

289286
SetSyclCacheTraceEnv("3");
290-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
287+
sycl::detail::SYCLConfigTrace::reset();
291288
TestConfig(3, 1, 1, 0);
292289

293290
SetSyclCacheTraceEnv("4");
294-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
291+
sycl::detail::SYCLConfigTrace::reset();
295292
TestConfig(4, 0, 0, 1);
296293

297294
SetSyclCacheTraceEnv("5");
298-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
295+
sycl::detail::SYCLConfigTrace::reset();
299296
TestConfig(5, 1, 0, 1);
300297

301298
SetSyclCacheTraceEnv("6");
302-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
299+
sycl::detail::SYCLConfigTrace::reset();
303300
TestConfig(6, 0, 1, 1);
304301

305302
SetSyclCacheTraceEnv("7");
306-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
303+
sycl::detail::SYCLConfigTrace::reset();
307304
TestConfig(7, 1, 1, 1);
308305

309306
SetSyclCacheTraceEnv("8");
310-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
307+
sycl::detail::SYCLConfigTrace::reset();
311308
TestConfig(1, 1, 0, 0);
312309

313310
// Set random non-null value. It should default to 1.
314311
SetSyclCacheTraceEnv("random");
315-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
312+
sycl::detail::SYCLConfigTrace::reset();
316313
TestConfig(1, 1, 0, 0);
317314

318315
// When SYCL_CACHE_TRACE is not set, it should default to 0.
@@ -321,7 +318,7 @@ TEST(ConfigTests, CheckSyclCacheTraceTest) {
321318
#else
322319
unsetenv("SYCL_CACHE_TRACE");
323320
#endif
324-
sycl::detail::SYCLConfig<SYCL_CACHE_TRACE>::reset();
321+
sycl::detail::SYCLConfigTrace::reset();
325322
TestConfig(0, 0, 0, 0);
326323
}
327324

0 commit comments

Comments
 (0)