hl_cudart_wrap.cc 8.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Z
zhangjinchao01 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

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

17
#include <cuda_runtime.h>
Y
Yu Yang 已提交
18
#include <mutex>
Z
zhangjinchao01 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31
#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;
32
extern void *cudart_dso_handle;
Z
zhangjinchao01 已提交
33 34 35 36 37 38

/**
 * The following macro definition can generate structs
 * (for each function) to dynamic load cuda routine
 * via operator overloading.
 **/
39 40 41 42 43 44 45 46 47 48
#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 已提交
49 50

/* include all needed cuda functions in HPPL */
L
Luo Tao 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64
// 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 已提交
65 66 67 68

CUDA_ROUTINE_EACH(DYNAMIC_LOAD_CUDART_WRAP)

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

#undef CUDA_ROUNTINE_EACH

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

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

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

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

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

extern "C" {

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

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

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
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 已提交
135 136
}

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
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 已提交
153 154
}

155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
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 已提交
171 172
}

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

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

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

} /* extern "C" */

#endif