forked from BVLC/caffe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel.cpp
More file actions
1277 lines (1110 loc) · 37.1 KB
/
Copy pathparallel.cpp
File metadata and controls
1277 lines (1110 loc) · 37.1 KB
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
209
210
211
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdlib>
#include <string>
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <sstream>
#include <pthread.h>
#include <glog/logging.h>
#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <netdb.h>
#include <net/if.h>
#include <unistd.h>
#include <iomanip>
#include <caffe/caffe.hpp>
#include "caffe/filler.hpp"
#include "caffe/parallel.hpp"
using namespace std;
namespace caffe {
void Meter::show(std::ostream& s) const {
ptime now = microsec_clock::local_time();
uint64_t value = value_;
uint64_t delta = value - last_;
uint64_t u_sec = (now - time_).total_microseconds();
double per_s = delta * 1e6 / (u_sec ? u_sec : 1);
last_ = value;
time_ = now;
s << name_ << " " << value << " (";
if (unit_size_)
s << (int) (per_s * unit_size_ / (1024 * 1024)) << " mb";
else
s << std::setprecision(2) << per_s;
s << "/s)";
}
//
template<typename Dtype>
static size_t len(const vector<shared_ptr<Blob<Dtype> > >& params) {
size_t len = 0;
for (int i = 0; i < params.size(); ++i)
len += params[i]->count();
return len;
}
// Align arrays to all potential chunk sizes to avoid boundary checks
template<typename Dtype>
static size_t align(const size_t len) {
size_t m = len;
#ifndef CPU_ONLY
m = max(m, CPUGPUSync<Dtype>::chunks(len) * CPUGPUSync<Dtype>::CHUNK);
#endif
#ifdef __linux__
m = max(m, RawSync<Dtype>::chunks(len) * RawSync<Dtype>::CHUNK);
#endif
#ifdef INFINIBAND
m = max(m, IBSync<Dtype>::chunks(len) * IBSync<Dtype>::CHUNK);
#endif
return m;
}
template<typename Dtype>
Params<Dtype>::Params(const vector<shared_ptr<Blob<Dtype> > >& blobs,
const string& file_map)
: len_used_(len<Dtype>(blobs)),
len_buff_(align<Dtype>(len_used_)) {
bool exists = false;
if (file_map.empty()) {
CaffeMallocHost((void**) &cpu_, len_buff_ * sizeof(Dtype));
memset(cpu_, 0, len_buff_ * sizeof(Dtype));
} else {
struct stat st_buf;
exists = stat(file_map.c_str(), &st_buf) == 0;
int fd = open(file_map.c_str(), O_RDWR | O_CREAT, //
S_IRWXU | S_IRWXG | S_IRWXO);
CHECK(!ftruncate(fd, len_buff_ * sizeof(Dtype)));
cpu_ = (Dtype*) mmap(NULL, //
len_buff_ * sizeof(Dtype),
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
close(fd);
}
Dtype* cpu = cpu_;
for (int i = 0; i < blobs.size(); ++i) {
int size = blobs[i]->data()->size();
// Init to current values of blobs if file doesn't already exists
if (!exists)
memcpy(cpu, blobs[i]->data()->cpu_data(), size);
cpu += size / sizeof(Dtype);
CHECK(cpu <= cpu_ + len_used_);
}
size_t check = 0;
for (int i = 0; i < blobs.size(); ++i)
check += blobs[i]->count();
Dtype* expect = cpu_ + check;
CHECK_EQ(expect, cpu);
iterations_ = 0;
}
template<typename Dtype>
Params<Dtype>::~Params() {
CaffeFreeHost((void*) cpu_);
}
template<typename Dtype>
void Params<Dtype>::configure(Solver<Dtype>* solver) const {
// Replace weights
vector<shared_ptr<Blob<Dtype> > > &blobs = solver->net()->params();
Dtype* cpu = cpu_;
for (int i = 0; i < blobs.size(); ++i) {
blobs[i]->data()->set_cpu_data(cpu);
cpu += blobs[i]->data()->size() / sizeof(Dtype);
CHECK(cpu <= cpu_ + len_used_);
}
// Check sizes
size_t check = 0;
for (int i = 0; i < blobs.size(); ++i)
check += blobs[i]->count();
Dtype* expect = cpu_ + check;
CHECK_EQ(expect, cpu);
solver->iter_total(&iterations_);
}
//
#ifndef CPU_ONLY
#include <cuda_runtime.h>
template<typename Dtype>
GPUParams<Dtype>::GPUParams(const Params<Dtype>& params, int device)
: params_(params),
device_(device) {
int current;
CUDA_CHECK(cudaGetDevice(¤t));
CUDA_CHECK(cudaSetDevice(device));
const size_t size = params.len_buff() * sizeof(Dtype);
CUDA_CHECK(cudaMalloc((void** ) &gpu_, size));
CUDA_CHECK(cudaMemcpy(gpu_, params.cpu(), size, cudaMemcpyHostToDevice));
CUDA_CHECK(cudaSetDevice(current));
}
template<typename Dtype>
GPUParams<Dtype>::~GPUParams() {
CUDA_CHECK(cudaFree((void* ) gpu_));
}
template<typename Dtype>
void GPUParams<Dtype>::configure(Solver<Dtype>* solver) const {
// Replace GPU weights
vector<shared_ptr<Blob<Dtype> > > &blobs = solver->net()->params();
Dtype* gpu = gpu_;
for (int i = 0; i < blobs.size(); ++i) {
blobs[i]->data()->set_gpu_data(gpu);
gpu += blobs[i]->data()->size() / sizeof(Dtype);
CHECK(gpu <= gpu_ + params_.len_used());
}
size_t check = 0;
for (int i = 0; i < blobs.size(); ++i)
check += blobs[i]->count();
Dtype* expect = gpu_ + check;
CHECK_EQ(expect, gpu);
solver->iter_total(¶ms_.iterations_);
}
//
template<typename Dtype>
GPUStream<Dtype>::GPUStream() {
int least, greatest;
cudaDeviceGetStreamPriorityRange(&least, &greatest);
cudaStreamCreateWithPriority(&stream_, cudaStreamNonBlocking, least);
}
template<typename Dtype>
GPUStream<Dtype>::~GPUStream() {
cudaStreamDestroy(stream_);
}
//
template<typename Dtype>
GPUSync<Dtype>::GPUSync(const GPUParams<Dtype>& params)
: params_(params) {
size_t size = params.params().len_buff() * sizeof(Dtype);
Dtype* gpu = params.gpu();
CUDA_CHECK(cudaMalloc((void** ) &gpu_last_, size));
CUDA_CHECK(cudaMemcpy(gpu_last_, gpu, size, cudaMemcpyDeviceToDevice));
}
template<typename Dtype>
GPUSync<Dtype>::~GPUSync() {
CUDA_CHECK(cudaFree((void* ) gpu_last_));
}
//
template<typename Dtype>
CPUGPUSync<Dtype>::CPUGPUSync(const GPUParams<Dtype>& params)
: GPUSync<Dtype>(params),
chunks_(chunks(params.params().len_used())),
calls_("calls", CHUNK * sizeof(Dtype)),
cycles_("cycles") {
}
template<typename Dtype>
CPUGPUSync<Dtype>::~CPUGPUSync() {
stop();
}
template<typename Dtype>
void CPUGPUSync<Dtype>::run() {
CUDA_CHECK(cudaSetDevice(this->params_.device()));
GPUStream<Dtype> gpu_stream;
const cudaStream_t& stream = gpu_stream.stream();
// Current cpu values when invoking kernel, gradients on the way back
Dtype* buf;
Dtype* tmp;
CUDA_CHECK(cudaMalloc((void** ) &buf, CHUNK * sizeof(Dtype)));
CaffeMallocHost((void**) &tmp, CHUNK * sizeof(Dtype));
const size_t len = CHUNK * sizeof(Dtype);
// Explicit directions for readability
const cudaMemcpyKind put = cudaMemcpyHostToDevice;
const cudaMemcpyKind get = cudaMemcpyDeviceToHost;
uint32_t index = 0;
Dtype* cpu = this->params_.params().cpu();
Dtype* gpu = this->params_.gpu();
Dtype* last = this->gpu_last_;
uint8_t get_grads = true;
while (!must_stop()) {
size_t off = index * CHUNK;
CUDA_CHECK(cudaMemcpyAsync(buf, &cpu[off], len, put, stream));
// TODO simpler kernel
sync_worker_kernel<Dtype>(gpu, last, &buf, &off, &buf, &get_grads, //
0, 1, stream, CHUNK);
CUDA_CHECK(cudaMemcpyAsync(tmp, buf, len, get, stream));
cudaStreamSynchronize(stream);
for (size_t i = 0; i < CHUNK; ++i)
cpu[off + i] += tmp[i];
if (++index == chunks_) {
index = 0;
cycles_++;
}
calls_++;
}
CaffeFreeHost((void*) tmp);
CUDA_CHECK(cudaFree((void* ) buf));
}
#endif
//
template<typename Dtype>
DistSync<Dtype>::DistSync(uint32_t nodes, uint32_t chunks)
: nodes_(nodes),
chunks_(chunks),
received_(chunks),
remaining_(chunks),
cycles_("cycles") {
own_start_ = own_until_ = chunk_ = 0;
}
template<typename Dtype>
void DistSync<Dtype>::dist_init(int rank) {
own_start_ = (rank + 0) * chunks_ / nodes_;
own_until_ = (rank + 1) * chunks_ / nodes_;
LOG(INFO)<< "range: " << own_start_ << " " << own_until_;
chunk_ = own_start_;
for (uint32_t chunk = own_start_; chunk < own_until_; ++chunk) {
received_[chunk] = true;
remaining_--;
}
}
template<typename Dtype>
inline int DistSync<Dtype>::chunk_master(uint32_t chunk) {
// TODO find range without loop?
for (int i = nodes_ - 1; i >= 0; --i) {
uint32_t start = i * chunks_ / nodes_;
if (start <= chunk)
return i;
}
CHECK(false);
return -1;
}
//
INSTANTIATE_CLASS(Params);
#ifndef CPU_ONLY
INSTANTIATE_CLASS(GPUParams);
INSTANTIATE_CLASS(GPUSync);
INSTANTIATE_CLASS(CPUGPUSync);
#endif
INSTANTIATE_CLASS(DistSync);
#ifdef RDMA
ibv_context* IBChannel::open_device(ibv_device* ib_dev) {
ibv_context* context = ibv_open_device(ib_dev);
CHECK(context) << "Open context failed for " << ibv_get_device_name(ib_dev);
return context;
}
ibv_pd* IBChannel::alloc_pd(ibv_context* context) {
ibv_pd* pd = ibv_alloc_pd(context);
CHECK(pd) << "Failed to allocate protection domain";
return pd;
}
IBChannel::IBChannel(ibv_device* ib_dev)
: context_(open_device(ib_dev)),
pd_(alloc_pd(context_)),
buf_send_(),
buf_recv_(),
mr_send_(),
mr_recv_(),
send_queue_(FRAMES),
recv_queue_(FRAMES),
sent_("sent", MTU),
recv_("recv", MTU) {
cq_ = ibv_create_cq(context_, FRAMES * 2, NULL, NULL, 0);
CHECK(cq_) << "Failed to create completion queue";
// Create queue pair
{
ibv_qp_init_attr attr;
memset(&attr, 0, sizeof attr);
attr.send_cq = cq_;
attr.recv_cq = cq_;
attr.cap.max_send_wr = FRAMES;
attr.cap.max_recv_wr = FRAMES;
attr.cap.max_send_sge = 1;
attr.cap.max_recv_sge = 1;
attr.qp_type = IBV_QPT_UD,
qp_ = ibv_create_qp(pd_, &attr);
CHECK(qp_) << "Failed to create queue pair";
}
// Init queue pair
{
ibv_qp_attr attr;
memset(&attr, 0, sizeof attr);
attr.qp_state = IBV_QPS_INIT;
attr.pkey_index = 0;
attr.port_num = PORT;
attr.qkey = 0x11111111;
int mask = IBV_QP_STATE | IBV_QP_PKEY_INDEX | IBV_QP_PORT | IBV_QP_QKEY;
CHECK(!ibv_modify_qp(qp_, &attr, mask)) << "Failed to set QP to INIT";
}
// Local address
{
memset(&local_, 0, sizeof(local_));
ibv_port_attr attr;
CHECK(!ibv_query_port(context_, PORT, &attr)) << "Query port";
CHECK(attr.active_mtu == IBV_MTU_4096);
local_.lid = attr.lid;
local_.qpn = qp_->qp_num;
local_.psn = caffe_rng_rand() & 0xffffff;
}
// Queue pair to recv & send
{
struct ibv_qp_attr attr;
attr.qp_state = IBV_QPS_RTR; // Ready to receive
CHECK(!ibv_modify_qp(qp_, &attr, IBV_QP_STATE)) << "QP to RTR";
attr.qp_state = IBV_QPS_RTS; // Ready to send
attr.sq_psn = local_.psn;
int mask = IBV_QP_STATE | IBV_QP_SQ_PSN;
CHECK(!ibv_modify_qp(qp_, &attr, mask)) << "QP to RTS";
}
for (int i = 0; i < 2 * FRAMES; ++i)
wc_[i].wr_id = i;
}
static ib_addr mcast_init(ibv_context* context, int port, const ibv_gid* mgid) {
mcast_parameters params;
memset(¶ms, 0, sizeof(struct mcast_parameters));
string ib_devname(ibv_get_device_name(context->device));
params.ib_devname = const_cast<char*>(ib_devname.c_str());
CHECK(!ibv_query_gid(context, port, 0, ¶ms.port_gid));
CHECK(!ibv_query_pkey(context, port, DEF_PKEY_IDX, ¶ms.pkey));
ibv_port_attr port_attr;
CHECK(!ibv_query_port(context, port, &port_attr));
params.sm_lid = port_attr.sm_lid;
params.sm_sl = port_attr.sm_sl;
params.ib_port = port;
if (mgid)
memcpy(¶ms.mgid.raw, &mgid->raw, 16);
CHECK(!join_multicast_group(SUBN_ADM_METHOD_SET, ¶ms))
<< "Failed to create multicast group";
ib_addr addr;
memcpy(&addr.gid.raw, ¶ms.mgid.raw, 16);
addr.lid = params.mlid;
addr.qpn = QPNUM_MCAST;
return addr;
}
ib_addr IBChannel::mcast_create() const {
ib_addr addr = mcast_init(context_, PORT, NULL);
addr.psn = caffe_rng_rand() & 0xffffff;
return addr;
}
void IBChannel::mcast_join(const ib_addr& addr) const {
mcast_init(context_, PORT, &addr.gid);
}
void IBChannel::mcast_attach_qp(const ib_addr& addr) const {
CHECK(!ibv_attach_mcast(qp_, &addr.gid, addr.lid))
<< "Failed to attach to the multicast group";
}
void IBChannel::start(uint8_t* buf_send, size_t buf_size, bool gpu) const {
size_t send_size = buf_send ? buf_size : FRAMES * MTU;
size_t recv_size = FRAMES * (GRH + MTU);
if (gpu) {
if (buf_send) {
buf_send_ = buf_send;
} else {
CUDA_CHECK(cudaMalloc((void** ) &buf_send_, send_size));
}
CUDA_CHECK(cudaMalloc((void** ) &buf_recv_, recv_size));
} else {
buf_send_ = buf_send ? buf_send : (uint8_t*) malloc(send_size);
buf_recv_ = (uint8_t*) malloc(recv_size);
}
LOG(INFO)<< "range: " << hex << (uint64_t) buf_send_ << " " << (uint64_t) send_size;
LOG(INFO)<< "range: " << hex << (uint64_t) buf_recv_ << " " << (uint64_t) recv_size;
mr_send_ = ibv_reg_mr(pd_, buf_send_, send_size, IBV_ACCESS_LOCAL_WRITE);
mr_recv_ = ibv_reg_mr(pd_, buf_recv_, recv_size, IBV_ACCESS_LOCAL_WRITE);
CHECK(mr_send_ && mr_recv_) << "Failed to register memory regions";
// Create initial requests, start the recv ones
for (int i = 0; i < FRAMES; ++i) {
send_queue_[i] = i;
recv_done(i + FRAMES);
}
recv_queue_.clear();
}
IBChannel::~IBChannel() {
CHECK(!ibv_destroy_qp(qp_)) << "Failed to destroy QP";
CHECK(!ibv_destroy_cq(cq_)) << "Failed to destroy CQ";
CHECK(!ibv_dereg_mr(mr_send_)) << "Failed to deregister MR";
CHECK(!ibv_dereg_mr(mr_recv_)) << "Failed to deregister MR";
CHECK(!ibv_dealloc_pd(pd_)) << "Failed to deallocate PD";
CHECK(!ibv_close_device(context_)) << "Failed to release context";
free(buf_send_);
free(buf_recv_);
}
bool IBChannel::can_send() const {
return !send_queue_.empty();
}
int IBChannel::send_init(uint8_t*& buf) const {
int id = send_queue_.front();
send_queue_.pop_front();
buf = buf_send_ + id * MTU;
return id;
}
void IBChannel::send(int id, const ib_addr& addr, uint8_t* buf,
uint32_t imm_data) const {
struct ibv_sge list;
struct ibv_send_wr wr;
struct ibv_send_wr *bad_wr;
list.addr = (uintptr_t) buf;
list.length = MTU;
list.lkey = mr_send_->lkey;
wr.wr_id = id;
wr.next = NULL;
wr.sg_list = &list;
wr.num_sge = 1;
wr.opcode = IBV_WR_SEND_WITH_IMM;
wr.send_flags = IBV_SEND_SIGNALED;
wr.imm_data = imm_data;
wr.wr.ud.ah = addr.ah;
wr.wr.ud.remote_qpn = addr.qpn;
wr.wr.ud.remote_qkey = 0x11111111;
CHECK(!ibv_post_send(qp_, &wr, &bad_wr)) << "Failed send";
}
bool IBChannel::can_recv() const {
return !recv_queue_.empty();
}
int IBChannel::recv(uint8_t*& buf, uint32_t& imm_data) const {
recv_msg& msg = recv_queue_.front();
int id = msg.id_;
buf = buf_recv_ + (id - FRAMES) * (GRH + MTU) + GRH;
imm_data = msg.imm_;
recv_queue_.pop_front();
return id;
}
void IBChannel::recv_done(int id) const {
struct ibv_sge list;
struct ibv_recv_wr wr;
struct ibv_recv_wr* bad_wr;
list.addr = (uintptr_t) (buf_recv_ + (id - FRAMES) * (GRH + MTU));
list.length = GRH + MTU;
list.lkey = mr_recv_->lkey;
wr.wr_id = id;
wr.next = NULL;
wr.sg_list = &list;
wr.num_sge = 1;
CHECK(!ibv_post_recv(qp_, &wr, &bad_wr)) << "Failed receive";
}
void IBChannel::poll() const {
int ne = ibv_poll_cq(cq_, FRAMES * 2, wc_);
CHECK(ne >= 0) << "Poll CQ failed";
for (int i = 0; i < ne; ++i) {
CHECK(wc_[i].status == IBV_WC_SUCCESS) << "Failed status \n"
<< ibv_wc_status_str(wc_[i].status)
<< " " << wc_[i].status << " "
<< (int) wc_[i].wr_id << " "
<< wc_[i].vendor_err;
if (wc_[i].wr_id < IBChannel::FRAMES) {
sent_++;
send_queue_.push_back(wc_[i].wr_id);
} else {
recv_++;
CHECK(wc_[i].byte_len == GRH + MTU);
recv_msg msg;
msg.id_ = wc_[i].wr_id;
msg.imm_ = wc_[i].imm_data;
recv_queue_.push_back(msg);
}
}
}
//
template<typename Dtype>
IBSync<Dtype>::IBSync(const Params<Dtype>& params, int rank,
const IBChannel& ucast, const IBChannel& mcast,
const vector<ib_addr>& ucast_addrs,
const vector<ib_addr>& mcast_addrs)
: DistSync<Dtype>(ucast_addrs.size(), chunks(params.len_used())),
rank_(rank),
ucast_(ucast),
mcast_(mcast),
ucast_addrs_(ucast_addrs),
mcast_addr_(mcast_addrs[rank]) {
for (int i = 0; i < ucast_addrs_.size(); ++i) {
CHECK(ucast_addrs_[i].ah == NULL);
if (i != rank) {
struct ibv_ah_attr ah_attr;
memset(&ah_attr, 0, sizeof ah_attr);
ah_attr.dlid = (uint16_t) ucast_addrs[i].lid;
ah_attr.sl = (uint8_t) 0; // Service level
ah_attr.src_path_bits = 0;
ah_attr.is_global = 0;
ah_attr.port_num = IBChannel::PORT;
ucast_addrs_[i].ah = ibv_create_ah(ucast.pd_, &ah_attr);
CHECK(ucast_addrs_[i].ah) << "Failed to create address handle";
}
}
struct ibv_ah_attr ah_attr;
memset(&ah_attr, 0, sizeof ah_attr);
ah_attr.grh.dgid = mcast_addr_.gid;
ah_attr.dlid = (uint16_t) mcast_addr_.lid;
ah_attr.sl = (uint8_t) 0; // Service level
ah_attr.src_path_bits = 0;
ah_attr.is_global = 1;
ah_attr.port_num = IBChannel::PORT;
mcast_addr_.ah = ibv_create_ah(mcast.pd_, &ah_attr);
CHECK(mcast_addr_.ah) << "Failed to create address handle";
for (int i = 0; i < mcast_addrs.size(); ++i) {
if (i != rank) {
mcast_.mcast_join(mcast_addrs[i]);
mcast_.mcast_attach_qp(mcast_addrs[i]);
}
}
this->dist_init(rank);
}
template<typename Dtype>
IBSync<Dtype>::~IBSync() {
for (int i = 0; i < this->ucast_addrs_.size(); ++i) {
if (i == rank_) {
CHECK(!ibv_destroy_ah(this->ucast_addrs_[i].ah))
<< "Failed to destroy ucast AH";
}
}
CHECK(!ibv_destroy_ah(this->mcast_addr_.ah)) << "Failed to destroy mcast AH";
}
//
template<typename Dtype>
CPUIBSync<Dtype>::CPUIBSync(const Params<Dtype>& params, int rank,
const IBChannel& ucast, const IBChannel& mcast,
const vector<ib_addr>& ucast_addrs,
const vector<ib_addr>& mcast_addrs)
: IBSync<Dtype>(params, rank, ucast, mcast, ucast_addrs, mcast_addrs) {
cpu_ = params.cpu();
CaffeMallocHost((void**) &cpu_last_, params.len_buff() * sizeof(Dtype));
memcpy(cpu_last_, cpu_, params.len_used() * sizeof(Dtype));
}
template<typename Dtype>
CPUIBSync<Dtype>::~CPUIBSync() {
CaffeFreeHost((void*) cpu_last_);
}
template<typename Dtype>
void CPUIBSync<Dtype>::run() {
// TODO
}
//
template<typename Dtype>
GPUIBSync<Dtype>::GPUIBSync(const GPUParams<Dtype>& params, int rank,
const IBChannel& ucast, const IBChannel& mcast,
const vector<ib_addr>& ucast_addrs,
const vector<ib_addr>& mcast_addrs)
: GPUSync<Dtype>(params),
IBSync<Dtype>(params.params(), rank, //
ucast, mcast, //
ucast_addrs, mcast_addrs) {
gpu_ = params.gpu();
int device;
CUDA_CHECK(cudaGetDevice(&device));
CUDA_CHECK(cudaSetDevice(params.device()));
size_t size = params.params().len_buff() * sizeof(Dtype);
CUDA_CHECK(cudaMalloc((void** ) &gpu_last_, size));
CUDA_CHECK(cudaMemcpy(gpu_last_, gpu_, size, cudaMemcpyDeviceToDevice));
CUDA_CHECK(cudaSetDevice(device));
}
template<typename Dtype>
GPUIBSync<Dtype>::~GPUIBSync() {
CUDA_CHECK(cudaFree((void* ) gpu_last_));
}
class Queue {
public:
Queue()
: front_(),
back_(),
size_() {
}
void push() {
CHECK(size_ < IBChannel::FRAMES);
back_ = (back_ + 1) & (IBChannel::FRAMES - 1);
size_++;
}
void pop() {
CHECK(size_ > 0);
front_ = (front_ + 1) & (IBChannel::FRAMES - 1);
size_--;
}
int front_;
int back_;
int size_;
};
class EventQueue : Queue {
public:
EventQueue(const cudaStream_t& stream)
: stream_(stream) {
for (int i = 0; i < IBChannel::FRAMES; ++i)
cudaEventCreateWithFlags(&items_[i].event_, cudaEventDisableTiming);
}
~EventQueue() {
for (int i = 0; i < IBChannel::FRAMES; ++i)
cudaEventDestroy(items_[i].event_);
}
void record(int tag) {
cudaEventRecord(items_[back_].event_, this->stream_);
items_[back_].tag_ = tag;
push();
}
bool query(int& tag) {
if (size_ && cudaEventQuery(items_[front_].event_) == cudaSuccess) {
tag = items_[front_].tag_;
pop();
return true;
}
return false;
}
protected:
const cudaStream_t& stream_;
struct item {
cudaEvent_t event_;
int tag_;
};
item items_[IBChannel::FRAMES];
};
template<typename Dtype>
void GPUIBSync<Dtype>::run() {
CUDA_CHECK(cudaSetDevice(this->params_.device()));
const IBChannel& ucast = this->ucast_;
const IBChannel& mcast = this->mcast_;
ucast.start(NULL, 0, true);
mcast.start((uint8_t*) gpu_, (size_t) this->chunks_ * IBChannel::MTU, true);
GPUStream<Dtype> master_stream;
Queue master_queue;
uint16_t master_ids_[FRAMES];
EventQueue master_events(master_stream.stream());
GPUStream<Dtype> worker_stream;
Queue worker_queue;
struct worker_item {
int recv_id, send_id;
Dtype* grd;
uint32_t chunk;
};
worker_item worker_items[FRAMES];
EventQueue worker_events(worker_stream.stream());
const size_t real_size = FRAMES * sizeof(Dtype*);
const size_t size_size = FRAMES * sizeof(size_t);
const size_t bool_size = FRAMES * sizeof(size_t);
Dtype** master_gpu_grds;
size_t* master_gpu_offs;
CUDA_CHECK(cudaMalloc((void** ) &master_gpu_grds, real_size));
CUDA_CHECK(cudaMalloc((void** ) &master_gpu_offs, size_size));
Dtype** master_cpu_grds;
size_t* master_cpu_offs;
CUDA_CHECK(cudaMallocHost((void** ) &master_cpu_grds, real_size));
CUDA_CHECK(cudaMallocHost((void** ) &master_cpu_offs, size_size));
Dtype** worker_gpu_pos;
size_t* worker_gpu_offs;
Dtype** worker_gpu_grds;
uint8_t* worker_gpu_gets;
CUDA_CHECK(cudaMalloc((void** ) &worker_gpu_pos, real_size));
CUDA_CHECK(cudaMalloc((void** ) &worker_gpu_offs, size_size));
CUDA_CHECK(cudaMalloc((void** ) &worker_gpu_grds, real_size));
CUDA_CHECK(cudaMalloc((void** ) &worker_gpu_gets, bool_size));
Dtype** worker_cpu_pos;
size_t* worker_cpu_offs;
Dtype** worker_cpu_grds;
uint8_t* worker_cpu_gets;
CUDA_CHECK(cudaMallocHost((void** ) &worker_cpu_pos, real_size));
CUDA_CHECK(cudaMallocHost((void** ) &worker_cpu_offs, size_size));
CUDA_CHECK(cudaMallocHost((void** ) &worker_cpu_grds, real_size));
CUDA_CHECK(cudaMallocHost((void** ) &worker_cpu_gets, bool_size));
int master_batch_start = 0;
int master_batch_count = 0;
int worker_batch_start = 0;
int worker_batch_count = 0;
const int batch = 128; // TODO bench
while (!this->must_stop()) {
ucast.poll();
mcast.poll();
// Receive gradients for chunks for which we are master
{
while (ucast.can_recv()) {
uint8_t* buf;
uint32_t chunk;
int id = ucast.recv(buf, chunk);
Dtype* grd = (Dtype*) buf;
CHECK(this->chunk_master(chunk) == this->rank_);
size_t off = ((size_t) chunk) * IBSync<Dtype>::CHUNK;
int index = master_queue.back_;
master_ids_[index] = id;
master_cpu_grds[index] = grd;
master_cpu_offs[index] = off;
master_queue.push();
master_batch_count++;
}
// Add gradients to our weights
if (master_batch_count >= batch) {
CUDA_CHECK(
cudaMemcpyAsync(master_gpu_grds, master_cpu_grds, real_size,
cudaMemcpyHostToDevice, master_stream.stream()));
CUDA_CHECK(
cudaMemcpyAsync(master_gpu_offs, master_cpu_offs, size_size,
cudaMemcpyHostToDevice, master_stream.stream()));
sync_master_kernel<Dtype>(gpu_, master_gpu_grds, master_gpu_offs,
master_batch_start, master_batch_count,
master_stream.stream(), IBSync<Dtype>::CHUNK);
master_events.record(master_batch_count);
master_batch_start = master_queue.back_;
master_batch_count = 0;
}
}
// Start receiving again once kernels are done with buffers
for (;;) {
int batch;
if (!master_events.query(batch)) {
break;
}
for (int i = 0; i < batch; ++i) {
int index = master_queue.front_;
master_queue.pop();
ucast.recv_done(master_ids_[index]);
}
}
// Send absolute positions for chunks for which we are master
while (mcast.can_send()) {
uint8_t* buf;
int id = mcast.send_init(buf); // buf ignored
size_t off = (size_t) this->chunk_ * IBSync<Dtype>::CHUNK;
buf = (uint8_t*) (gpu_ + off);
CHECK(id >= 0 && id < FRAMES);
mcast.send(id, this->mcast_addr_, buf, this->chunk_);
if (++this->chunk_ == this->own_until_) {
this->chunk_ = this->own_start_;
this->cycles_++;
}
}
// Receive absolute positions for other chunks
{
while (mcast.can_recv()) {
Dtype* pos;
uint32_t chunk;
int recv_id, send_id;
size_t off;
{
uint8_t* buf;
recv_id = mcast.recv(buf, chunk);
pos = (Dtype*) buf;
off = ((size_t) chunk) * IBSync<Dtype>::CHUNK;
}
// Send back the gradients if frame is available
Dtype* grd = NULL;
if (ucast.can_send()) {
uint8_t* buf;
send_id = ucast.send_init(buf);
grd = (Dtype*) buf;
}
int index = worker_queue.back_;
worker_items[index].recv_id = recv_id;
worker_items[index].send_id = send_id;
worker_items[index].grd = grd;
worker_items[index].chunk = chunk;
worker_cpu_pos[index] = pos;
worker_cpu_offs[index] = off;
worker_cpu_grds[index] = grd;
worker_cpu_gets[index] = grd != NULL;
worker_queue.push();
worker_batch_count++;
}
if (worker_batch_count >= batch) {
CUDA_CHECK(
cudaMemcpyAsync(worker_gpu_pos, worker_cpu_pos, real_size,
cudaMemcpyHostToDevice, worker_stream.stream()));
CUDA_CHECK(
cudaMemcpyAsync(worker_gpu_offs, worker_cpu_offs, size_size,
cudaMemcpyHostToDevice, worker_stream.stream()));
CUDA_CHECK(
cudaMemcpyAsync(worker_gpu_grds, worker_cpu_grds, real_size,
cudaMemcpyHostToDevice, worker_stream.stream()));
CUDA_CHECK(
cudaMemcpyAsync(worker_gpu_gets, worker_cpu_gets, bool_size,
cudaMemcpyHostToDevice, worker_stream.stream()));
sync_worker_kernel<Dtype>(gpu_, gpu_last_, worker_gpu_pos,
worker_gpu_offs, worker_gpu_grds,
worker_gpu_gets, worker_batch_start,
worker_batch_count, worker_stream.stream(),
IBSync<Dtype>::CHUNK);
worker_events.record(worker_batch_count);
worker_batch_start = worker_queue.back_;
worker_batch_count = 0;
}
}
for (;;) {
int batch;
if (!worker_events.query(batch)) {
break;
}
for (int i = 0; i < batch; ++i) {
int index = worker_queue.front_;
worker_queue.pop();
int recv_id = worker_items[index].recv_id;
int send_id = worker_items[index].send_id;
Dtype* grd = worker_items[index].grd;
uint32_t chunk = worker_items[index].chunk;
mcast.recv_done(recv_id);
if (grd) {
int master = this->chunk_master(chunk);
CHECK(master != this->rank_);
ib_addr& a = this->ucast_addrs_[master];
ucast.send(send_id, a, (uint8_t*) grd, chunk);
}
if (this->remaining_ > 0 && !this->received_[chunk]) {
this->received_[chunk] = true;
this->remaining_--;
}
}
}
}
}
INSTANTIATE_CLASS(IBSync);
INSTANTIATE_CLASS(CPUIBSync);
INSTANTIATE_CLASS(GPUIBSync);
#endif
#ifdef __linux__
// Parse MAC address to byte array
// TODO remove optional ':' chars
static uint8_t* parse_mac(const char* str) {
uint8_t* bytes = (uint8_t*) malloc(ETH_ALEN);
for (int i = 0; i < ETH_ALEN; ++i) {
int value;
sscanf(str + 2 * i, "%02x", &value);
bytes[i] = value;
}
return bytes;
}
static vector<uint8_t*> parse_macs(const vector<string>& macs) {
vector<uint8_t*> res;
for (int i = 0; i < macs.size(); ++i)
res.push_back(parse_mac(macs[i].c_str()));
return res;
}
// Adapter name from MAC address
static string adapter(const uint8_t* mac) {
int s = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
CHECK(s != -1);
// Iterate over adapters
struct ifconf ifc;
char buf[1024];
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
CHECK(ioctl(s, SIOCGIFCONF, &ifc) != -1);
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
// Look for a MAC match