OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OverlapsTuningParamRecycler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
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 
18 
19 std::optional<AutoTunerMetaInfo> OverlapsTuningParamRecycler::getItemFromCache(
20  QueryPlanHash key,
21  CacheItemType item_type,
22  DeviceIdentifier device_identifier,
23  std::optional<EMPTY_META_INFO> meta_info) {
26  return std::nullopt;
27  }
29  std::lock_guard<std::mutex> lock(getCacheLock());
30  auto param_cache = getCachedItemContainer(item_type, device_identifier);
31  auto cached_param = getCachedItemWithoutConsideringMetaInfo(
32  key, item_type, device_identifier, *param_cache, lock);
33  if (cached_param) {
34  CHECK(!cached_param->isDirty());
35  VLOG(1) << "[" << item_type << ", "
37  << "] Recycle auto tuner parameters in cache (key: " << key << ")";
38  return cached_param->cached_item;
39  }
40  return std::nullopt;
41 }
42 
44  QueryPlanHash key,
45  std::optional<AutoTunerMetaInfo> item,
46  CacheItemType item_type,
47  DeviceIdentifier device_identifier,
48  size_t item_size,
49  size_t compute_time,
50  std::optional<EMPTY_META_INFO> meta_info) {
53  return;
54  }
56  std::lock_guard<std::mutex> lock(getCacheLock());
57  auto param_cache = getCachedItemContainer(item_type, device_identifier);
58  auto cached_param = getCachedItemWithoutConsideringMetaInfo(
59  key, item_type, device_identifier, *param_cache, lock);
60  if (cached_param) {
61  return;
62  }
63  param_cache->emplace_back(key, item, nullptr, meta_info);
64  VLOG(1) << "[" << item_type << ", "
66  << "] Put auto tuner parameters to cache (key: " << key << ")";
67  return;
68 }
69 
71  QueryPlanHash key,
72  CacheItemType item_type,
73  DeviceIdentifier device_identifier,
74  std::lock_guard<std::mutex>& lock,
75  std::optional<EMPTY_META_INFO> meta_info) const {
78  return false;
79  }
81  auto param_cache = getCachedItemContainer(item_type, device_identifier);
82  auto candidate_it = std::find_if(
83  param_cache->begin(), param_cache->end(), [&key](const auto& cached_item) {
84  return cached_item.key == key;
85  });
86  return candidate_it != param_cache->end();
87 }
88 
90  QueryPlanHash key,
91  CacheItemType item_type,
92  DeviceIdentifier device_identifier,
93  std::lock_guard<std::mutex>& lock,
94  std::optional<EMPTY_META_INFO> meta_info) {
95  auto param_cache = getCachedItemContainer(item_type, device_identifier);
96  auto filter = [key](auto const& item) { return item.key == key; };
97  auto itr = std::find_if(param_cache->cbegin(), param_cache->cend(), filter);
98  if (itr == param_cache->cend()) {
99  return;
100  } else {
101  VLOG(1) << "[" << item_type << ", "
102  << DataRecyclerUtil::getDeviceIdentifierString(device_identifier)
103  << "] remove cached item from cache (key: " << key << ")";
104  param_cache->erase(itr);
105  }
106 }
107 
109  std::lock_guard<std::mutex> lock(getCacheLock());
112  if (!param_cache->empty()) {
115  << "] clear cache (# items: " << param_cache->size() << ")";
116  param_cache->clear();
117  }
118 }
119 
121  size_t table_key,
122  std::unordered_set<QueryPlanHash>& key_set,
123  CacheItemType item_type,
124  DeviceIdentifier device_identifier) {
125  if (!g_enable_data_recycler || !g_use_hashtable_cache || key_set.empty()) {
126  return;
127  }
129  std::lock_guard<std::mutex> lock(getCacheLock());
130  auto param_cache = getCachedItemContainer(item_type, device_identifier);
131  for (auto key : key_set) {
132  markCachedItemAsDirtyImpl(key, *param_cache);
133  }
134 }
135 
137  std::ostringstream oss;
138  oss << "A current status of the Overlaps Join Hashtable Tuning Parameter Recycler:\n";
139  oss << "\t# cached parameters:\n";
140  oss << "\t\tDevice" << PARAM_CACHE_DEVICE_IDENTIFIER << "\n";
142  PARAM_CACHE_DEVICE_IDENTIFIER);
143  for (auto& cache_container : *param_cache) {
144  oss << "\t\t\tCache_key: " << cache_container.key;
145  if (cache_container.cached_item.has_value()) {
146  oss << ", Max_hashtable_size: " << cache_container.cached_item->max_hashtable_size
147  << ", Bucket_threshold: " << cache_container.cached_item->bucket_threshold
148  << ", Bucket_sizes: " << ::toString(cache_container.cached_item->bucket_sizes)
149  << "\n";
150  } else {
151  oss << ", Params info is not available\n";
152  }
153  }
154  return oss.str();
155 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
size_t DeviceIdentifier
Definition: DataRecycler.h:129
static std::string getDeviceIdentifierString(DeviceIdentifier device_identifier)
Definition: DataRecycler.h:138
void removeItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override
std::optional< CachedItem< std::optional< AutoTunerMetaInfo >, EMPTY_META_INFO > > getCachedItemWithoutConsideringMetaInfo(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, CachedItemContainer &m, std::lock_guard< std::mutex > &lock)
Definition: DataRecycler.h:545
bool hasItemInCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) const override
constexpr QueryPlanHash EMPTY_HASHED_PLAN_DAG_KEY
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:530
void markCachedItemAsDirtyImpl(QueryPlanHash key, CachedItemContainer &m) const
Definition: DataRecycler.h:506
bool g_enable_data_recycler
Definition: Execute.cpp:146
void markCachedItemAsDirty(size_t table_key, std::unordered_set< QueryPlanHash > &key_set, CacheItemType item_type, DeviceIdentifier device_identifier) override
CacheItemType
Definition: DataRecycler.h:38
constexpr DeviceIdentifier PARAM_CACHE_DEVICE_IDENTIFIER
size_t QueryPlanHash
#define CHECK(condition)
Definition: Logger.h:222
virtual std::optional< AutoTunerMetaInfo > getItemFromCache(QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< EMPTY_META_INFO > meta_info=std::nullopt)=0
std::string toString() const override
bool g_use_hashtable_cache
Definition: Execute.cpp:147
#define VLOG(n)
Definition: Logger.h:316
void putItemToCache(QueryPlanHash key, std::optional< AutoTunerMetaInfo > item, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< EMPTY_META_INFO > meta_info=std::nullopt) override