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

#include <chrono>
#include <memory>
#include <string>
20
#include <unordered_map>
21 22
#include <vector>

W
Wen Sun 已提交
23
#include "paddle/fluid/distributed/collective/types.h"
24 25 26 27 28
#include "paddle/fluid/eager/api/utils/tensor_utils.h"  // NOTE: this header is required somewhere
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/device_context.h"
#include "paddle/phi/core/enforce.h"
#include "paddle/phi/core/errors.h"
29 30 31 32 33 34

constexpr auto kWaitTimeout = std::chrono::milliseconds(0);

namespace paddle {
namespace distributed {

35
constexpr int kIgnoreId = -1;
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

enum class CommType : std::uint8_t {
  BROADCAST = 0,
  ALLREDUCE = 1,
  ALLREDUCE_SPARSE = 2,  // TODO(shenliang03): to support sparse in allreduce
  REDUCE = 3,
  ALLGATHER = 4,
  GATHER = 5,
  SCATTER = 6,
  REDUCE_SCATTER = 7,
  ALLTOALL = 8,
  SEND = 9,
  RECV = 10,
  BARRIER = 11,
  UNKNOWN = 100,
};

class ProcessGroup {
 public:
  class Task {
   public:
57 58 59
    Task(int rank, CommType comm_type, bool sync_op)
        : rank_(rank), comm_type_(comm_type), sync_op_(sync_op) {}
    virtual ~Task() = default;
60 61

    virtual bool IsCompleted();
G
Galaxy1458 已提交
62
    virtual bool Wait(std::chrono::milliseconds timeout UNUSED = kWaitTimeout) {
63 64 65
      return false;
    }
    virtual void Synchronize() {}
G
Galaxy1458 已提交
66
    virtual void UpdateWaitChain(const phi::DeviceContext& ctx UNUSED) {}
67
    bool IsSync() const { return sync_op_; }
68

69 70
    // TODO(sunyilun): methods below will be removed later
    Task(int rank,
G
Galaxy1458 已提交
71
         const std::vector<phi::DenseTensor>& inputs UNUSED,
72 73
         CommType comm_type)
        : rank_(rank), comm_type_(comm_type) {}
74
    Task(int rank,
G
Galaxy1458 已提交
75
         const std::vector<phi::DenseTensor>& inputs UNUSED,
76
         CommType comm_type,
77 78
         bool sync_op)
        : rank_(rank), comm_type_(comm_type), sync_op_(sync_op) {}
79

80 81
   protected:
    const int rank_;
82
    CommType comm_type_{CommType::UNKNOWN};
83
    std::mutex mutex_;
84 85 86 87
    bool is_completed_{false};

   private:
    bool sync_op_{true};
88 89
  };

90
 public:
L
LiYuRio 已提交
91
  ProcessGroup(int rank, int size, int gid);
92
  virtual ~ProcessGroup() = default;
W
wuhuachaocoding 已提交
93

94 95 96 97
  int GetRank() const { return rank_; }

  int GetSize() const { return size_; }

L
LiYuRio 已提交
98
  virtual std::string GetBackendName() const = 0;
99

G
Galaxy1458 已提交
100 101
  virtual phi::DeviceContext* GetDeviceContext(
      const Place& place UNUSED) const {
102
    PADDLE_THROW(phi::errors::Unimplemented(
103
        "ProcessGroup%s does not support get device_context.",
L
LiYuRio 已提交
104 105
        GetBackendName()));
  }
106

G
Galaxy1458 已提交
107 108
  virtual phi::DeviceContext* GetDeviceContext(
      const Place& place UNUSED, bool use_calc_stream UNUSED) const {
109 110 111 112 113 114
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support get device_context.",
        GetBackendName()));
  }

  // without stream APIs
W
Wen Sun 已提交
115
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
G
Galaxy1458 已提交
116 117 118
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      bool sync_op UNUSED) {
119 120 121
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support all_gather with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
122 123
  }

