OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 
17 #include "CudaAllocator.h"
18 
19 #include <CudaMgr/CudaMgr.h>
20 #include <DataMgr/DataMgr.h>
21 #include <Shared/types.h>
22 #include "../Rendering/RenderAllocator.h"
23 #include "Shared/Logger.h"
24 
26  : data_mgr_(data_mgr), device_id_(device_id) {
28 #ifdef HAVE_CUDA
29  const auto cuda_mgr = data_mgr_->getCudaMgr();
30  CHECK(cuda_mgr);
31  cuda_mgr->setContext(device_id);
32 #endif // HAVE_CUDA
33 }
34 
36  const size_t num_bytes,
37  const int device_id) {
38  CHECK(data_mgr);
39  auto ab = CudaAllocator::allocGpuAbstractBuffer(data_mgr, num_bytes, device_id);
40  return ab->getMemoryPtr();
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) {
60  return CudaAllocator::alloc(data_mgr_, num_bytes, device_id_);
61 }
62 
64  data_mgr_->free(ab);
65 }
66 
67 void CudaAllocator::copyToDevice(int8_t* device_dst,
68  const int8_t* host_src,
69  const size_t num_bytes) const {
70  const auto cuda_mgr = data_mgr_->getCudaMgr();
71  CHECK(cuda_mgr);
72  cuda_mgr->copyHostToDevice(device_dst, host_src, num_bytes, device_id_);
73 }
74 
75 void CudaAllocator::copyFromDevice(int8_t* host_dst,
76  const int8_t* device_src,
77  const size_t num_bytes) const {
78  const auto cuda_mgr = data_mgr_->getCudaMgr();
79  CHECK(cuda_mgr);
80  cuda_mgr->copyDeviceToHost(host_dst, device_src, num_bytes, device_id_);
81 }
82 
83 void CudaAllocator::zeroDeviceMem(int8_t* device_ptr, const size_t num_bytes) const {
84  const auto cuda_mgr = data_mgr_->getCudaMgr();
85  CHECK(cuda_mgr);
86  cuda_mgr->zeroDeviceMem(device_ptr, num_bytes, device_id_);
87 }
88 
89 void CudaAllocator::setDeviceMem(int8_t* device_ptr,
90  unsigned char uc,
91  const size_t num_bytes) const {
92  const auto cuda_mgr = data_mgr_->getCudaMgr();
93  CHECK(cuda_mgr);
94  cuda_mgr->setDeviceMem(device_ptr, uc, num_bytes, device_id_);
95 }
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
Definition: DataMgr.h:117
#define CHECK_EQ(x, y)
Definition: Logger.h:198
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:80
static int8_t * alloc(Data_Namespace::DataMgr *data_mgr, const size_t num_bytes, const int device_id)
CHECK(cgen_state)
An AbstractBuffer is a unit of data management for a data manager.
void free(Data_Namespace::AbstractBuffer *ab) const override
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
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
AbstractBuffer * alloc(const MemoryLevel memoryLevel, const int deviceId, const size_t numBytes)
Definition: DataMgr.cpp:374