提交 9b5fb276 编写于 作者: Y Yi Wang

Make place_test buildable and passed.

上级 bb882028
...@@ -16,10 +16,7 @@ ...@@ -16,10 +16,7 @@
--- ---
Language: Cpp Language: Cpp
BasedOnStyle: Google BasedOnStyle: Google
IndentWidth: 2 UseTab Never
TabWidth: 2
ContinuationIndentWidth: 4
AccessModifierOffset: -2 # The private/protected/public has no indent in class
Standard: Cpp11 Standard: Cpp11
AllowAllParametersOfDeclarationOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true
BinPackParameters: false BinPackParameters: false
......
...@@ -17,11 +17,13 @@ add_subdirectory(strings) ...@@ -17,11 +17,13 @@ add_subdirectory(strings)
find_package(Boost QUIET) find_package(Boost QUIET)
if(Boost_FOUND) add_subdirectory(platform)
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}) # if(Boost_FOUND)
add_subdirectory(majel) # include_directories(${Boost_INCLUDE_DIRS})
endif() # include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# add_subdirectory(majel)
# endif()
if(WITH_C_API) if(WITH_C_API)
add_subdirectory(capi) add_subdirectory(capi)
......
#include "paddle/majel/place.h" #include "paddle/platform/place.h"
namespace majel { namespace paddle {
namespace platform {
namespace detail { namespace detail {
class PlacePrinter : public boost::static_visitor<> { class PlacePrinter : public boost::static_visitor<> {
private:
std::ostream& os_;
public: public:
PlacePrinter(std::ostream& os) : os_(os) {} PlacePrinter(std::ostream &os) : os_(os) {}
void operator()(const CpuPlace &) { os_ << "CpuPlace"; }
void operator()(const GpuPlace &p) { os_ << "GpuPlace(" << p.device << ")"; }
void operator()(const CpuPlace&) { os_ << "CpuPlace"; } private:
std::ostream &os_;
void operator()(const GpuPlace& p) { os_ << "GpuPlace(" << p.device << ")"; }
}; };
} // namespace detail } // namespace detail
static Place the_default_place; static Place the_default_place;
void set_place(const Place& place) { the_default_place = place; } void set_place(const Place &place) { the_default_place = place; }
const Place &get_place() { return the_default_place; }
const Place& get_place() { return the_default_place; }
const GpuPlace default_gpu() { return GpuPlace(0); } const GpuPlace default_gpu() { return GpuPlace(0); }
const CpuPlace default_cpu() { return CpuPlace(); } const CpuPlace default_cpu() { return CpuPlace(); }
bool is_gpu_place(const Place& p) { bool is_gpu_place(const Place &p) {
return boost::apply_visitor(IsGpuPlace(), p); return boost::apply_visitor(IsGpuPlace(), p);
} }
bool is_cpu_place(const Place &p) {
bool is_cpu_place(const Place& p) {
return !boost::apply_visitor(IsGpuPlace(), p); return !boost::apply_visitor(IsGpuPlace(), p);
} }
bool places_are_same_class(const Place& p1, const Place& p2) { bool places_are_same_class(const Place &p1, const Place &p2) {
return is_gpu_place(p1) == is_gpu_place(p2); return is_gpu_place(p1) == is_gpu_place(p2);
} }
std::ostream& operator<<(std::ostream& os, const majel::Place& p) { std::ostream &operator<<(std::ostream &os, const Place &p) {
majel::detail::PlacePrinter printer(os); detail::PlacePrinter printer(os);
boost::apply_visitor(printer, p); boost::apply_visitor(printer, p);
return os; return os;
} }
} // namespace majel } // namespace platform
} // namespace paddle
...@@ -2,49 +2,48 @@ ...@@ -2,49 +2,48 @@
#include <boost/variant.hpp> #include <boost/variant.hpp>
#include <iostream> #include <iostream>
namespace majel { namespace paddle {
namespace platform {
struct CpuPlace { struct CpuPlace {
CpuPlace() {} // WORKAROUND: for some reason, omitting this constructor // WORKAROUND: for some reason, omitting this constructor
// causes errors with boost 1.59 and OSX // causes errors with boost 1.59 and OSX
// needed for variant equality comparison CpuPlace() {}
inline bool operator==(const CpuPlace&) const { return true; }
inline bool operator!=(const CpuPlace&) const { return false; } // needed for variant equality comparison
inline bool operator==(const CpuPlace &) const { return true; }
inline bool operator!=(const CpuPlace &) const { return false; }
}; };
struct GpuPlace { struct GpuPlace {
GpuPlace() : GpuPlace(0) {}
GpuPlace(int d) : device(d) {} GpuPlace(int d) : device(d) {}
// needed for variant equality comparison // needed for variant equality comparison
inline bool operator==(const GpuPlace& o) const { return device == o.device; } inline bool operator==(const GpuPlace &o) const { return device == o.device; }
inline bool operator!=(const GpuPlace &o) const { return !(*this == o); }
inline bool operator!=(const GpuPlace& o) const { return !(*this == o); }
GpuPlace() : GpuPlace(0) {}
int device; int device;
}; };
class IsGpuPlace : public boost::static_visitor<bool> { struct IsGpuPlace : public boost::static_visitor<bool> {
public: bool operator()(const CpuPlace &) const { return false; }
bool operator()(const CpuPlace&) const { return false; } bool operator()(const GpuPlace &gpu) const { return true; }
bool operator()(const GpuPlace& gpu) const { return true; }
}; };
typedef boost::variant<GpuPlace, CpuPlace> Place; typedef boost::variant<GpuPlace, CpuPlace> Place;
void set_place(const Place&); void set_place(const Place &);
const Place &get_place();
const Place& get_place();
const GpuPlace default_gpu(); const GpuPlace default_gpu();
const CpuPlace default_cpu(); const CpuPlace default_cpu();
bool is_gpu_place(const Place&); bool is_gpu_place(const Place &);
bool is_cpu_place(const Place&); bool is_cpu_place(const Place &);
bool places_are_same_class(const Place&, const Place&); bool places_are_same_class(const Place &, const Place &);
std::ostream& operator<<(std::ostream&, const majel::Place&); std::ostream &operator<<(std::ostream &, const Place &);
} // namespace majel } // namespace platform
} // namespace paddle
#include "paddle/majel/place.h" #include "paddle/platform/place.h"
#include <sstream>
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <sstream>
TEST(Place, Equality) { TEST(Place, Equality) {
majel::CpuPlace cpu; paddle::platform::CpuPlace cpu;
majel::GpuPlace g0(0), g1(1), gg0(0); paddle::platform::GpuPlace g0(0), g1(1), gg0(0);
EXPECT_EQ(cpu, cpu); EXPECT_EQ(cpu, cpu);
EXPECT_EQ(g0, g0); EXPECT_EQ(g0, g0);
...@@ -13,28 +13,28 @@ TEST(Place, Equality) { ...@@ -13,28 +13,28 @@ TEST(Place, Equality) {
EXPECT_NE(g0, g1); EXPECT_NE(g0, g1);
EXPECT_TRUE(majel::places_are_same_class(g0, gg0)); EXPECT_TRUE(paddle::platform::places_are_same_class(g0, gg0));
EXPECT_FALSE(majel::places_are_same_class(g0, cpu)); EXPECT_FALSE(paddle::platform::places_are_same_class(g0, cpu));
} }
TEST(Place, Default) { TEST(Place, Default) {
EXPECT_TRUE(majel::is_gpu_place(majel::get_place())); EXPECT_TRUE(paddle::platform::is_gpu_place(paddle::platform::get_place()));
EXPECT_TRUE(majel::is_gpu_place(majel::default_gpu())); EXPECT_TRUE(paddle::platform::is_gpu_place(paddle::platform::default_gpu()));
EXPECT_TRUE(majel::is_cpu_place(majel::default_cpu())); EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::default_cpu()));
majel::set_place(majel::CpuPlace()); paddle::platform::set_place(paddle::platform::CpuPlace());
EXPECT_TRUE(majel::is_cpu_place(majel::get_place())); EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
} }
TEST(Place, Print) { TEST(Place, Print) {
{ {
std::stringstream ss; std::stringstream ss;
ss << majel::GpuPlace(1); ss << paddle::platform::GpuPlace(1);
EXPECT_EQ("GpuPlace(1)", ss.str()); EXPECT_EQ("GpuPlace(1)", ss.str());
} }
{ {
std::stringstream ss; std::stringstream ss;
ss << majel::CpuPlace(); ss << paddle::platform::CpuPlace();
EXPECT_EQ("CpuPlace", ss.str()); EXPECT_EQ("CpuPlace", ss.str());
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册