device_context.h 2.8 KB
Newer Older
Q
QI JUN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 PaddlePaddle Authors. 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. */

#pragma once

L
liaogang 已提交
14
#include "paddle/platform/enforce.h"
L
liaogang 已提交
15 16
#include "paddle/platform/place.h"

Q
QI JUN 已提交
17 18 19 20
#ifndef PADDLE_ONLY_CPU
#include "paddle/platform/dynload/cublas.h"
#include "paddle/platform/dynload/cudnn.h"
#include "paddle/platform/dynload/curand.h"
L
liaogang 已提交
21
#include "paddle/platform/gpu_info.h"
Q
QI JUN 已提交
22 23
#define EIGEN_USE_GPU
#endif
Q
qijun 已提交
24
#include <memory>
Q
qijun 已提交
25 26
#include "paddle/platform/place.h"
#include "unsupported/Eigen/CXX11/Tensor"
Q
QI JUN 已提交
27 28 29 30 31 32 33

namespace paddle {
namespace platform {

class DeviceContext {
 public:
  virtual ~DeviceContext() {}
34
  virtual Place place() const = 0;
Q
QI JUN 已提交
35

Q
qijun 已提交
36
  template <typename DeviceType>
Q
qijun 已提交
37
  DeviceType* get_eigen_device() const;
Q
QI JUN 已提交
38 39
};

Q
qijun 已提交
40 41
class CPUDeviceContext : public DeviceContext {
 public:
42 43 44
  CPUDeviceContext();
  CPUDeviceContext(CPUPlace);
  virtual ~CPUDeviceContext() {}
Q
qijun 已提交
45

46
  Eigen::DefaultDevice* eigen_device() const;
Q
qijun 已提交
47

48
  Place place() const override;
Y
Yu Yang 已提交
49

Q
qijun 已提交
50
 private:
Q
qijun 已提交
51
  std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
Q
QI JUN 已提交
52 53 54
};

#ifndef PADDLE_ONLY_CPU
D
dongzhihong 已提交
55

56
class CUDADeviceContext : public DeviceContext {
Q
QI JUN 已提交
57
 public:
58 59
  explicit CUDADeviceContext(GPUPlace);
  virtual ~CUDADeviceContext();
Q
QI JUN 已提交
60

61 62
  /*! \brief  Wait for all operations completion in the stream. */
  void wait() const;
Q
QI JUN 已提交
63

64 65
  /*! \brief  Return CUDA stream in the device context. */
  cudaStream_t stream() const;
Q
QI JUN 已提交
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  /*! \brief  Return place in the device context. */
  Place place() const override;

  /*! \brief  Return eigen device in the device context. */
  Eigen::GpuDevice* eigen_device() const;

  // clang-format off
  /*! \brief  Return cublas handle in the device context. */
  cublasHandle_t    cublas_handle   ();

  /*! \brief  Return cudnn  handle in the device context. */
  cudnnHandle_t     cudnn_handle    ();

  /*! \brief  Return curand handle in the device context. */
  curandGenerator_t curand_generator();
  // clang-format on
Q
QI JUN 已提交
83 84

 private:
85
  GPUPlace place_;
Q
QI JUN 已提交
86

87
 private:
Q
qijun 已提交
88
  std::unique_ptr<Eigen::GpuDevice> eigen_device_;
89
  std::unique_ptr<Eigen::CudaStreamDevice> eigen_stream_;
Q
QI JUN 已提交
90

91 92
 private:
  uint64_t seed_;
Q
QI JUN 已提交
93

94
  cudaStream_t stream_;
Q
QI JUN 已提交
95

96 97 98 99 100
  // clang-format off
  cudnnHandle_t     cudnn_handle_     = nullptr;
  cublasHandle_t    cublas_handle_    = nullptr;
  curandGenerator_t curand_generator_ = nullptr;
  // clang-format on
Q
QI JUN 已提交
101
};
Q
qijun 已提交
102

Q
QI JUN 已提交
103
#endif
Q
qijun 已提交
104

Q
QI JUN 已提交
105 106
}  // namespace platform
}  // namespace paddle