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  * https://apache.ac.cn/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 <sstream>
22 #include <stdexcept>
23 #include <string>
24 
25 #include <kvikio/compat_mode.hpp>
26 #include <kvikio/http_status_codes.hpp>
27 #include <kvikio/shim/cufile.hpp>
28 #include <kvikio/threadpool_wrapper.hpp>
29 
33 namespace kvikio {
34 
35 template <typename T>
36 T getenv_or(std::string_view env_var_name, T default_val)
37 {
38  auto const* env_val = std::getenv(env_var_name.data());
39  if (env_val == nullptr) { return default_val; }
40 
41  std::stringstream sstream(env_val);
42  T converted_val;
43  sstream >> converted_val;
44  KVIKIO_EXPECT(!sstream.fail(),
45  "unknown config value " + std::string{env_var_name} + "=" + std::string{env_val},
46  std::invalid_argument);
47  return converted_val;
48 }
49 
50 template <>
51 bool getenv_or(std::string_view env_var_name, bool default_val);
52 
53 template <>
54 CompatMode getenv_or(std::string_view env_var_name, CompatMode default_val);
55 
56 template <>
57 std::vector<int> getenv_or(std::string_view env_var_name, std::vector<int> default_val);
58 
63 class defaults {
64  private
65  BS_thread_pool _thread_pool{get_num_threads_from_env()};
66  CompatMode _compat_mode;
67  std::size_t _task_size;
68  std::size_t _gds_threshold;
69  std::size_t _bounce_buffer_size;
70  std::size_t _http_max_attempts;
71  long _http_timeout;
72  std::vector<int> _http_status_codes;
73 
74  static unsigned int get_num_threads_from_env();
75 
76  defaults();
77 
78  KVIKIO_EXPORT static defaults* instance();
79 
80  public
99  [[nodiscard]] static CompatMode compat_mode();
100 
110 
121 
138 
154 
164  [[nodiscard]] static BS_thread_pool& thread_pool();
165 
174  [[nodiscard]] static unsigned int thread_pool_nthreads();
175 
184  static void set_thread_pool_nthreads(unsigned int nthreads);
185 
194  [[nodiscard]] static std::size_t task_size();
195 
201  static void set_task_size(std::size_t nbytes);
202 
214  [[nodiscard]] static std::size_t gds_threshold();
215 
220  static void set_gds_threshold(std::size_t nbytes);
221 
230  [[nodiscard]] static std::size_t bounce_buffer_size();
231 
237  static void set_bounce_buffer_size(std::size_t nbytes);
238 
248  [[nodiscard]] static std::size_t http_max_attempts();
249 
255  static void set_http_max_attempts(std::size_t attempts);
256 
265  [[nodiscard]] static long http_timeout();
266 
272  static void set_http_timeout(long timeout_seconds);
273 
288  [[nodiscard]] static std::vector<int> const& http_status_codes();
289 
295  static void set_http_status_codes(std::vector<int> status_codes);
296 };
297 } // namespace kvikio
298 
贯穿 KvikIO 使用的默认值的单例类。
定义: defaults.hpp:63
static CompatMode infer_compat_mode_if_auto(CompatMode compat_mode) noexcept
从系统运行时推断 AUTO 兼容模式。
static std::size_t task_size()
获取并行 IO 操作使用的默认任务大小。
static std::vector< int > const & http_status_codes()
要重试的 HTTP 状态码列表。
static void set_task_size(std::size_t nbytes)
设置并行 IO 操作使用的默认任务大小。
static bool is_compat_mode_preferred()
是否期望来自 defaults 类的全局兼容模式处于开启状态。
static void set_http_status_codes(std::vector< int > status_codes)
设置要重试的 HTTP 状态码列表。
static void set_compat_mode(CompatMode compat_mode)
设置 kvikio::defaults::compat_mode() 的值。
static void set_gds_threshold(std::size_t nbytes)
设置默认 GDS 阈值,这是使用 GDS 的最小大小(以字节为单位)。
static void set_http_timeout(long timeout_seconds)
重置 http 超时。
static bool is_compat_mode_preferred(CompatMode compat_mode) noexcept
给定请求的兼容模式,是否期望它会被简化为 ON。
static void set_thread_pool_nthreads(unsigned int nthreads)
设置默认线程池中的线程数。等待所有当前正在运行的任务完成...
static std::size_t http_max_attempts()
获取每个远程 IO 读取的最大尝试次数。
static BS_thread_pool & thread_pool()
获取默认线程池。
static long http_timeout()
允许传输完成的最大时间,以秒为单位。
static std::size_t gds_threshold()
获取默认 GDS 阈值,这是使用 GDS 的最小大小(以字节为单位)。
static unsigned int thread_pool_nthreads()
获取默认线程池中的线程数。
static std::size_t bounce_buffer_size()
获取用于在主机内存中暂存数据的 bounce buffer 大小。
static void set_http_max_attempts(std::size_t attempts)
设置每个远程 IO 读取的最大尝试次数。
static void set_bounce_buffer_size(std::size_t nbytes)
设置用于在主机内存中暂存数据的 bounce buffer 大小。
static CompatMode compat_mode()
返回 KvikIO 库是否正在以兼容模式运行。
#define KVIKIO_EXPECT(...)
用于检查前置条件或条件,当条件被违反时抛出异常...
定义: error.hpp:216
KvikIO 命名空间。
定义: batch.hpp:27
CompatMode
I/O 兼容模式。