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>, 8> &
getSyclBeMap();
265 static bool Initialized =
false;
269 return DeviceTargets;
271 const char *ValStr = BaseT::getRawValue();
274 if (ValStr[0] ==
'\0')
275 throw invalid_parameter_error(
276 "Invalid value for ONEAPI_DEVICE_SELECTOR environment "
277 "variable: value should not be null.",
278 PI_ERROR_INVALID_VALUE);
284 return DeviceTargets;
293 constexpr
bool DefaultValue =
true;
295 const char *ValStr = getCachedValue();
300 return ValStr[0] ==
'1';
303 static void reset() { (void)getCachedValue(
true); }
306 (void)getCachedValue(
true, Val);
309 static const char *
getName() {
return BaseT::MConfigName; }
312 static const char *getCachedValue(
bool ResetCache =
false,
313 const char *Val =
nullptr) {
314 static const char *ValStr = BaseT::getRawValue();
316 ValStr = (Val !=
nullptr) ? Val : BaseT::getRawValue();
327 static int Value = [] {
328 const char *ValueStr = BaseT::getRawValue();
334 Result = std::stoi(ValueStr);
336 throw invalid_parameter_error(
337 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
338 "variable: value should be a number",
339 PI_ERROR_INVALID_VALUE);
343 throw invalid_parameter_error(
344 "Invalid value for SYCL_QUEUE_THREAD_POOL_SIZE environment "
345 "variable: value should be larger than zero",
346 PI_ERROR_INVALID_VALUE);
359 static constexpr
bool Default =
false;
361 static bool get() {
return getCachedValue(); }
363 static void reset() { (void)getCachedValue(
true); }
365 static const char *
getName() {
return BaseT::MConfigName; }
368 static bool parseValue() {
373 <<
" environment variable is deprecated "
374 <<
"and has no effect. By default, persistent device code caching is "
375 << (Default ?
"enabled." :
"disabled.") <<
" Use " <<
getName()
376 <<
"=1/0 to enable/disable.\n";
379 const char *ValStr = BaseT::getRawValue();
382 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
384 std::string{
"Invalid value for bool configuration variable "} +
385 getName() + std::string{
": "} + ValStr;
386 throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
388 return ValStr[0] ==
'1';
391 static bool getCachedValue(
bool ResetCache =
false) {
392 static bool Val = parseValue();
403 static std::string
get() {
return getCachedValue(); }
405 static void reset() { (void)getCachedValue(
true); }
407 static const char *
getName() {
return BaseT::MConfigName; }
412 static std::string parseValue() {
413 const char *RootDir = BaseT::getRawValue();
417 constexpr
char DeviceCodeCacheDir[] =
"/libsycl_cache";
419 #if defined(__SYCL_RT_OS_LINUX)
420 const char *CacheDir = std::getenv(
"XDG_CACHE_HOME");
421 const char *HomeDir = std::getenv(
"HOME");
422 if (!CacheDir && !HomeDir)
425 std::string(CacheDir ? CacheDir : (std::string(HomeDir) +
"/.cache")) +
428 const char *AppDataDir = std::getenv(
"AppData");
431 std::string Res{std::string(AppDataDir) + DeviceCodeCacheDir};
436 static std::string getCachedValue(
bool ResetCache =
false) {
437 static std::string Val = parseValue();
444 template <>
class SYCLConfig<SYCL_REDUCTION_PREFERRED_WORKGROUP_SIZE> {
450 size_t Accelerator = 0;
455 ParsedValue Value = getCachedValue();
456 return getRefByDeviceType(Value, DeviceType);
459 static void reset() { (void)getCachedValue(
true); }
461 static const char *
getName() {
return BaseT::MConfigName; }
464 static size_t &getRefByDeviceType(ParsedValue &Value,
466 switch (DeviceType) {
472 return Value.Accelerator;
477 BaseT,
"Device types must be \"cpu\", \"gpu\", \"acc\", or \"*\".");
481 static ParsedValue parseValue() {
482 const char *ValueRaw = BaseT::getRawValue();
483 ParsedValue Result{};
489 std::string ValueStr{ValueRaw};
493 size_t Start = 0, End = 0;
495 End = ValueStr.find(
',', Start);
496 if (End == std::string::npos)
497 End = ValueStr.size();
500 std::string DeviceConfigStr = ValueStr.substr(Start, End - Start);
503 size_t ConfigDelimLoc = DeviceConfigStr.find(
':');
504 if (ConfigDelimLoc == std::string::npos)
506 BaseT,
"Device-value pair \"" + DeviceConfigStr +
507 "\" does not contain the ':' delimiter.");
510 std::string DeviceConfigTypeStr =
511 DeviceConfigStr.substr(0, ConfigDelimLoc);
512 std::string DeviceConfigValueStr = DeviceConfigStr.substr(
513 ConfigDelimLoc + 1, DeviceConfigStr.size() - ConfigDelimLoc - 1);
516 auto DeviceTypeIter = std::find_if(
517 std::begin(DeviceTypeMap), std::end(DeviceTypeMap),
518 [&](
auto Element) {
return DeviceConfigTypeStr == Element.first; });
519 if (DeviceTypeIter == DeviceTypeMap.end())
522 "\"" + DeviceConfigTypeStr +
"\" is not a recognized device type.");
525 int DeviceConfigValue = 1;
527 DeviceConfigValue = std::stoi(DeviceConfigValueStr);
530 BaseT,
"Value \"" + DeviceConfigValueStr +
"\" must be a number");
533 if (DeviceConfigValue < 1)
535 "Value \"" + DeviceConfigValueStr +
536 "\" must be larger than zero");
540 Result.GPU = DeviceConfigValue;
541 Result.CPU = DeviceConfigValue;
542 Result.Accelerator = DeviceConfigValue;
545 getRefByDeviceType(Result, DeviceTypeIter->second) = DeviceConfigValue;
551 }
while (Start < ValueStr.size());
555 static ParsedValue getCachedValue(
bool ResetCache =
false) {
556 static ParsedValue Val = parseValue();
568 constexpr
bool DefaultValue =
true;
570 const char *ValStr = getCachedValue();
575 return ValStr[0] ==
'1';
578 static void reset() { (void)getCachedValue(
true); }
580 static const char *
getName() {
return BaseT::MConfigName; }
583 static const char *getCachedValue(
bool ResetCache =
false) {
584 static const char *ValStr = BaseT::getRawValue();
586 ValStr = BaseT::getRawValue();
595 static constexpr
bool Default =
true;
596 static bool get() {
return getCachedValue(); }
597 static const char *
getName() {
return BaseT::MConfigName; }
600 static bool parseValue() {
601 const char *ValStr = BaseT::getRawValue();
604 if (strlen(ValStr) != 1 || (ValStr[0] !=
'0' && ValStr[0] !=
'1')) {
606 std::string{
"Invalid value for bool configuration variable "} +
607 getName() + std::string{
": "} + ValStr;
608 throw runtime_error(Msg, PI_ERROR_INVALID_OPERATION);
610 return ValStr[0] ==
'1';
613 static bool getCachedValue() {
614 static bool Val = parseValue();
619 #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 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)
const std::array< std::pair< std::string, backend >, 8 > & getSyclBeMap()
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
constexpr bool ConfigFromCompileDefEnabled
constexpr int MAX_CONFIG_NAME
C++ wrapper of extern "C" PI interfaces.