27 inline namespace _V1 {
30 #ifdef DISABLE_CONFIG_FROM_ENV
36 #ifdef DISABLE_CONFIG_FROM_CONFIG_FILE
42 #ifdef DISABLE_CONFIG_FROM_COMPILE_TIME
54 #define CONFIG(name, ...) name,
62 return (Str[0] ==
'_' && Str[1] ==
'_') ? nullptr : Str;
66 void readConfig(
bool ForceInitialization =
false);
70 #define CONFIG(Name, MaxSize, CompileTimeDef) \
71 template <> class SYCLConfigBase<Name> { \
75 static char MStorage[MaxSize + 1]; \
77 static const char *MValueFromFile; \
79 static const char *const MConfigName; \
84 static const char *const MCompileTimeDef; \
86 static const char *getRawValue() { \
87 if (ConfigFromEnvEnabled) \
88 if (const char *ValStr = getenv(MConfigName)) \
91 if (ConfigFromFileEnabled) { \
94 return MValueFromFile; \
97 if (ConfigFromCompileDefEnabled && MCompileTimeDef) \
98 return MCompileTimeDef; \
103 #include "config.def"
106 #define INVALID_CONFIG_EXCEPTION(BASE, MSG) \
107 sycl::exception(sycl::make_error_code(sycl::errc::invalid), \
108 "Invalid value for " + std::string{BASE::MConfigName} + \
109 " environment variable: " + MSG)
115 static const char *
get() {
return getCachedValue(); }
117 static void reset() { (void)getCachedValue(
true); }
119 static const char *
getName() {
return BaseT::MConfigName; }
122 static const char *getCachedValue(
bool ResetCache =
false) {
123 static const char *ValStr = BaseT::getRawValue();
125 ValStr = BaseT::getRawValue();
135 static bool Initialized =
false;
138 static int Level = 0;
145 const char *ValStr = BaseT::getRawValue();
146 Level = (ValStr ? std::atoi(ValStr) : 0);
156 static unsigned int get() {
return getCachedValue(); }
158 static void reset() { (void)getCachedValue(
true); }
161 static unsigned int getCachedValue(
bool ResetCache =
false) {
162 const auto Parser = []() {
163 const char *ValStr = BaseT::getRawValue();
164 int SignedLevel = ValStr ? std::atoi(ValStr) : 0;
165 return SignedLevel >= 0 ? SignedLevel : 0;
168 static unsigned int Level = Parser();
176 template <>
class SYCLConfig<SYCL_PARALLEL_FOR_RANGE_ROUNDING_TRACE> {
181 static const char *ValStr = BaseT::getRawValue();
182 return ValStr !=
nullptr;
186 template <>
class SYCLConfig<SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING> {
191 static const char *ValStr = BaseT::getRawValue();
192 return ValStr !=
nullptr;
196 template <>
class SYCLConfig<SYCL_PARALLEL_FOR_RANGE_ROUNDING_PARAMS> {
203 static const char *RoundParams = BaseT::getRawValue();
204 if (RoundParams ==
nullptr)
207 static bool ProcessedFactors =
false;
211 if (!ProcessedFactors) {
214 std::string Params(RoundParams);
215 size_t Pos = Params.find(
':');
216 if (Pos != std::string::npos) {
217 MF = std::stoi(Params.substr(0, Pos));
218 Params.erase(0, Pos + 1);
219 Pos = Params.find(
':');
220 if (Pos != std::string::npos) {
221 GF = std::stoi(Params.substr(0, Pos));
222 Params.erase(0, Pos + 1);
223 MR = std::stoi(Params);
226 ProcessedFactors =
true;
237 template <
bool supportAcc = false>
238 const std::array<std::pair<std::string, info::device_type>, 6> &
240 static const std::array<std::pair<std::string, info::device_type>, 6>
241 SyclDeviceTypeMap = {
249 return SyclDeviceTypeMap;
254 const std::array<std::pair<std::string, backend>, 7> &
getSyclBeMap();
265 static bool Initialized =
false;
269 return DeviceTargets;
271 const char *ValStr = BaseT::getRawValue();
274 if (ValStr[0] ==
'\0')
276 "Invalid value for ONEAPI_DEVICE_SELECTOR environment "
277 "variable: value should not be null.");
283 return DeviceTargets;
292 constexpr
bool DefaultValue =
true;
294 const char *ValStr = getCachedValue();
299 return ValStr[0] ==
'1';
302 static void reset() { (void)getCachedValue(
true); }
305 (void)getCachedValue(
true, Val);
308 static const char *
getName() {
return BaseT::MConfigName; }
311 static const char *getCachedValue(
bool ResetCache =
false,
312 const char *Val =
nullptr) {
313 static const char *ValStr = BaseT::getRawValue();
315 ValStr = (Val !=
nullptr) ? Val : BaseT::getRawValue();
326 static int Value = [] {
327 const char *ValueStr = BaseT::getRawValue();
333 Result = std::stoi(ValueStr);
336 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE "
337 "environment variable: value should be a number");
343 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
344 "variable: value should be larger than zero");
357 static constexpr
bool Default =
false;
359 static bool get() {
return getCachedValue(); }
361 static void reset() { (void)getCachedValue(
true); }
363 static const char *
getName() {
return BaseT::MConfigName; }
366 static bool parseValue() {
371 <<
" environment variable is deprecated "
372 <<
"and has no effect. By default, persistent device code caching is "
373 << (Default ?
"enabled." :
"disabled.") <<
" Use " <<
getName()
374 <<
"=1/0 to enable/disable.\n";
377 const char *ValStr = BaseT::getRawValue();
380 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
382 std::string{
"Invalid value for bool configuration variable "} +
383 getName() + std::string{
": "} + ValStr;
386 return ValStr[0] ==
'1';
389 static bool getCachedValue(
bool ResetCache =
false) {
390 static bool Val = parseValue();
401 static std::string
get() {
return getCachedValue(); }
403 static void reset() { (void)getCachedValue(
true); }
405 static const char *
getName() {
return BaseT::MConfigName; }
410 static std::string parseValue() {
411 const char *RootDir = BaseT::getRawValue();
415 constexpr
char DeviceCodeCacheDir[] =
"/libsycl_cache";
417 #if defined(__SYCL_RT_OS_LINUX)
418 const char *CacheDir = std::getenv(
"XDG_CACHE_HOME");
419 const char *HomeDir = std::getenv(
"HOME");
420 if (!CacheDir && !HomeDir)
423 std::string(CacheDir ? CacheDir : (std::string(HomeDir) +
"/.cache")) +
426 const char *AppDataDir = std::getenv(
"AppData");
429 std::string Res{std::string(AppDataDir) + DeviceCodeCacheDir};
434 static std::string getCachedValue(
bool ResetCache =
false) {
435 static std::string Val = parseValue();
442 template <>
class SYCLConfig<SYCL_REDUCTION_PREFERRED_WORKGROUP_SIZE> {
448 size_t Accelerator = 0;
453 ParsedValue Value = getCachedValue();
454 return getRefByDeviceType(Value, DeviceType);
457 static void reset() { (void)getCachedValue(
true); }
459 static const char *
getName() {
return BaseT::MConfigName; }
462 static size_t &getRefByDeviceType(ParsedValue &Value,
464 switch (DeviceType) {
470 return Value.Accelerator;
475 BaseT,
"Device types must be \"cpu\", \"gpu\", \"acc\", or \"*\".");
479 static ParsedValue parseValue() {
480 const char *ValueRaw = BaseT::getRawValue();
481 ParsedValue Result{};
487 std::string ValueStr{ValueRaw};
491 size_t Start = 0, End = 0;
493 End = ValueStr.find(
',', Start);
494 if (End == std::string::npos)
495 End = ValueStr.size();
498 std::string DeviceConfigStr = ValueStr.substr(Start, End - Start);
501 size_t ConfigDelimLoc = DeviceConfigStr.find(
':');
502 if (ConfigDelimLoc == std::string::npos)
504 BaseT,
"Device-value pair \"" + DeviceConfigStr +
505 "\" does not contain the ':' delimiter.");
508 std::string DeviceConfigTypeStr =
509 DeviceConfigStr.substr(0, ConfigDelimLoc);
510 std::string DeviceConfigValueStr = DeviceConfigStr.substr(
511 ConfigDelimLoc + 1, DeviceConfigStr.size() - ConfigDelimLoc - 1);
514 auto DeviceTypeIter = std::find_if(
515 std::begin(DeviceTypeMap), std::end(DeviceTypeMap),
516 [&](
auto Element) {
return DeviceConfigTypeStr == Element.first; });
517 if (DeviceTypeIter == DeviceTypeMap.end())
520 "\"" + DeviceConfigTypeStr +
"\" is not a recognized device type.");
523 int DeviceConfigValue = 1;
525 DeviceConfigValue = std::stoi(DeviceConfigValueStr);
528 BaseT,
"Value \"" + DeviceConfigValueStr +
"\" must be a number");
531 if (DeviceConfigValue < 1)
533 "Value \"" + DeviceConfigValueStr +
534 "\" must be larger than zero");
538 Result.GPU = DeviceConfigValue;
539 Result.CPU = DeviceConfigValue;
540 Result.Accelerator = DeviceConfigValue;
543 getRefByDeviceType(Result, DeviceTypeIter->second) = DeviceConfigValue;
549 }
while (Start < ValueStr.size());
553 static ParsedValue getCachedValue(
bool ResetCache =
false) {
554 static ParsedValue Val = parseValue();
566 constexpr
bool DefaultValue =
true;
568 const char *ValStr = getCachedValue();
573 return ValStr[0] ==
'1';
576 static void reset() { (void)getCachedValue(
true); }
578 static const char *
getName() {
return BaseT::MConfigName; }
581 static const char *getCachedValue(
bool ResetCache =
false) {
582 static const char *ValStr = BaseT::getRawValue();
584 ValStr = BaseT::getRawValue();
593 static constexpr
bool Default =
true;
594 static bool get() {
return getCachedValue(); }
595 static const char *
getName() {
return BaseT::MConfigName; }
596 static void reset() { (void)getCachedValue(
true); }
599 static bool parseValue() {
600 const char *ValStr = BaseT::getRawValue();
603 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
605 std::string{
"Invalid value for bool configuration variable "} +
606 getName() + std::string{
": "} + ValStr;
609 return ValStr[0] ==
'1';
612 static bool getCachedValue(
bool ResetCache =
false) {
613 static bool Val = parseValue();
615 Val = BaseT::getRawValue();
626 constexpr
bool DefaultValue =
false;
627 const char *ValStr = getCachedValue();
631 return ValStr[0] ==
'1';
634 static const char *
getName() {
return BaseT::MConfigName; }
637 static const char *getCachedValue(
bool ResetCache =
false) {
638 static const char *ValStr = BaseT::getRawValue();
640 ValStr = BaseT::getRawValue();
645 template <>
class SYCLConfig<SYCL_JIT_AMDGCN_PTX_TARGET_CPU> {
649 static std::string
get() {
650 const std::string DefaultValue{
""};
652 const char *ValStr = getCachedValue();
657 return std::string{ValStr};
660 static void reset() { (void)getCachedValue(
true); }
662 static const char *
getName() {
return BaseT::MConfigName; }
665 static const char *getCachedValue(
bool ResetCache =
false) {
666 static const char *ValStr = BaseT::getRawValue();
668 ValStr = BaseT::getRawValue();
673 template <>
class SYCLConfig<SYCL_JIT_AMDGCN_PTX_TARGET_FEATURES> {
677 static std::string
get() {
678 const std::string DefaultValue{
""};
680 const char *ValStr = getCachedValue();
685 return std::string{ValStr};
688 static void reset() { (void)getCachedValue(
true); }
690 static const char *
getName() {
return BaseT::MConfigName; }
693 static const char *getCachedValue(
bool ResetCache =
false) {
694 static const char *ValStr = BaseT::getRawValue();
696 ValStr = BaseT::getRawValue();
701 #undef INVALID_CONFIG_EXCEPTION
ods_target_list & getOneapiDeviceSelectorTargets(const std::string &InitValue)
static GlobalHandler & instance()
static ods_target_list * get()
static const char * getName()
static const char * getName()
static const char * getName()
static const char * getName()
static void resetWithValue(const char *Val)
static const char * getName()
static const char * getName()
static const char * getName()
static const char * getName()
static void GetSettings(size_t &MinFactor, size_t &GoodFactor, size_t &MinRange)
static const char * getName()
static size_t get(info::device_type DeviceType)
static unsigned int get()
static const char * getName()
static const char * get()
#define INVALID_CONFIG_EXCEPTION(BASE, MSG)
__SYCL_EXTERN_STREAM_ATTRS ostream cerr
Linked to standard error (unbuffered)
constexpr bool ConfigFromEnvEnabled
void readConfig(bool ForceInitialization)
constexpr int MAX_CONFIG_VALUE
const std::array< std::pair< std::string, info::device_type >, 6 > & getSyclDeviceTypeMap()
constexpr const char * getStrOrNullptr(const char *Str)
constexpr bool ConfigFromFileEnabled
const std::array< std::pair< std::string, backend >, 7 > & getSyclBeMap()
constexpr bool ConfigFromCompileDefEnabled
constexpr int MAX_CONFIG_NAME
std::error_code make_error_code(sycl::errc E) noexcept
Constructs an error code using e and sycl_category()
C++ wrapper of extern "C" PI interfaces.