29 #ifdef DISABLE_CONFIG_FROM_ENV
33 #endif // DISABLE_CONFIG_FROM_ENV
35 #ifdef DISABLE_CONFIG_FROM_CONFIG_FILE
39 #endif // DISABLE_CONFIG_FROM_CONFIG_FILE
41 #ifdef DISABLE_CONFIG_FROM_COMPILE_TIME
45 #endif // DISABLE_CONFIG_FROM_COMPILE_TIME
53 #define CONFIG(name, ...) name,
61 return (Str[0] ==
'_' && Str[1] ==
'_') ? nullptr : Str;
65 void readConfig(
bool ForceInitialization =
false);
69 #define CONFIG(Name, MaxSize, CompileTimeDef) \
70 template <> class SYCLConfigBase<Name> { \
74 static char MStorage[MaxSize + 1]; \
76 static const char *MValueFromFile; \
78 static const char *const MConfigName; \
83 static const char *const MCompileTimeDef; \
85 static const char *getRawValue() { \
86 if (ConfigFromEnvEnabled) \
87 if (const char *ValStr = getenv(MConfigName)) \
90 if (ConfigFromFileEnabled) { \
93 return MValueFromFile; \
96 if (ConfigFromCompileDefEnabled && MCompileTimeDef) \
97 return MCompileTimeDef; \
102 #include "config.def"
105 #define INVALID_CONFIG_EXCEPTION(BASE, MSG) \
106 sycl::exception(sycl::make_error_code(sycl::errc::invalid), \
107 "Invalid value for " + std::string{BASE::MConfigName} + \
108 " environment variable: " + MSG)
114 static const char *
get() {
return getCachedValue(); }
116 static void reset() { (void)getCachedValue(
true); }
118 static const char *
getName() {
return BaseT::MConfigName; }
121 static const char *getCachedValue(
bool ResetCache =
false) {
122 static const char *ValStr = BaseT::getRawValue();
124 ValStr = BaseT::getRawValue();
134 static bool Initialized =
false;
137 static int Level = 0;
144 const char *ValStr = BaseT::getRawValue();
145 Level = (ValStr ? std::atoi(ValStr) : 0);
155 static unsigned int get() {
return getCachedValue(); }
157 static void reset() { (void)getCachedValue(
true); }
160 static unsigned int getCachedValue(
bool ResetCache =
false) {
161 const auto Parser = []() {
162 const char *ValStr = BaseT::getRawValue();
163 int SignedLevel = ValStr ? std::atoi(ValStr) : 0;
164 return SignedLevel >= 0 ? SignedLevel : 0;
167 static unsigned int Level = Parser();
175 template <>
class SYCLConfig<SYCL_PARALLEL_FOR_RANGE_ROUNDING_TRACE> {
180 static const char *ValStr = BaseT::getRawValue();
181 return ValStr !=
nullptr;
185 template <>
class SYCLConfig<SYCL_DISABLE_PARALLEL_FOR_RANGE_ROUNDING> {
190 static const char *ValStr = BaseT::getRawValue();
191 return ValStr !=
nullptr;
195 template <>
class SYCLConfig<SYCL_PARALLEL_FOR_RANGE_ROUNDING_PARAMS> {
202 static const char *RoundParams = BaseT::getRawValue();
203 if (RoundParams ==
nullptr)
206 static bool ProcessedFactors =
false;
210 if (!ProcessedFactors) {
213 std::string Params(RoundParams);
214 size_t Pos = Params.find(
':');
215 if (Pos != std::string::npos) {
216 MF = std::stoi(Params.substr(0, Pos));
217 Params.erase(0, Pos + 1);
218 Pos = Params.find(
':');
219 if (Pos != std::string::npos) {
220 GF = std::stoi(Params.substr(0, Pos));
221 Params.erase(0, Pos + 1);
222 MR = std::stoi(Params);
225 ProcessedFactors =
true;
235 const std::array<std::pair<std::string, info::device_type>, 6> &
240 const std::array<std::pair<std::string, backend>, 8> &
getSyclBeMap();
251 static bool Initialized =
false;
255 return DeviceTargets;
257 const char *ValStr = BaseT::getRawValue();
260 &GlobalHandler::instance().getOneapiDeviceSelectorTargets(ValStr);
263 return DeviceTargets;
277 static bool Initialized =
false;
286 const char *ValStr = BaseT::getRawValue();
290 <<
"\nWARNING: The enviroment variable SYCL_DEVICE_FILTER"
291 " is deprecated. Please use ONEAPI_DEVICE_SELECTOR instead.\n"
292 "For more details, please refer to:\n"
293 "https://github.com/intel/llvm/blob/sycl/sycl/doc/"
294 "EnvironmentVariables.md#oneapi_device_selector\n\n";
296 FilterList = &GlobalHandler::instance().getDeviceFilterList(ValStr);
315 constexpr
bool DefaultValue =
false;
317 constexpr
bool DefaultValue =
true;
320 const char *ValStr = getCachedValue();
325 return ValStr[0] ==
'1';
328 static void reset() { (void)getCachedValue(
true); }
330 static const char *
getName() {
return BaseT::MConfigName; }
333 static const char *getCachedValue(
bool ResetCache =
false) {
334 static const char *ValStr = BaseT::getRawValue();
336 ValStr = BaseT::getRawValue();
346 static int Value = [] {
347 const char *ValueStr = BaseT::getRawValue();
353 Result = std::stoi(ValueStr);
355 throw invalid_parameter_error(
356 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
357 "variable: value should be a number",
358 PI_ERROR_INVALID_VALUE);
362 throw invalid_parameter_error(
363 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
364 "variable: value should be larger than zero",
365 PI_ERROR_INVALID_VALUE);
378 static constexpr
bool Default =
false;
380 static bool get() {
return getCachedValue(); }
382 static void reset() { (void)getCachedValue(
true); }
384 static const char *
getName() {
return BaseT::MConfigName; }
387 static bool parseValue() {
392 <<
" environment variable is deprecated "
393 <<
"and has no effect. By default, persistent device code caching is "
394 << (Default ?
"enabled." :
"disabled.") <<
" Use " << getName()
395 <<
"=1/0 to enable/disable.\n";
398 const char *ValStr = BaseT::getRawValue();
401 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
403 std::string{
"Invalid value for bool configuration variable "} +
404 getName() + std::string{
": "} + ValStr;
405 throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
407 return ValStr[0] ==
'1';
410 static bool getCachedValue(
bool ResetCache =
false) {
411 static bool Val = parseValue();
422 static std::string
get() {
return getCachedValue(); }
424 static void reset() { (void)getCachedValue(
true); }
426 static const char *
getName() {
return BaseT::MConfigName; }
431 static std::string parseValue() {
432 const char *RootDir = BaseT::getRawValue();
436 constexpr
char DeviceCodeCacheDir[] =
"/libsycl_cache";
438 #if defined(__SYCL_RT_OS_LINUX)
439 const char *CacheDir = std::getenv(
"XDG_CACHE_HOME");
440 const char *HomeDir = std::getenv(
"HOME");
441 if (!CacheDir && !HomeDir)
444 std::string(CacheDir ? CacheDir : (std::string(HomeDir) +
"/.cache")) +
447 const char *AppDataDir = std::getenv(
"AppData");
450 std::string Res{std::string(AppDataDir) + DeviceCodeCacheDir};
455 static std::string getCachedValue(
bool ResetCache =
false) {
456 static std::string Val = parseValue();
463 template <>
class SYCLConfig<SYCL_REDUCTION_PREFERRED_WORKGROUP_SIZE> {
469 size_t Accelerator = 0;
474 ParsedValue Value = getCachedValue();
475 return getRefByDeviceType(Value, DeviceType);
478 static void reset() { (void)getCachedValue(
true); }
480 static const char *
getName() {
return BaseT::MConfigName; }
483 static size_t &getRefByDeviceType(ParsedValue &Value,
485 switch (DeviceType) {
486 case info::device_type::cpu:
488 case info::device_type::gpu:
490 case info::device_type::accelerator:
491 return Value.Accelerator;
496 BaseT,
"Device types must be \"cpu\", \"gpu\", \"acc\", or \"*\".");
500 static ParsedValue parseValue() {
501 const char *ValueRaw = BaseT::getRawValue();
502 ParsedValue Result{};
508 std::string ValueStr{ValueRaw};
512 size_t Start = 0, End = 0;
514 End = ValueStr.find(
',', Start);
515 if (End == std::string::npos)
516 End = ValueStr.size();
519 std::string DeviceConfigStr = ValueStr.substr(Start, End - Start);
522 size_t ConfigDelimLoc = DeviceConfigStr.find(
':');
523 if (ConfigDelimLoc == std::string::npos)
525 BaseT,
"Device-value pair \"" + DeviceConfigStr +
526 "\" does not contain the ':' delimiter.");
529 std::string DeviceConfigTypeStr =
530 DeviceConfigStr.substr(0, ConfigDelimLoc);
531 std::string DeviceConfigValueStr = DeviceConfigStr.substr(
532 ConfigDelimLoc + 1, DeviceConfigStr.size() - ConfigDelimLoc - 1);
535 auto DeviceTypeIter = std::find_if(
536 std::begin(DeviceTypeMap), std::end(DeviceTypeMap),
537 [&](
auto Element) {
return DeviceConfigTypeStr == Element.first; });
538 if (DeviceTypeIter == DeviceTypeMap.end())
541 "\"" + DeviceConfigTypeStr +
"\" is not a recognized device type.");
544 int DeviceConfigValue = 1;
546 DeviceConfigValue = std::stoi(DeviceConfigValueStr);
549 BaseT,
"Value \"" + DeviceConfigValueStr +
"\" must be a number");
552 if (DeviceConfigValue < 1)
554 "Value \"" + DeviceConfigValueStr +
555 "\" must be larger than zero");
557 if (DeviceTypeIter->second == info::device_type::all) {
559 Result.GPU = DeviceConfigValue;
560 Result.CPU = DeviceConfigValue;
561 Result.Accelerator = DeviceConfigValue;
564 getRefByDeviceType(Result, DeviceTypeIter->second) = DeviceConfigValue;
570 }
while (Start < ValueStr.size());
574 static ParsedValue getCachedValue(
bool ResetCache =
false) {
575 static ParsedValue Val = parseValue();
587 constexpr
bool DefaultValue =
true;
589 const char *ValStr = getCachedValue();
594 return ValStr[0] ==
'1';
597 static void reset() { (void)getCachedValue(
true); }
599 static const char *
getName() {
return BaseT::MConfigName; }
602 static const char *getCachedValue(
bool ResetCache =
false) {
603 static const char *ValStr = BaseT::getRawValue();
605 ValStr = BaseT::getRawValue();
610 #undef INVALID_CONFIG_EXCEPTION