OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NvidiaKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 #include "NvidiaKernel.h"
18 
19 #include "../Shared/mapdpath.h"
20 #include "Shared/Logger.h"
21 
22 #include <boost/filesystem/operations.hpp>
23 
24 #ifdef HAVE_CUDA
25 namespace {
26 
27 void fill_options(std::vector<CUjit_option>& option_keys,
28  std::vector<void*>& option_values,
29  const unsigned block_size_x) {
30  option_keys.push_back(CU_JIT_LOG_VERBOSE);
31  option_values.push_back(reinterpret_cast<void*>(1));
32  option_keys.push_back(CU_JIT_THREADS_PER_BLOCK);
33  option_values.push_back(reinterpret_cast<void*>(block_size_x));
34 }
35 
36 } // namespace
37 
38 CubinResult ptx_to_cubin(const std::string& ptx,
39  const unsigned block_size,
40  const CudaMgr_Namespace::CudaMgr* cuda_mgr) {
41  CHECK(!ptx.empty());
42  CHECK(cuda_mgr->getDeviceCount() > 0);
43  static_cast<const CudaMgr_Namespace::CudaMgr*>(cuda_mgr)->setContext(0);
44  std::vector<CUjit_option> option_keys;
45  std::vector<void*> option_values;
46  fill_options(option_keys, option_values, block_size);
47  CHECK_EQ(option_values.size(), option_keys.size());
48  unsigned num_options = option_keys.size();
49  CUlinkState link_state;
51  cuLinkCreate(num_options, &option_keys[0], &option_values[0], &link_state));
52 
53  boost::filesystem::path gpu_rt_path{mapd_root_abs_path()};
54  gpu_rt_path /= "QueryEngine";
55  gpu_rt_path /= "cuda_mapd_rt.a";
56  if (!boost::filesystem::exists(gpu_rt_path)) {
57  throw std::runtime_error("OmniSci GPU runtime library not found at " +
58  gpu_rt_path.string());
59  }
60 
61  if (!gpu_rt_path.empty()) {
62  // How to create a static CUDA library:
63  // 1. nvcc -std=c++11 -arch=sm_30 --device-link -c [list of .cu files]
64  // 2. nvcc -std=c++11 -arch=sm_30 -lib [list of .o files generated by step 1] -o
65  // [library_name.a]
66  checkCudaErrors(cuLinkAddFile(link_state,
67  CU_JIT_INPUT_LIBRARY,
68  gpu_rt_path.c_str(),
69  num_options,
70  &option_keys[0],
71  &option_values[0]));
72  }
73  checkCudaErrors(cuLinkAddData(link_state,
74  CU_JIT_INPUT_PTX,
75  static_cast<void*>(const_cast<char*>(ptx.c_str())),
76  ptx.length() + 1,
77  0,
78  num_options,
79  &option_keys[0],
80  &option_values[0]));
81  void* cubin{nullptr};
82  size_t cubinSize{0};
83  checkCudaErrors(cuLinkComplete(link_state, &cubin, &cubinSize));
84  CHECK(cubin);
85  CHECK_GT(cubinSize, size_t(0));
86  return {cubin, option_keys, option_values, link_state};
87 }
88 #endif
89 
90 #ifdef HAVE_CUDA
92  const std::string& kernel_name,
93  const int device_id,
94  const void* cuda_mgr,
95  unsigned int num_options,
96  CUjit_option* options,
97  void** option_vals)
98  : module_(nullptr)
99  , kernel_(nullptr)
100  , device_id_(device_id)
101  , cuda_mgr_(static_cast<const CudaMgr_Namespace::CudaMgr*>(cuda_mgr)) {
102  LOG_IF(FATAL, cuda_mgr_ == nullptr)
103  << "Unable to initialize GPU compilation context without CUDA manager";
104  cuda_mgr_->loadGpuModuleData(
105  &module_, image, num_options, options, option_vals, device_id_);
106  CHECK(module_);
107  checkCudaErrors(cuModuleGetFunction(&kernel_, module_, kernel_name.c_str()));
108 }
109 #endif // HAVE_CUDA
110 
112 #ifdef HAVE_CUDA
113  CHECK(cuda_mgr_);
114  cuda_mgr_->unloadGpuModuleData(&module_, device_id_);
115 #endif
116 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
int CUjit_option
Definition: nocuda.h:25
void checkCudaErrors(CUresult err)
Definition: sample.cpp:38
std::string mapd_root_abs_path()
Definition: mapdpath.h:30
#define CHECK_GT(x, y)
Definition: Logger.h:202
#define LOG_IF(severity, condition)
Definition: Logger.h:276
CHECK(cgen_state)
int getDeviceCount() const
Definition: CudaMgr.h:93
const int32_t groups_buffer_size return nullptr
int CUlinkState
Definition: nocuda.h:26
GpuCompilationContext(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 unsigned block_size, const CudaMgr_Namespace::CudaMgr *cuda_mgr)
cgen_state module_