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