OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TreeModelPredictionMgr Class Reference

#include <TreeModelPredictionMgr.h>

+ Collaboration diagram for TreeModelPredictionMgr:

Public Member Functions

 TreeModelPredictionMgr (const Data_Namespace::MemoryLevel memory_level, Executor *executor, const std::vector< std::vector< DecisionTreeEntry >> &decision_trees, const std::vector< int64_t > &decision_tree_offsets, const bool compute_avg)
 
 ~TreeModelPredictionMgr ()
 
llvm::Value * codegen (const std::vector< llvm::Value * > &regressor_inputs, const CompilationOptions &co) const
 

Private Member Functions

void allocateAndPopulateHostBuffers (const std::vector< std::vector< DecisionTreeEntry >> &decision_trees, const std::vector< int64_t > &decision_tree_offsets)
 
void createKernelBuffers ()
 

Private Attributes

const Data_Namespace::MemoryLevel memory_level_
 
Executorexecutor_
 
Data_Namespace::DataMgrdata_mgr_
 
const int device_count_
 
const int32_t num_trees_
 
const bool compute_avg_
 
int8_t * host_decision_tree_table_
 
int8_t * host_decision_tree_offsets_
 
int64_t decision_tree_table_size_bytes_
 
int64_t decision_tree_offsets_size_bytes_
 
std::vector
< Data_Namespace::AbstractBuffer * > 
decision_tree_table_device_buffers_
 
std::vector
< Data_Namespace::AbstractBuffer * > 
decision_tree_offsets_device_buffers_
 
std::vector< const int8_t * > kernel_decision_tree_tables_
 
std::vector< const int8_t * > kernel_decision_tree_offsets_
 

Detailed Description

Definition at line 37 of file TreeModelPredictionMgr.h.

Constructor & Destructor Documentation

TreeModelPredictionMgr::TreeModelPredictionMgr ( const Data_Namespace::MemoryLevel  memory_level,
Executor executor,
const std::vector< std::vector< DecisionTreeEntry >> &  decision_trees,
const std::vector< int64_t > &  decision_tree_offsets,
const bool  compute_avg 
)

Definition at line 28 of file TreeModelPredictionMgr.cpp.

References allocateAndPopulateHostBuffers(), CHECK, CHECK_EQ, Data_Namespace::CPU_LEVEL, createKernelBuffers(), Data_Namespace::GPU_LEVEL, and memory_level_.

