carrier.h 2.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// Copyright (c) 2021 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
#include <condition_variable>
18
#include <memory>
19
#include <mutex>
20 21
#include <string>
#include <unordered_map>
22
#include <vector>
23

24
#include "paddle/fluid/distributed/fleet_executor/interceptor.h"
25
#include "paddle/fluid/distributed/fleet_executor/interceptor_message.pb.h"
26 27
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/errors.h"
28
#include "paddle/fluid/platform/macros.h"
29
#include "paddle/fluid/platform/place.h"
30 31

namespace paddle {
32 33 34 35
namespace framework {
class Scope;
}

36 37 38 39 40
namespace distributed {

class TaskNode;
class InterceptorMessageServiceImpl;

41
// A singleton MessageBus
42 43
class Carrier final {
 public:
44 45 46 47
  static Carrier& Instance() {
    static Carrier carrier;
    return carrier;
  }
48

49
  void Init(
50 51 52 53
      const std::unordered_map<int64_t, TaskNode*>& interceptor_id_to_node,
      framework::Scope* minibatch_scope,
      const std::vector<framework::Scope*>& microbatch_scopes,
      const platform::Place& place);
54

55
  ~Carrier();
56 57 58 59

  // Enqueue a message to corresponding interceptor id
  bool EnqueueInterceptorMessage(const InterceptorMessage& interceptor_message);

60 61 62 63 64 65 66
  // get interceptor based on the interceptor id
  Interceptor* GetInterceptor(int64_t interceptor_id);

  // set interceptor with interceptor id
  Interceptor* SetInterceptor(int64_t interceptor_id,
                              std::unique_ptr<Interceptor>);

67 68
  void SetCreatingFlag(bool flag);

69 70
  std::condition_variable& GetCondVar();

71 72 73 74
  void Start();

  bool IsInit() const;

75 76 77
  DISABLE_COPY_AND_ASSIGN(Carrier);

 private:
78 79
  Carrier() = default;

80 81 82
  // create each Interceptor
  void CreateInterceptors();

83 84
  void HandleTmpMessages();

85 86 87 88 89 90
  // interceptor logic id to the Nodes info
  std::unordered_map<int64_t, TaskNode*> interceptor_id_to_node_;

  // interceptor logic id to actually interceptor
  std::unordered_map<int64_t, std::unique_ptr<Interceptor>>
      interceptor_idx_to_interceptor_;
91 92

  std::vector<InterceptorMessage> message_tmp_{};
93
  std::mutex tmp_message_mutex_;
94
  bool creating_interceptors_{true};
95
  std::mutex creating_flag_mutex_;
96
  bool is_init_{false};
97 98 99

  std::mutex running_mutex_;
  std::condition_variable cond_var_;
100 101 102
  std::vector<framework::Scope*> microbatch_scopes_;
  framework::Scope* minibatch_scope_;
  paddle::platform::Place place_;
103 104 105 106
};

}  // namespace distributed
}  // namespace paddle