|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION. |
| 2 | + * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION. |
3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
4 | 4 | */ |
5 | 5 |
|
| 6 | +#include "io/utilities/getenv_or.hpp" |
6 | 7 | #include "runtime/context.hpp" |
7 | 8 |
|
8 | 9 | #include <cudf/context.hpp> |
@@ -95,40 +96,63 @@ std::string get_program_cache_dir() |
95 | 96 | #endif |
96 | 97 | } |
97 | 98 |
|
98 | | -std::size_t try_parse_numeric_env_var(char const* const env_name, std::size_t default_val) |
99 | | -{ |
100 | | - auto const value = std::getenv(env_name); |
101 | | - return value != nullptr ? std::stoull(value) : default_val; |
102 | | -} |
103 | 99 | } // namespace |
104 | 100 |
|
105 | 101 | jitify2::ProgramCache<>& jit::program_cache::get(jitify2::PreprocessedProgramData const& preprog) |
106 | 102 | { |
107 | 103 | CUDF_FUNC_RANGE(); |
108 | | - std::lock_guard<std::mutex> const caches_lock(_caches_mutex); |
| 104 | + std::lock_guard caches_lock(_caches_mutex); |
109 | 105 |
|
110 | 106 | auto existing_cache = _caches.find(preprog.name()); |
111 | 107 |
|
112 | | - if (existing_cache == _caches.end()) { |
113 | | - auto const kernel_limit_proc = |
114 | | - try_parse_numeric_env_var("LIBCUDF_KERNEL_CACHE_LIMIT_PER_PROCESS", 10'000); |
115 | | - auto const kernel_limit_disk = |
116 | | - try_parse_numeric_env_var("LIBCUDF_KERNEL_CACHE_LIMIT_DISK", 100'000); |
117 | | - |
118 | | - // if kernel_limit_disk is zero, jitify will assign it the value of kernel_limit_proc. |
119 | | - // to avoid this, we treat zero as "disable disk caching" by not providing the cache dir. |
120 | | - auto const cache_dir = kernel_limit_disk == 0 ? std::string{} : get_program_cache_dir(); |
121 | | - |
122 | | - auto const res = |
123 | | - _caches.insert({preprog.name(), |
| 108 | + if (existing_cache == _caches.end() || _disabled.load(std::memory_order_seq_cst)) { |
| 109 | + auto res = |
| 110 | + _caches.emplace(preprog.name(), |
124 | 111 | std::make_unique<jitify2::ProgramCache<>>( |
125 | | - kernel_limit_proc, preprog, nullptr, cache_dir, kernel_limit_disk)}); |
| 112 | + _kernel_limit_proc, preprog, nullptr, _cache_dir, _kernel_limit_disk)); |
126 | 113 | existing_cache = res.first; |
127 | 114 | } |
128 | 115 |
|
129 | 116 | return *(existing_cache->second); |
130 | 117 | } |
131 | 118 |
|
| 119 | +void jit::program_cache::clear() |
| 120 | +{ |
| 121 | + CUDF_FUNC_RANGE(); |
| 122 | + std::lock_guard caches_lock(_caches_mutex); |
| 123 | + |
| 124 | + _caches.clear(); |
| 125 | + |
| 126 | + // non-atomic |
| 127 | + std::filesystem::remove_all(_cache_dir); |
| 128 | +} |
| 129 | + |
| 130 | +void jit::program_cache::enable(bool enable) |
| 131 | +{ |
| 132 | + _disabled.store(!enable, std::memory_order_seq_cst); |
| 133 | +} |
| 134 | + |
| 135 | +bool jit::program_cache::is_enabled() const { return !_disabled.load(std::memory_order_seq_cst); } |
| 136 | + |
| 137 | +std::unique_ptr<jit::program_cache> jit::program_cache::create() |
| 138 | +{ |
| 139 | + auto const kernel_limit_proc = getenv_or("LIBCUDF_KERNEL_CACHE_LIMIT_PER_PROCESS", 10'000); |
| 140 | + auto const kernel_limit_disk = getenv_or("LIBCUDF_KERNEL_CACHE_LIMIT_DISK", 100'000); |
| 141 | + auto const disabled = get_bool_env_or("LIBCUDF_KERNEL_CACHE_DISABLED", false); |
| 142 | + auto const clear_cache = get_bool_env_or("LIBCUDF_KERNEL_CACHE_CLEAR", false); |
| 143 | + |
| 144 | + // if kernel_limit_disk is zero, jitify will assign it the value of kernel_limit_proc. |
| 145 | + // to avoid this, we treat zero as "disable disk caching" by not providing the cache dir. |
| 146 | + auto cache_dir = kernel_limit_disk == 0 ? std::string{} : get_program_cache_dir(); |
| 147 | + |
| 148 | + auto cache = |
| 149 | + std::make_unique<jit::program_cache>(kernel_limit_proc, kernel_limit_disk, cache_dir, disabled); |
| 150 | + |
| 151 | + if (clear_cache) { cache->clear(); } |
| 152 | + |
| 153 | + return cache; |
| 154 | +} |
| 155 | + |
132 | 156 | jitify2::ProgramCache<>& jit::get_program_cache(jitify2::PreprocessedProgramData const& preprog) |
133 | 157 | { |
134 | 158 | return cudf::get_context().program_cache().get(preprog); |
|
0 commit comments