OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE > Class Template Referenceabstract

#include <DataRecycler.h>

Public Types

using CachedItemContainer = std::vector< CachedItem< CACHED_ITEM_TYPE, META_INFO_TYPE >>
 
using PerDeviceCacheItemContainer = std::unordered_map< DeviceIdentifier, std::shared_ptr< CachedItemContainer >>
 
using PerTypeCacheItemContainer = std::unordered_map< CacheItemType, std::shared_ptr< PerDeviceCacheItemContainer >>
 
using PerTypeCacheMetricTracker = std::unordered_map< CacheItemType, CacheMetricTracker >
 

Public Member Functions

 DataRecycler (const std::vector< CacheItemType > &item_types, size_t total_cache_size, size_t max_item_size, int num_gpus)
 
virtual ~DataRecycler ()=default
 
virtual CACHED_ITEM_TYPE getItemFromCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::optional< META_INFO_TYPE > meta_info=std::nullopt) const =0
 
virtual void putItemToCache (QueryPlanHash key, CACHED_ITEM_TYPE item_ptr, CacheItemType item_type, DeviceIdentifier device_identifier, size_t item_size, size_t compute_time, std::optional< META_INFO_TYPE > meta_info=std::nullopt)=0
 
virtual void initCache ()=0
 
virtual void clearCache ()=0
 
virtual std::string toString () const =0
 
