Skip to content
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

remove unused place #6972

Merged
merged 2 commits into from
Dec 25, 2017
Merged
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
8 changes: 0 additions & 8 deletions paddle/operators/math/math_function.cc
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,6 @@ void set_constant_with_place<platform::CPUPlace>(
TensorSetConstantCPU(tensor, value));
}

template <>
void set_constant_with_place<platform::MKLDNNPlace>(
const platform::DeviceContext& context, framework::Tensor* tensor,
float value) {
framework::VisitDataType(framework::ToDataType(tensor->type()),
TensorSetConstantCPU(tensor, value));
}

struct TensorSetConstantWithPlace : public boost::static_visitor<void> {
TensorSetConstantWithPlace(const platform::DeviceContext& context,
framework::Tensor* tensor, float value)
Expand Down
7 changes: 0 additions & 7 deletions paddle/operators/math/math_function.cu
Original file line number Diff line number Diff line change
Expand Up @@ -273,13 +273,6 @@ void set_constant_with_place<platform::CUDAPlace>(
TensorSetConstantGPU(context, tensor, value));
}

template <>
void set_constant_with_place<platform::CUDNNPlace>(
const platform::DeviceContext& context, framework::Tensor* tensor,
float value) {
set_constant_with_place<platform::CUDAPlace>(context, tensor, value);
}

template struct RowwiseAdd<platform::CUDADeviceContext, float>;
template struct RowwiseAdd<platform::CUDADeviceContext, double>;
template struct ColwiseSum<platform::CUDADeviceContext, float>;
Expand Down
8 changes: 3 additions & 5 deletions paddle/platform/device_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,18 @@ cudnnHandle_t CUDADeviceContext::cudnn_handle() const { return cudnn_handle_; }

cudaStream_t CUDADeviceContext::stream() const { return stream_; }

