device_manager.h 11.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

17 18
#include <unordered_map>

19 20 21 22
#include "paddle/phi/common/data_type.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/utils/rw_lock.h"

23
#include "paddle/phi/backends/c_comm_lib.h"
24 25
#include "paddle/phi/backends/device_base.h"
#include "paddle/phi/backends/device_ext.h"
26
#include "paddle/phi/backends/dynload/port.h"
27 28
#include "paddle/phi/backends/event.h"
#include "paddle/phi/backends/stream.h"
29

30
namespace phi {
31 32 33 34
class Device final {
 public:
  Device(size_t dev_id, DeviceInterface* impl) : dev_id_(dev_id), impl_(impl) {}

35 36 37 38
  ~Device();

  void CheckInitialized();

39 40 41
  // Stream
  // ! Create an asynchronous stream
  void CreateStream(
42 43 44
      stream::Stream* stream,
      const stream::Stream::Priority& priority =
          stream::Stream::Priority::kNormal,
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
      const stream::Stream::Flag& flag = stream::Stream::Flag::kDefaultFlag);

  // ! Destroys an asynchronous stream.
  void DestroyStream(stream::Stream* stream);

  // ! Waits for stream tasks to complete.
  void SynchronizeStream(const stream::Stream* stream);

  // ! Queries an asynchronous stream for completion status.
  bool QueryStream(const stream::Stream* stream);

  // ! Add a callback to a compute stream.
  void AddCallback(stream::Stream* stream, stream::Stream::Callback* callback);

  // Event
  // ! Create an event.
61 62
  void CreateEvent(event::Event* event,
                   event::Event::Flag flags = event::Event::Flag::Default);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  // ! Destroy an event.
  void DestroyEvent(event::Event* event);

  // ! Records an event.
  void RecordEvent(const event::Event* event, const stream::Stream* stream);

  // ! Waits for event to complete.
  void SynchronizeEvent(const event::Event* event);

  // ! Queries an event for completion status.
  bool QueryEvent(const event::Event* event);

  // ! Make a compute stream wait on an event
  void StreamWaitEvent(const stream::Stream* stream, const event::Event* event);

  // Memory
80 81 82
  void MemoryCopyH2D(void* dst,
                     const void* src,
                     size_t size,
83 84
                     const stream::Stream* stream = nullptr);

85 86 87
  void MemoryCopyD2H(void* dst,
                     const void* src,
                     size_t size,
88 89
                     const stream::Stream* stream = nullptr);

90 91 92
  void MemoryCopyD2D(void* dst,
                     const void* src,
                     size_t size,
93 94
                     const stream::Stream* stream = nullptr);

95 96 97 98 99
  void MemoryCopyP2P(const Place& dst_place,
                     void* dst,
                     const void* src,
                     size_t size,
                     const stream::Stream* stream = nullptr);
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

  void* MemoryAllocate(size_t size);

  void MemoryDeallocate(void* ptr, size_t size);

  void* MemoryAllocateHost(size_t size);

  void MemoryDeallocateHost(void* ptr, size_t size);

  void* MemoryAllocateUnified(size_t size);

  void MemoryDeallocateUnified(void* ptr, size_t size);

  void MemorySet(void* ptr, uint8_t value, size_t size);

115 116 117 118 119 120 121 122 123 124
  // Blas
  // ! y = alpha * x + beta * y
  template <typename T>
  void BlasAXPBY(const stream::Stream& stream,
                 size_t numel,
                 float alpha,
                 const T* x,
                 float beta,
                 T* y);

125 126 127 128 129
  std::string Type();

 private:
  size_t dev_id_;
  DeviceInterface* impl_;
130 131
  std::once_flag initialized_once_flag_;
  bool initialized_{false};
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
};

class DeviceManager {
 public:
  static bool Register(std::unique_ptr<DeviceInterface> device);
  static bool RegisterPinnedDevice(DeviceInterface* device);
  static Device* GetDeviceWithPlace(const Place& place);
  static std::vector<std::string> GetAllDeviceTypes();
  static std::vector<std::string> GetAllCustomDeviceTypes();
  static std::vector<std::string> GetAllDeviceList();
  static std::vector<std::string> GetAllCustomDeviceList();
  static bool HasDeviceType(const std::string& device_type);
  static bool IsCustom(const std::string& device_type);

  // platform & device
  static void Initialize(const std::string& device_type);

  static void Finalize(const std::string& device_type);

  static void SynchronizeDevice(const Place& place);

  static void SetDevice(const std::string& device_type, size_t device_id);

  static void SetDevice(const Place& place);

  static int GetDevice(const std::string& device_type);

  static size_t GetMinChunkSize(const Place& place);

  static size_t GetMaxChunkSize(const Place& place);

  static size_t GetMaxAllocSize(const Place& place);

  static size_t GetInitAllocSize(const Place& place);

  static size_t GetReallocSize(const Place& place);

  static size_t GetExtraPaddingSize(const Place& place);

  static void MemoryStats(const Place& place, size_t* total, size_t* free);

  static size_t GetDeviceCount(const std::string& device_type);

  static std::vector<size_t> GetDeviceList(const std::string& device_type);
176

177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
  static std::vector<size_t> GetSelectedDeviceList(
      const std::string& device_type);

