hl_cudart_wrap.cc 8.0 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#ifdef PADDLE_USE_DSO

#include <mutex>
#include "hl_dso_loader.h"

/**
 * cudart wrapper: for dynamic load libcudart.so.
 * When nvcc compile cuda kernels, it will insert
 * some build-in runtime routines, which must be
 * provided by us if PADDLE_USE_DSO is true. If
 * PADDLE_USE_DSO is false, all of them must be
 * ignored to avoid multiple definitions.
 */
namespace dynload {

extern std::once_flag cudart_dso_flag;
31
extern void *cudart_dso_handle;
Z
zhangjinchao01 已提交
32 33 34 35 36 37

/**
 * The following macro definition can generate structs
 * (for each function) to dynamic load cuda routine
 * via operator overloading.
 **/
38 39 40 41 42 43 44 45 46 47
#define DYNAMIC_LOAD_CUDART_WRAP(__name, __type)                               \
  struct DynLoad__##__name {                                                   \
    template <typename... Args>                                                \
    __type operator()(Args... args) {                                          \
      typedef __type (*cudartFunc)(Args...);                                   \
      std::call_once(cudart_dso_flag, GetCudartDsoHandle, &cudart_dso_handle); \
      void *p_##__name = dlsym(cudart_dso_handle, #__name);                    \
      return reinterpret_cast<cudartFunc>(p_##__name)(args...);                \
    }                                                                          \
  } __name; /* struct DynLoad__##__name */
Z
zhangjinchao01 已提交
48 49

/* include all needed cuda functions in HPPL */
L
Luo Tao 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
// clang-format off
#define CUDA_ROUTINE_EACH(__macro)          \
  __macro(cudaLaunch, cudaError_t)          \
  __macro(cudaSetupArgument, cudaError_t)   \
  __macro(cudaConfigureCall, cudaError_t)   \
  __macro(__cudaRegisterFatBinary, void**)  \
  __macro(__cudaUnregisterFatBinary, void)  \
  __macro(__cudaRegisterFunction, void)     \
  __macro(__cudaRegisterVar, void)          \
  __macro(__cudaRegisterManagedVar, void)   \
  __macro(__cudaInitModule, char)           \
  __macro(__cudaRegisterTexture, void)      \
  __macro(__cudaRegisterSurface, void)
// clang-format on
Z
zhangjinchao01 已提交
64 65 66 67

CUDA_ROUTINE_EACH(DYNAMIC_LOAD_CUDART_WRAP)

#if CUDART_VERSION >= 7000
68
DYNAMIC_LOAD_CUDART_WRAP(cudaLaunchKernel, cudaError_t)
Z
zhangjinchao01 已提交
69 70 71 72
#endif

#undef CUDA_ROUNTINE_EACH

73
} /* namespace dynload */
Z
zhangjinchao01 已提交
74 75 76 77 78 79 80

#if CUDART_VERSION >= 7000
__host__ cudaError_t CUDARTAPI cudaLaunchKernel(const void *func,
                                                dim3 gridDim,
                                                dim3 blockDim,
                                                void **args,
                                                size_t sharedMem,
81
                                                cudaStream_t stream) {
82 83
  return dynload::cudaLaunchKernel(
      func, gridDim, blockDim, args, sharedMem, stream);
Z
zhangjinchao01 已提交
84 85 86
}
#endif /* CUDART_VERSION >= 7000 */

87
__host__ cudaError_t CUDARTAPI cudaLaunch(const void *func) {
Z
zhangjinchao01 已提交
88 89 90 91 92
  return dynload::cudaLaunch(func);
}

__host__ cudaError_t CUDARTAPI cudaSetupArgument(const void *arg,
                                                 size_t size,
93
                                                 size_t offset) {
Z
zhangjinchao01 已提交
94 95 96 97 98 99
  return dynload::cudaSetupArgument(arg, size, offset);
}

