OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaselineHashTable.h
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 
17 #pragma once
18 
19 #include "DataMgr/AbstractBuffer.h"
23 
25 
27  public:
28  BaselineHashTableEntryInfo(size_t num_hash_entries,
29  size_t num_keys,
30  size_t rowid_size_in_bytes,
31  size_t num_join_keys,
32  size_t join_key_size_in_byte,
33  HashType layout,
34  bool for_window_framing = false)
35  : HashTableEntryInfo(num_hash_entries,
36  num_keys,
37  rowid_size_in_bytes,
38  layout,
39  for_window_framing)
40  , num_join_keys_(num_join_keys)
41  , join_key_size_in_byte_(join_key_size_in_byte) {}
42 
43  size_t computeTotalNumSlots() const override { return num_hash_entries_; }
44 
45  size_t computeKeySize() const {
46  auto const entry_cnt = (num_join_keys_ + (layout_ == HashType::OneToOne ? 1 : 0));
47  return entry_cnt * join_key_size_in_byte_;
48  }
49 
53  : 0;
54  }
55 
56  size_t computeHashTableSize() const override {
59  }
60 
61  size_t getNumJoinKeys() const { return num_join_keys_; }
62 
63  size_t getJoinKeysSize() const { return join_key_size_in_byte_; }
64 
65  private:
66  size_t const num_join_keys_;
67  size_t const join_key_size_in_byte_;
68 };
69 
70 class BaselineHashTable : public HashTable {
71  public:
72  // CPU + GPU constructor
74  BaselineHashTableEntryInfo hash_table_entry_info,
75  Data_Namespace::DataMgr* data_mgr = nullptr,
76  const int device_id = -1)
77  : memory_level_(memory_level)
78  , hash_table_entry_info_(hash_table_entry_info)
79  , data_mgr_(data_mgr)
80  , device_id_(device_id) {
81  auto const hash_table_size = hash_table_entry_info.computeHashTableSize();
83 #ifdef HAVE_CUDA
87 #else
88  UNREACHABLE();
89 #endif
90  } else {
91  CHECK(!data_mgr_); // we do not need `data_mgr` for CPU hash table
92  cpu_hash_table_buff_.reset(new int8_t[hash_table_size]);
93  }
94  std::string device_str = memory_level_ == MemoryLevel::GPU_LEVEL ? "GPU" : "CPU";
95  std::string layout_str =
97  : "OneToMany";
98  std::ostringstream oss;
99  oss << "Initialize a " << device_str << " baseline hash table";
101  CHECK_GE(device_id, 0);
102  oss << " for device " << device_id_;
103  }
104  oss << " with join type " << layout_str << ", hash table size: " << hash_table_size
105  << " Bytes"
106  << ", # hash entries: " << hash_table_entry_info_.getNumHashEntries()
107  << ", # entries stored in the payload buffer: "
109  << ", rowid size: " << hash_table_entry_info_.getRowIdSizeInBytes() << " Bytes";
110  VLOG(1) << oss.str();
111  }
112 
113  ~BaselineHashTable() override {
114 #ifdef HAVE_CUDA
115  if (gpu_hash_table_buff_) {
116  CHECK(data_mgr_);
118  }
119 #endif
120  }
121 
122  int8_t* getGpuBuffer() const override {
124  }
125 
126  size_t getHashTableBufferSize(const ExecutorDeviceType device_type) const override {
128  }
129 
130  int8_t* getCpuBuffer() override {
131  return cpu_hash_table_buff_.get();
132  }
133 
134  HashType getLayout() const override {
136  }
137  size_t getEntryCount() const override {
139  }
140  size_t getEmittedKeysCount() const override {
142  }
143  size_t getRowIdSize() const override {
145  }
147  return hash_table_entry_info_;
148  }
149 
150  private:
151  std::unique_ptr<int8_t[]> cpu_hash_table_buff_;
153 
157  const int device_id_;
158 };
int8_t * getGpuBuffer() const override
size_t getRowIdSize() const override
size_t getEntryCount() const override
size_t num_hash_entries_
Definition: HashTable.h:65
size_t computeKeySize() const
size_t const num_join_keys_
virtual int8_t * getMemoryPtr()=0
BaselineHashTable(MemoryLevel memory_level, BaselineHashTableEntryInfo hash_table_entry_info, Data_Namespace::DataMgr *data_mgr=nullptr, const int device_id=-1)
HashType getLayout() const override
#define UNREACHABLE()
Definition: Logger.h:338
size_t num_keys_
Definition: HashTable.h:66
#define CHECK_GE(x, y)
Definition: Logger.h:306
Data_Namespace::DataMgr * data_mgr_
size_t getEmittedKeysCount() const override
BaselineHashTableEntryInfo(size_t num_hash_entries, size_t num_keys, size_t rowid_size_in_bytes, size_t num_join_keys, size_t join_key_size_in_byte, HashType layout, bool for_window_framing=false)
ExecutorDeviceType
bool for_window_framing_
Definition: HashTable.h:69
size_t getHashTableBufferSize(const ExecutorDeviceType device_type) const override
size_t computeHashTableSize() const override
size_t computeNumAdditionalSlotsForOneToManyJoin() const
BaselineHashTableEntryInfo getHashTableEntryInfo() const
HashType getHashTableLayout() const
Definition: HashTable.h:53
size_t getNumHashEntries() const
Definition: HashTable.h:50
size_t const join_key_size_in_byte_
size_t computeTotalNumSlots() const override
An AbstractBuffer is a unit of data management for a data manager.
std::unique_ptr< int8_t[]> cpu_hash_table_buff_
HashType layout_
Definition: HashTable.h:68
size_t getNumKeys() const
Definition: HashTable.h:51
BaselineHashTableEntryInfo hash_table_entry_info_
~BaselineHashTable() override
Data_Namespace::AbstractBuffer * gpu_hash_table_buff_
static Data_Namespace::AbstractBuffer * allocGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
Definition: HashTable.h:34
int8_t * getCpuBuffer() override
#define CHECK(condition)
Definition: Logger.h:291
size_t getNumJoinKeys() const
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614
size_t getRowIdSizeInBytes() const
Definition: HashTable.h:52
size_t getJoinKeysSize() const
size_t rowid_size_in_bytes_
Definition: HashTable.h:67
HashType
Definition: HashTable.h:19
#define VLOG(n)
Definition: Logger.h:388
static bool layoutRequiresAdditionalBuffers(HashType layout) noexcept
Definition: HashJoin.h:176
MemoryLevel memory_level_