CUDNNDeviceContext::CUDNNDeviceContext(CUDNNPlace place)
: CUDADeviceContext(place), place_(place) {
CUDNNDeviceContext::CUDNNDeviceContext(CUDAPlace place)
: CUDADeviceContext(place) {
PADDLE_ENFORCE(dynload::cudnnCreate(&cudnn_handle_));
PADDLE_ENFORCE(dynload::cudnnSetStream(cudnn_handle_, stream()));
}

CUDNNDeviceContext::~CUDNNDeviceContext() {
SetDeviceId(place_.device);
SetDeviceId(boost::get<CUDAPlace>(GetPlace()).device);
Wait();
PADDLE_ENFORCE(dynload::cudnnDestroy(cudnn_handle_));
}

Place CUDNNDeviceContext::GetPlace() const { return CUDNNPlace(); }

cudnnHandle_t CUDNNDeviceContext::cudnn_handle() const { return cudnn_handle_; }

#endif
Expand Down
6 changes: 1 addition & 5 deletions paddle/platform/device_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,14 @@ class CUDADeviceContext : public DeviceContext {

class CUDNNDeviceContext : public CUDADeviceContext {
public:
explicit CUDNNDeviceContext(CUDNNPlace place);
explicit CUDNNDeviceContext(CUDAPlace place);
virtual ~CUDNNDeviceContext();

/*! \brief Return place in the device context. */
Place GetPlace() const final;

/*! \brief Return cudnn handle in the device context. */
cudnnHandle_t cudnn_handle() const;

private:
cudnnHandle_t cudnn_handle_;
CUDNNPlace place_;
};

#endif
Expand Down
5 changes: 2 additions & 3 deletions paddle/platform/device_context_test.cu
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,11 @@ TEST(Device, CUDADeviceContext) {

TEST(Device, CUDNNDeviceContext) {
using paddle::platform::CUDNNDeviceContext;
using paddle::platform::CUDNNPlace;
using paddle::platform::CUDAPlace;
if (paddle::platform::dynload::HasCUDNN()) {
int count = paddle::platform::GetCUDADeviceCount();
for (int i = 0; i < count; ++i) {
CUDNNDeviceContext* device_context =
new CUDNNDeviceContext(CUDNNPlace(i));
CUDNNDeviceContext* device_context = new CUDNNDeviceContext(CUDAPlace(i));
cudnnHandle_t cudnn_handle = device_context->cudnn_handle();
ASSERT_NE(nullptr, cudnn_handle);
ASSERT_NE(nullptr, device_context->stream());
Expand Down
9 changes: 1 addition & 8 deletions paddle/platform/place.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ class PlacePrinter : public boost::static_visitor<> {
public:
explicit PlacePrinter(std::ostream &os) : os_(os) {}
void operator()(const CPUPlace &) { os_ << "CPUPlace"; }
void operator()(const MKLDNNPlace &) { os_ << "MKLDNNPlace"; }
void operator()(const CUDAPlace &p) {
os_ << "CUDAPlace(" << p.device << ")";
}
Expand All @@ -41,18 +40,12 @@ const Place &get_place() { return the_default_place; }

const CUDAPlace default_gpu() { return CUDAPlace(0); }
const CPUPlace default_cpu() { return CPUPlace(); }
const MKLDNNPlace default_mkldnn() { return MKLDNNPlace(); }

bool is_gpu_place(const Place &p) {
return boost::apply_visitor(IsCUDAPlace(), p);
}
bool is_cpu_place(const Place &p) {
return !is_gpu_place(p) && !is_mkldnn_place(p);
}

bool is_mkldnn_place(const Place &p) {
return boost::apply_visitor(IsMKLDNNPlace(), p);
}
bool is_cpu_place(const Place &p) { return !is_gpu_place(p); }

bool places_are_same_class(const Place &p1, const Place &p2) {
return p1.which() == p2.which();
Expand Down
26 changes: 1 addition & 25 deletions paddle/platform/place.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ struct CPUPlace {
inline bool operator!=(const CPUPlace &) const { return false; }
};

struct MKLDNNPlace {
MKLDNNPlace() {}

// needed for variant equality comparison
inline bool operator==(const MKLDNNPlace &) const { return true; }
inline bool operator!=(const MKLDNNPlace &) const { return false; }
};

struct CUDAPlace {
CUDAPlace() : CUDAPlace(0) {}
explicit CUDAPlace(int d) : device(d) {}
Expand All @@ -53,37 +45,21 @@ struct CUDAPlace {
int device;
};

struct CUDNNPlace : public CUDAPlace {
CUDNNPlace() : CUDAPlace() {}
explicit CUDNNPlace(int d) : CUDAPlace(d) {}
};

struct IsCUDAPlace : public boost::static_visitor<bool> {
bool operator()(const CPUPlace &) const { return false; }
bool operator()(const MKLDNNPlace &) const { return false; }
bool operator()(const CUDAPlace &gpu) const { return true; }
bool operator()(const CUDNNPlace &) const { return true; }
};

struct IsMKLDNNPlace : public boost::static_visitor<bool> {
bool operator()(const MKLDNNPlace &) const { return true; }
bool operator()(const CPUPlace &) const { return false; }
bool operator()(const CUDAPlace &) const { return false; }
bool operator()(const CUDNNPlace &) const { return false; }
};

typedef boost::variant<CUDNNPlace, CUDAPlace, CPUPlace, MKLDNNPlace> Place;
typedef boost::variant<CUDAPlace, CPUPlace> Place;

void set_place(const Place &);
const Place &get_place();

const CUDAPlace default_gpu();
const CPUPlace default_cpu();
const MKLDNNPlace default_mkldnn();

bool is_gpu_place(const Place &);
bool is_cpu_place(const Place &);
bool is_mkldnn_place(const Place &);
bool places_are_same_class(const Place &, const Place &);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rename with is_same_place

Copy link
Member Author

@QiJune QiJune Dec 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CUDAPlace(0) and CUDAPlace(1) are the same place class, but they are not the same place.


std::ostream &operator<<(std::ostream &, const Place &);
Expand Down
13 changes: 1 addition & 12 deletions paddle/platform/place_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,26 @@
TEST(Place, Equality) {
paddle::platform::CPUPlace cpu;
paddle::platform::CUDAPlace g0(0), g1(1), gg0(0);
paddle::platform::CUDNNPlace d0(0), d1(1), dd0(0);

EXPECT_EQ(cpu, cpu);
EXPECT_EQ(g0, g0);
EXPECT_EQ(g1, g1);
EXPECT_EQ(g0, gg0);
EXPECT_EQ(d0, dd0);

EXPECT_NE(g0, g1);
EXPECT_NE(d0, d1);

EXPECT_TRUE(paddle::platform::places_are_same_class(g0, gg0));
EXPECT_FALSE(paddle::platform::places_are_same_class(g0, cpu));

EXPECT_TRUE(paddle::platform::is_gpu_place(d0));
EXPECT_FALSE(paddle::platform::places_are_same_class(g0, d0));
}

TEST(Place, Default) {
EXPECT_TRUE(paddle::platform::is_gpu_place(paddle::platform::get_place()));
EXPECT_TRUE(paddle::platform::is_gpu_place(paddle::platform::default_gpu()));
EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::default_cpu()));
EXPECT_TRUE(
paddle::platform::is_mkldnn_place(paddle::platform::default_mkldnn()));

EXPECT_FALSE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
paddle::platform::set_place(paddle::platform::CPUPlace());
EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::get_place()));

paddle::platform::set_place(paddle::platform::MKLDNNPlace());
EXPECT_FALSE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
EXPECT_TRUE(paddle::platform::is_mkldnn_place(paddle::platform::get_place()));
}

TEST(Place, Print) {
Expand Down