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