From afb96c8b86b2324c25274623aa3cf1c7c03f4e92 Mon Sep 17 00:00:00 2001 From: Yuchen Zhong Date: Sat, 16 May 2020 20:37:50 +0800 Subject: [PATCH] rename: more readable (#13) --- byteps/common/compressor/strategy/onebit.cc | 59 ++++++++++---------- byteps/common/compressor/strategy/onebit.h | 13 ++++- byteps/common/compressor/strategy/randomk.cc | 20 +++---- byteps/common/compressor/strategy/randomk.h | 4 +- byteps/common/compressor/strategy/topk.cc | 20 +++---- byteps/common/compressor/strategy/topk.h | 4 +- byteps/common/global.cc | 2 +- 7 files changed, 68 insertions(+), 54 deletions(-) diff --git a/byteps/common/compressor/strategy/onebit.cc b/byteps/common/compressor/strategy/onebit.cc index 7fe6f01c4..10bae0da5 100644 --- a/byteps/common/compressor/strategy/onebit.cc +++ b/byteps/common/compressor/strategy/onebit.cc @@ -40,9 +40,9 @@ OnebitCompressor::OnebitCompressor(bool use_scale) : _use_scale(use_scale){}; OnebitCompressor::~OnebitCompressor() = default; -template -static size_t _Packing(T* data, size_t len) { - constexpr int PACKING_SIZE = sizeof(T) * 8; +template +size_t OnebitCompressor::PackingImpl(scalar_t* data, size_t len) { + constexpr int PACKING_SIZE = sizeof(scalar_t) * 8; size_t padding_len = (PACKING_SIZE - (len % PACKING_SIZE)) % PACKING_SIZE; size_t chunk_size = (len + padding_len) / PACKING_SIZE; @@ -53,22 +53,22 @@ static size_t _Packing(T* data, size_t len) { } } - return chunk_size * sizeof(T); + return chunk_size * sizeof(scalar_t); } -static size_t Packing(void* data, size_t len, int dtype) { +size_t OnebitCompressor::Packing(void* data, size_t len, int dtype) { switch (dtype) { case BYTEPS_INT8: case BYTEPS_UINT8: - return _Packing(reinterpret_cast(data), len); + return PackingImpl(reinterpret_cast(data), len); case BYTEPS_FLOAT16: - return _Packing(reinterpret_cast(data), len); + return PackingImpl(reinterpret_cast(data), len); case BYTEPS_INT32: case BYTEPS_FLOAT32: - return _Packing(reinterpret_cast(data), len); + return PackingImpl(reinterpret_cast(data), len); case BYTEPS_INT64: case BYTEPS_FLOAT64: - return _Packing(reinterpret_cast(data), len); + return PackingImpl(reinterpret_cast(data), len); default: BPS_CHECK(0) << "Unsupported data type: " << dtype; } @@ -95,11 +95,13 @@ void OnebitCompressor::Compress(ByteBuf grad, int dtype, ByteBuf& compressed) { compressed.size = compressed_size + sizeof(float); } -template -static size_t _Unpacking(T1* dst, const T2* src, size_t size) { - static_assert(sizeof(T1) == sizeof(T2), "T1 should be the same size as T2"); - constexpr int PACKING_SIZE = sizeof(T2) * 8; - auto chunk_size = (size - sizeof(float)) / sizeof(T2); +template +size_t OnebitCompressor::UnpackingImpl(scalar_t* dst, const packing_t* src, + size_t size) { + static_assert(sizeof(scalar_t) == sizeof(packing_t), + "scalar_t should be the same size as packing_t"); + constexpr int PACKING_SIZE = sizeof(packing_t) * 8; + auto chunk_size = (size - sizeof(float)) / sizeof(packing_t); float scale; auto pf = reinterpret_cast(src + chunk_size); @@ -118,30 +120,31 @@ static size_t _Unpacking(T1* dst, const T2* src, size_t size) { return chunk_size; } -static size_t Unpacking(void* dst, const void* src, size_t len, int dtype) { +size_t OnebitCompressor::Unpacking(void* dst, const void* src, size_t len, + int dtype) { switch (dtype) { case BYTEPS_INT8: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); case BYTEPS_UINT8: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); // TODO: // case BYTEPS_FLOAT16: - // return _Unpacking(reinterpret_cast(dst), + // return UnpackingImpl(reinterpret_cast(dst), // reinterpret_cast(src), len); case BYTEPS_INT32: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); case BYTEPS_FLOAT32: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); case BYTEPS_INT64: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); case BYTEPS_FLOAT64: - return _Unpacking(reinterpret_cast(dst), - reinterpret_cast(src), len); + return UnpackingImpl(reinterpret_cast(dst), + reinterpret_cast(src), len); default: BPS_CHECK(0) << "Unsupported data type: " << dtype; } diff --git a/byteps/common/compressor/strategy/onebit.h b/byteps/common/compressor/strategy/onebit.h index d917dd456..500c53aa8 100644 --- a/byteps/common/compressor/strategy/onebit.h +++ b/byteps/common/compressor/strategy/onebit.h @@ -38,7 +38,7 @@ namespace compressor { */ class OnebitCompressor : public BaseCompressor { public: - OnebitCompressor(bool use_scale=false); + OnebitCompressor(bool use_scale = false); virtual ~OnebitCompressor(); /*! @@ -65,6 +65,17 @@ class OnebitCompressor : public BaseCompressor { void Decompress(ByteBuf compressed, int dtype, ByteBuf& decompressed) override; + private: + size_t Packing(void* data, size_t len, int dtype); + + template + size_t PackingImpl(scalar_t* data, size_t len); + + size_t Unpacking(void* dst, const void* src, size_t len, int dtype); + + template + size_t UnpackingImpl(scalar_t* dst, const packing_t* src, size_t size); + private: bool _use_scale; }; diff --git a/byteps/common/compressor/strategy/randomk.cc b/byteps/common/compressor/strategy/randomk.cc index 63827fbdd..8099701af 100644 --- a/byteps/common/compressor/strategy/randomk.cc +++ b/byteps/common/compressor/strategy/randomk.cc @@ -41,7 +41,7 @@ RandomkCompressor::RandomkCompressor(int k) : _k(k) { _gen.seed(_rd()); }; RandomkCompressor::~RandomkCompressor() = default; template -size_t RandomkCompressor::_Packing(index_t* dst, const scalar_t* src, +size_t RandomkCompressor::PackingImpl(index_t* dst, const scalar_t* src, size_t len) { static_assert(sizeof(index_t) == sizeof(scalar_t), "index_t should be the same size as scalar_t"); @@ -61,22 +61,22 @@ size_t RandomkCompressor::_Packing(index_t* dst, const scalar_t* src, size_t RandomkCompressor::Packing(const void* src, size_t size, int dtype) { switch (dtype) { case BYTEPS_INT8: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int8_t)); case BYTEPS_UINT8: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(uint8_t)); // case BYTEPS_FLOAT16: // return _Packing(reinterpret_cast(_buf.get()), // reinterpret_cast(src), size); case BYTEPS_FLOAT32: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int32_t)); case BYTEPS_FLOAT64: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int64_t)); default: @@ -91,7 +91,7 @@ void RandomkCompressor::Compress(ByteBuf grad, int dtype, ByteBuf& compressed) { } template -size_t RandomkCompressor::_Unpacking(scalar_t* dst, const index_t* src, +size_t RandomkCompressor::UnpackingImpl(scalar_t* dst, const index_t* src, size_t len) { static_assert(sizeof(index_t) == sizeof(scalar_t), "index_t should be the same size as scalar_t"); @@ -116,22 +116,22 @@ size_t RandomkCompressor::Unpacking(void* dst, const void* src, size_t size, int dtype) { switch (dtype) { case BYTEPS_INT8: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(int8_t) / 2); case BYTEPS_UINT8: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(uint8_t) / 2); // case BYTEPS_FLOAT16: // return _Unpacking(reinterpret_cast(_buf.get()), // reinterpret_cast(src), size); case BYTEPS_FLOAT32: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(float) / 2); case BYTEPS_FLOAT64: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(double) / 2); default: diff --git a/byteps/common/compressor/strategy/randomk.h b/byteps/common/compressor/strategy/randomk.h index 1e464663a..764f2f602 100644 --- a/byteps/common/compressor/strategy/randomk.h +++ b/byteps/common/compressor/strategy/randomk.h @@ -64,12 +64,12 @@ class RandomkCompressor : public BaseCompressor { size_t Packing(const void* src, size_t size, int dtype); template - size_t _Packing(index_t* dst, const scalar_t* src, size_t len); + size_t PackingImpl(index_t* dst, const scalar_t* src, size_t len); size_t Unpacking(void* dst, const void* src, size_t size, int dtype); template - size_t _Unpacking(scalar_t* dst, const index_t* src, size_t len); + size_t UnpackingImpl(scalar_t* dst, const index_t* src, size_t len); private: int _k; diff --git a/byteps/common/compressor/strategy/topk.cc b/byteps/common/compressor/strategy/topk.cc index a4c1264fe..949642406 100644 --- a/byteps/common/compressor/strategy/topk.cc +++ b/byteps/common/compressor/strategy/topk.cc @@ -43,7 +43,7 @@ TopkCompressor::TopkCompressor(int k) : _k(k){}; TopkCompressor::~TopkCompressor() = default; template -size_t TopkCompressor::_Packing(index_t* dst, const scalar_t* src, size_t len) { +size_t TopkCompressor::PackingImpl(index_t* dst, const scalar_t* src, size_t len) { static_assert(sizeof(index_t) == sizeof(scalar_t), "index_t should be the same size as scalar_t"); BPS_CHECK_LE(this->_k, len / 2); @@ -78,22 +78,22 @@ size_t TopkCompressor::_Packing(index_t* dst, const scalar_t* src, size_t len) { size_t TopkCompressor::Packing(const void* src, size_t size, int dtype) { switch (dtype) { case BYTEPS_INT8: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int8_t)); case BYTEPS_UINT8: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(uint8_t)); // case BYTEPS_FLOAT16: // return _Packing(reinterpret_cast(_buf.get()), // reinterpret_cast(src), size); case BYTEPS_FLOAT32: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int32_t)); case BYTEPS_FLOAT64: - return _Packing(reinterpret_cast(_buf.get()), + return PackingImpl(reinterpret_cast(_buf.get()), reinterpret_cast(src), size / sizeof(int64_t)); default: @@ -108,7 +108,7 @@ void TopkCompressor::Compress(ByteBuf grad, int dtype, ByteBuf& compressed) { } template -size_t TopkCompressor::_Unpacking(scalar_t* dst, const index_t* src, +size_t TopkCompressor::UnpackingImpl(scalar_t* dst, const index_t* src, size_t len) { static_assert(sizeof(index_t) == sizeof(scalar_t), "index_t should be the same size as scalar_t"); @@ -133,22 +133,22 @@ size_t TopkCompressor::Unpacking(void* dst, const void* src, size_t size, int dtype) { switch (dtype) { case BYTEPS_INT8: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(int8_t) / 2); case BYTEPS_UINT8: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(uint8_t) / 2); // case BYTEPS_FLOAT16: // return _Unpacking(reinterpret_cast(_buf.get()), // reinterpret_cast(src), size); case BYTEPS_FLOAT32: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(float) / 2); case BYTEPS_FLOAT64: - return _Unpacking(reinterpret_cast(dst), + return UnpackingImpl(reinterpret_cast(dst), reinterpret_cast(src), size / sizeof(double) / 2); default: diff --git a/byteps/common/compressor/strategy/topk.h b/byteps/common/compressor/strategy/topk.h index 3c6d06956..0f5721d35 100644 --- a/byteps/common/compressor/strategy/topk.h +++ b/byteps/common/compressor/strategy/topk.h @@ -65,12 +65,12 @@ class TopkCompressor : public BaseCompressor { size_t Packing(const void* src, size_t size, int dtype); template - size_t _Packing(index_t* dst, const scalar_t* src, size_t len); + size_t PackingImpl(index_t* dst, const scalar_t* src, size_t len); size_t Unpacking(void* dst, const void* src, size_t size, int dtype); template - size_t _Unpacking(scalar_t* dst, const index_t* src, size_t len); + size_t UnpackingImpl(scalar_t* dst, const index_t* src, size_t len); private: int _k; diff --git a/byteps/common/global.cc b/byteps/common/global.cc index 7c2939f88..f6e9076d5 100644 --- a/byteps/common/global.cc +++ b/byteps/common/global.cc @@ -216,8 +216,8 @@ void BytePSGlobal::Init() { size_t pool_size = 4; if (getenv("BYTEPS_THREADPOOL_SIZE")) { pool_size = atoi(getenv("BYTEPS_THREADPOOL_SIZE")); + _thread_pool.reset(new ThreadPool(pool_size)); } - _thread_pool.reset(new ThreadPool(pool_size)); } // ReadyTable for cross-PCIe-switch reduce