OmniSciDB  5ade3759e0
GpuCudaBufferMgr.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 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 "GpuCudaBufferMgr.h"
18 #include "../../../CudaMgr/CudaMgr.h"
19 #include "GpuCudaBuffer.h"
20 #include "Shared/Logger.h"
21 //#include "../CudaUtils.h"
22 
23 namespace Buffer_Namespace {
24 
26  const size_t maxBufferSize,
28  const size_t bufferAllocIncrement,
29  const size_t pageSize,
30  AbstractBufferMgr* parentMgr)
31  : BufferMgr(deviceId, maxBufferSize, bufferAllocIncrement, pageSize, parentMgr)
32  , cudaMgr_(cudaMgr) {}
33 
35  try {
37  freeAllMem();
38 #ifdef HAVE_CUDA
39  } catch (const CudaMgr_Namespace::CudaErrorException& e) {
40  if (e.getStatus() == CUDA_ERROR_DEINITIALIZED) {
41  // TODO(adb / asuhan): Verify cuModuleUnload removes the context
42  return;
43  }
44 #endif
45  } catch (const std::runtime_error& e) {
46  LOG(ERROR) << "CUDA Error: " << e.what();
47  }
48 }
49 
50 void GpuCudaBufferMgr::addSlab(const size_t slabSize) {
51  slabs_.resize(slabs_.size() + 1);
52  try {
53  slabs_.back() = cudaMgr_->allocateDeviceMem(slabSize, deviceId_);
54  } catch (std::runtime_error& error) {
55  slabs_.resize(slabs_.size() - 1);
56  throw FailedToCreateSlab(slabSize);
57  }
58  slabSegments_.resize(slabSegments_.size() + 1);
59  slabSegments_[slabSegments_.size() - 1].push_back(BufferSeg(0, slabSize / pageSize_));
60 }
61 
63  for (auto bufIt = slabs_.begin(); bufIt != slabs_.end(); ++bufIt) {
64  cudaMgr_->freeDeviceMem(*bufIt);
65  }
66 }
67 
68 void GpuCudaBufferMgr::allocateBuffer(BufferList::iterator segIt,
69  const size_t pageSize,
70  const size_t initialSize) {
71  new GpuCudaBuffer(this,
72  segIt,
73  deviceId_,
74  cudaMgr_,
75  pageSize,
76  initialSize); // this line is admittedly a bit weird
77  // but the segment iterator passed into
78  // buffer takes the address of the new
79  // Buffer in its buffer member
80 }
81 
82 } // namespace Buffer_Namespace
void allocateBuffer(BufferList::iterator segIt, const size_t pageSize, const size_t initialSize) override
void addSlab(const size_t slabSize) override
#define LOG(tag)
Definition: Logger.h:182
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:96
void freeDeviceMem(int8_t *device_ptr)
Definition: CudaMgr.cpp:215
void synchronizeDevices() const
Definition: CudaMgr.cpp:64
GpuCudaBufferMgr(const int deviceId, const size_t maxBufferSize, CudaMgr_Namespace::CudaMgr *cudaMgr, const size_t bufferAllocIncrement=1073741824, const size_t pageSize=512, AbstractBufferMgr *parentMgr=0)
std::vector< int8_t * > slabs_
Definition: BufferMgr.h:176
int8_t * allocateDeviceMem(const size_t num_bytes, const int device_num)
Definition: CudaMgr.cpp:204
CudaMgr_Namespace::CudaMgr * cudaMgr_
std::vector< BufferList > slabSegments_
Definition: BufferMgr.h:178