OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
18 
19 #include "CudaMgr/CudaMgr.h"
21 #include "Shared/Logger.h"
22 
23 namespace Buffer_Namespace {
24 
26  const size_t max_buffer_size,
28  const size_t buffer_alloc_increment,
29  const size_t page_size,
30  AbstractBufferMgr* parent_mgr)
31  : BufferMgr(device_id, max_buffer_size, buffer_alloc_increment, page_size, parent_mgr)
32  , cuda_mgr_(cuda_mgr) {}
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 slab_size) {
51  slabs_.resize(slabs_.size() + 1);
52  try {
53  slabs_.back() = cuda_mgr_->allocateDeviceMem(slab_size, device_id_);
54  } catch (std::runtime_error& error) {
55  slabs_.resize(slabs_.size() - 1);
56  throw FailedToCreateSlab(slab_size);
57  }
58  slab_segments_.resize(slab_segments_.size() + 1);
59  slab_segments_[slab_segments_.size() - 1].push_back(
60  BufferSeg(0, slab_size / page_size_));
61 }
62 
64  for (auto buf_it = slabs_.begin(); buf_it != slabs_.end(); ++buf_it) {
65  cuda_mgr_->freeDeviceMem(*buf_it);
66  }
67 }
68 
69 void GpuCudaBufferMgr::allocateBuffer(BufferList::iterator seg_it,
70  const size_t page_size,
71  const size_t initial_size) {
72  new GpuCudaBuffer(this,
73  seg_it,
74  device_id_,
75  cuda_mgr_,
76  page_size,
77  initial_size); // this line is admittedly a bit weird
78  // but the segment iterator passed into
79  // buffer takes the address of the new
80  // Buffer in its buffer member
81 }
82 
83 } // namespace Buffer_Namespace
#define LOG(tag)
Definition: Logger.h:185
std::vector< BufferList > slab_segments_
Definition: BufferMgr.h:177
Note(s): Forbid Copying Idiom 4.1.
Definition: BufferMgr.h:97
void addSlab(const size_t slab_size) override
void freeDeviceMem(int8_t *device_ptr)
Definition: CudaMgr.cpp:228
void allocateBuffer(BufferList::iterator seg_it, const size_t page_size, const size_t initial_size) override
CudaMgr_Namespace::CudaMgr * cuda_mgr_
void synchronizeDevices() const
Definition: CudaMgr.cpp:74
GpuCudaBufferMgr(const int device_id, const size_t max_buffer_size, CudaMgr_Namespace::CudaMgr *cuda_mgr, const size_t buffer_alloc_increment=1073741824, const size_t page_size=512, AbstractBufferMgr *parent_mgr=0)
std::vector< int8_t * > slabs_
Definition: BufferMgr.h:175
int8_t * allocateDeviceMem(const size_t num_bytes, const int device_num)
Definition: CudaMgr.cpp:217