OmniSciDB  c07336695a
ThrustAllocator.h
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 /*
18  * @file ThrustAllocator.h
19  * @author Minggang Yu <miyu@mapd.com>
20  * @brief Allocate GPU memory using GpuBuffers via DataMgr. Unlike the CudaAllocator,
21  * these buffers are destroyed and memory is released when the parent object goes out of
22  * scope.
23  *
24  */
25 
26 #pragma once
27 
28 #include <unordered_map>
29 #include <vector>
30 
31 namespace Data_Namespace {
32 class DataMgr;
33 class AbstractBuffer;
34 }; // namespace Data_Namespace
35 
37  public:
38  using value_type = int8_t;
40  : data_mgr_(mgr), device_id_(id) {}
41  ~ThrustAllocator();
42 
43  int8_t* allocate(std::ptrdiff_t num_bytes);
44  void deallocate(int8_t* ptr, size_t num_bytes);
45 
46  int8_t* allocateScopedBuffer(std::ptrdiff_t num_bytes);
47 
48  Data_Namespace::DataMgr* getDataMgr() const { return data_mgr_; }
49 
50  int getDeviceId() const { return device_id_; }
51 
52  private:
54  const int device_id_;
55  using PtrMapperType = std::unordered_map<int8_t*, Data_Namespace::AbstractBuffer*>;
57  std::vector<Data_Namespace::AbstractBuffer*> scoped_buffers_;
58  std::vector<int8_t*> default_alloc_scoped_buffers_; // for unit tests only
59 };
std::unordered_map< int8_t *, Data_Namespace::AbstractBuffer * > PtrMapperType
std::vector< Data_Namespace::AbstractBuffer * > scoped_buffers_
Data_Namespace::DataMgr * getDataMgr() const
std::vector< int8_t * > default_alloc_scoped_buffers_
Data_Namespace::DataMgr * data_mgr_
const int device_id_
PtrMapperType raw_to_ab_ptr_
ThrustAllocator(Data_Namespace::DataMgr *mgr, const int id)
int getDeviceId() const