34  : memory_level_(memory_level)
35  , executor_(executor)
36  , data_mgr_(executor->getDataMgr())
37  , device_count_(executor->deviceCount(memory_level == Data_Namespace::GPU_LEVEL
40  , num_trees_(decision_trees.size())
41  , compute_avg_(compute_avg) {
42 #ifdef HAVE_CUDA
45 #else
47 #endif // HAVE_CUDA
48  allocateAndPopulateHostBuffers(decision_trees, decision_tree_offsets);
50 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
Data_Namespace::DataMgr * data_mgr_
#define CHECK(condition)
Definition: Logger.h:291
void allocateAndPopulateHostBuffers(const std::vector< std::vector< DecisionTreeEntry >> &decision_trees, const std::vector< int64_t > &decision_tree_offsets)
const Data_Namespace::MemoryLevel memory_level_

+ Here is the call graph for this function:

TreeModelPredictionMgr::~TreeModelPredictionMgr ( )

Definition at line 52 of file TreeModelPredictionMgr.cpp.

References CHECK, data_mgr_, decision_tree_offsets_device_buffers_, decision_tree_table_device_buffers_, and Data_Namespace::DataMgr::free().

52  {
54  for (auto* buffer : decision_tree_table_device_buffers_) {
55  CHECK(buffer);
56  data_mgr_->free(buffer);
57  }
58  for (auto* buffer : decision_tree_offsets_device_buffers_) {
59  CHECK(buffer);
60  data_mgr_->free(buffer);
61  }
62 }
std::vector< Data_Namespace::AbstractBuffer * > decision_tree_table_device_buffers_
Data_Namespace::DataMgr * data_mgr_
std::vector< Data_Namespace::AbstractBuffer * > decision_tree_offsets_device_buffers_
#define CHECK(condition)
Definition: Logger.h:291
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614

+ Here is the call graph for this function:

Member Function Documentation

void TreeModelPredictionMgr::allocateAndPopulateHostBuffers ( const std::vector< std::vector< DecisionTreeEntry >> &  decision_trees,
const std::vector< int64_t > &  decision_tree_offsets 
)
private

Definition at line 64 of file TreeModelPredictionMgr.cpp.

References CHECK_EQ, DEBUG_TIMER, decision_tree_offsets_size_bytes_, decision_tree_table_size_bytes_, executor_, host_decision_tree_offsets_, host_decision_tree_table_, num_trees_, and threading_serial::parallel_for().

Referenced by TreeModelPredictionMgr().

66  {
67  auto timer = DEBUG_TIMER(__func__);
68  const size_t num_trees = decision_trees.size();
69  CHECK_EQ(num_trees, static_cast<size_t>(num_trees_));
70  CHECK_EQ(num_trees, decision_tree_offsets.size() - 1);
71  const size_t num_tree_entries = decision_tree_offsets[num_trees];
72  decision_tree_table_size_bytes_ = num_tree_entries * sizeof(DecisionTreeEntry);
73  decision_tree_offsets_size_bytes_ = decision_tree_offsets.size() * sizeof(size_t);
75  executor_->getRowSetMemoryOwner()->allocate(decision_tree_table_size_bytes_);
77  executor_->getRowSetMemoryOwner()->allocate(decision_tree_offsets_size_bytes_);
78  // Take this opportunity to copy offsets buffer over
79  std::memcpy(host_decision_tree_offsets_,
80  reinterpret_cast<const int8_t*>(decision_tree_offsets.data()),
81  decision_tree_offsets_size_bytes_);
82 
84  tbb::blocked_range<size_t>(0, num_trees), [&](const tbb::blocked_range<size_t>& r) {
85  const auto start_tree_idx = r.begin();
86  const auto end_tree_idx = r.end();
87  for (size_t tree_idx = start_tree_idx; tree_idx < end_tree_idx; ++tree_idx) {
88  std::memcpy(host_decision_tree_table_ +
89  decision_tree_offsets[tree_idx] * sizeof(DecisionTreeEntry),
90  reinterpret_cast<const int8_t*>(decision_trees[tree_idx].data()),
91  decision_trees[tree_idx].size() * sizeof(DecisionTreeEntry));
92  }
93  });
94 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
void parallel_for(const blocked_range< Int > &range, const Body &body, const Partitioner &p=Partitioner())
#define DEBUG_TIMER(name)
Definition: Logger.h:412

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * TreeModelPredictionMgr::codegen ( const std::vector< llvm::Value * > &  regressor_inputs,
const CompilationOptions co 
) const

Definition at line 158 of file TreeModelPredictionMgr.cpp.

References AUTOMATIC_IR_METADATA, CHECK, CodeGenerator::codegen(), CodeGenerator::codegenHoistedConstants(), compute_avg_, device_count_, CompilationOptions::device_type, executor_, generate_kernel_buffer_constants(), GPU, Data_Namespace::GPU_LEVEL, CompilationOptions::hoist_literals, inline_fp_null_value< double >(), kENCODING_NONE, kernel_decision_tree_offsets_, kernel_decision_tree_tables_, memory_level_, and num_trees_.

Referenced by CodeGenerator::codegenTreeRegPredict().

160  {
162  CHECK(kernel_decision_tree_tables_.size() == static_cast<size_t>(device_count_));
163  if (!co.hoist_literals && kernel_decision_tree_tables_.size() > 1UL) {
166  throw QueryMustRunOnCpu();
167  }
169 
170  auto cgen_state_ptr = executor_->getCgenStatePtr();
171  AUTOMATIC_IR_METADATA(cgen_state_ptr);
172 
173  const auto [decision_tree_table_constants_owned, decision_tree_table_constants] =
175  cgen_state_ptr, kernel_decision_tree_tables_, co.hoist_literals);
176 
177  const auto [decision_tree_offsets_constants_owned, decision_tree_offsets_constants] =
179  cgen_state_ptr, kernel_decision_tree_offsets_, co.hoist_literals);
180 
181  CodeGenerator code_generator(executor_);
182 
183  const auto decision_tree_table_handle_lvs =
184  co.hoist_literals
185  ? code_generator.codegenHoistedConstants(
186  decision_tree_table_constants, kENCODING_NONE, {})
187  : code_generator.codegen(decision_tree_table_constants[0], false, co);
188 
189  const auto decision_tree_offsets_handle_lvs =
190  co.hoist_literals
191  ? code_generator.codegenHoistedConstants(
192  decision_tree_offsets_constants, kENCODING_NONE, {})
193  : code_generator.codegen(decision_tree_offsets_constants[0], false, co);
194 
195  auto& builder = cgen_state_ptr->ir_builder_;
196  const int32_t num_regressors = static_cast<int32_t>(regressor_inputs.size());
197  auto regressor_ty = llvm::Type::getDoubleTy(cgen_state_ptr->context_);
198  llvm::ArrayType* regressor_arr_type =
199  llvm::ArrayType::get(regressor_ty, num_regressors);
200  auto regressor_local_storage_lv =
201  builder.CreateAlloca(regressor_arr_type, nullptr, "Regressor_Local_Storage");
202  auto idx_lv = cgen_state_ptr->llInt(0);
203  auto regressor_local_storage_gep = llvm::GetElementPtrInst::CreateInBounds(
204  regressor_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
205  regressor_local_storage_lv,
206  {idx_lv, idx_lv},
207  "",
208  builder.GetInsertBlock());
209  for (int32_t reg_idx = 0; reg_idx < num_regressors; ++reg_idx) {
210  auto reg_ptr = builder.CreateGEP(
211  regressor_local_storage_lv->getType()->getScalarType()->getPointerElementType(),
212  regressor_local_storage_lv,
213  {cgen_state_ptr->llInt(0), cgen_state_ptr->llInt(reg_idx)},
214  "");
215  builder.CreateStore(regressor_inputs[reg_idx], reg_ptr);
216  }
217  const double translated_null_value = inline_fp_null_value<double>();
218 
219  return cgen_state_ptr->emitCall(
220  "tree_model_reg_predict",
221  {regressor_local_storage_gep,
222  cgen_state_ptr->castToTypeIn(decision_tree_table_handle_lvs.front(), 64),
223  cgen_state_ptr->castToTypeIn(decision_tree_offsets_handle_lvs.front(), 64),
224  cgen_state_ptr->llInt(num_regressors),
225  cgen_state_ptr->llInt(num_trees_),
226  cgen_state_ptr->llBool(compute_avg_),
227  cgen_state_ptr->llFp(translated_null_value)});
228 }
std::vector< const int8_t * > kernel_decision_tree_tables_
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
std::pair< std::vector< std::shared_ptr< const Analyzer::Constant > >, std::vector< const Analyzer::Constant * > > generate_kernel_buffer_constants(CgenState *cgen_state_ptr, const std::vector< const int8_t * > &kernel_buffers, const bool hoist_literals)
constexpr double inline_fp_null_value< double >()
std::vector< const int8_t * > kernel_decision_tree_offsets_
#define CHECK(condition)
Definition: Logger.h:291
const Data_Namespace::MemoryLevel memory_level_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void TreeModelPredictionMgr::createKernelBuffers ( )
private

Definition at line 96 of file TreeModelPredictionMgr.cpp.

References CudaAllocator::allocGpuAbstractBuffer(), CHECK_EQ, copy_to_nvidia_gpu(), Data_Namespace::CPU_LEVEL, data_mgr_, DEBUG_TIMER, decision_tree_offsets_device_buffers_, decision_tree_offsets_size_bytes_, decision_tree_table_device_buffers_, decision_tree_table_size_bytes_, device_count_, Data_Namespace::GPU_LEVEL, host_decision_tree_offsets_, host_decision_tree_table_, kernel_decision_tree_offsets_, kernel_decision_tree_tables_, and memory_level_.

Referenced by TreeModelPredictionMgr().

96  {
97  auto timer = DEBUG_TIMER(__func__);
98 #ifdef HAVE_CUDA
100  for (int device_id = 0; device_id < device_count_; ++device_id) {
107  auto decision_tree_table_device_buffer = reinterpret_cast<const int8_t*>(
108  decision_tree_table_device_buffers_.back()->getMemoryPtr());
109  auto decision_tree_offsets_device_buffer = reinterpret_cast<const int8_t*>(
110  decision_tree_offsets_device_buffers_.back()->getMemoryPtr());
112  reinterpret_cast<CUdeviceptr>(decision_tree_table_device_buffer),
113  reinterpret_cast<const int8_t*>(host_decision_tree_table_),
115  device_id);
117  data_mgr_,
118  reinterpret_cast<CUdeviceptr>(decision_tree_offsets_device_buffer),
119  reinterpret_cast<const int8_t*>(host_decision_tree_offsets_),
121  device_id);
122  kernel_decision_tree_tables_.push_back(decision_tree_table_device_buffer);
123  kernel_decision_tree_offsets_.push_back(decision_tree_offsets_device_buffer);
124  }
125  }
126 #else
127  CHECK_EQ(1, device_count_);
128 #endif
132  }
133 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
std::vector< Data_Namespace::AbstractBuffer * > decision_tree_table_device_buffers_
Data_Namespace::DataMgr * data_mgr_
std::vector< const int8_t * > kernel_decision_tree_tables_
static Data_Namespace::AbstractBuffer * allocGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
std::vector< Data_Namespace::AbstractBuffer * > decision_tree_offsets_device_buffers_
void copy_to_nvidia_gpu(Data_Namespace::DataMgr *data_mgr, CUdeviceptr dst, const void *src, const size_t num_bytes, const int device_id)
Definition: GpuMemUtils.cpp:35
std::vector< const int8_t * > kernel_decision_tree_offsets_
#define DEBUG_TIMER(name)
Definition: Logger.h:412
const Data_Namespace::MemoryLevel memory_level_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

