OmniSciDB  8a228a1076
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 "Logger/Logger.h"
22 
23 namespace Buffer_Namespace {
24 
26  const size_t max_buffer_pool_size,
28  const size_t min_slab_size,
29  const size_t max_slab_size,
30  const size_t page_size,
31  AbstractBufferMgr* parent_mgr)
32  : BufferMgr(device_id,
33  max_buffer_pool_size,
34  min_slab_size,
35  max_slab_size,
36  page_size,
37  parent_mgr)
38  , cuda_mgr_(cuda_mgr) {}
39 
41  try {
43  freeAllMem();
44 #ifdef HAVE_CUDA
45  } catch (const CudaMgr_Namespace::CudaErrorException& e) {
46  if (e.getStatus() == CUDA_ERROR_DEINITIALIZED) {
47  // TODO(adb / asuhan): Verify cuModuleUnload removes the context
48  return;
49  }
50 #endif
51  } catch (const std::runtime_error& e) {
52  LOG(ERROR) << "CUDA Error: " << e.what();
53  }
54 }
55 
56 void GpuCudaBufferMgr::addSlab(const size_t slab_size) {
57  slabs_.resize(slabs_.size() + 1);
58  try {
59  slabs_.back() = cuda_mgr_->allocateDeviceMem(slab_size, device_id_);
60  } catch (std::runtime_error& error) {
61  slabs_.resize(slabs_.size() - 1);
62  throw FailedToCreateSlab(slab_size);
63  }
64  slab_segments_.resize(slab_segments_.size() + 1);
65  slab_segments_[slab_segments_.size() - 1].push_back(
66  BufferSeg(0, slab_size / page_size_));
67 }
68 
70  for (auto buf_it = slabs_.begin(); buf_it != slabs_.end(); ++buf_it) {
71  cuda_mgr_->freeDeviceMem(*buf_it);
72  }
73 }
74 
75 void GpuCudaBufferMgr::allocateBuffer(BufferList::iterator seg_it,
76  const size_t page_size,
77  const size_t initial_size) {
78  new GpuCudaBuffer(this,
79  seg_it,
80  device_id_,
81  cuda_mgr_,
82  page_size,
83  initial_size); // this line is admittedly a bit weird
84  // but the segment iterator passed into
85  // buffer takes the address of the new
86  // Buffer in its buffer member
87 }
88 
89 } // namespace Buffer_Namespace
#define LOG(tag)
Definition: Logger.h:188
std::vector< BufferList > slab_segments_
Definition: BufferMgr.h:184
GpuCudaBufferMgr(const int device_id, const size_t max_buffer_pool_size, CudaMgr_Namespace::CudaMgr *cuda_mgr, const size_t min_slab_size, const size_t max_slab_size, const size_t page_size, AbstractBufferMgr *parent_mgr=0)
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:264
void allocateBuffer(BufferList::iterator seg_it, const size_t page_size, const size_t initial_size) override
void synchronizeDevices() const
Definition: CudaMgr.cpp:102
CudaMgr_Namespace::CudaMgr * cuda_mgr_
std::vector< int8_t * > slabs_
Definition: BufferMgr.h:182
int8_t * allocateDeviceMem(const size_t num_bytes, const int device_num)
Definition: CudaMgr.cpp:253