hl_cudart_wrap.cc 6.5 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/* 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;
extern void* cudart_dso_handle;

/**
 * The following macro definition can generate structs
 * (for each function) to dynamic load cuda routine
 * via operator overloading.
 **/
#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 */

/* include all needed cuda functions in HPPL */
#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)

CUDA_ROUTINE_EACH(DYNAMIC_LOAD_CUDART_WRAP)

#if CUDART_VERSION >= 7000
  DYNAMIC_LOAD_CUDART_WRAP(cudaLaunchKernel, cudaError_t)
#endif

#undef CUDA_ROUNTINE_EACH

}  /* namespace dynload */

#if CUDART_VERSION >= 7000
__host__ cudaError_t CUDARTAPI cudaLaunchKernel(const void *func,
                                                dim3 gridDim,
                                                dim3 blockDim,
                                                void **args,
                                                size_t sharedMem,
81 82 83
                                                cudaStream_t stream) {
  return dynload::cudaLaunchKernel(func, gridDim, blockDim,
                                   args, sharedMem, stream);
Z
zhangjinchao01 已提交
84 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) {
Z
zhangjinchao01 已提交
102 103 104 105 106 107
  return dynload::cudaConfigureCall(gridDim, blockDim,
                                    sharedMem, stream);
}

extern "C" {

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

112
void CUDARTAPI __cudaUnregisterFatBinary(void **fatCubinHandle) {
Z
zhangjinchao01 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  return dynload::__cudaUnregisterFatBinary(fatCubinHandle);
}

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);
}

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);
}



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);
}

char CUDARTAPI __cudaInitModule(
        void **fatCubinHandle
) {
  return dynload::__cudaInitModule(fatCubinHandle);
}

void CUDARTAPI __cudaRegisterTexture(
        void                    **fatCubinHandle,
  const struct textureReference  *hostVar,
  const void                    **deviceAddress,
  const char                     *deviceName,
        int                       dim,
        int                       norm,
        int                       ext
) {
  return dynload::__cudaRegisterTexture(
                fatCubinHandle, hostVar, deviceAddress,
                deviceName, dim, norm, ext);
}

void CUDARTAPI __cudaRegisterSurface(
        void                    **fatCubinHandle,
  const struct surfaceReference  *hostVar,
  const void                    **deviceAddress,
  const char                     *deviceName,
        int                       dim,
        int                       ext
) {
  return dynload::__cudaRegisterSurface(
                fatCubinHandle, hostVar, deviceAddress,
                deviceName, dim, ext);
}

} /* extern "C" */

#endif