Skip to content

Feature/device threadpool #11407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion paddle/fluid/platform/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ENDIF()
# memcpy depends on device_context, here add deps individually for
# avoiding cycle dependencies
cc_library(device_context SRCS device_context.cc DEPS malloc
place eigen3 ${GPU_CTX_DEPS} ${MKLDNN_CTX_DEPS})
place eigen3 threadpool ${GPU_CTX_DEPS} ${MKLDNN_CTX_DEPS})
nv_test(device_context_test SRCS device_context_test.cu DEPS device_context gpu_info)

nv_test(cudnn_helper_test SRCS cudnn_helper_test.cc DEPS dynload_cuda)
Expand Down
20 changes: 14 additions & 6 deletions paddle/fluid/platform/device_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,18 @@ DeviceContextPool::DeviceContextPool(
}

CPUDeviceContext::CPUDeviceContext() {
eigen_device_.reset(new Eigen::DefaultDevice());
eigen_threadpool_.reset(new EigenThreadPoolDevice);
eigen_device_.reset(new Eigen::ThreadPoolDevice(
eigen_threadpool_.get(), eigen_threadpool_->NumThreads()));
}

CPUDeviceContext::CPUDeviceContext(CPUPlace place) : place_(place) {
eigen_device_.reset(new Eigen::DefaultDevice());
eigen_threadpool_.reset(new EigenThreadPoolDevice);
eigen_device_.reset(new Eigen::ThreadPoolDevice(
eigen_threadpool_.get(), eigen_threadpool_->NumThreads()));
}

Eigen::DefaultDevice* CPUDeviceContext::eigen_device() const {
Eigen::ThreadPoolDevice* CPUDeviceContext::eigen_device() const {
return eigen_device_.get();
}

Expand Down Expand Up @@ -200,15 +204,19 @@ cudnnHandle_t CUDADeviceContext::cudnn_handle() const { return cudnn_handle_; }
cudaStream_t CUDADeviceContext::stream() const { return stream_; }

CUDAPinnedDeviceContext::CUDAPinnedDeviceContext() {
eigen_device_.reset(new Eigen::DefaultDevice());
eigen_threadpool_.reset(new EigenThreadPoolDevice);
eigen_device_.reset(new Eigen::ThreadPoolDevice(
eigen_threadpool_.get(), eigen_threadpool_->NumThreads()));
}

CUDAPinnedDeviceContext::CUDAPinnedDeviceContext(CUDAPinnedPlace place)
: place_(place) {
eigen_device_.reset(new Eigen::DefaultDevice());
eigen_threadpool_.reset(new EigenThreadPoolDevice);
eigen_device_.reset(new Eigen::ThreadPoolDevice(
eigen_threadpool_.get(), eigen_threadpool_->NumThreads()));
}

Eigen::DefaultDevice* CUDAPinnedDeviceContext::eigen_device() const {
Eigen::ThreadPoolDevice* CUDAPinnedDeviceContext::eigen_device() const {
return eigen_device_.get();
}

Expand Down
33 changes: 29 additions & 4 deletions paddle/fluid/platform/device_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ limitations under the License. */
#include <mkldnn.hpp>
#endif

#define EIGEN_USE_THREADS

#include "paddle/fluid/framework/threadpool.h"
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
#include "unsupported/Eigen/CXX11/Tensor"
#include "unsupported/Eigen/CXX11/ThreadPool"

#include "glog/logging.h"

Expand All @@ -44,18 +48,37 @@ class DeviceContext {
virtual void Wait() const {}
};

using framework::ThreadPool;
class EigenThreadPoolDevice : public Eigen::ThreadPoolInterface {
public:
EigenThreadPoolDevice() { pool_ = ThreadPool::GetInstance(); }
~EigenThreadPoolDevice() override {}
void Schedule(std::function<void()> fn) override { pool_->Run(fn); }

void Cancel() override {}

int NumThreads() const override { return pool_->Threads(); }

int CurrentThreadId() const override { return 0; };

private:
ThreadPool* pool_; // singleton now owned
};

class CPUDeviceContext : public DeviceContext {
public:
CPUDeviceContext();
explicit CPUDeviceContext(CPUPlace place);

Eigen::DefaultDevice* eigen_device() const;
Eigen::ThreadPoolDevice* eigen_device() const;

Place GetPlace() const override;

private:
CPUPlace place_;
std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
std::unique_ptr<Eigen::ThreadPoolDevice> eigen_device_; // eigen device
std::unique_ptr<EigenThreadPoolDevice>
eigen_threadpool_; // wrapper on paddle threadpool
};

template <typename Place>
Expand Down Expand Up @@ -135,11 +158,13 @@ class CUDAPinnedDeviceContext : public DeviceContext {

Place GetPlace() const override;

Eigen::DefaultDevice* eigen_device() const;
Eigen::ThreadPoolDevice* eigen_device() const;

private:
CUDAPinnedPlace place_;
std::unique_ptr<Eigen::DefaultDevice> eigen_device_;
std::unique_ptr<Eigen::ThreadPoolDevice> eigen_device_; // eigen device
std::unique_ptr<EigenThreadPoolDevice>
eigen_threadpool_; // wrapper on paddle threadpool
};

template <>
Expand Down