std::shared_ptr
< CachedItemContainer
getCachedItemContainer (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
std::optional< CachedItem
< CACHED_ITEM_TYPE,
META_INFO_TYPE > > 
getCachedItem (QueryPlanHash key, CachedItemContainer &m) const
 
size_t getCurrentNumCachedItems (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
size_t getCurrentCacheSizeForDevice (CacheItemType item_type, DeviceIdentifier device_identifier) const
 
std::shared_ptr< CacheItemMetricgetCachedItemMetric (CacheItemType item_type, DeviceIdentifier device_identifier, QueryPlanHash key) const
 
void setTotalCacheSize (CacheItemType item_type, size_t new_total_cache_size)
 
void setMaxCacheItemSize (CacheItemType item_type, size_t new_max_cache_item_size)
 
std::function< void()> getCacheInvalidator ()
 

Protected Member Functions

void removeCachedItemFromBeginning (CacheItemType item_type, DeviceIdentifier device_identifier, int offset)
 
void sortCacheContainerByQueryMetric (CacheItemType item_type, DeviceIdentifier device_identifier)
 
std::mutex & getCacheLock () const
 
CacheMetricTrackergetMetricTracker (CacheItemType item_type)
 
CacheMetricTracker constgetMetricTracker (CacheItemType item_type) const
 
std::unordered_set
< CacheItemType > const
getCacheItemType () const
 
PerTypeCacheItemContainer constgetItemCache () const
 

Private Member Functions

virtual bool hasItemInCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< META_INFO_TYPE > meta_info=std::nullopt) const =0
 
virtual void removeItemFromCache (QueryPlanHash key, CacheItemType item_type, DeviceIdentifier device_identifier, std::lock_guard< std::mutex > &lock, std::optional< META_INFO_TYPE > meta_info=std::nullopt)=0
 
virtual void cleanupCacheForInsertion (CacheItemType item_type, DeviceIdentifier device_identifier, size_t required_size, std::lock_guard< std::mutex > &lock, std::optional< META_INFO_TYPE > meta_info=std::nullopt)=0
 

Private Attributes

std::unordered_set< CacheItemTypecache_item_types_
 
PerTypeCacheMetricTracker metric_tracker_
 
PerTypeCacheItemContainer cached_items_container_
 
std::mutex cache_lock_
 

Detailed Description

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
class DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >

Definition at line 394 of file DataRecycler.h.

Member Typedef Documentation

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
using DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::CachedItemContainer = std::vector<CachedItem<CACHED_ITEM_TYPE, META_INFO_TYPE>>

Definition at line 396 of file DataRecycler.h.

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
using DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::PerDeviceCacheItemContainer = std::unordered_map<DeviceIdentifier, std::shared_ptr<CachedItemContainer>>

Definition at line 398 of file DataRecycler.h.

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
using DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::PerTypeCacheItemContainer = std::unordered_map<CacheItemType, std::shared_ptr<PerDeviceCacheItemContainer>>

Definition at line 400 of file DataRecycler.h.

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
using DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::PerTypeCacheMetricTracker = std::unordered_map<CacheItemType, CacheMetricTracker>

Definition at line 401 of file DataRecycler.h.

Constructor & Destructor Documentation

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::DataRecycler ( const std::vector< CacheItemType > &  item_types,
size_t  total_cache_size,
size_t  max_item_size,
int  num_gpus 
)
inline

Definition at line 403 of file DataRecycler.h.

406  {
407  for (auto& item_type : item_types) {
408  cache_item_types_.insert(item_type);
409  metric_tracker_.emplace(
410  item_type,
411  CacheMetricTracker(item_type, total_cache_size, max_item_size, num_gpus));
412  auto item_container = std::make_shared<PerDeviceCacheItemContainer>();
413  for (int gpu_device_identifier = num_gpus; gpu_device_identifier >= 1;
414  --gpu_device_identifier) {
415  item_container->emplace(gpu_device_identifier,
416  std::make_shared<CachedItemContainer>());
417  }
418  item_container->emplace(DataRecyclerUtil::CPU_DEVICE_IDENTIFIER,
419  std::make_shared<CachedItemContainer>());
420  cached_items_container_.emplace(item_type, item_container);
421  }
422  }
std::unordered_set< CacheItemType > cache_item_types_
Definition: DataRecycler.h:590
PerTypeCacheMetricTracker metric_tracker_
Definition: DataRecycler.h:593
PerTypeCacheItemContainer cached_items_container_
Definition: DataRecycler.h:596
static constexpr DeviceIdentifier CPU_DEVICE_IDENTIFIER
Definition: DataRecycler.h:132
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::~DataRecycler ( )
virtualdefault

Member Function Documentation

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::cleanupCacheForInsertion ( CacheItemType  item_type,
DeviceIdentifier  device_identifier,
size_t  required_size,
std::lock_guard< std::mutex > &  lock,
std::optional< META_INFO_TYPE >  meta_info = std::nullopt 
)
privatepure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::clearCache ( )
pure virtual

Implemented in HashtableRecycler, OverlapsTuningParamRecycler, and HashingSchemeRecycler.

Referenced by DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::getCacheInvalidator().

+ Here is the caller graph for this function:

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::optional<CachedItem<CACHED_ITEM_TYPE, META_INFO_TYPE> > DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCachedItem ( QueryPlanHash  key,
CachedItemContainer m 
) const
inline

Definition at line 460 of file DataRecycler.h.

462  {
463  for (auto& candidate : m) {
464  if (candidate.key == key) {
465  return candidate;
466  }
467  }
468  return std::nullopt;
469  }
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::shared_ptr<CachedItemContainer> DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCachedItemContainer ( CacheItemType  item_type,
DeviceIdentifier  device_identifier 
) const
inline

Definition at line 446 of file DataRecycler.h.

Referenced by DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::getCurrentNumCachedItems(), DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::removeCachedItemFromBeginning(), and DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::sortCacheContainerByQueryMetric().

448  {
449  auto item_type_container_itr = cached_items_container_.find(item_type);
450  if (item_type_container_itr != cached_items_container_.end()) {
451  auto device_type_container_itr =
452  item_type_container_itr->second->find(device_identifier);
453  return device_type_container_itr != item_type_container_itr->second->end()
454  ? device_type_container_itr->second
455  : nullptr;
456  }
457  return nullptr;
458  }
PerTypeCacheItemContainer cached_items_container_
Definition: DataRecycler.h:596

+ Here is the caller graph for this function:

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::shared_ptr<CacheItemMetric> DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCachedItemMetric ( CacheItemType  item_type,
DeviceIdentifier  device_identifier,
QueryPlanHash  key 
) const
inline

Definition at line 486 of file DataRecycler.h.

488  {
489  std::lock_guard<std::mutex> lock(cache_lock_);
490  auto cache_metric_tracker = getMetricTracker(item_type);
491  return cache_metric_tracker.getCacheItemMetric(key, device_identifier);
492  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:546
std::mutex cache_lock_
Definition: DataRecycler.h:598
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::function<void()> DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCacheInvalidator ( )
inline

Definition at line 508 of file DataRecycler.h.

508  {
509  return [this]() -> void { clearCache(); };
510  }
virtual void clearCache()=0
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::unordered_set<CacheItemType> const& DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCacheItemType ( ) const
inlineprotected

Definition at line 556 of file DataRecycler.h.

556  {
557  return cache_item_types_;
558  }
std::unordered_set< CacheItemType > cache_item_types_
Definition: DataRecycler.h:590
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::mutex& DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCacheLock ( ) const
inlineprotected

Definition at line 544 of file DataRecycler.h.

544 { return cache_lock_; }
std::mutex cache_lock_
Definition: DataRecycler.h:598
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
size_t DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCurrentCacheSizeForDevice ( CacheItemType  item_type,
DeviceIdentifier  device_identifier 
) const
inline

Definition at line 478 of file DataRecycler.h.

479  {
480  std::lock_guard<std::mutex> lock(cache_lock_);
481  auto metric_tracker = getMetricTracker(item_type);
482  auto current_size_opt = metric_tracker.getCurrentCacheSize(device_identifier);
483  return current_size_opt ? current_size_opt.value() : 0;
484  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:546
std::mutex cache_lock_
Definition: DataRecycler.h:598
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
size_t DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getCurrentNumCachedItems ( CacheItemType  item_type,
DeviceIdentifier  device_identifier 
) const
inline

Definition at line 471 of file DataRecycler.h.

472  {
473  std::lock_guard<std::mutex> lock(cache_lock_);
474  auto container = getCachedItemContainer(item_type, device_identifier);
475  return container ? container->size() : 0;
476  }
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:446
std::mutex cache_lock_
Definition: DataRecycler.h:598
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
PerTypeCacheItemContainer const& DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getItemCache ( ) const
inlineprotected

Definition at line 560 of file DataRecycler.h.

560  {
562  }
PerTypeCacheItemContainer cached_items_container_
Definition: DataRecycler.h:596
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual CACHED_ITEM_TYPE DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getItemFromCache ( QueryPlanHash  key,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
std::optional< META_INFO_TYPE >  meta_info = std::nullopt 
) const
pure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
CacheMetricTracker& DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getMetricTracker ( CacheItemType  item_type)
inlineprotected

Definition at line 546 of file DataRecycler.h.

Referenced by DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::getCachedItemMetric(), DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::getCurrentCacheSizeForDevice(), DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::getMetricTracker(), DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::setMaxCacheItemSize(), and DataRecycler< std::optional< HashType >, EMPTY_META_INFO >::setTotalCacheSize().

546  {
547  auto metric_iter = metric_tracker_.find(item_type);
548  CHECK(metric_iter != metric_tracker_.end());
549  return metric_iter->second;
550  }
PerTypeCacheMetricTracker metric_tracker_
Definition: DataRecycler.h:593
#define CHECK(condition)
Definition: Logger.h:211

+ Here is the caller graph for this function:

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
CacheMetricTracker const& DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::getMetricTracker ( CacheItemType  item_type) const
inlineprotected

Definition at line 552 of file DataRecycler.h.

552  {
553  return const_cast<DataRecycler*>(this)->getMetricTracker(item_type);
554  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:546
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual bool DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::hasItemInCache ( QueryPlanHash  key,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
std::lock_guard< std::mutex > &  lock,
std::optional< META_INFO_TYPE >  meta_info = std::nullopt 
) const
privatepure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::initCache ( )
pure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::putItemToCache ( QueryPlanHash  key,
CACHED_ITEM_TYPE  item_ptr,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
size_t  item_size,
size_t  compute_time,
std::optional< META_INFO_TYPE >  meta_info = std::nullopt 
)
pure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::removeCachedItemFromBeginning ( CacheItemType  item_type,
DeviceIdentifier  device_identifier,
int  offset 
)
inlineprotected

Definition at line 513 of file DataRecycler.h.

515  {
516  // it removes cached items located from `idx 0` to `offset`
517  // so, call this function after sorting the cached items container vec
518  // and we should call this function under the proper locking scheme
519  auto container = getCachedItemContainer(item_type, device_identifier);
520  CHECK(container);
521  container->erase(container->begin(), container->begin() + offset);
522  }
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:446
#define CHECK(condition)
Definition: Logger.h:211
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::removeItemFromCache ( QueryPlanHash  key,
CacheItemType  item_type,
DeviceIdentifier  device_identifier,
std::lock_guard< std::mutex > &  lock,
std::optional< META_INFO_TYPE >  meta_info = std::nullopt 
)
privatepure virtual
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::setMaxCacheItemSize ( CacheItemType  item_type,
size_t  new_max_cache_item_size 
)
inline

Definition at line 501 of file DataRecycler.h.

501  {
502  if (new_max_cache_item_size > 0) {
503  std::lock_guard<std::mutex> lock(cache_lock_);
504  getMetricTracker(item_type).setMaxCacheItemSize(new_max_cache_item_size);
505  }
506  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:546
void setMaxCacheItemSize(size_t new_max_cache_item_size)
Definition: DataRecycler.h:352
std::mutex cache_lock_
Definition: DataRecycler.h:598
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::setTotalCacheSize ( CacheItemType  item_type,
size_t  new_total_cache_size 
)
inline

Definition at line 494 of file DataRecycler.h.

494  {
495  if (new_total_cache_size > 0) {
496  std::lock_guard<std::mutex> lock(cache_lock_);
497  getMetricTracker(item_type).setTotalCacheSize(new_total_cache_size);
498  }
499  }
CacheMetricTracker & getMetricTracker(CacheItemType item_type)
Definition: DataRecycler.h:546
std::mutex cache_lock_
Definition: DataRecycler.h:598
void setTotalCacheSize(size_t new_total_cache_size)
Definition: DataRecycler.h:347
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
void DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::sortCacheContainerByQueryMetric ( CacheItemType  item_type,
DeviceIdentifier  device_identifier 
)
inlineprotected

Definition at line 524 of file DataRecycler.h.

525  {
526  // should call this function under the proper locking scheme
527  auto container = getCachedItemContainer(item_type, device_identifier);
528  CHECK(container);
529  std::sort(container->begin(),
530  container->end(),
533  auto& left_metrics = left.item_metric->getMetrics();
534  auto& right_metrics = right.item_metric->getMetrics();
535  for (size_t i = 0; i < CacheMetricType::NUM_METRIC_TYPE; ++i) {
536  if (left_metrics[i] != right_metrics[i]) {
537  return left_metrics[i] < right_metrics[i];
538  }
539  }
540  return false;
541  });
542  }
DEVICE void sort(ARGS &&...args)
Definition: gpu_enabled.h:105
std::shared_ptr< CachedItemContainer > getCachedItemContainer(CacheItemType item_type, DeviceIdentifier device_identifier) const
Definition: DataRecycler.h:446
#define CHECK(condition)
Definition: Logger.h:211
std::shared_ptr< CacheItemMetric > item_metric
Definition: DataRecycler.h:384
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
virtual std::string DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::toString ( ) const
pure virtual

Member Data Documentation

template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
std::unordered_set<CacheItemType> DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::cache_item_types_
private
template<typename CACHED_ITEM_TYPE, typename META_INFO_TYPE>
PerTypeCacheMetricTracker DataRecycler< CACHED_ITEM_TYPE, META_INFO_TYPE >::metric_tracker_
private

The documentation for this class was generated from the following file: