27 inline namespace _V1 {
30 #ifdef DISABLE_CONFIG_FROM_ENV
34 #endif // DISABLE_CONFIG_FROM_ENV
36 #ifdef DISABLE_CONFIG_FROM_CONFIG_FILE
40 #endif // DISABLE_CONFIG_FROM_CONFIG_FILE
42 #ifdef DISABLE_CONFIG_FROM_COMPILE_TIME
46 #endif // 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;
236 const std::array<std::pair<std::string, info::device_type>, 6> &
241 const std::array<std::pair<std::string, backend>, 8> &
getSyclBeMap();
252 static bool Initialized =
false;
256 return DeviceTargets;
258 const char *ValStr = BaseT::getRawValue();
264 return DeviceTargets;
278 static bool Initialized =
false;
287 const char *ValStr = BaseT::getRawValue();
291 <<
"\nWARNING: The enviroment variable SYCL_DEVICE_FILTER"
292 " is deprecated. Please use ONEAPI_DEVICE_SELECTOR instead.\n"
293 "For more details, please refer to:\n"
294 "https://github.com/intel/llvm/blob/sycl/sycl/doc/"
295 "EnvironmentVariables.md#oneapi_device_selector\n\n";
316 constexpr
bool DefaultValue =
false;
318 constexpr
bool DefaultValue =
true;
321 const char *ValStr = getCachedValue();
326 return ValStr[0] ==
'1';
329 static void reset() { (void)getCachedValue(
true); }
332 (void)getCachedValue(
true, Val);
335 static const char *
getName() {
return BaseT::MConfigName; }
338 static const char *getCachedValue(
bool ResetCache =
false,
339 const char *Val =
nullptr) {
340 static const char *ValStr = BaseT::getRawValue();
342 ValStr = (Val !=
nullptr) ? Val : BaseT::getRawValue();
353 static int Value = [] {
354 const char *ValueStr = BaseT::getRawValue();
360 Result = std::stoi(ValueStr);
362 throw invalid_parameter_error(
363 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
364 "variable: value should be a number",
365 PI_ERROR_INVALID_VALUE);
369 throw invalid_parameter_error(
370 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
371 "variable: value should be larger than zero",
372 PI_ERROR_INVALID_VALUE);
385 static constexpr
bool Default =
false;
387 static bool get() {
return getCachedValue(); }
389 static void reset() { (void)getCachedValue(
true); }
391 static const char *
getName() {
return BaseT::MConfigName; }
394 static bool parseValue() {
399 <<
" environment variable is deprecated "
400 <<
"and has no effect. By default, persistent device code caching is "
401 << (Default ?
"enabled." :
"disabled.") <<
" Use " <<
getName()
402 <<
"=1/0 to enable/disable.\n";
405 const char *ValStr = BaseT::getRawValue();
408 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
410 std::string{
"Invalid value for bool configuration variable "} +
411 getName() + std::string{
": "} + ValStr;
412 throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
414 return ValStr[0] ==
'1';
417 static bool getCachedValue(
bool ResetCache =
false) {
418 static bool Val = parseValue();
429 static std::string
get() {
return getCachedValue(); }
431 static void reset() { (void)getCachedValue(
true); }
433 static const char *
getName() {
return BaseT::MConfigName; }
438 static std::string parseValue() {
439 const char *RootDir = BaseT::getRawValue();
443 constexpr
char DeviceCodeCacheDir[] =
"/libsycl_cache";
445 #if defined(__SYCL_RT_OS_LINUX)
446 const char *CacheDir = std::getenv(
"XDG_CACHE_HOME");
447 const char *HomeDir = std::getenv(
"HOME");
448 if (!CacheDir && !HomeDir)
451 std::string(CacheDir ? CacheDir : (std::string(HomeDir) +
"/.cache")) +
454 const char *AppDataDir = std::getenv(
"AppData");
457 std::string Res{std::string(AppDataDir) + DeviceCodeCacheDir};
462 static std::string getCachedValue(
bool ResetCache =
false) {
463 static std::string Val = parseValue();
470 template <>
class SYCLConfig<SYCL_REDUCTION_PREFERRED_WORKGROUP_SIZE> {
476 size_t Accelerator = 0;
481 ParsedValue Value = getCachedValue();
482 return getRefByDeviceType(Value, DeviceType);
485 static void reset() { (void)getCachedValue(
true); }
487 static const char *
getName() {
return BaseT::MConfigName; }
490 static size_t &getRefByDeviceType(ParsedValue &Value,
492 switch (DeviceType) {
498 return Value.Accelerator;
503 BaseT,
"Device types must be \"cpu\", \"gpu\", \"acc\", or \"*\".");
507 static ParsedValue parseValue() {
508 const char *ValueRaw = BaseT::getRawValue();
509 ParsedValue Result{};
515 std::string ValueStr{ValueRaw};
519 size_t Start = 0, End = 0;
521 End = ValueStr.find(
',', Start);
522 if (End == std::string::npos)
523 End = ValueStr.size();
526 std::string DeviceConfigStr = ValueStr.substr(Start, End - Start);
529 size_t ConfigDelimLoc = DeviceConfigStr.find(
':');
530 if (ConfigDelimLoc == std::string::npos)
532 BaseT,
"Device-value pair \"" + DeviceConfigStr +
533 "\" does not contain the ':' delimiter.");
536 std::string DeviceConfigTypeStr =
537 DeviceConfigStr.substr(0, ConfigDelimLoc);
538 std::string DeviceConfigValueStr = DeviceConfigStr.substr(
539 ConfigDelimLoc + 1, DeviceConfigStr.size() - ConfigDelimLoc - 1);
542 auto DeviceTypeIter = std::find_if(
543 std::begin(DeviceTypeMap), std::end(DeviceTypeMap),
544 [&](
auto Element) {
return DeviceConfigTypeStr == Element.first; });
545 if (DeviceTypeIter == DeviceTypeMap.end())
548 "\"" + DeviceConfigTypeStr +
"\" is not a recognized device type.");
551 int DeviceConfigValue = 1;
553 DeviceConfigValue = std::stoi(DeviceConfigValueStr);
556 BaseT,
"Value \"" + DeviceConfigValueStr +
"\" must be a number");
559 if (DeviceConfigValue < 1)
561 "Value \"" + DeviceConfigValueStr +
562 "\" must be larger than zero");
566 Result.GPU = DeviceConfigValue;
567 Result.CPU = DeviceConfigValue;
568 Result.Accelerator = DeviceConfigValue;
571 getRefByDeviceType(Result, DeviceTypeIter->second) = DeviceConfigValue;
577 }
while (Start < ValueStr.size());
581 static ParsedValue getCachedValue(
bool ResetCache =
false) {
582 static ParsedValue Val = parseValue();
594 constexpr
bool DefaultValue =
true;
596 const char *ValStr = getCachedValue();
601 return ValStr[0] ==
'1';
604 static void reset() { (void)getCachedValue(
true); }
606 static const char *
getName() {
return BaseT::MConfigName; }
609 static const char *getCachedValue(
bool ResetCache =
false) {
610 static const char *ValStr = BaseT::getRawValue();
612 ValStr = BaseT::getRawValue();
621 static constexpr
bool Default =
true;
622 static bool get() {
return getCachedValue(); }
623 static const char *
getName() {
return BaseT::MConfigName; }
626 static bool parseValue() {
627 const char *ValStr = BaseT::getRawValue();
630 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
632 std::string{
"Invalid value for bool configuration variable "} +
633 getName() + std::string{
": "} + ValStr;
634 throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
636 return ValStr[0] ==
'1';
639 static bool getCachedValue() {
640 static bool Val = parseValue();
645 #undef INVALID_CONFIG_EXCEPTION