__host__ cudaError_t CUDARTAPI cudaConfigureCall(dim3 gridDim,
                                                 dim3 blockDim,
                                                 size_t sharedMem,
100
                                                 cudaStream_t stream) {
101
  return dynload::cudaConfigureCall(gridDim, blockDim, sharedMem, stream);
Z
zhangjinchao01 已提交
102 103 104 105
}

extern "C" {

106
void **CUDARTAPI __cudaRegisterFatBinary(void *fatCubin) {
Z
zhangjinchao01 已提交
107 108 109
  return dynload::__cudaRegisterFatBinary(fatCubin);
}

110
void CUDARTAPI __cudaUnregisterFatBinary(void **fatCubinHandle) {
Z
zhangjinchao01 已提交
111 112 113
  return dynload::__cudaUnregisterFatBinary(fatCubinHandle);
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
void CUDARTAPI __cudaRegisterFunction(void **fatCubinHandle,
                                      const char *hostFun,
                                      char *deviceFun,
                                      const char *deviceName,
                                      int thread_limit,
                                      uint3 *tid,
                                      uint3 *bid,
                                      dim3 *bDim,
                                      dim3 *gDim,
                                      int *wSize) {
  return dynload::__cudaRegisterFunction(fatCubinHandle,
                                         hostFun,
                                         deviceFun,
                                         deviceName,
                                         thread_limit,
                                         tid,
                                         bid,
                                         bDim,
                                         gDim,
                                         wSize);
Z
zhangjinchao01 已提交
134 135
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
void CUDARTAPI __cudaRegisterVar(void **fatCubinHandle,
                                 char *hostVar,
                                 char *deviceAddress,
                                 const char *deviceName,
                                 int ext,
                                 int size,
                                 int constant,
                                 int global) {
  return dynload::__cudaRegisterVar(fatCubinHandle,
                                    hostVar,
                                    deviceAddress,
                                    deviceName,
                                    ext,
                                    size,
                                    constant,
                                    global);
Z
zhangjinchao01 已提交
152 153
}

154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
extern void CUDARTAPI __cudaRegisterManagedVar(void **fatCubinHandle,
                                               void **hostVarPtrAddress,
                                               char *deviceAddress,
                                               const char *deviceName,
                                               int ext,
                                               int size,
                                               int constant,
                                               int global) {
  return dynload::__cudaRegisterManagedVar(fatCubinHandle,
                                           hostVarPtrAddress,
                                           deviceAddress,
                                           deviceName,
                                           ext,
                                           size,
                                           constant,
                                           global);
Z
zhangjinchao01 已提交
170 171
}

172
char CUDARTAPI __cudaInitModule(void **fatCubinHandle) {
Z
zhangjinchao01 已提交
173 174 175
  return dynload::__cudaInitModule(fatCubinHandle);
}

176 177 178 179 180 181 182
void CUDARTAPI __cudaRegisterTexture(void **fatCubinHandle,
                                     const struct textureReference *hostVar,
                                     const void **deviceAddress,
                                     const char *deviceName,
                                     int dim,
                                     int norm,
                                     int ext) {
Z
zhangjinchao01 已提交
183
  return dynload::__cudaRegisterTexture(
184
      fatCubinHandle, hostVar, deviceAddress, deviceName, dim, norm, ext);
Z
zhangjinchao01 已提交
185 186
}

187 188 189 190 191 192
void CUDARTAPI __cudaRegisterSurface(void **fatCubinHandle,
                                     const struct surfaceReference *hostVar,
                                     const void **deviceAddress,
                                     const char *deviceName,
                                     int dim,
                                     int ext) {
Z
zhangjinchao01 已提交
193
  return dynload::__cudaRegisterSurface(
194
      fatCubinHandle, hostVar, deviceAddress, deviceName, dim, ext);
Z
zhangjinchao01 已提交
195 196 197 198 199
}

} /* extern "C" */

#endif