OmniSciDB  6686921089
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CudaAllocator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 MapD Technologies, 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 #include <CudaMgr/CudaMgr.h>
20 #include <DataMgr/DataMgr.h>
21 #include <Logger/Logger.h>
22 #include <Shared/types.h>
23 
25  : data_mgr_(data_mgr), device_id_(device_id) {
27 #ifdef HAVE_CUDA
28  const auto cuda_mgr = data_mgr_->getCudaMgr();
29  CHECK(cuda_mgr);
30  cuda_mgr->setContext(device_id);
31 #endif // HAVE_CUDA
32 }
33 
36  for (auto& buffer_ptr : owned_buffers_) {
37  data_mgr_->free(buffer_ptr);
38  }
39 }
40 
42  Data_Namespace::DataMgr* data_mgr,
43  const size_t num_bytes,
44  const int device_id) {
45  CHECK(data_mgr);
46  auto ab = data_mgr->alloc(Data_Namespace::GPU_LEVEL, device_id, num_bytes);
47  CHECK_EQ(ab->getPinCount(), 1);
48  return ab;
49 }
50 
53  CHECK(data_mgr);
54  data_mgr->free(ab);
55 }
56 
57 int8_t* CudaAllocator::alloc(const size_t num_bytes) {
59  owned_buffers_.emplace_back(
61  return owned_buffers_.back()->getMemoryPtr();
62 }
63 
65  data_mgr_->free(ab);
66 }
67 
68 void CudaAllocator::copyToDevice(void* device_dst,
69  const void* host_src,
70  const size_t num_bytes) const {
71  const auto cuda_mgr = data_mgr_->getCudaMgr();
72  CHECK(cuda_mgr);
73  cuda_mgr->copyHostToDevice(
74  (int8_t*)device_dst, (int8_t*)host_src, num_bytes, device_id_);
75 }
76 
77 void CudaAllocator::copyFromDevice(void* host_dst,
78  const void* device_src,
79  const size_t num_bytes) const {
80  const auto cuda_mgr = data_mgr_->getCudaMgr();
81  CHECK(cuda_mgr);
82  cuda_mgr->copyDeviceToHost(
83  (int8_t*)host_dst, (int8_t*)device_src, num_bytes, device_id_);
84 }
85 
86 void CudaAllocator::zeroDeviceMem(int8_t* device_ptr, const size_t num_bytes) const {
87  const auto cuda_mgr = data_mgr_->getCudaMgr();
88  CHECK(cuda_mgr);
89  cuda_mgr->zeroDeviceMem(device_ptr, num_bytes, device_id_);
90 }
91 
92 void CudaAllocator::setDeviceMem(int8_t* device_ptr,
93  unsigned char uc,
94  const size_t num_bytes) const {
95  const auto cuda_mgr = data_mgr_->getCudaMgr();
96  CHECK(cuda_mgr);
97  cuda_mgr->setDeviceMem(device_ptr, uc, num_bytes, device_id_);
98 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:218
#define CHECK_EQ(x, y)
Definition: Logger.h:217
CudaAllocator(Data_Namespace::DataMgr *data_mgr, const int device_id)
void zeroDeviceMem(int8_t *device_ptr, const size_t num_bytes) const override
Data_Namespace::DataMgr * data_mgr_
Definition: CudaAllocator.h:78
An AbstractBuffer is a unit of data management for a data manager.
void free(Data_Namespace::AbstractBuffer *ab) const override
void copyToDevice(void *device_dst, const void *host_src, const size_t num_bytes) const override
int8_t * alloc(const size_t num_bytes) override
std::vector< Data_Namespace::AbstractBuffer * > owned_buffers_
Definition: CudaAllocator.h:76
static void freeGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, Data_Namespace::AbstractBuffer *ab)
void setDeviceMem(int8_t *device_ptr, unsigned char uc, const size_t num_bytes) const override
~CudaAllocator() override
static Data_Namespace::AbstractBuffer * allocGpuAbstractBuffer(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
data_mgr_(data_mgr)
#define CHECK(condition)
Definition: Logger.h:209
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:530
void copyFromDevice(void *host_dst, const void *device_src, const size_t num_bytes) const override
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:513