const bool TreeModelPredictionMgr::compute_avg_
private

Definition at line 61 of file TreeModelPredictionMgr.h.

Referenced by codegen().

Data_Namespace::DataMgr* TreeModelPredictionMgr::data_mgr_
private

Definition at line 58 of file TreeModelPredictionMgr.h.

Referenced by createKernelBuffers(), and ~TreeModelPredictionMgr().

std::vector<Data_Namespace::AbstractBuffer*> TreeModelPredictionMgr::decision_tree_offsets_device_buffers_
private

Definition at line 67 of file TreeModelPredictionMgr.h.

Referenced by createKernelBuffers(), and ~TreeModelPredictionMgr().

int64_t TreeModelPredictionMgr::decision_tree_offsets_size_bytes_
private

Definition at line 65 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and createKernelBuffers().

std::vector<Data_Namespace::AbstractBuffer*> TreeModelPredictionMgr::decision_tree_table_device_buffers_
private

Definition at line 66 of file TreeModelPredictionMgr.h.

Referenced by createKernelBuffers(), and ~TreeModelPredictionMgr().

int64_t TreeModelPredictionMgr::decision_tree_table_size_bytes_
private

Definition at line 64 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and createKernelBuffers().

const int TreeModelPredictionMgr::device_count_
private

Definition at line 59 of file TreeModelPredictionMgr.h.

