-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
/
communicator.cc
86 lines (79 loc) · 2.9 KB
/
communicator.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*!
* Copyright 2022 XGBoost contributors
*/
#include <map>
#include "communicator.h"
#include "comm.h"
#include "in_memory_communicator.h"
#include "noop_communicator.h"
#include "rabit_communicator.h"
#if defined(XGBOOST_USE_FEDERATED)
#include "../../plugin/federated/federated_communicator.h"
#endif
#include "../processing/processor.h"
processing::Processor *processor_instance;
namespace xgboost::collective {
thread_local std::unique_ptr<Communicator> Communicator::communicator_{new NoOpCommunicator()};
thread_local CommunicatorType Communicator::type_{};
thread_local std::string Communicator::nccl_path_{};
void Communicator::Init(Json const& config) {
auto nccl = OptionalArg<String>(config, "dmlc_nccl_path", std::string{DefaultNcclName()});
nccl_path_ = nccl;
auto type = GetTypeFromEnv();
auto const arg = GetTypeFromConfig(config);
if (arg != CommunicatorType::kUnknown) {
type = arg;
}
if (type == CommunicatorType::kUnknown) {
// Default to Rabit if unspecified.
type = CommunicatorType::kRabit;
}
type_ = type;
switch (type) {
case CommunicatorType::kRabit: {
communicator_.reset(RabitCommunicator::Create(config));
break;
}
case CommunicatorType::kFederated: {
#if defined(XGBOOST_USE_FEDERATED)
communicator_.reset(FederatedCommunicator::Create(config));
// Get processor configs
std::string plugin_name{};
std::string loader_params_key{};
std::string loader_params_map{};
std::string proc_params_key{};
std::string proc_params_map{};
plugin_name = OptionalArg<String>(config, "plugin_name", plugin_name);
loader_params_key = OptionalArg<String>(config, "loader_params_key", loader_params_key);
loader_params_map = OptionalArg<String>(config, "loader_params_map", loader_params_map);
proc_params_key = OptionalArg<String>(config, "proc_params_key", proc_params_key);
proc_params_map = OptionalArg<String>(config, "proc_params_map", proc_params_map);
// Initialize processor if plugin_name is provided
if (!plugin_name.empty()) {
std::map<std::string, std::string> loader_params = {{loader_params_key, loader_params_map}};
std::map<std::string, std::string> proc_params = {{proc_params_key, proc_params_map}};
processing::ProcessorLoader loader(loader_params);
processor_instance = loader.load(plugin_name);
processor_instance->Initialize(collective::GetRank() == 0, proc_params);
}
#else
LOG(FATAL) << "XGBoost is not compiled with Federated Learning support.";
#endif
break;
}
case CommunicatorType::kInMemory:
case CommunicatorType::kInMemoryNccl: {
communicator_.reset(InMemoryCommunicator::Create(config));
break;
}
case CommunicatorType::kUnknown:
LOG(FATAL) << "Unknown communicator type.";
}
}
#ifndef XGBOOST_USE_CUDA
void Communicator::Finalize() {
communicator_->Shutdown();
communicator_.reset(new NoOpCommunicator());
}
#endif
} // namespace xgboost::collective