OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
L0Mgr.h
Go to the documentation of this file.
1 /*
2  * Copyright 2021 OmniSci, 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 #pragma once
17 
18 #include <iostream>
19 #include <memory>
20 #include <vector>
21 
22 #include "L0Mgr/L0Exception.h"
23 #include "L0Mgr/Utils.h"
24 
25 #ifdef HAVE_L0
26 #include <level_zero/ze_api.h>
27 #endif
28 
29 namespace l0 {
30 
31 class L0Device;
32 class L0Driver {
33  private:
34  std::vector<std::shared_ptr<L0Device>> devices_;
35 
36 #ifdef HAVE_L0
37  ze_context_handle_t context_;
38  ze_driver_handle_t driver_;
39 #endif
40 
41  public:
42 #ifdef HAVE_L0
43  explicit L0Driver(ze_driver_handle_t handle);
44  ze_context_handle_t ctx() const;
45  ze_driver_handle_t driver() const;
46  ~L0Driver();
47 #endif
48 
49  const std::vector<std::shared_ptr<L0Device>>& devices() const;
50 };
51 
52 class L0Module;
53 class L0Kernel;
54 class L0CommandList;
55 class L0CommandQueue;
56 
57 class L0Device {
58  private:
59 #ifdef HAVE_L0
60  ze_device_handle_t device_;
61 #endif
62 
64  std::shared_ptr<L0CommandQueue> command_queue_;
65 
66  public:
67  std::shared_ptr<L0CommandQueue> command_queue() const;
68  std::unique_ptr<L0CommandList> create_command_list() const;
69 
70  std::shared_ptr<L0Module> create_module(uint8_t* code,
71  size_t len,
72  bool log = false) const;
73 
74 #ifdef HAVE_L0
75  L0Device(const L0Driver& driver, ze_device_handle_t device);
76  ze_device_handle_t device() const;
77  ze_context_handle_t ctx() const;
78  ~L0Device();
79 #endif
80 };
81 
82 class L0Module {
83  private:
84 #ifdef HAVE_L0
85  ze_module_handle_t handle_;
86 #endif
87 
88  public:
89  std::shared_ptr<L0Kernel> create_kernel(const char* name,
90  uint32_t x,
91  uint32_t y,
92  uint32_t z) const;
93 #ifdef HAVE_L0
94  explicit L0Module(ze_module_handle_t handle);
95  ze_module_handle_t handle() const;
96  ~L0Module();
97 #endif
98 };
99 
100 #ifdef HAVE_L0
101 template <int Index>
102 void set_kernel_args(ze_kernel_handle_t kernel) {}
103 
104 template <int Index, typename Head>
105 void set_kernel_args(ze_kernel_handle_t kernel, Head&& head) {
106  L0_SAFE_CALL(zeKernelSetArgumentValue(
107  kernel, Index, sizeof(std::remove_reference_t<Head>), head));
108 }
109 
110 template <int Index, typename Head, typename... Tail>
111 void set_kernel_args(ze_kernel_handle_t kernel, Head&& head, Tail&&... tail) {
112  set_kernel_args<Index>(kernel, head);
113  set_kernel_args<Index + 1>(kernel, std::forward<Tail>(tail)...);
114 }
115 #endif
116 
117 class L0Kernel {
118  private:
119 #ifdef HAVE_L0
120  ze_kernel_handle_t handle_;
121  ze_group_count_t group_size_;
122 #endif
123  public:
124 #ifdef HAVE_L0
125  L0Kernel(ze_kernel_handle_t handle, uint32_t x, uint32_t y, uint32_t z);
126  ze_group_count_t& group_size();
127  ze_kernel_handle_t handle() const;
128  ~L0Kernel();
129 #endif
130 };
131 
133 #ifdef HAVE_L0
134  private:
135  ze_command_queue_handle_t handle_;
136 
137  public:
138  L0CommandQueue(ze_command_queue_handle_t handle);
139  ze_command_queue_handle_t handle() const;
140  ~L0CommandQueue();
141 #endif
142 };
143 
145  private:
146 #ifdef HAVE_L0
147  ze_command_list_handle_t handle_;
148 #endif
149 
150  public:
151  void copy(void* dst, const void* src, const size_t num_bytes);
152 
153  template <typename... Args>
154  void launch(L0Kernel& kernel, Args&&... args) {
155 #ifdef HAVE_L0
156  set_kernel_args<0>(kernel.handle(), std::forward<Args>(args)...);
157 
158  L0_SAFE_CALL(zeCommandListAppendLaunchKernel(
159  handle_, kernel.handle(), &kernel.group_size(), nullptr, 0, nullptr));
160 
161  L0_SAFE_CALL(zeCommandListAppendBarrier(handle_, nullptr, 0, nullptr));
162 #endif
163  }
164 
165  void submit(L0CommandQueue& queue);
166 
167 #ifdef HAVE_L0
168  ze_command_list_handle_t handle() const;
169  L0CommandList(ze_command_list_handle_t handle);
170  ~L0CommandList();
171 #endif
172 };
173 
174 void* allocate_device_mem(const size_t num_bytes, L0Device& device);
175 
176 class L0Manager {
177  public:
178  L0Manager();
179 
180  void copyHostToDevice(int8_t* device_ptr,
181  const int8_t* host_ptr,
182  const size_t num_bytes,
183  const int device_num);
184  void copyDeviceToHost(int8_t* host_ptr,
185  const int8_t* device_ptr,
186  const size_t num_bytes,
187  const int device_num);
188  void copyDeviceToDevice(int8_t* dest_ptr,
189  int8_t* src_ptr,
190  const size_t num_bytes,
191  const int dest_device_num,
192  const int src_device_num);
193 
194  int8_t* allocatePinnedHostMem(const size_t num_bytes);
195  int8_t* allocateDeviceMem(const size_t num_bytes, const int device_num);
196  void freePinnedHostMem(int8_t* host_ptr);
197  void freeDeviceMem(int8_t* device_ptr);
198  void zeroDeviceMem(int8_t* device_ptr, const size_t num_bytes, const int device_num);
199  void setDeviceMem(int8_t* device_ptr,
200  const unsigned char uc,
201  const size_t num_bytes,
202  const int device_num);
203 
204  void synchronizeDevices() const;
205 
206  const std::vector<std::shared_ptr<L0Driver>>& drivers() const;
207 
208  private:
209  std::vector<std::shared_ptr<L0Driver>> drivers_;
210 };
211 
212 } // namespace l0
L0Driver driver_
Definition: L0Mgr.h:63
void freeDeviceMem(int8_t *device_ptr)
Definition: L0Mgr.cpp:307
void zeroDeviceMem(int8_t *device_ptr, const size_t num_bytes, const int device_num)
Definition: L0Mgr.cpp:312
#define L0_SAFE_CALL(call)
Definition: Utils.h:19
void copyHostToDevice(int8_t *device_ptr, const int8_t *host_ptr, const size_t num_bytes, const int device_num)
Definition: L0Mgr.cpp:266
string name
Definition: setup.in.py:72
void setDeviceMem(int8_t *device_ptr, const unsigned char uc, const size_t num_bytes, const int device_num)
Definition: L0Mgr.cpp:317
std::unique_ptr< L0CommandList > create_command_list() const
Definition: L0Mgr.cpp:146
std::shared_ptr< L0Module > create_module(uint8_t *code, size_t len, bool log=false) const
Definition: L0Mgr.cpp:171
void launch(L0Kernel &kernel, Args &&...args)
Definition: L0Mgr.h:154
void copy(void *dst, const void *src, const size_t num_bytes)
Definition: L0Mgr.cpp:99
void submit(L0CommandQueue &queue)
Definition: L0Mgr.cpp:84
std::shared_ptr< L0CommandQueue > command_queue() const
Definition: L0Mgr.cpp:142
void copyDeviceToDevice(int8_t *dest_ptr, int8_t *src_ptr, const size_t num_bytes, const int dest_device_num, const int src_device_num)
Definition: L0Mgr.cpp:290
std::vector< std::shared_ptr< L0Device > > devices_
Definition: L0Mgr.h:34
void synchronizeDevices() const
Definition: L0Mgr.cpp:328
std::shared_ptr< L0CommandQueue > command_queue_
Definition: L0Mgr.h:64
void copyDeviceToHost(int8_t *host_ptr, const int8_t *device_ptr, const size_t num_bytes, const int device_num)
Definition: L0Mgr.cpp:278
void freePinnedHostMem(int8_t *host_ptr)
Definition: L0Mgr.cpp:303
const std::vector< std::shared_ptr< L0Driver > > & drivers() const
Definition: L0Mgr.cpp:209
int8_t * allocatePinnedHostMem(const size_t num_bytes)
Definition: L0Mgr.cpp:298
std::vector< std::shared_ptr< L0Driver > > drivers_
Definition: L0Mgr.h:209
void * allocate_device_mem(const size_t num_bytes, L0Device &device)
Definition: L0Mgr.cpp:105
int8_t * allocateDeviceMem(const size_t num_bytes, const int device_num)
Definition: L0Mgr.cpp:213
const std::vector< std::shared_ptr< L0Device > > & devices() const
Definition: L0Mgr.cpp:63
std::shared_ptr< L0Kernel > create_kernel(const char *name, uint32_t x, uint32_t y, uint32_t z) const
Definition: L0Mgr.cpp:231