124
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
G
Galaxy1458 已提交
125 126 127 128 129
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      int64_t offset UNUSED,
      int64_t numel UNUSED,
      bool sync_op UNUSED) {
130
    PADDLE_THROW(phi::errors::Unimplemented(
131
        "ProcessGroup%s does not support all_gather with sync_op flag.",
132 133 134 135
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
G
Galaxy1458 已提交
136 137 138 139
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const AllreduceOptions& opts UNUSED,
      bool sync_op UNUSED) {
140
    PADDLE_THROW(phi::errors::Unimplemented(
141
        "ProcessGroup%s does not support all_reduce with sync_op flag.",
142 143 144
        GetBackendName()));
  }

145
  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
G
Galaxy1458 已提交
146 147 148 149 150
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const std::vector<int64_t>& out_size_each_rank UNUSED,
      const std::vector<int64_t>& in_size_each_rank UNUSED,
      bool sync_op UNUSED) {
151
    PADDLE_THROW(phi::errors::Unimplemented(
152 153 154 155
        "ProcessGroup%s does not support all_to_all with sync_op flag.",
        GetBackendName()));
  }

156
  virtual std::shared_ptr<ProcessGroup::Task> Barrier(
G
Galaxy1458 已提交
157
      const BarrierOptions& UNUSED = BarrierOptions()) {
158
    PADDLE_THROW(phi::errors::Unimplemented(
159
        "ProcessGroup%s does not support barrier.", GetBackendName()));
160 161 162
  }

  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
G
Galaxy1458 已提交
163 164 165 166
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const BroadcastOptions& opts UNUSED,
      bool sync_op UNUSED) {
167
    PADDLE_THROW(phi::errors::Unimplemented(
168 169 170 171
        "ProcessGroup%s does not support broadcast with sync_op flag",
        GetBackendName()));
  }

172
  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
