30 #include <BS_thread_pool.hpp>
32 #include <kvikio/shim/cufile.hpp>
61 std::string tmp{compat_mode_str};
63 tmp.begin(), tmp.end(), tmp.begin(), [](
unsigned char c) { return std::tolower(c); });
66 if (tmp ==
"on" || tmp ==
"true" || tmp ==
"yes" || tmp ==
"1") {
68 }
else if (tmp ==
"off" || tmp ==
"false" || tmp ==
"no" || tmp ==
"0") {
69 res = CompatMode::OFF;
70 }
else if (tmp ==
"auto") {
71 res = CompatMode::AUTO;
73 throw std::invalid_argument(
"Unknown compatibility mode: " + std::string{tmp});
79 T getenv_or(std::string_view env_var_name, T default_val)
81 const auto* env_val = std::getenv(env_var_name.data());
82 if (env_val ==
nullptr) {
return default_val; }
84 std::stringstream sstream(env_val);
86 sstream >> converted_val;
88 throw std::invalid_argument(
"unknown config value " + std::string{env_var_name} +
"=" +
89 std::string{env_val});
95 inline bool getenv_or(std::string_view env_var_name,
bool default_val)
97 const auto* env_val = std::getenv(env_var_name.data());
98 if (env_val ==
nullptr) {
return default_val; }
101 return static_cast<bool>(std::stoi(env_val));
102 }
catch (
const std::invalid_argument&) {
105 std::string str{env_val};
113 str.begin(), str.end(), str.begin(), [](
unsigned char c) { return std::tolower(c); });
115 std::stringstream trimmer;
120 if (str ==
"true" || str ==
"on" || str ==
"yes") {
return true; }
121 if (str ==
"false" || str ==
"off" || str ==
"no") {
return false; }
122 throw std::invalid_argument(
"unknown config value " + std::string{env_var_name} +
"=" +
123 std::string{env_val});
127 inline CompatMode getenv_or(std::string_view env_var_name, CompatMode default_val)
129 auto* env_val = std::getenv(env_var_name.data());
130 if (env_val ==
nullptr) {
return default_val; }
142 BS::thread_pool _thread_pool{get_num_threads_from_env()};
144 std::size_t _task_size;
145 std::size_t _gds_threshold;
146 std::size_t _bounce_buffer_size;
148 static unsigned int get_num_threads_from_env()
150 const int ret = detail::getenv_or(
"KVIKIO_NTHREADS", 1);
152 throw std::invalid_argument(
"KVIKIO_NTHREADS has to be a positive integer greater than zero");
161 _compat_mode = detail::getenv_or(
"KVIKIO_COMPAT_MODE", CompatMode::AUTO);
165 const ssize_t env = detail::getenv_or(
"KVIKIO_TASK_SIZE", 4 * 1024 * 1024);
167 throw std::invalid_argument(
168 "KVIKIO_TASK_SIZE has to be a positive integer greater than zero");
174 const ssize_t env = detail::getenv_or(
"KVIKIO_GDS_THRESHOLD", 1024 * 1024);
176 throw std::invalid_argument(
"KVIKIO_GDS_THRESHOLD has to be a positive integer");
178 _gds_threshold = env;
182 const ssize_t env = detail::getenv_or(
"KVIKIO_BOUNCE_BUFFER_SIZE", 16 * 1024 * 1024);
184 throw std::invalid_argument(
185 "KVIKIO_BOUNCE_BUFFER_SIZE has to be a positive integer greater than zero");
187 _bounce_buffer_size = env;
191 KVIKIO_EXPORT
static defaults* instance()
240 static auto inferred_compat_mode_for_auto = []() ->
CompatMode {
241 return is_cufile_available() ? CompatMode::OFF : CompatMode::ON;
243 return inferred_compat_mode_for_auto;
295 [[nodiscard]]
static BS::thread_pool&
thread_pool() {
return instance()->_thread_pool; }
322 throw std::invalid_argument(
"number of threads must be a positive integer greater than zero");
335 [[nodiscard]]
static std::size_t
task_size() {
return instance()->_task_size; }
345 throw std::invalid_argument(
"task size must be a positive integer greater than zero");
347 instance()->_task_size = nbytes;
361 [[nodiscard]]
static std::size_t
gds_threshold() {
return instance()->_gds_threshold; }
387 throw std::invalid_argument(
388 "size of the bounce buffer must be a positive integer greater than zero");
390 instance()->_bounce_buffer_size = nbytes;
Singleton class of default values used throughout KvikIO.
static std::size_t task_size()
Get the default task size used for parallel IO operations.
static void gds_threshold_reset(std::size_t nbytes)
Reset the default GDS threshold, which is the minimum size to use GDS (in bytes).
static BS::thread_pool & thread_pool()
Get the default thread pool.
static void bounce_buffer_size_reset(std::size_t nbytes)
Reset the size of the bounce buffer used to stage data in host memory.
static bool is_compat_mode_preferred()
Whether the global compatibility mode from class defaults is expected to be ON.
static CompatMode infer_compat_mode_if_auto(CompatMode compat_mode)
Infer the AUTO compatibility mode from the system runtime.
static void task_size_reset(std::size_t nbytes)
Reset the default task size used for parallel IO operations.
static void compat_mode_reset(CompatMode compat_mode)
Reset the value of kvikio::defaults::compat_mode().
static void thread_pool_nthreads_reset(unsigned int nthreads)
Reset the number of threads in the default thread pool. Waits for all currently running tasks to be c...
static std::size_t gds_threshold()
Get the default GDS threshold, which is the minimum size to use GDS (in bytes).
static unsigned int thread_pool_nthreads()
Get the number of threads in the default thread pool.
static bool is_compat_mode_preferred(CompatMode compat_mode)
Given a requested compatibility mode, whether it is expected to reduce to ON.
static std::size_t bounce_buffer_size()
Get the size of the bounce buffer used to stage data in host memory.
static CompatMode compat_mode()
Return whether the KvikIO library is running in compatibility mode or not.
CompatMode
I/O compatibility mode.
CompatMode parse_compat_mode_str(std::string_view compat_mode_str)
Parse a string into a CompatMode enum.