This repository has been archived by the owner on Nov 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 358
/
rdma_sync.cpp
208 lines (183 loc) · 5.69 KB
/
rdma_sync.cpp
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
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
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
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache 2.0 license.
// Please see LICENSE file in the project root for terms.
#ifdef INFINIBAND
#include <vector>
#include "boost/algorithm/string.hpp"
#include "boost/thread.hpp"
#include "caffe/caffe.hpp"
#include "util/rdma_sync.hpp"
namespace caffe {
template<typename Dtype>
RDMASync<Dtype>::RDMASync(shared_ptr<Solver<Dtype> > root_solver,
const vector<shared_ptr<RDMAChannel> >& peers,
int rank)
: P2PSync<Dtype>(root_solver, NULL, root_solver->param()),
adapter_(rank == 0 ? peers[1]->adapter() : peers[0]->adapter()),
peers_(peers),
rank_(rank),
data_send_(peers.size()),
data_recv_(peers.size()),
diff_send_(peers.size()),
diff_recv_(peers.size()),
ctrl_send_(peers.size()),
ctrl_recv_(peers.size()) {
#ifndef CPU_ONLY
int initial_device;
CUDA_CHECK(cudaGetDevice(&initial_device));
CUDA_CHECK(cudaSetDevice(root_solver->param().device_id()));
chunk(rank_, &own_offs_, &own_size_);
for (int peer = 0; peer < peers_.size(); ++peer) {
if (peer == rank_) {
// Chunk for which we are master, connected to all peers. Loops must be
// imbricated to have buffers created in the same order on all boxes.
for (int i = 0; i < peers_.size(); ++i) {
if (i != rank_) {
CreateMasterBuffers(i);
}
}
} else {
// Other chunks are connected to their respective masters
CreateWorkerBuffers(peer);
}
}
CUDA_CHECK(cudaSetDevice(initial_device));
#else
NO_GPU;
#endif
}
template<typename Dtype>
void RDMASync<Dtype>::chunk(int peer, size_t* offs, size_t* size) {
// TODO align chunks to page size?
size_t start = (peer + 0) * size_ / peers_.size();
size_t until = (peer + 1) * size_ / peers_.size();
*offs = start;
*size = until - start;
}
template<typename Dtype>
void RDMASync<Dtype>::CreateMasterBuffers(int peer) {
RDMAChannel* channel = peers_[peer].get();
size_t size = own_size_ * sizeof(Dtype);
// Send data from local (rank_) to remote (peer)
uint8_t* data = reinterpret_cast<uint8_t*>(data_ + own_offs_);
data_send_[peer].reset(new RDMABuffer(channel, data, size));
// Recv diff from remote (peer) to local (rank_)
uint8_t* buffer;
CUDA_CHECK(cudaMalloc(&buffer, size));
diff_recv_[peer].reset(new RDMABuffer(channel, buffer, size));
// Send ctrl from local (rank_) to remote (peer)
uint8_t* ctrl;
CUDA_CHECK(cudaMalloc(&ctrl, CTRL_BUF_SIZE));
ctrl_send_[peer].reset(new RDMABuffer(channel, ctrl, CTRL_BUF_SIZE));
}
template<typename Dtype>
void RDMASync<Dtype>::CreateWorkerBuffers(int peer) {
RDMAChannel* channel = peers_[peer].get();
size_t offs, size;
chunk(peer, &offs, &size);
size *= sizeof(Dtype);
// Recv data from remote (peer) to local (rank_)
uint8_t* data = reinterpret_cast<uint8_t*>(data_ + offs);
data_recv_[peer].reset(new RDMABuffer(channel, data, size));
// Send diff from local (rank_) to remote (peer)
uint8_t* diff = reinterpret_cast<uint8_t*>(diff_ + offs);
diff_send_[peer].reset(new RDMABuffer(channel, diff, size));
// Recv ctrl from remote (peer) to local (rank_)
uint8_t* ctrl;
CUDA_CHECK(cudaMalloc(&ctrl, CTRL_BUF_SIZE));
ctrl_recv_[peer].reset(new RDMABuffer(channel, ctrl, CTRL_BUF_SIZE));
}
template<typename Dtype>
RDMASync<Dtype>::~RDMASync() {
for (int i = 0; i < peers_.size(); ++i) {
if (i != rank_) {
CUDA_CHECK(cudaFree(diff_recv_[i]->addr()));
CUDA_CHECK(cudaFree(ctrl_send_[i]->addr()));
CUDA_CHECK(cudaFree(ctrl_recv_[i]->addr()));
}
}
}
template<typename Dtype>
void RDMASync<Dtype>::on_start() {
// Send weights to each node
sync();
// Send weights to local GPUs
P2PSync<Dtype>::on_start();
}
template<typename Dtype>
void RDMASync<Dtype>::on_gradients_ready() {
// Reduce gradients from local GPUs
P2PSync<Dtype>::on_gradients_ready();
// Send gradients to corresponding parameter server node
int peer = rank_ + 1;
for (int n = 0; n < peers_.size() - 1; ++n) {
if (peer == peers_.size()) {
peer = 0;
}
diff_send_[peer]->Write();
peer++;
}
// Sum gradients as they are received
for (int n = 0; n < peers_.size() - 1; ++n) {
RDMABuffer* buffer = adapter_.received().pop();
#ifdef DEBUG
bool ok = false;
for (int i = 0; i < diff_recv_.size(); ++i) {
if (buffer == diff_recv_[i].get()) {
ok = true;
}
}
CHECK(ok);
CHECK(buffer->size() == own_size_ * sizeof(Dtype));
#endif
Dtype* src = reinterpret_cast<Dtype*>(buffer->addr());
Dtype* dst = diff_ + own_offs_;
caffe_gpu_add(own_size_, src, dst, dst);
}
}
template<typename Dtype>
void RDMASync<Dtype>::sync(bool data) {
// Send weights to each peer
int peer = rank_ + 1; // To avoid all sending to same peer at the same time
for (int n = 0; n < peers_.size() - 1; ++n) {
if (peer == peers_.size()) {
peer = 0;
}
if (data) {
data_send_[peer]->Write();
} else {
ctrl_send_[peer]->Write(data);
}
peer++;
}
for (int n = 0; n < peers_.size() - 1; ++n) {
#ifdef DEBUG
BlockingQueue<RDMABuffer*> * bq = NULL;
vector<shared_ptr<RDMABuffer> > * recvd = NULL;
if (data) {
bq = &adapter_.received();
recvd = &data_recv_;
} else {
bq = &adapter_.ctrl_received();
recvd = &ctrl_recv_;
}
RDMABuffer* buffer = (*bq).pop();
bool ok = false;
for (int i = 0; i < (*recvd).size(); ++i) {
if (buffer == (*recvd)[i].get()) {
ok = true;
}
}
CHECK(ok);
#else
if (data) {
adapter_.received().pop();
} else {
adapter_.ctrl_received().pop();
}
#endif
}
}
INSTANTIATE_CLASS(RDMASync);
} // namespace caffe
#endif