G
Galaxy1458 已提交
173 174 175 176
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ReduceOptions& opts UNUSED,
      bool sync_op UNUSED) {
177
    PADDLE_THROW(phi::errors::Unimplemented(
178 179 180 181 182
        "ProcessGroup%s does not support reduce with sync_op flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> ReduceScatter(
G
Galaxy1458 已提交
183 184 185 186
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ReduceScatterOptions& opts UNUSED,
      bool sync_op UNUSED) {
187
    PADDLE_THROW(phi::errors::Unimplemented(
188 189 190 191 192
        "ProcessGroup%s does not support reduce_scatter with sync_op flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
G
Galaxy1458 已提交
193 194 195 196
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ScatterOptions& opts UNUSED,
      bool sync_op UNUSED) {
197
    PADDLE_THROW(phi::errors::Unimplemented(
198 199 200 201
        "ProcessGroup%s does not support scatter with sync_op flag.",
        GetBackendName()));
  }

G
Galaxy1458 已提交
202 203 204 205
  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor
                                                       UNUSED,
                                                   int src_rank UNUSED,
                                                   bool sync_op UNUSED) {
206 207 208
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support recv with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
209 210
  }

G
Galaxy1458 已提交
211 212 213 214 215 216
  virtual std::shared_ptr<ProcessGroup::Task> Recv(phi::DenseTensor* tensor
                                                       UNUSED,
                                                   int src_rank UNUSED,
                                                   int64_t offset UNUSED,
                                                   int64_t numel UNUSED,
                                                   bool sync_op UNUSED) {
217
    PADDLE_THROW(phi::errors::Unimplemented(
218
        "ProcessGroup%s does not support recv with sync_op flag.",
219 220 221
        GetBackendName()));
  }

W
Wen Sun 已提交
222
  virtual std::shared_ptr<ProcessGroup::Task> Send(
G
Galaxy1458 已提交
223 224 225
      const phi::DenseTensor& tensor UNUSED,
      int dst_rank UNUSED,
      bool sync_op UNUSED) {
226 227 228
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support send with sync_op flag.",
        GetBackendName()));
W
Wen Sun 已提交
229 230
  }

231
  virtual std::shared_ptr<ProcessGroup::Task> Send(
G
Galaxy1458 已提交
232 233 234 235 236
      const phi::DenseTensor& tensor UNUSED,
      int dst_rank UNUSED,
      int64_t offset UNUSED,
      int64_t numel UNUSED,
      bool sync_op UNUSED) {
237
    PADDLE_THROW(phi::errors::Unimplemented(
238
        "ProcessGroup%s does not support send with sync_op flag.",
239 240 241
        GetBackendName()));
  }

242 243
  // stream APIs
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
G
Galaxy1458 已提交
244 245 246 247
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
248 249 250 251 252 253 254
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
G
Galaxy1458 已提交
255 256 257 258 259 260
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      int64_t offset UNUSED,
      int64_t numel UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
261 262 263 264 265 266 267
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
G
Galaxy1458 已提交
268 269 270 271 272
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const AllreduceOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
273 274 275 276 277 278 279
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_reduce "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
G
Galaxy1458 已提交
280 281 282 283 284 285
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const std::vector<int64_t>& out_size_each_rank UNUSED,
      const std::vector<int64_t>& in_size_each_rank UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
286 287 288 289 290 291 292
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support all_to_all "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
G
Galaxy1458 已提交
293 294 295 296 297
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const BroadcastOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
298 299 300 301 302 303 304
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support broadcast "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
G
Galaxy1458 已提交
305 306 307 308 309
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ReduceOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
310 311 312 313 314 315 316
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support reduce "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> ReduceScatter(
G
Galaxy1458 已提交
317 318 319 320 321
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ReduceScatterOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
322 323 324 325 326 327 328
    PADDLE_THROW(phi::errors::Unimplemented(
        "ProcessGroup%s does not support reduce_scatter "
        "with sync_op and use_calc_stream flag.",
        GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
G
Galaxy1458 已提交
329 330 331 332 333
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const ScatterOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
334 335 336 337 338 339
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support scatter "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

zhenhailiu's avatar
zhenhailiu 已提交
340
  virtual std::shared_ptr<ProcessGroup::Task> Gather(
G
Galaxy1458 已提交
341 342 343 344 345
      phi::DenseTensor* out_tensor UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const GatherOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
zhenhailiu's avatar
zhenhailiu 已提交
346 347 348 349 350 351 352
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Gather(
G
Galaxy1458 已提交
353 354 355 356 357
      std::vector<phi::DenseTensor>* gather_tensors_ptr UNUSED,
      const phi::DenseTensor& in_tensor UNUSED,
      const GatherOptions& opts UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
zhenhailiu's avatar
zhenhailiu 已提交
358 359 360 361 362 363
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support gather "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

G
Galaxy1458 已提交
364 365 366 367 368
  virtual std::shared_ptr<ProcessGroup::Task> Recv(
      phi::DenseTensor* tensor UNUSED,
      int src_rank UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
369 370 371 372 373 374
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support recv with "
                                   "sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

G
Galaxy1458 已提交
375 376 377 378 379 380 381
  virtual std::shared_ptr<ProcessGroup::Task> Recv(
      phi::DenseTensor* tensor UNUSED,
      int src_rank UNUSED,
      int64_t offset UNUSED,
      int64_t numel UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
382 383 384 385 386 387 388
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support recv "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Send(
G
Galaxy1458 已提交
389 390 391 392
      const phi::DenseTensor& tensor UNUSED,
      int dst_rank UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
393 394 395 396 397 398 399
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support send "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Send(
G
Galaxy1458 已提交
400 401 402 403 404 405
      const phi::DenseTensor& tensor UNUSED,
      int dst_rank UNUSED,
      int64_t offset UNUSED,
      int64_t numel UNUSED,
      bool sync_op UNUSED,
      bool use_calc_stream UNUSED) {
406 407 408 409 410 411 412
    PADDLE_THROW(
        phi::errors::Unimplemented("ProcessGroup%s does not support send "
                                   "with sync_op and use_calc_stream flag.",
                                   GetBackendName()));
  }

  // legacy APIs
413
  // TODO(liyurui): This API will be moved later
414
  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
415 416
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
G
Galaxy1458 已提交
417
      const AllreduceOptions& UNUSED = AllreduceOptions()) {
418
    PADDLE_THROW(phi::errors::InvalidArgument(
419 420 421
        "ProcessGroup%s does not support allreduce", GetBackendName()));
  }

422 423 424
  virtual std::shared_ptr<ProcessGroup::Task> AllReduce(
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
G
Galaxy1458 已提交
425
      const AllreduceOptions& UNUSED,
426
      bool) {
427
    PADDLE_THROW(phi::errors::InvalidArgument(
428 429 430 431
        "ProcessGroup%s does not support allreduce with sync_op flag",
        GetBackendName()));
  }

432
  // TODO(sunyilun): methods below will be removed later
433
  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
434 435
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
G
Galaxy1458 已提交
436
      const BroadcastOptions& UNUSED = BroadcastOptions()) {
437
    PADDLE_THROW(phi::errors::InvalidArgument(
B
Baibaifan 已提交
438 439 440
        "ProcessGroup%s does not support broadcast", GetBackendName()));
  }

441 442 443
  virtual std::shared_ptr<ProcessGroup::Task> Broadcast(
      std::vector<phi::DenseTensor>& /* input tensors */,   // NOLINT
      std::vector<phi::DenseTensor>& /* output tensors */,  // NOLINT
G
Galaxy1458 已提交
444
      const BroadcastOptions& UNUSED,
445
      bool) {
446
    PADDLE_THROW(phi::errors::InvalidArgument(
447 448 449 450
        "ProcessGroup%s does not support broadcast with sync_op flag",
        GetBackendName()));
  }

B
Baibaifan 已提交
451
  virtual std::shared_ptr<ProcessGroup::Task> Send(
452
      std::vector<phi::DenseTensor>&, int) {  // NOLINT
453
    PADDLE_THROW(phi::errors::InvalidArgument(
B
Baibaifan 已提交
454 455 456 457
        "ProcessGroup%s does not support send", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Recv(
458
      std::vector<phi::DenseTensor>&, int) {  // NOLINT
459
    PADDLE_THROW(phi::errors::InvalidArgument(
460
        "ProcessGroup%s does not support recv", GetBackendName()));
461 462
  }

463
  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
464 465
      std::vector<phi::DenseTensor>&,    // NOLINT
      std::vector<phi::DenseTensor>&) {  // NOLINT
466
    PADDLE_THROW(phi::errors::InvalidArgument(
467 468 469 470 471 472 473
        "ProcessGroup%s does not support all_gather", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllGather(
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
      bool) {
474
    PADDLE_THROW(phi::errors::InvalidArgument(
475 476
        "ProcessGroup%s does not support all_gather with sync_op flag",
        GetBackendName()));
477 478 479
  }

  virtual std::shared_ptr<ProcessGroup::Task> AllToAll(
480 481
      std::vector<phi::DenseTensor>&,    // NOLINT
      std::vector<phi::DenseTensor>&) {  // NOLINT
482
    PADDLE_THROW(phi::errors::InvalidArgument(
483 484 485 486
        "ProcessGroup%s does not support AllToAll", GetBackendName()));
  }

  virtual std::shared_ptr<ProcessGroup::Task> Reduce(
487 488
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
489
      const ReduceOptions& opts UNUSED) {
490
    PADDLE_THROW(phi::errors::InvalidArgument(
491 492 493
        "ProcessGroup%s does not support reduce", GetBackendName()));
  }

494
  virtual std::shared_ptr<ProcessGroup::Task> Scatter(
495 496
      std::vector<phi::DenseTensor>&,  // NOLINT
      std::vector<phi::DenseTensor>&,  // NOLINT
497
      const ScatterOptions&) {
498
    PADDLE_THROW(phi::errors::InvalidArgument(
499 500 501
        "ProcessGroup%s does not support scatter", GetBackendName()));
  }

502
 protected:
L
LiYuRio 已提交
503 504 505
  int rank_;
  int size_;
  int gid_;
506 507
};

L
LiYuRio 已提交
508 509 510 511
class ProcessGroupIdMap
    : public std::unordered_map<int, std::shared_ptr<ProcessGroup>> {
 public:
  static ProcessGroupIdMap& GetInstance();
512
  static void DestroyProcessGroup();
L
LiYuRio 已提交
513 514 515
};

// TODO(dev): The following method will be removed soon.
L
lilong12 已提交
516 517
class ProcessGroupMapFromGid {
 public:
518
  bool has(int gid) { return map_.find(gid) != map_.end(); }
L
lilong12 已提交
519

520
  void insert(int gid, ProcessGroup* pg) { map_[gid] = pg; }
L
lilong12 已提交
521

522
  ProcessGroup* get(int gid) { return map_.find(gid)->second; }
L
lilong12 已提交
523 524 525 526 527 528 529 530 531 532 533 534 535

  static std::shared_ptr<ProcessGroupMapFromGid> getInstance() {
    static auto s_instance = std::make_shared<ProcessGroupMapFromGid>();
    return s_instance;
  }

  ProcessGroupMapFromGid() = default;
  ~ProcessGroupMapFromGid() = default;

 private:
  std::unordered_map<int, ProcessGroup*> map_;
};

536 537
}  //  namespace distributed
}  //  namespace paddle