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