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"

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

namespace paddle {
namespace platform {

class DeviceContext {
 public:
  virtual ~DeviceContext() {}
L
liaogang 已提交
33
  virtual Place GetPlace() const = 0;
Q
QI JUN 已提交
34

35
  virtual void Wait() const {}
Q
QI JUN 已提交
36 37
};

Q
qijun 已提交
38 39
class CPUDeviceContext : public DeviceContext {
 public:
40
  CPUDeviceContext();
Q
qijun 已提交
41
  explicit CPUDeviceContext(CPUPlace place);
Q
qijun 已提交
42

43
  Eigen::DefaultDevice* eigen_device() const;
Q
qijun 已提交
44

L
liaogang 已提交
45
  Place GetPlace() const override;
Y
Yu Yang 已提交
46

Q
qijun 已提交
47
 private:
D
dzhwinter 已提交
48
  CPUPlace place_;
Q
qijun 已提交
49
  std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
Q
QI JUN 已提交
50 51
};

52
#ifdef PADDLE_WITH_CUDA
53

Q
qijun 已提交
54
class EigenCudaStreamDevice;
D
dongzhihong 已提交
55

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

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

64
  /*! \brief  Return place in the device context. */
L
liaogang 已提交
65
  Place GetPlace() const override;
66 67 68 69 70

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

  /*! \brief  Return cublas handle in the device context. */
71
  cublasHandle_t cublas_handle() const;
72 73

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

Q
init  
qijun 已提交
76
  /*! \brief  Return cuda stream in the device context. */
77
  cudaStream_t stream() const;
Q
QI JUN 已提交
78 79

 private:
80
  GPUPlace place_;
Q
QI JUN 已提交
81

Q
qijun 已提交
82
  std::unique_ptr<Eigen::GpuDevice> eigen_device_;
Q
init  
qijun 已提交
83
  std::unique_ptr<EigenCudaStreamDevice> eigen_stream_;
Q
QI JUN 已提交
84

85 86 87
  cudaStream_t stream_;
  cudnnHandle_t cudnn_handle_;
  cublasHandle_t cublas_handle_;
Q
QI JUN 已提交
88
};
Q
qijun 已提交
89

90
class CUDNNDeviceContext : public CUDADeviceContext {
D
dzhwinter 已提交
91
 public:
92 93
  explicit CUDNNDeviceContext(CUDNNPlace place);
  virtual ~CUDNNDeviceContext();
D
dzhwinter 已提交
94 95 96 97 98 99 100 101 102

  /*! \brief  Return place in the device context. */
  Place GetPlace() const final;

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

 private:
  cudnnHandle_t cudnn_handle_;
103
  CUDNNPlace place_;
D
dzhwinter 已提交
104 105
};

Q
QI JUN 已提交
106
#endif
Q
qijun 已提交
107

Q
QI JUN 已提交
108 109
}  // namespace platform
}  // namespace paddle