resource_manager.h 6.3 KB
Newer Older
W
Wilber 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
//
// 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

W
Wilber 已提交
16
#include <atomic>
W
Wilber 已提交
17
#include <functional>
W
Wilber 已提交
18
#include <map>
W
Wilber 已提交
19
#include <memory>
W
Wilber 已提交
20
#include <mutex>
21

W
Wilber 已提交
22
#include "paddle/fluid/platform/macros.h"
W
Wilber 已提交
23 24
#include "paddle/phi/api/include/tensor.h"
#include "paddle/phi/backends/cpu/forwards.h"
W
Wilber 已提交
25
#include "paddle/phi/common/place.h"
Z
zyfncg 已提交
26
#include "unsupported/Eigen/CXX11/Tensor"
W
Wilber 已提交
27 28 29 30 31 32 33 34 35 36 37 38 39

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#include "paddle/fluid/platform/device/gpu/gpu_types.h"
#include "paddle/phi/backends/gpu/forwards.h"
#include "paddle/phi/backends/gpu/gpu_decls.h"
#include "paddle/phi/backends/gpu/gpu_resources.h"
#endif

namespace paddle {
namespace internal {
class EigenGpuStreamDevice;
}  // namespace internal

W
Wilber 已提交
40
class CPUContextResource {
W
Wilber 已提交
41
 public:
W
Wilber 已提交
42 43
  CPUContextResource();
  Eigen::DefaultDevice* GetCPUEigenDevice() const;
W
Wilber 已提交
44 45 46 47 48 49

 private:
  void InitCPUResource();

 private:
  std::unique_ptr<Eigen::DefaultDevice> cpu_eigen_device_;
W
Wilber 已提交
50
};
W
Wilber 已提交
51 52

#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
W
Wilber 已提交
53
class GPUContextResource {
W
Wilber 已提交
54
 public:
W
Wilber 已提交
55 56
  explicit GPUContextResource(const phi::Place& place, void* stream);
  ~GPUContextResource();
W
Wilber 已提交
57
  phi::Place Place() const;
W
Wilber 已提交
58

59 60 61 62 63 64 65 66 67
  std::function<phi::dnnHandle_t()> GetDnnHandleCreator();
  std::function<phi::blasHandle_t()> GetBlasHandleCreator();
  std::function<phi::blasHandle_t()> GetBlasTensorCoreHandleCreator();
  std::function<phi::blasHandle_t()> GetBlasTF32TensorCoreHandleCreator();
  std::function<phi::blasLtHandle_t()> GetBlasLtHandleCreator();
  std::function<phi::solverHandle_t()> GetSolverDnHandleCreator();
  std::function<phi::sparseHandle_t()> GetSparseHandleCreator();
  std::function<Eigen::GpuDevice*()> GetGpuEigenDeviceCreator();

W
Wilber 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
  gpuStream_t GetStream() const;
  dnnHandle_t GetDnnHandle() const;
  blasHandle_t GetBlasHandle() const;
  blasHandle_t GetBlasTensorCoreHandle() const;
  blasHandle_t GetBlasTF32Handle() const;
  blasLtHandle_t GetBlasLtHandle() const;
  phi::solverHandle_t GetSolverDnHandle() const;
  phi::sparseHandle_t GetSparseHandle() const;
  Eigen::GpuDevice* GetGpuEigenDevice() const;
  int GetGpuComputeCapability() const;
  int GetGpuRuntimeVersion() const;
  int GetGpuDriverVersion() const;
  int GetGPUMultiProcessors() const;
  int GetGpuMaxThreadsPerMp() const;
  int GetGpuMaxThreadsPerBlock() const;
  std::array<int, 3> GetGpuMaxGridDimSize() const;

 private:
  void InitGPUResource(void* stream);
  void DestroyGPUResource();
  void InitGpuProperties();
  void InitGpuEigenDevice();
  void InitDnnHanlde();
  void DestroyDnnHandle();
  void DestroyBlasHandle();
  void InitBlasLtHandle();
  void DestroyBlasLtHandle();
  void InitSolverHandle();
  void DestroySolverHandle();
  void InitSparseHandle();
  void DestroySparseHandle();