  // CCL
  static void CCLDestroyComm(const std::string& device_type,
                             ccl::CCLComm ccl_comm);
  static void CCLCommInitRank(const std::string& device_type,
                              size_t num_ranks,
                              ccl::CCLRootId* root_id,
                              size_t rank_id,
                              ccl::CCLComm* ccl_comm);
  static void CCLGetUniqueId(const std::string& device_type,
                             ccl::CCLRootId* root_id);
  static void CCLBroadcast(const std::string& device_type,
                           void* data,
                           size_t num,
                           ccl::CCLDataType data_type,
                           size_t root,
                           const ccl::CCLComm& ccl_comm,
                           const stream::Stream& stream);
  static void CCLAllReduce(const std::string& device_type,
                           void* in_data,
                           void* out_data,
                           size_t num,
                           ccl::CCLDataType data_type,
                           ccl::CCLReduceOp reduce_op,
                           const ccl::CCLComm& ccl_comm,
                           const stream::Stream& stream);
  static void CCLReduce(const std::string& device_type,
                        void* in_data,
                        void* out_data,
                        size_t num,
                        ccl::CCLDataType data_type,
                        ccl::CCLReduceOp reduce_op,
211
                        size_t root_id,
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
                        const ccl::CCLComm& ccl_comm,
                        const stream::Stream& stream);
  static void CCLAllGather(const std::string& device_type,
                           void* in_data,
                           void* out_data,
                           size_t num,
                           ccl::CCLDataType data_type,
                           const ccl::CCLComm& ccl_comm,
                           const stream::Stream& stream);
  static void CCLReduceScatter(const std::string& device_type,
                               void* in_data,
                               void* out_data,
                               size_t num,
                               ccl::CCLDataType data_type,
                               ccl::CCLReduceOp op,
                               const ccl::CCLComm& ccl_comm,
                               const stream::Stream& stream);
  static void CCLGroupStart(const std::string& device_type);
  static void CCLGroupEnd(const std::string& device_type);
  static void CCLSend(const std::string& device_type,
                      void* sendbuf,
                      size_t num,
                      ccl::CCLDataType data_type,
                      size_t dst_rank,
                      const ccl::CCLComm& ccl_comm,
                      const stream::Stream& stream);
  static void CCLRecv(const std::string& device_type,
                      void* recvbuf,
                      size_t num,
                      ccl::CCLDataType data_type,
                      size_t src_rank,
                      const ccl::CCLComm& ccl_comm,
                      const stream::Stream& stream);

246 247 248 249 250 251 252 253 254 255 256
  static void CCLAllToAll(const std::string& device_type,
                          const void** send_buf,
                          const size_t* send_count,
                          const ccl::CCLDataType* send_dtype,
                          void** recv_buf,
                          const size_t* recv_count,
                          const ccl::CCLDataType* recv_dtype,
                          size_t rank,
                          size_t nranks,
                          const ccl::CCLComm& comm,
                          const stream::Stream& stream);
257
  // profiler
258 259 260
  static void ProfilerInitialize(const std::string& dev_type,
                                 phi::TraceEventCollector* collector,
                                 void** context);
261
  static void ProfilerFinalize(const std::string& dev_type,
262
                               phi::TraceEventCollector* collector,
263
                               void* context);
264 265 266 267 268 269 270 271 272 273 274 275 276
  static void ProfilerPrepareTracing(const std::string& dev_type,
                                     phi::TraceEventCollector* collector,
                                     void* context);
  static void ProfilerStartTracing(const std::string& dev_type,
                                   phi::TraceEventCollector* collector,
                                   void* context);
  static void ProfilerStopTracing(const std::string& dev_type,
                                  phi::TraceEventCollector* collector,
                                  void* context);
  static void ProfilerCollectTraceData(const std::string& dev_type,
                                       phi::TraceEventCollector* collector,
                                       uint64_t start_ns,
                                       void* context);
277

278
  static void Release();
279 280 281 282 283 284 285 286 287 288 289 290 291 292

 private:
  DISABLE_COPY_AND_ASSIGN(DeviceManager);
  DeviceManager() {}
  static DeviceManager& Instance();
  static DeviceInterface* GetDeviceInterfaceWithType(
      const std::string& device_type);

  std::unordered_map<std::string, std::unique_ptr<DeviceInterface>>
      device_impl_map_;
  std::unordered_map<std::string, std::vector<std::unique_ptr<Device>>>
      device_map_;
};

293
std::vector<std::string> ListAllLibraries(const std::string& library_dir);
294

295
#ifdef PADDLE_WITH_CUSTOM_DEVICE
296
void LoadCustomRuntimeLib(const std::string& dso_lib_path, void* dso_handle);
297

298 299
void LoadCustomRuntimeLib(const CustomRuntimeParams& runtime_params,
                          std::unique_ptr<C_DeviceInterface> device_interface,
300 301
                          const std::string& dso_lib_path,
                          void* dso_handle);
302
#endif
303 304 305 306 307 308 309 310 311 312 313

class Registrar {
 public:
  template <typename DeviceT>
  explicit Registrar(DeviceT* device_ptr) {
    DeviceManager::Register(std::unique_ptr<DeviceT>(device_ptr));
  }

  void Touch() {}
};

314
}  // namespace phi