OmniSciDB  a5dc49c757
 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 2022 HEAVY.AI, 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  const int device_id,
26  CUstream cuda_stream)
27  : data_mgr_(data_mgr), device_id_(device_id), cuda_stream_(cuda_stream) {
29 #ifdef HAVE_CUDA
30  const auto cuda_mgr = data_mgr_->getCudaMgr();
31  CHECK(cuda_mgr);
32  cuda_mgr->setContext(device_id);
33 #endif // HAVE_CUDA
34 }
35 
38  for (auto& buffer_ptr : owned_buffers_) {
39  data_mgr_->free(buffer_ptr);
40  }
41 }
42 
44  Data_Namespace::DataMgr* data_mgr,
45  const size_t num_bytes,
46  const int device_id) {
47  CHECK(data_mgr);
48  auto ab = data_mgr->alloc(Data_Namespace::GPU_LEVEL, device_id, num_bytes);
49  CHECK_EQ(ab->getPinCount(), 1);
50  return ab;
51 }
52 
55  CHECK(data_mgr);
56  data_mgr->free(ab);
57 }
58 
59 int8_t* CudaAllocator::alloc(const size_t num_bytes) {
61  owned_buffers_.emplace_back(
63  return owned_buffers_.back()->getMemoryPtr();
64 }
65 
67  data_mgr_->free(ab);
68 }
69 
70 void CudaAllocator::copyToDevice(void* device_dst,
71  const void* host_src,
72  const size_t num_bytes) const {
73  const auto cuda_mgr = data_mgr_->getCudaMgr();
74  CHECK(cuda_mgr);
75  cuda_mgr->copyHostToDevice(
76  (int8_t*)device_dst, (int8_t*)host_src, num_bytes, device_id_, cuda_stream_);
77 }
78 
79 void CudaAllocator::copyFromDevice(void* host_dst,
80  const void* device_src,
81  const size_t num_bytes) const {
82  const auto cuda_mgr = data_mgr_->getCudaMgr();
83  CHECK(cuda_mgr);
84  cuda_mgr->copyDeviceToHost(
85  (int8_t*)host_dst, (int8_t*)device_src, num_bytes, cuda_stream_);
86 }
87 
88 void CudaAllocator::zeroDeviceMem(int8_t* device_ptr, const size_t num_bytes) const {
89  const auto cuda_mgr = data_mgr_->getCudaMgr();
90  CHECK(cuda_mgr);
91  cuda_mgr->zeroDeviceMem(device_ptr, num_bytes, device_id_, cuda_stream_);
92 }
93 
94 void CudaAllocator::setDeviceMem(int8_t* device_ptr,
95  unsigned char uc,
96  const size_t num_bytes) const {
97  const auto cuda_mgr = data_mgr_->getCudaMgr();
98  CHECK(cuda_mgr);
99  cuda_mgr->setDeviceMem(device_ptr, uc, num_bytes, device_id_, cuda_stream_);
100 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:177
#define CHECK_EQ(x, y)
Definition: Logger.h:301
void * CUstream
Definition: nocuda.h:23
void zeroDeviceMem(int8_t *device_ptr, const size_t num_bytes) const override
CudaAllocator(Data_Namespace::DataMgr *data_mgr, const int device_id, CUstream cuda_stream)
Data_Namespace::DataMgr * data_mgr_
Definition: CudaAllocator.h:79
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:77
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:291
CUstream cuda_stream_
Definition: CudaAllocator.h:81
Allocate GPU memory using GpuBuffers via DataMgr.
void free(AbstractBuffer *buffer)
Definition: DataMgr.cpp:614
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:605