| #ifndef CAFFE_PARALLEL_HPP_ |
| #define CAFFE_PARALLEL_HPP_ |
|
|
| #ifdef USE_NCCL |
|
|
| #include <boost/thread.hpp> |
|
|
| #include <string> |
| #include <vector> |
|
|
| #include "caffe/blob.hpp" |
| #include "caffe/common.hpp" |
| #include "caffe/internal_thread.hpp" |
| #include "caffe/layer.hpp" |
| #include "caffe/proto/caffe.pb.h" |
| #include "caffe/solver.hpp" |
| #include "caffe/syncedmem.hpp" |
| #include "caffe/util/blocking_queue.hpp" |
| #include "caffe/util/nccl.hpp" |
|
|
| namespace caffe { |
|
|
| |
| |
| |
| template<typename Dtype> |
| class Params { |
| public: |
| explicit Params(shared_ptr<Solver<Dtype> > root_solver); |
| virtual ~Params() { |
| } |
|
|
| inline size_t size() const { |
| return size_; |
| } |
| inline Dtype* data() const { |
| return data_; |
| } |
| inline Dtype* diff() const { |
| return diff_; |
| } |
|
|
| protected: |
| const size_t size_; |
| Dtype* data_; |
| Dtype* diff_; |
|
|
| DISABLE_COPY_AND_ASSIGN(Params); |
| }; |
|
|
| |
| template<typename Dtype> |
| class GPUParams : public Params<Dtype> { |
| public: |
| GPUParams(shared_ptr<Solver<Dtype> > root_solver, int device); |
| virtual ~GPUParams(); |
|
|
| void Configure(Solver<Dtype>* solver) const; |
|
|
| protected: |
| using Params<Dtype>::size_; |
| using Params<Dtype>::data_; |
| using Params<Dtype>::diff_; |
| }; |
|
|
| template<typename Dtype> |
| class NCCL : public GPUParams<Dtype>, |
| public Solver<Dtype>::Callback, |
| public Net<Dtype>::Callback { |
| public: |
| |
| |
| |
| explicit NCCL(shared_ptr<Solver<Dtype> > solver); |
| |
| |
| |
| |
| NCCL(shared_ptr<Solver<Dtype> > solver, const string& uid); |
| ~NCCL(); |
|
|
| boost::barrier* barrier(); |
| void set_barrier(boost::barrier* value); |
|
|
| |
| |
| |
| |
| static void InitSingleProcess(vector<NCCL<Dtype>*>* nccls); |
|
|
| static string new_uid(); |
|
|
| |
| |
| |
| void Broadcast(); |
|
|
| |
| |
| |
| void Run(const vector<int>& gpus, const char* restore); |
|
|
| protected: |
| void Init(); |
| void on_start() {} |
| void run(int layer); |
| void on_gradients_ready(); |
|
|
| ncclComm_t comm_; |
| cudaStream_t stream_; |
|
|
| shared_ptr<Solver<Dtype> > solver_; |
| |
| boost::barrier* barrier_; |
| using Params<Dtype>::size_; |
| using Params<Dtype>::data_; |
| using Params<Dtype>::diff_; |
| }; |
|
|
| } |
|
|
| #endif |
| #endif |
|
|