device_context.h 2.9 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
#ifndef PADDLE_ONLY_CPU
Q
qijun 已提交
18 19
#include <thrust/device_ptr.h>
#include <thrust/random.h>
Q
QI JUN 已提交
20 21
#include "paddle/platform/dynload/cublas.h"
#include "paddle/platform/dynload/cudnn.h"
L
liaogang 已提交
22
#include "paddle/platform/gpu_info.h"
Q
QI JUN 已提交
23 24
#define EIGEN_USE_GPU
#endif
Q
qijun 已提交
25
#include <memory>
Q
qijun 已提交
26 27
#include "paddle/platform/place.h"
#include "unsupported/Eigen/CXX11/Tensor"
Q
QI JUN 已提交
28 29 30 31 32 33 34

namespace paddle {
namespace platform {

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

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

Q
qijun 已提交
41 42
class CPUDeviceContext : public DeviceContext {
 public:
43
  CPUDeviceContext();
Q
qijun 已提交
44
  explicit CPUDeviceContext(CPUPlace place, int rand_seed = 0);
45
  virtual ~CPUDeviceContext() {}
Q
qijun 已提交
46

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

Q
qijun 已提交
49 50
  std::minstd_rand& rand_engine();

L
liaogang 已提交
51
  Place GetPlace() const override;
Y
Yu Yang 已提交
52

Q
qijun 已提交
53
 private:
Q
qijun 已提交
54 55
  int rand_seed_;
  std::unique_ptr<std::minstd_rand> rand_engine_;
Q
qijun 已提交
56
  std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
Q
QI JUN 已提交
57 58 59
};

#ifndef PADDLE_ONLY_CPU
Q
qijun 已提交
60
class EigenCudaStreamDevice;
D
dongzhihong 已提交
61

62
class CUDADeviceContext : public DeviceContext {
Q
QI JUN 已提交
63
 public:
Q
qijun 已提交
64
  explicit CUDADeviceContext(GPUPlace place, uint64_t rand_seed = 0);
65
  virtual ~CUDADeviceContext();
Q
QI JUN 已提交
66

67
  /*! \brief  Wait for all operations completion in the stream. */
L
liaogang 已提交
68
  void Wait() const;
Q
QI JUN 已提交
69

70
  /*! \brief  Return place in the device context. */
L
liaogang 已提交
71
  Place GetPlace() const override;
72 73 74 75 76 77

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

  // clang-format off
  /*! \brief  Return cublas handle in the device context. */
L
liaogang 已提交
78
  cublasHandle_t    cublas_handle();
79 80

  /*! \brief  Return cudnn  handle in the device context. */
L
liaogang 已提交
81
  cudnnHandle_t     cudnn_handle();
82

Q
qijun 已提交
83
  thrust::minstd_rand& CPUDeviceContext::rand_engine();
Q
init  
qijun 已提交
84 85 86

  /*! \brief  Return cuda stream in the device context. */
  cudaStream_t      stream();
87
  // clang-format on
Q
QI JUN 已提交
88 89

 private:
90
  GPUPlace place_;
Q
QI JUN 已提交
91

Q
qijun 已提交
92
  std::unique_ptr<Eigen::GpuDevice> eigen_device_;
Q
init  
qijun 已提交
93
  std::unique_ptr<EigenCudaStreamDevice> eigen_stream_;
Q
QI JUN 已提交
94

Q
qijun 已提交
95 96
  uint64_t rand_seed_;
  std::unique_ptr<thrust::minstd_rand> rand_engine_;
Q
QI JUN 已提交
97

98
  // clang-format off
Q
qijun 已提交
99
  cudaStream_t       stream_{nullptr};
Q
init  
qijun 已提交
100 101
  cudnnHandle_t      cudnn_handle_{nullptr};
  cublasHandle_t     cublas_handle_{nullptr};
102
  // clang-format on
Q
QI JUN 已提交
103
};
Q
qijun 已提交
104

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

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