OmniSciDB  8a228a1076
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(int8_t* device_dst,
69  const int8_t* 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(device_dst, host_src, num_bytes, device_id_);
74 }
75 
76 void CudaAllocator::copyFromDevice(int8_t* host_dst,
77  const int8_t* device_src,
78  const size_t num_bytes) const {
79  const auto cuda_mgr = data_mgr_->getCudaMgr();
80  CHECK(cuda_mgr);
81  cuda_mgr->copyDeviceToHost(host_dst, device_src, num_bytes, device_id_);
82 }
83 
84 void CudaAllocator::zeroDeviceMem(int8_t* device_ptr, const size_t num_bytes) const {
85  const auto cuda_mgr = data_mgr_->getCudaMgr();
86  CHECK(cuda_mgr);
87  cuda_mgr->zeroDeviceMem(device_ptr, num_bytes, device_id_);
88 }
89 
90 void CudaAllocator::setDeviceMem(int8_t* device_ptr,
91  unsigned char uc,
92  const size_t num_bytes) const {
93  const auto cuda_mgr = data_mgr_->getCudaMgr();
94  CHECK(cuda_mgr);
95  cuda_mgr->setDeviceMem(device_ptr, uc, num_bytes, device_id_);
96 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
void copyFromDevice(int8_t *host_dst, const int8_t *device_src, const size_t num_bytes) const override
CudaAllocator(Data_Namespace::DataMgr *data_mgr, const int device_id)
void copyToDevice(int8_t *device_dst, const int8_t *host_src, const size_t num_bytes) const override
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
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
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:209
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:478
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:469