OmniSciDB  94e8789169
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaselineHashTable.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 OmniSci, 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 
17 #pragma once
18 
19 #include "Catalog/Catalog.h"
20 #include "DataMgr/AbstractBuffer.h"
24 
26 
27 class BaselineHashTable : public HashTable {
28  public:
29  // CPU constructor
31  HashType layout,
32  const size_t entry_count,
33  const size_t emitted_keys_count,
34  const size_t hash_table_size)
35  : device_id_(0)
36  , catalog_(catalog)
37  , layout_(layout)
38  , entry_count_(entry_count)
39  , emitted_keys_count_(emitted_keys_count) {
40  cpu_hash_table_buff_.resize(hash_table_size);
41  }
42 
43  // GPU constructor
45  HashType layout,
46  const size_t entry_count,
47  const size_t emitted_keys_count,
48  const size_t hash_table_size,
49  const size_t device_id)
50  : device_id_(device_id)
51  , catalog_(catalog)
52  , layout_(layout)
53  , entry_count_(entry_count)
54  , emitted_keys_count_(emitted_keys_count) {
55 #ifdef HAVE_CUDA
56  CHECK(catalog_);
57  auto& data_mgr = catalog_->getDataMgr();
58  gpu_hash_table_buff_ =
59  CudaAllocator::allocGpuAbstractBuffer(&data_mgr, hash_table_size, device_id_);
60 #else
61  UNREACHABLE();
62 #endif
63  }
64 
66 #ifdef HAVE_CUDA
67  CHECK(catalog_);
68  auto& data_mgr = catalog_->getDataMgr();
69  if (gpu_hash_table_buff_) {
70  data_mgr.free(gpu_hash_table_buff_);
71  }
72 #endif
73  }
74 
75  int8_t* getGpuBuffer() const override {
76  return gpu_hash_table_buff_ ? gpu_hash_table_buff_->getMemoryPtr() : nullptr;
77  }
78 
79  size_t getHashTableBufferSize(const ExecutorDeviceType device_type) const override {
80  if (device_type == ExecutorDeviceType::CPU) {
81  return cpu_hash_table_buff_.size() *
82  sizeof(decltype(cpu_hash_table_buff_)::value_type);
83  } else {
84  return gpu_hash_table_buff_ ? gpu_hash_table_buff_->reservedSize() : 0;
85  }
86  }
87 
88  int8_t* getCpuBuffer() override {
89  return reinterpret_cast<int8_t*>(cpu_hash_table_buff_.data());
90  }
91 
92  HashType getLayout() const override { return layout_; }
93  size_t getEntryCount() const override { return entry_count_; }
94  size_t getEmittedKeysCount() const override { return emitted_keys_count_; }
95 
96  private:
97  std::vector<int8_t> cpu_hash_table_buff_;
98  Data_Namespace::AbstractBuffer* gpu_hash_table_buff_{nullptr};
99  const size_t device_id_;
100 
101  // TODO: only required for cuda
103 
105  size_t entry_count_; // number of keys in the hash table
106  size_t emitted_keys_count_; // number of keys emitted across all rows
107 };
int8_t * getGpuBuffer() const override
class for a per-database catalog. also includes metadata for the current database and the current use...
Definition: Catalog.h:101
size_t getEntryCount() const override
ExecutorDeviceType
Data_Namespace::DataMgr & getDataMgr() const
Definition: Catalog.h:222
HashType getLayout() const override
#define UNREACHABLE()
Definition: Logger.h:241
size_t getEmittedKeysCount() const override
const Catalog_Namespace::Catalog * catalog_
std::vector< int8_t > cpu_hash_table_buff_
size_t getHashTableBufferSize(const ExecutorDeviceType device_type) const override
const size_t device_id_
This file contains the class specification and related data structures for Catalog.
An AbstractBuffer is a unit of data management for a data manager.
BaselineHashTable(const Catalog_Namespace::Catalog *catalog, HashType layout, const size_t entry_count, const size_t emitted_keys_count, const size_t hash_table_size, const size_t device_id)
BaselineHashTable(const Catalog_Namespace::Catalog *catalog, HashType layout, const size_t entry_count, const size_t emitted_keys_count, const size_t hash_table_size)
static Data_Namespace::AbstractBuffer * allocGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
int8_t * getCpuBuffer() override
#define CHECK(condition)
Definition: Logger.h:197
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:469
HashType
Definition: HashTable.h:19