OmniSciDB  c1a53651b2
 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 };
36 
41 void nvidia_jit_warmup();
42 
47 CubinResult ptx_to_cubin(const std::string& ptx,
48  const CudaMgr_Namespace::CudaMgr* cuda_mgr);
49 
51  public:
52  GpuDeviceCompilationContext(const void* image,
53  const std::string& kernel_name,
54  const int device_id,
55  const void* cuda_mgr,
56  unsigned int num_options,
57  CUjit_option* options,
58  void** option_vals);
60  CUfunction kernel() { return kernel_; }
61  CUmodule module() { return module_; }
62 
63  private:
66 #ifdef HAVE_CUDA
67  const int device_id_;
68  const CudaMgr_Namespace::CudaMgr* cuda_mgr_;
69 #endif // HAVE_CUDA
70 };
71 
73  public:
75 
76  void addDeviceCode(std::unique_ptr<GpuDeviceCompilationContext>&& device_context) {
77  contexts_per_device_.push_back(std::move(device_context));
78  }
79 
80  std::pair<void*, void*> getNativeCode(const size_t device_id) const {
81  CHECK_LT(device_id, contexts_per_device_.size());
82  auto device_context = contexts_per_device_[device_id].get();
83  return std::make_pair<void*, void*>(device_context->kernel(),
84  device_context->module());
85  }
86 
87  std::vector<void*> getNativeFunctionPointers() const {
88  std::vector<void*> fn_ptrs;
89  for (auto& device_context : contexts_per_device_) {
90  CHECK(device_context);
91  fn_ptrs.push_back(device_context->kernel());
92  }
93  return fn_ptrs;
94  }
95 
96  private:
97  std::vector<std::unique_ptr<GpuDeviceCompilationContext>> contexts_per_device_;
98 };
99 
100 #ifdef HAVE_CUDA
101 inline std::string ourCudaErrorStringHelper(CUresult error) {
102  char const* c1;
103  CUresult res1 = cuGetErrorName(error, &c1);
104  char const* c2;
105  CUresult res2 = cuGetErrorString(error, &c2);
106  std::string text;
107  if (res1 == CUDA_SUCCESS) {
108  text += c1;
109  text += " (";
110  text += std::to_string(error);
111  text += ")";
112  }
113  if (res2 == CUDA_SUCCESS) {
114  if (!text.empty()) {
115  text += ": ";
116  }
117  text += c2;
118  }
119  if (text.empty()) {
120  text = std::to_string(error); // never return an empty error string
121  }
122  return text;
123 }
124 
125 #define checkCudaErrors(ARG) \
126  if (CUresult const err = static_cast<CUresult>(ARG); err != CUDA_SUCCESS) \
127  CHECK_EQ(err, CUDA_SUCCESS) << ourCudaErrorStringHelper(err)
128 #endif // HAVE_CUDA
int CUjit_option
Definition: nocuda.h:26
void nvidia_jit_warmup()
std::pair< void *, void * > getNativeCode(const size_t device_id) const
Definition: NvidiaKernel.h:80
void * cubin
Definition: NvidiaKernel.h:31
std::string to_string(char const *&&v)
std::vector< CUjit_option > option_keys
Definition: NvidiaKernel.h:32
void addDeviceCode(std::unique_ptr< GpuDeviceCompilationContext > &&device_context)
Definition: NvidiaKernel.h:76
std::vector< void * > getNativeFunctionPointers() const
Definition: NvidiaKernel.h:87
void * CUfunction
Definition: nocuda.h:25
GpuDeviceCompilationContext(const void *image, const std::string &kernel_name, const int device_id, const void *cuda_mgr, unsigned int num_options, CUjit_option *options, void **option_vals)
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:97
std::vector< void * > option_values
Definition: NvidiaKernel.h:33
int CUlinkState
Definition: nocuda.h:27
#define CHECK_LT(x, y)
Definition: Logger.h:303
int CUresult
Definition: nocuda.h:21
#define CHECK(condition)
Definition: Logger.h:291
CUlinkState link_state
Definition: NvidiaKernel.h:34
void * CUmodule
Definition: nocuda.h:24