OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
17 #include "ThrustAllocator.h"
18 #include "CudaAllocator.h"
19 #include "Shared/Logger.h"
20 
21 #include <CudaMgr/CudaMgr.h>
22 #include <DataMgr/DataMgr.h>
23 
24 int8_t* ThrustAllocator::allocate(std::ptrdiff_t num_bytes) {
25 #ifdef HAVE_CUDA
26  if (!data_mgr_) { // only for unit tests
27  CUdeviceptr ptr;
28  const auto err = cuMemAlloc(&ptr, num_bytes);
29  CHECK_EQ(CUDA_SUCCESS, err);
30  return reinterpret_cast<int8_t*>(ptr);
31  }
32 #endif // HAVE_CUDA
35  int8_t* raw_ptr = reinterpret_cast<int8_t*>(ab->getMemoryPtr());
36  CHECK(!raw_to_ab_ptr_.count(raw_ptr));
37  raw_to_ab_ptr_.insert(std::make_pair(raw_ptr, ab));
38  return raw_ptr;
39 }
40 
41 void ThrustAllocator::deallocate(int8_t* ptr, size_t num_bytes) {
42 #ifdef HAVE_CUDA
43  if (!data_mgr_) { // only for unit tests
44  const auto err = cuMemFree(reinterpret_cast<CUdeviceptr>(ptr));
45  CHECK_EQ(CUDA_SUCCESS, err);
46  return;
47  }
48 #endif // HAVE_CUDA
49  PtrMapperType::iterator ab_it = raw_to_ab_ptr_.find(ptr);
50  CHECK(ab_it != raw_to_ab_ptr_.end());
51  data_mgr_->free(ab_it->second);
52  raw_to_ab_ptr_.erase(ab_it);
53 }
54 
55 int8_t* ThrustAllocator::allocateScopedBuffer(std::ptrdiff_t num_bytes) {
56 #ifdef HAVE_CUDA
57  if (!data_mgr_) { // only for unit tests
58  CUdeviceptr ptr;
59  const auto err = cuMemAlloc(&ptr, num_bytes);
60  CHECK_EQ(CUDA_SUCCESS, err);
61  default_alloc_scoped_buffers_.push_back(reinterpret_cast<int8_t*>(ptr));
62  return reinterpret_cast<int8_t*>(ptr);
63  }
64 #endif // HAVE_CUDA
67  scoped_buffers_.push_back(ab);
68  return reinterpret_cast<int8_t*>(ab->getMemoryPtr());
69 }
70 
72  for (auto ab : scoped_buffers_) {
73  data_mgr_->free(ab);
74  }
75 #ifdef HAVE_CUDA
76  for (auto ptr : default_alloc_scoped_buffers_) {
77  const auto err = cuMemFree(reinterpret_cast<CUdeviceptr>(ptr));
78  CHECK_EQ(CUDA_SUCCESS, err);
79  }
80 #endif // HAVE_CUDA
81 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
int8_t * allocate(std::ptrdiff_t num_bytes)
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)
CHECK(cgen_state)
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)
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:382