OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NvidiaKernel.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 "CudaMgr/CudaMgr.h"
21 
22 #ifdef HAVE_CUDA
23 #include <cuda.h>
24 #else
25 #include "../Shared/nocuda.h"
26 #endif // HAVE_CUDA
27 #include <string>
28 #include <vector>
29 
30 struct CubinResult {
31  void* cubin;
32  std::vector<CUjit_option> option_keys;
33  std::vector<void*> option_values;
35  size_t cubin_size;
36 
37  std::string info_log;
38  std::string error_log;
40 
41  CubinResult();
42  inline float jitWallTime() const {
43  return *reinterpret_cast<float const*>(&option_values[jit_wall_time_idx]);
44  }
45 };
46 
51 void nvidia_jit_warmup();
52 
57 CubinResult ptx_to_cubin(const std::string& ptx,
58  const CudaMgr_Namespace::CudaMgr* cuda_mgr);
59 
61  public:
62  GpuDeviceCompilationContext(const void* image,
63  const size_t module_size,
64  const std::string& kernel_name,
65  const int device_id,
66  const void* cuda_mgr,
67  unsigned int num_options,
68  CUjit_option* options,
69  void** option_vals);
71  CUfunction kernel() { return kernel_; }
72  CUmodule module() { return module_; }
73  std::string const& name() const { return kernel_name_; }
74  size_t getModuleSize() const { return module_size_; }
75 
76  private:
78  size_t module_size_;
80  std::string const kernel_name_;
81 #ifdef HAVE_CUDA
82  const int device_id_;
83  const CudaMgr_Namespace::CudaMgr* cuda_mgr_;
84 #endif // HAVE_CUDA
85 };
86 
88  public:
90 
91  void addDeviceCode(std::unique_ptr<GpuDeviceCompilationContext>&& device_context) {
92  contexts_per_device_.push_back(std::move(device_context));
93  }
94 
95  std::pair<void*, void*> getNativeCode(const size_t device_id) const {
96  CHECK_LT(device_id, contexts_per_device_.size());
97  auto device_context = contexts_per_device_[device_id].get();
98  return std::make_pair<void*, void*>(device_context->kernel(),
99  device_context->module());
100  }
101 
102  std::vector<void*> getNativeFunctionPointers() const {
103  std::vector<void*> fn_ptrs;
104  for (auto& device_context : contexts_per_device_) {
105  CHECK(device_context);
106  fn_ptrs.push_back(device_context->kernel());
107  }
108  return fn_ptrs;
109  }
110 
111  std::string const& name(size_t const device_id) const {
112  CHECK_LT(device_id, contexts_per_device_.size());
113  return contexts_per_device_[device_id]->name();
114  }
115 
116  size_t getMemSize() const {
117  return contexts_per_device_.begin()->get()->getModuleSize();
118  }
119 
120  private:
121  std::vector<std::unique_ptr<GpuDeviceCompilationContext>> contexts_per_device_;
122 };
123 
124 #ifdef HAVE_CUDA
125 inline std::string ourCudaErrorStringHelper(CUresult error) {
126  char const* c1;
127  CUresult res1 = cuGetErrorName(error, &c1);
128  char const* c2;
129  CUresult res2 = cuGetErrorString(error, &c2);
130  std::string text;
131  if (res1 == CUDA_SUCCESS) {
132  text += c1;
133  text += " (";
134  text += std::to_string(error);
135  text += ")";
136  }
137  if (res2 == CUDA_SUCCESS) {
138  if (!text.empty()) {
139  text += ": ";
140  }
141  text += c2;
142  }
143  if (text.empty()) {
144  text = std::to_string(error); // never return an empty error string
145  }
146  return text;
147 }
148 
149 #define checkCudaErrors(ARG) \
150  if (CUresult const err = static_cast<CUresult>(ARG); err != CUDA_SUCCESS) \
151  CHECK_EQ(err, CUDA_SUCCESS) << ourCudaErrorStringHelper(err)
152 #endif // HAVE_CUDA
float jitWallTime() const
Definition: NvidiaKernel.h:42
std::string info_log
Definition: NvidiaKernel.h:37
int CUjit_option
Definition: nocuda.h:26
size_t getModuleSize() const
Definition: NvidiaKernel.h:74
std::string const & name() const
Definition: NvidiaKernel.h:73
void nvidia_jit_warmup()
std::pair< void *, void * > getNativeCode(const size_t device_id) const
Definition: NvidiaKernel.h:95
void * cubin
Definition: NvidiaKernel.h:31
std::string to_string(char const *&&v)
std::vector< CUjit_option > option_keys
Definition: NvidiaKernel.h:32
std::string const & name(size_t const device_id) const
Definition: NvidiaKernel.h:111
std::string const kernel_name_
Definition: NvidiaKernel.h:80
void addDeviceCode(std::unique_ptr< GpuDeviceCompilationContext > &&device_context)
Definition: NvidiaKernel.h:91
std::vector< void * > getNativeFunctionPointers() const
Definition: NvidiaKernel.h:102
void * CUfunction
Definition: nocuda.h:25
CubinResult ptx_to_cubin(const std::string &ptx, const CudaMgr_Namespace::CudaMgr *cuda_mgr)
std::vector< std::unique_ptr< GpuDeviceCompilationContext > > contexts_per_device_
Definition: NvidiaKernel.h:121
GpuDeviceCompilationContext(const void *image, const size_t module_size, const std::string &kernel_name, const int device_id, const void *cuda_mgr, unsigned int num_options, CUjit_option *options, void **option_vals)
std::vector< void * > option_values
Definition: NvidiaKernel.h:33
int CUlinkState
Definition: nocuda.h:27
size_t jit_wall_time_idx
Definition: NvidiaKernel.h:39
#define CHECK_LT(x, y)
Definition: Logger.h:303
size_t cubin_size
Definition: NvidiaKernel.h:35
int CUresult
Definition: nocuda.h:21
#define CHECK(condition)
Definition: Logger.h:291
CUlinkState link_state
Definition: NvidiaKernel.h:34
std::string error_log
Definition: NvidiaKernel.h:38
size_t getMemSize() const
Definition: NvidiaKernel.h:116
void * CUmodule
Definition: nocuda.h:24