OmniSciDB  dfae7c3b14
NvidiaKernel.h
Go to the documentation of this file.
1 /*
2  * Copyright 2020 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 
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 #define checkCudaErrors(err) CHECK_EQ(err, CUDA_SUCCESS)
int CUjit_option
Definition: nocuda.h:25
void nvidia_jit_warmup()
std::vector< void * > getNativeFunctionPointers() const
Definition: NvidiaKernel.h:88
void * cubin
Definition: NvidiaKernel.h:31
std::vector< CUjit_option > option_keys
Definition: NvidiaKernel.h:32
void addDeviceCode(std::unique_ptr< GpuDeviceCompilationContext > &&device_context)
Definition: NvidiaKernel.h:77
void * CUfunction
Definition: nocuda.h:24
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:26
#define CHECK_LT(x, y)
Definition: Logger.h:207
#define CHECK(condition)
Definition: Logger.h:197
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:23
std::pair< void *, void * > getNativeCode(const size_t device_id) const
Definition: NvidiaKernel.h:81