hl_cudart_wrap.cc 8.3 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 */
50 51 52 53 54 55 56 57 58 59 60
#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)
Z
zhangjinchao01 已提交
61 62 63 64

CUDA_ROUTINE_EACH(DYNAMIC_LOAD_CUDART_WRAP)

#if CUDART_VERSION >= 7000
65
DYNAMIC_LOAD_CUDART_WRAP(cudaLaunchKernel, cudaError_t)
Z
zhangjinchao01 已提交
66 67 68 69
#endif

#undef CUDA_ROUNTINE_EACH

70
} /* namespace dynload */
Z
zhangjinchao01 已提交
71 72 73 74 75 76 77

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

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

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

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

extern "C" {

103
void **CUDARTAPI __cudaRegisterFatBinary(void *fatCubin) {
Z
zhangjinchao01 已提交
104 105 106
  return dynload::__cudaRegisterFatBinary(fatCubin);
}

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

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
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 已提交
131 132
}

133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
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 已提交
149 150
}

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
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 已提交
167 168
}

169
char CUDARTAPI __cudaInitModule(void **fatCubinHandle) {
Z
zhangjinchao01 已提交
170 171 172
  return dynload::__cudaInitModule(fatCubinHandle);
}

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

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

} /* extern "C" */

#endif