Referenced by codegen(), and createKernelBuffers().

Executor* TreeModelPredictionMgr::executor_
private

Definition at line 57 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and codegen().

int8_t* TreeModelPredictionMgr::host_decision_tree_offsets_
private

Definition at line 63 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and createKernelBuffers().

int8_t* TreeModelPredictionMgr::host_decision_tree_table_
private

Definition at line 62 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and createKernelBuffers().

std::vector<const int8_t*> TreeModelPredictionMgr::kernel_decision_tree_offsets_
private

Definition at line 69 of file TreeModelPredictionMgr.h.

Referenced by codegen(), and createKernelBuffers().

std::vector<const int8_t*> TreeModelPredictionMgr::kernel_decision_tree_tables_
private

Definition at line 68 of file TreeModelPredictionMgr.h.

Referenced by codegen(), and createKernelBuffers().

const Data_Namespace::MemoryLevel TreeModelPredictionMgr::memory_level_
private

Definition at line 56 of file TreeModelPredictionMgr.h.

Referenced by codegen(), createKernelBuffers(), and TreeModelPredictionMgr().

const int32_t TreeModelPredictionMgr::num_trees_
private

Definition at line 60 of file TreeModelPredictionMgr.h.

Referenced by allocateAndPopulateHostBuffers(), and codegen().


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