defaults.hpp
1 /*
2  * Copyright (c) 2022-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cstddef>
20 #include <cstdlib>
21 #include <initializer_list>
22 #include <sstream>
23 #include <stdexcept>
24 #include <string>
25 #include <type_traits>
26 
27 #include <kvikio/compat_mode.hpp>
28 #include <kvikio/error.hpp>
29 #include <kvikio/http_status_codes.hpp>
30 #include <kvikio/shim/cufile.hpp>
31 #include <kvikio/threadpool_wrapper.hpp>
32 
36 namespace kvikio {
37 
38 template <typename T>
39 T getenv_or(std::string_view env_var_name, T default_val)
40 {
41  auto const* env_val = std::getenv(env_var_name.data());
42  if (env_val == nullptr) { return default_val; }
43 
44  std::stringstream sstream(env_val);
45  T converted_val;
46  sstream >> converted_val;
47 
48  if constexpr (!std::is_same_v<T, std::string>) {
49  KVIKIO_EXPECT(!sstream.fail(),
50  "unknown config value " + std::string{env_var_name} + "=" + std::string{env_val},
51  std::invalid_argument);
52  }
53 
54  return converted_val;
55 }
56 
57 template <>
58 bool getenv_or(std::string_view env_var_name, bool default_val);
59 
60 template <>
61 CompatMode getenv_or(std::string_view env_var_name, CompatMode default_val);
62 
63 template <>
64 std::vector<int> getenv_or(std::string_view env_var_name, std::vector<int> default_val);
65 
87 template <typename T>
88 std::tuple<std::string_view, T, bool> getenv_or(
89  std::initializer_list<std::string_view> env_var_names, T default_val)
90 {
91  KVIKIO_EXPECT(env_var_names.size() > 0,
92  "`env_var_names` must contain at least one environment variable name.",
93  std::invalid_argument);
94  std::string_view env_name_target;
95  std::string_view env_val_target;
96 
97  for (auto const& env_var_name : env_var_names) {
98  auto const* env_val = std::getenv(env_var_name.data());
99  if (env_val == nullptr) { continue; }
100 
101  if (!env_name_target.empty() && env_val_target != env_val) {
102  std::stringstream ss;
103  ss << "Environment variable " << env_var_name << " (" << env_val
104  << ") has already been set by its alias " << env_name_target << " (" << env_val_target
105  << ") with a different value.";
106  KVIKIO_FAIL(ss.str(), std::invalid_argument);
107  }
108 
109  env_name_target = env_var_name;
110  env_val_target = env_val;
111  }
112 
113  if (env_name_target.empty()) { return {env_name_target, default_val, false}; }
114 
115  auto res = getenv_or<T>(env_name_target, default_val);
116  return {env_name_target, res, true};
117 }
118 
123 class defaults {
124  private:
125  BS_thread_pool _thread_pool{get_num_threads_from_env()};
126  CompatMode _compat_mode;
127  std::size_t _task_size;
128  std::size_t _gds_threshold;
129  std::size_t _bounce_buffer_size;
130  std::size_t _http_max_attempts;
131  long _http_timeout;
132  std::vector<int> _http_status_codes;
133 
134  static unsigned int get_num_threads_from_env();
135 
136  defaults();
137 
138  KVIKIO_EXPORT static defaults* instance();
139 
140  public:
159  [[nodiscard]] static CompatMode compat_mode();
160 
170 
181 
198 
214 
224  [[nodiscard]] static BS_thread_pool& thread_pool();
225 
234  [[nodiscard]] static unsigned int thread_pool_nthreads();
235 
244  static void set_thread_pool_nthreads(unsigned int nthreads);
245 
251  [[nodiscard]] static unsigned int num_threads();
252 
258  static void set_num_threads(unsigned int nthreads);
259 
268  [[nodiscard]] static std::size_t task_size();
269 
275  static void set_task_size(std::size_t nbytes);
276 
288  [[nodiscard]] static std::size_t gds_threshold();
289 
294  static void set_gds_threshold(std::size_t nbytes);
295 
304  [[nodiscard]] static std::size_t bounce_buffer_size();
305 
311  static void set_bounce_buffer_size(std::size_t nbytes);
312 
322  [[nodiscard]] static std::size_t http_max_attempts();
323 
329  static void set_http_max_attempts(std::size_t attempts);
330 
339  [[nodiscard]] static long http_timeout();
340 
346  static void set_http_timeout(long timeout_seconds);
347 
362  [[nodiscard]] static std::vector<int> const& http_status_codes();
363 
369  static void set_http_status_codes(std::vector<int> status_codes);
370 };
371 
372 } // namespace kvikio
Singleton class of default values used throughout KvikIO.
Definition: defaults.hpp:123
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:216
#define KVIKIO_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:252
KvikIO namespace.
Definition: batch.hpp:27
CompatMode
I/O compatibility mode.
Definition: compat_mode.hpp:28