curand.h 2.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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. */
#pragma once

Q
qijun 已提交
16
#include <curand.h>
17 18

#include <mutex>  // NOLINT
D
dzhwinter 已提交
19
#include "paddle/fluid/platform/port.h"
20

Y
Yi Wang 已提交
21
#include "paddle/fluid/platform/dynload/dynamic_loader.h"
Q
qijun 已提交
22 23

namespace paddle {
Q
qijun 已提交
24
namespace platform {
Q
qijun 已提交
25
namespace dynload {
Y
Yu Yang 已提交
26 27
extern std::once_flag curand_dso_flag;
extern void *curand_dso_handle;
Q
qijun 已提交
28
#ifdef PADDLE_USE_DSO
29 30 31 32
#define DECLARE_DYNAMIC_LOAD_CURAND_WRAP(__name)                             \
  struct DynLoad__##__name {                                                 \
    template <typename... Args>                                              \
    curandStatus_t operator()(Args... args) {                                \
33
      using curandFunc = decltype(&::__name);                                \
34 35 36
      std::call_once(curand_dso_flag, []() {                                 \
        curand_dso_handle = paddle::platform::dynload::GetCurandDsoHandle(); \
      });                                                                    \
Y
yuyang18 已提交
37
      static void *p_##__name = dlsym(curand_dso_handle, #__name);           \
38 39 40
      return reinterpret_cast<curandFunc>(p_##__name)(args...);              \
    }                                                                        \
  };                                                                         \
Y
Yu Yang 已提交
41
  extern DynLoad__##__name __name
Q
qijun 已提交
42
#else
Y
Yu Yang 已提交
43 44 45 46
#define DECLARE_DYNAMIC_LOAD_CURAND_WRAP(__name) \
  struct DynLoad__##__name {                     \
    template <typename... Args>                  \
    curandStatus_t operator()(Args... args) {    \
47
      return ::__name(args...);                  \
Y
Yu Yang 已提交
48 49 50
    }                                            \
  };                                             \
  extern DynLoad__##__name __name
Q
qijun 已提交
51 52
#endif

Y
Yu Yang 已提交
53 54 55 56 57 58
#define CURAND_RAND_ROUTINE_EACH(__macro)      \
  __macro(curandCreateGenerator);              \
  __macro(curandSetStream);                    \
  __macro(curandSetPseudoRandomGeneratorSeed); \
  __macro(curandGenerateUniform);              \
  __macro(curandGenerateUniformDouble);        \
Y
Yu Yang 已提交
59
  __macro(curandGenerateNormal);               \
Y
Yu Yang 已提交
60
  __macro(curandDestroyGenerator);
Q
qijun 已提交
61

Y
Yu Yang 已提交
62
CURAND_RAND_ROUTINE_EACH(DECLARE_DYNAMIC_LOAD_CURAND_WRAP);
Q
qijun 已提交
63

Q
qijun 已提交
64
}  // namespace dynload
Q
qijun 已提交
65
}  // namespace platform
Q
qijun 已提交
66
}  // namespace paddle