 private:
W
Wilber 已提交
101 102
  phi::Place place_;

W
Wilber 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  int compute_capability_;
  int runtime_version_;
  int driver_version_;
  int multi_process_;
  int max_threads_per_mp_;
  int max_threads_per_block_;
  std::array<int, 3> max_grid_dim_size_;

  bool owned_stream_{true};
  gpuStream_t stream_;
  std::unique_ptr<Eigen::GpuDevice> gpu_eigen_device_;
  std::unique_ptr<internal::EigenGpuStreamDevice> eigen_stream_;

  blasHandle_t blas_handle_{nullptr};
  blasHandle_t blas_tensor_core_handle_{nullptr};
  blasHandle_t blas_tf32_tensor_core_handle_{nullptr};
  blasLtHandle_t blaslt_handle_{nullptr};
  dnnHandle_t dnn_handle_{nullptr};
  phi::solverHandle_t solver_handle_{nullptr};
  phi::sparseHandle_t sparse_handle_{nullptr};
W
Wilber 已提交
123 124
  // DnnWorkspaceHandle
};
W
Wilber 已提交
125
#endif
W
Wilber 已提交
126

C
csy0225 已提交
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
#if defined(PADDLE_WITH_XPU)
class XPUContextResource {
 public:
  explicit XPUContextResource(const phi::Place& place, void* stream);
  ~XPUContextResource();
  phi::Place Place() const;
  void* GetStream() const;
  int GetDriverVersion() const;
  int GetRuntimeVersion() const;
  int GetXpuVersion() const;
  void ReBindStream(void* stream);

 private:
  void InitXPUResource(void* stream);
  void InitXpuProperties();

 private:
  bool owned_stream_{true};
  void* stream_;
  phi::Place place_;

  int driver_version_;
  int runtime_version_;
  int xpu_version_;
};  // class XPUContextResource
#endif

W
Wilber 已提交
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
class ResourceManager {
 public:
  ResourceManager() = default;
  static ResourceManager& Instance() {
    static ResourceManager* resource_manager = new ResourceManager;
    return *resource_manager;
  }

  // CPU Resource
 public:
  void InitCPUResource();
  CPUContextResource* GetCPUResource() const;

 private:
  std::mutex cpu_mutex_;
  std::unique_ptr<CPUContextResource> cpu_resource_{nullptr};

// GPU Resource
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)

 public:
  void* InitGPUResource(const phi::Place& place, void* stream);
  void DestroyGPUResource(void* stream);
  GPUContextResource* GetGPUResource(void* stream) const;
  int RefCount(void* stream) const;
179
  void GpuResourceSwitchStream(void* old_stream, void* new_stream);
W
Wilber 已提交
180 181 182 183 184 185 186 187 188 189 190 191 192

 private:
  void Decrease(void* stream);
  void Increase(void* stream);

 private:
  std::mutex gpu_mutex_;
  // a stream corresponding to a series of resource.
  std::map<void* /*stream*/, std::atomic<int>> ref_count_;
  std::map<void* /*stream*/, std::unique_ptr<GPUContextResource>>
      gpu_resources_;
#endif

C
csy0225 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
// XPU Resource
#if defined(PADDLE_WITH_XPU)

 public:
  void* InitXPUResource(const phi::Place& place, void* stream);
  void DestroyXPUResource(void* stream);
  XPUContextResource* GetXPUResource(void* stream) const;
  int RefCount(void* stream) const;
  void XpuResourceReBindStream(void* old_stream, void* new_stream);

 private:
  void Decrease(void* stream);
  void Increase(void* stream);

 private:
  std::mutex xpu_mutex_;
  // a stream corresponding to a series of resource.
  std::map<void* /*stream*/, std::atomic<int>> ref_count_;
  std::map<void* /*stream*/, std::unique_ptr<XPUContextResource>>
      xpu_resources_;
#endif

W
Wilber 已提交
215 216
 private:
  DISABLE_COPY_AND_ASSIGN(ResourceManager);
W
Wilber 已提交
217 218 219
};

}  // namespace paddle