OmniSciDB  62f1aeabe6
ThrustAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 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 
18 
19 #define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED 1
20 #include <boost/stacktrace.hpp>
21 
22 #include <cstdint>
23 
24 #include "CudaMgr/CudaMgr.h"
26 #include "DataMgr/DataMgr.h"
27 #include "Shared/Logger.h"
28 
29 int8_t* ThrustAllocator::allocate(std::ptrdiff_t num_bytes) {
30  VLOG(1) << "Thrust allocation: Device #" << device_id_ << " Allocation #"
31  << ++num_allocations_ << ": " << num_bytes << " bytes";
32 #ifdef HAVE_CUDA
33  if (!data_mgr_) { // only for unit tests
34  CUdeviceptr ptr;
35  const auto err = cuMemAlloc(&ptr, num_bytes);
36  CHECK_EQ(CUDA_SUCCESS, err);
37  return reinterpret_cast<int8_t*>(ptr);
38  }
39 #endif // HAVE_CUDA
42  int8_t* raw_ptr = reinterpret_cast<int8_t*>(ab->getMemoryPtr());
43  CHECK(!raw_to_ab_ptr_.count(raw_ptr));
44  raw_to_ab_ptr_.insert(std::make_pair(raw_ptr, ab));
45  return raw_ptr;
46 }
47 
48 void ThrustAllocator::deallocate(int8_t* ptr, size_t num_bytes) {
49 #ifdef HAVE_CUDA
50  if (!data_mgr_) { // only for unit tests
51  const auto err = cuMemFree(reinterpret_cast<CUdeviceptr>(ptr));
52  CHECK_EQ(CUDA_SUCCESS, err);
53  return;
54  }
55 #endif // HAVE_CUDA
56  PtrMapperType::iterator ab_it = raw_to_ab_ptr_.find(ptr);
57  CHECK(ab_it != raw_to_ab_ptr_.end());
58  data_mgr_->free(ab_it->second);
59  raw_to_ab_ptr_.erase(ab_it);
60 }
61 
62 int8_t* ThrustAllocator::allocateScopedBuffer(std::ptrdiff_t num_bytes) {
63 #ifdef HAVE_CUDA
64  if (!data_mgr_) { // only for unit tests
65  CUdeviceptr ptr;
66  const auto err = cuMemAlloc(&ptr, num_bytes);
67  CHECK_EQ(CUDA_SUCCESS, err);
68  default_alloc_scoped_buffers_.push_back(reinterpret_cast<int8_t*>(ptr));
69  return reinterpret_cast<int8_t*>(ptr);
70  }
71 #endif // HAVE_CUDA
74  scoped_buffers_.push_back(ab);
75  return reinterpret_cast<int8_t*>(ab->getMemoryPtr());
76 }
77 
79  for (auto ab : scoped_buffers_) {
80  data_mgr_->free(ab);
81  }
82 #ifdef HAVE_CUDA
83  for (auto ptr : default_alloc_scoped_buffers_) {
84  const auto err = cuMemFree(reinterpret_cast<CUdeviceptr>(ptr));
85  CHECK_EQ(CUDA_SUCCESS, err);
86  }
87 #endif // HAVE_CUDA
88  if (!raw_to_ab_ptr_.empty()) {
89  LOG(ERROR) << "Not all GPU buffers deallocated before destruction of Thrust "
90  "allocator for device "
91  << device_id_ << ". Remaining buffers: ";
92  for (auto& kv : raw_to_ab_ptr_) {
93  auto& ab = kv.second;
94  CHECK(ab);
95  LOG(ERROR) << (ab->pageCount() * ab->pageSize()) / (1024. * 1024.) << " MB";
96  }
97  VLOG(1) << boost::stacktrace::stacktrace();
98  }
99 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
int8_t * allocate(std::ptrdiff_t num_bytes)
#define LOG(tag)
Definition: Logger.h:188
std::vector< Data_Namespace::AbstractBuffer * > scoped_buffers_
virtual int8_t * getMemoryPtr()=0
unsigned long long CUdeviceptr
Definition: nocuda.h:27
void deallocate(int8_t *ptr, size_t num_bytes)
std::vector< int8_t * > default_alloc_scoped_buffers_
Data_Namespace::DataMgr * data_mgr_
const int device_id_
PtrMapperType raw_to_ab_ptr_
int8_t * allocateScopedBuffer(std::ptrdiff_t num_bytes)
An AbstractBuffer is a unit of data management for a data manager.
static Data_Namespace::AbstractBuffer * allocGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
#define CHECK(condition)
Definition: Logger.h:197
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:461
#define VLOG(n)
Definition: Logger.h:291