OmniSciDB  8a228a1076
ThrustAllocator Class Reference

#include <ThrustAllocator.h>

+ Collaboration diagram for ThrustAllocator:

Public Types

using value_type = int8_t
 

Public Member Functions

 ThrustAllocator (Data_Namespace::DataMgr *mgr, const int id)
 
 ~ThrustAllocator ()
 
int8_t * allocate (std::ptrdiff_t num_bytes)
 
void deallocate (int8_t *ptr, size_t num_bytes)
 
int8_t * allocateScopedBuffer (std::ptrdiff_t num_bytes)
 
Data_Namespace::DataMgrgetDataMgr () const
 
int getDeviceId () const
 

Private Types

using PtrMapperType = std::unordered_map< int8_t *, Data_Namespace::AbstractBuffer * >
 

Private Attributes

Data_Namespace::DataMgrdata_mgr_
 
const int device_id_
 
PtrMapperType raw_to_ab_ptr_
 
std::vector< Data_Namespace::AbstractBuffer * > scoped_buffers_
 
std::vector< int8_t * > default_alloc_scoped_buffers_
 
size_t num_allocations_ {0}
 

Detailed Description

Definition at line 37 of file ThrustAllocator.h.

Member Typedef Documentation

◆ PtrMapperType

using ThrustAllocator::PtrMapperType = std::unordered_map<int8_t*, Data_Namespace::AbstractBuffer*>
private

Definition at line 56 of file ThrustAllocator.h.

◆ value_type

Definition at line 39 of file ThrustAllocator.h.

Constructor & Destructor Documentation

◆ ThrustAllocator()

ThrustAllocator::ThrustAllocator ( Data_Namespace::DataMgr mgr,
const int  id 
)
inline

Definition at line 40 of file ThrustAllocator.h.

41  : data_mgr_(mgr), device_id_(id) {}
Data_Namespace::DataMgr * data_mgr_
const int device_id_

◆ ~ThrustAllocator()

ThrustAllocator::~ThrustAllocator ( )

Definition at line 78 of file ThrustAllocator.cpp.

References CHECK, CHECK_EQ, data_mgr_, default_alloc_scoped_buffers_, device_id_, logger::ERROR, Data_Namespace::DataMgr::free(), LOG, raw_to_ab_ptr_, scoped_buffers_, and VLOG.

78  {
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
#define LOG(tag)
Definition: Logger.h:188
std::vector< Data_Namespace::AbstractBuffer * > scoped_buffers_
std::vector< int8_t * > default_alloc_scoped_buffers_
Data_Namespace::DataMgr * data_mgr_
const int device_id_
PtrMapperType raw_to_ab_ptr_
#define CHECK(condition)
Definition: Logger.h:197
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:478
#define VLOG(n)
Definition: Logger.h:291
+ Here is the call graph for this function:

Member Function Documentation

◆ allocate()

int8_t * ThrustAllocator::allocate ( std::ptrdiff_t  num_bytes)

Definition at line 29 of file ThrustAllocator.cpp.

References CudaAllocator::allocGpuAbstractBuffer(), CHECK, CHECK_EQ, data_mgr_, device_id_, Data_Namespace::AbstractBuffer::getMemoryPtr(), num_allocations_, raw_to_ab_ptr_, and VLOG.

Referenced by Data_Namespace::detail::DataMgrMemoryResource< Pointer >::do_allocate().

29  {
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 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
virtual int8_t * getMemoryPtr()=0
unsigned long long CUdeviceptr
Definition: nocuda.h:27
Data_Namespace::DataMgr * data_mgr_
const int device_id_
PtrMapperType raw_to_ab_ptr_
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
#define VLOG(n)
Definition: Logger.h:291
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ allocateScopedBuffer()

int8_t * ThrustAllocator::allocateScopedBuffer ( std::ptrdiff_t  num_bytes)

Definition at line 62 of file ThrustAllocator.cpp.

References CudaAllocator::allocGpuAbstractBuffer(), CHECK_EQ, data_mgr_, default_alloc_scoped_buffers_, device_id_, Data_Namespace::AbstractBuffer::getMemoryPtr(), and scoped_buffers_.

62  {
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 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::vector< Data_Namespace::AbstractBuffer * > scoped_buffers_
virtual int8_t * getMemoryPtr()=0
unsigned long long CUdeviceptr
Definition: nocuda.h:27
std::vector< int8_t * > default_alloc_scoped_buffers_
Data_Namespace::DataMgr * data_mgr_
const int device_id_
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)
+ Here is the call graph for this function:

◆ deallocate()

void ThrustAllocator::deallocate ( int8_t *  ptr,
size_t  num_bytes 
)

Definition at line 48 of file ThrustAllocator.cpp.

References CHECK, CHECK_EQ, data_mgr_, Data_Namespace::DataMgr::free(), and raw_to_ab_ptr_.

Referenced by Data_Namespace::detail::DataMgrMemoryResource< Pointer >::do_deallocate().

48  {
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 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
Data_Namespace::DataMgr * data_mgr_
PtrMapperType raw_to_ab_ptr_
#define CHECK(condition)
Definition: Logger.h:197
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:478
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getDataMgr()

Data_Namespace::DataMgr* ThrustAllocator::getDataMgr ( ) const
inline

Definition at line 49 of file ThrustAllocator.h.

49 { return data_mgr_; }
Data_Namespace::DataMgr * data_mgr_

◆ getDeviceId()

int ThrustAllocator::getDeviceId ( ) const
inline

Definition at line 51 of file ThrustAllocator.h.

51 { return device_id_; }
const int device_id_

Member Data Documentation

◆ data_mgr_

Data_Namespace::DataMgr* ThrustAllocator::data_mgr_
private

Definition at line 54 of file ThrustAllocator.h.

Referenced by allocate(), allocateScopedBuffer(), deallocate(), and ~ThrustAllocator().

◆ default_alloc_scoped_buffers_

std::vector<int8_t*> ThrustAllocator::default_alloc_scoped_buffers_
private

Definition at line 59 of file ThrustAllocator.h.

Referenced by allocateScopedBuffer(), and ~ThrustAllocator().

◆ device_id_

const int ThrustAllocator::device_id_
private

Definition at line 55 of file ThrustAllocator.h.

Referenced by allocate(), allocateScopedBuffer(), and ~ThrustAllocator().

◆ num_allocations_

size_t ThrustAllocator::num_allocations_ {0}
private

Definition at line 60 of file ThrustAllocator.h.

Referenced by allocate().

◆ raw_to_ab_ptr_

PtrMapperType ThrustAllocator::raw_to_ab_ptr_
private

Definition at line 57 of file ThrustAllocator.h.

Referenced by allocate(), deallocate(), and ~ThrustAllocator().

◆ scoped_buffers_

std::vector<Data_Namespace::AbstractBuffer*> ThrustAllocator::scoped_buffers_
private

Definition at line 58 of file ThrustAllocator.h.

Referenced by allocateScopedBuffer(), and ~ThrustAllocator().


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