defaults.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cstddef>
9 #include <cstdlib>
10 #include <initializer_list>
11 #include <sstream>
12 #include <stdexcept>
13 #include <string>
14 #include <type_traits>
15 
16 #include <kvikio/compat_mode.hpp>
17 #include <kvikio/error.hpp>
18 #include <kvikio/http_status_codes.hpp>
19 #include <kvikio/shim/cufile.hpp>
20 #include <kvikio/threadpool_wrapper.hpp>
21 
25 namespace kvikio {
26 
27 template <typename T>
28 T getenv_or(std::string_view env_var_name, T default_val)
29 {
30  auto const* env_val = std::getenv(env_var_name.data());
31  if (env_val == nullptr) { return default_val; }
32 
33  std::stringstream sstream(env_val);
34  T converted_val;
35  sstream >> converted_val;
36 
37  if constexpr (!std::is_same_v<T, std::string>) {
38  KVIKIO_EXPECT(!sstream.fail(),
39  "unknown config value " + std::string{env_var_name} + "=" + std::string{env_val},
40  std::invalid_argument);
41  }
42 
43  return converted_val;
44 }
45 
46 template <>
47 bool getenv_or(std::string_view env_var_name, bool default_val);
48 
49 template <>
50 CompatMode getenv_or(std::string_view env_var_name, CompatMode default_val);
51 
52 template <>
53 std::vector<int> getenv_or(std::string_view env_var_name, std::vector<int> default_val);
54 
76 template <typename T>
77 std::tuple<std::string_view, T, bool> getenv_or(
78  std::initializer_list<std::string_view> env_var_names, T default_val)
79 {
80  KVIKIO_EXPECT(env_var_names.size() > 0,
81  "`env_var_names` must contain at least one environment variable name.",
82  std::invalid_argument);
83  std::string_view env_name_target;
84  std::string_view env_val_target;
85 
86  for (auto const& env_var_name : env_var_names) {
87  auto const* env_val = std::getenv(env_var_name.data());
88  if (env_val == nullptr) { continue; }
89 
90  if (!env_name_target.empty() && env_val_target != env_val) {
91  std::stringstream ss;
92  ss << "Environment variable " << env_var_name << " (" << env_val
93  << ") has already been set by its alias " << env_name_target << " (" << env_val_target
94  << ") with a different value.";
95  KVIKIO_FAIL(ss.str(), std::invalid_argument);
96  }
97 
98  env_name_target = env_var_name;
99  env_val_target = env_val;
100  }
101 
102  if (env_name_target.empty()) { return {env_name_target, default_val, false}; }
103 
104  auto res = getenv_or<T>(env_name_target, default_val);
105  return {env_name_target, res, true};
106 }
107 
112 class defaults {
113  private:
114  BS_thread_pool _thread_pool{get_num_threads_from_env()};
115  CompatMode _compat_mode;
116  std::size_t _task_size;
117  std::size_t _gds_threshold;
118  std::size_t _bounce_buffer_size;
119  std::size_t _http_max_attempts;
120  long _http_timeout;
121  std::vector<int> _http_status_codes;
122 
123  static unsigned int get_num_threads_from_env();
124 
125  defaults();
126 
127  KVIKIO_EXPORT static defaults* instance();
128 
129  public:
148  [[nodiscard]] static CompatMode compat_mode();
149 
159 
170 
187 
203 
213  [[nodiscard]] static BS_thread_pool& thread_pool();
214 
223  [[nodiscard]] static unsigned int thread_pool_nthreads();
224 
233  static void set_thread_pool_nthreads(unsigned int nthreads);
234 
240  [[nodiscard]] static unsigned int num_threads();
241 
247  static void set_num_threads(unsigned int nthreads);
248 
257  [[nodiscard]] static std::size_t task_size();
258 
264  static void set_task_size(std::size_t nbytes);
265 
277  [[nodiscard]] static std::size_t gds_threshold();
278 
283  static void set_gds_threshold(std::size_t nbytes);
284 
293  [[nodiscard]] static std::size_t bounce_buffer_size();
294 
300  static void set_bounce_buffer_size(std::size_t nbytes);
301 
311  [[nodiscard]] static std::size_t http_max_attempts();
312 
318  static void set_http_max_attempts(std::size_t attempts);
319 
328  [[nodiscard]] static long http_timeout();
329 
335  static void set_http_timeout(long timeout_seconds);
336 
351  [[nodiscard]] static std::vector<int> const& http_status_codes();
352 
358  static void set_http_status_codes(std::vector<int> status_codes);
359 };
360 
361 } // namespace kvikio
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:112
static CompatMode infer_compat_mode_if_auto(CompatMode compat_mode) noexcept
Infer the AUTO compatibility mode from the system runtime.
static std::size_t task_size()
Get the default task size used for parallel IO operations.
static std::vector< int > const & http_status_codes()
The list of HTTP status codes to retry.
static void set_task_size(std::size_t nbytes)
Set the default task size used for parallel IO operations.
static void set_num_threads(unsigned int nthreads)
Alias of set_thread_pool_nthreads
static bool is_compat_mode_preferred()
Whether the global compatibility mode from class defaults is expected to be ON.
static void set_http_status_codes(std::vector< int > status_codes)
Set the list of HTTP status codes to retry.
static void set_compat_mode(CompatMode compat_mode)
Set the value of kvikio::defaults::compat_mode().
static void set_gds_threshold(std::size_t nbytes)
Set the default GDS threshold, which is the minimum size to use GDS (in bytes).
static unsigned int num_threads()
Alias of thread_pool_nthreads
static void set_http_timeout(long timeout_seconds)
Reset the http timeout.
static bool is_compat_mode_preferred(CompatMode compat_mode) noexcept
Given a requested compatibility mode, whether it is expected to reduce to ON.
static void set_thread_pool_nthreads(unsigned int nthreads)
Set the number of threads in the default thread pool. Waits for all currently running tasks to be com...
static std::size_t http_max_attempts()
Get the maximum number of attempts per remote IO read.
static BS_thread_pool & thread_pool()
Get the default thread pool.
static long http_timeout()
The maximum time, in seconds, the transfer is allowed to complete.
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 std::size_t bounce_buffer_size()
Get the size of the bounce buffer used to stage data in host memory.
static void set_http_max_attempts(std::size_t attempts)
Set the maximum number of attempts per remote IO read.
static void set_bounce_buffer_size(std::size_t nbytes)
Set 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.
#define KVIKIO_EXPECT(...)
Macro for checking pre-conditions or conditions that throws an exception when a condition is violated...
Definition: error.hpp:205
#define KVIKIO_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:241
KvikIO namespace.
Definition: batch.hpp:16
CompatMode
I/O compatibility mode.
Definition: compat_mode.hpp:15