OmniSciDB  8a228a1076
RenderAllocator.h
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 #pragma once
18 
19 #ifdef HAVE_CUDA
20 #include <cuda.h>
21 #else
22 #include "Shared/nocuda.h"
23 #endif // HAVE_CUDA
24 
25 #include <cstdlib>
26 #include <memory>
27 #include <mutex>
28 #include <stdexcept>
29 #include <vector>
30 
32 
33 namespace QueryRenderer {
34 class JSONLocation;
35 class QueryRenderManager;
36 struct QueryDataLayout;
37 } // namespace QueryRenderer
38 
39 class OutOfRenderMemory : public std::runtime_error {
40  public:
41  OutOfRenderMemory(const size_t device_id,
42  const size_t total_bytes,
43  const size_t requested_bytes);
44 };
45 
46 class StreamingTopNNotSupportedInRenderQuery : public std::runtime_error {
47  public:
49  : std::runtime_error("Streaming-Top-N not supported in Render Query") {}
50 };
51 
52 enum class RAExecutionPolicy { Host, Device };
53 
54 class RenderAllocator : public Allocator {
55  public:
56  RenderAllocator(int8_t* preallocated_ptr,
57  const size_t preallocated_size,
58  const size_t device_id,
59  const unsigned block_size_x,
60  const unsigned grid_size_x,
61  const RAExecutionPolicy execution_policy = RAExecutionPolicy::Device);
62 
63  int8_t* alloc(const size_t bytes) final;
64 
65  void markChunkComplete();
66 
67  size_t getCurrentChunkOffset() const;
68  size_t getCurrentChunkSize() const;
69  size_t getAllocatedSize() const;
70 
71  int8_t* getBasePtr() const;
72 
73  RAExecutionPolicy getExecutionPolicy() const;
74 
75  private:
77  const size_t preallocated_size_;
78  const size_t device_id_;
81 
82  std::unique_ptr<std::mutex> alloc_mtx_ptr_;
83 
85 };
86 
88  public:
89  RenderAllocatorMap(::QueryRenderer::QueryRenderManager* render_manager,
90  const unsigned block_size_x,
91  const unsigned grid_size_x);
93 
94  RenderAllocator* getRenderAllocator(size_t device_id);
95  RenderAllocator* operator[](size_t device_id);
96  size_t size() const { return render_allocator_map_.size(); }
97 
98  void bufferData(int8_t* data, const size_t num_data_bytes, const size_t device_id);
99  void setDataLayout(
100  const std::shared_ptr<::QueryRenderer::QueryDataLayout>& query_data_layout);
101  void prepForRendering(
102  const std::shared_ptr<::QueryRenderer::QueryDataLayout>& query_data_layout);
103 
104  private:
105  ::QueryRenderer::QueryRenderManager* render_manager_;
106  std::vector<RenderAllocator> render_allocator_map_;
107 
108  // NOTE(adb): Duplicating the CheckedAllocDeleter here since this header is included in
109  // multiple Cuda files. Including the checked_alloc header is currently problematic for
110  // nvcc.
112  void operator()(void* p) { free(p); }
113  };
114  using HostBufPtrType = std::unique_ptr<int8_t, HostBufferDeleter>;
116 };
std::vector< RenderAllocator > render_allocator_map_
const size_t preallocated_size_
::QueryRenderer::QueryRenderManager * render_manager_
std::unique_ptr< std::mutex > alloc_mtx_ptr_
const size_t device_id_
size_t crt_chunk_offset_bytes_
RAExecutionPolicy
int8_t * preallocated_ptr_
Abstract class for managing device memory allocations.
std::unique_ptr< int8_t, HostBufferDeleter > HostBufPtrType
HostBufPtrType host_render_buffer_
size_t size() const
size_t crt_allocated_bytes_
RAExecutionPolicy execution_policy_