diff --git a/paddle/CMakeLists.txt b/paddle/CMakeLists.txt index bf1b01309ec5d023af33f6d9d6da8a8d7295d8d1..22e55d3b3b764294c479238b698d0193d1784a40 100644 --- a/paddle/CMakeLists.txt +++ b/paddle/CMakeLists.txt @@ -18,10 +18,10 @@ add_subdirectory(strings) find_package(Boost QUIET) + if(Boost_FOUND) - include_directories(${Boost_INCLUDE_DIRS}) - include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - add_subdirectory(majel) + add_subdirectory(platform) + add_subdirectory(framework) endif() if(WITH_C_API) diff --git a/paddle/framework/.clang-format b/paddle/framework/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..29282dc87e2c499988c17d90d47d44cd5cf7f115 --- /dev/null +++ b/paddle/framework/.clang-format @@ -0,0 +1,5 @@ +--- +Language: Cpp +BasedOnStyle: Google +Standard: Cpp11 +... diff --git a/paddle/framework/CMakeLists.txt b/paddle/framework/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..673cfa19ac35116288a2481b85858b6f88f3378e --- /dev/null +++ b/paddle/framework/CMakeLists.txt @@ -0,0 +1,4 @@ +cc_library(ddim SRCS ddim.cc) +cc_test(ddim_test SRCS ddim_test.cc DEPS ddim) + +nv_test(dim_test SRCS dim_test.cu DEPS ddim) diff --git a/paddle/majel/ddim.cc b/paddle/framework/ddim.cc similarity index 94% rename from paddle/majel/ddim.cc rename to paddle/framework/ddim.cc index f32408ed53074234873ec0ea8ee7f4e449e5e908..3f949a6595ea326b97ac567daf9b35a68c8cf7f8 100644 --- a/paddle/majel/ddim.cc +++ b/paddle/framework/ddim.cc @@ -1,6 +1,7 @@ -#include "paddle/majel/ddim.h" +#include "paddle/framework/ddim.h" -namespace majel { +namespace paddle { +namespace framework { ///@cond HIDDEN @@ -66,7 +67,7 @@ DDim make_ddim(const std::vector& dims) { ///@cond HIDDEN // XXX For some reason, putting this in an anonymous namespace causes errors class DynamicMutableIndexer : public boost::static_visitor { -public: + public: DynamicMutableIndexer(int idx) : idx_(idx) {} template @@ -74,12 +75,12 @@ public: return dim[idx_]; } -private: + private: int idx_; }; class DynamicConstIndexer : public boost::static_visitor { -public: + public: DynamicConstIndexer(int idx) : idx_(idx) {} template @@ -87,7 +88,7 @@ public: return dim[idx_]; } -private: + private: int idx_; }; @@ -213,10 +214,11 @@ struct DDimPrinter : boost::static_visitor { ///\endcond -std::ostream& operator<<(std::ostream& os, const majel::DDim& ddim) { +std::ostream& operator<<(std::ostream& os, const DDim& ddim) { DDimPrinter printer(os); boost::apply_visitor(printer, ddim); return os; } -} // namespace majel +} // namespace framework +} // namespace paddle diff --git a/paddle/majel/ddim.h b/paddle/framework/ddim.h similarity index 79% rename from paddle/majel/ddim.h rename to paddle/framework/ddim.h index 7be756f8c098ba5aa3a5ff4380c90f4b90a55bb7..223c4180bee45e21547364441476b27051daca56 100644 --- a/paddle/majel/ddim.h +++ b/paddle/framework/ddim.h @@ -5,20 +5,14 @@ #include #include -#include "paddle/majel/dim.h" +#include "paddle/framework/dim.h" -namespace majel { +namespace paddle { +namespace framework { namespace { -typedef boost::variant, - Dim<2>, - Dim<3>, - Dim<4>, - Dim<5>, - Dim<6>, - Dim<7>, - Dim<8>, - Dim<9>> +typedef boost::variant, Dim<2>, Dim<3>, Dim<4>, Dim<5>, Dim<6>, Dim<7>, + Dim<8>, Dim<9>> DDimVar; } @@ -95,14 +89,15 @@ ssize_t product(const DDim& ddim); int arity(const DDim& ddim); -std::ostream& operator<<(std::ostream&, const majel::DDim&); +std::ostream& operator<<(std::ostream&, const DDim&); -} // namespace majel +} // namespace framework +} // namespace paddle namespace boost { template -T get(const majel::DDim& in) { +T get(const paddle::framework::DDim& in) { return boost::get(in.var); } diff --git a/paddle/majel/ddim_test.cc b/paddle/framework/ddim_test.cc similarity index 59% rename from paddle/majel/ddim_test.cc rename to paddle/framework/ddim_test.cc index a5b8a7c4d26740c1c4169547e76a0cf5558facc0..e5c84d7abe9d476f285c8c5cd904d2e570eb0e4f 100644 --- a/paddle/majel/ddim_test.cc +++ b/paddle/framework/ddim_test.cc @@ -4,18 +4,18 @@ #include #include "gtest/gtest.h" -#include "paddle/majel/ddim.h" +#include "paddle/framework/ddim.h" TEST(DDim, Equality) { // construct a DDim from an initialization list - majel::DDim ddim = majel::make_ddim({9, 1, 5}); + paddle::framework::DDim ddim = paddle::framework::make_ddim({9, 1, 5}); EXPECT_EQ(ddim[0], 9); EXPECT_EQ(ddim[1], 1); EXPECT_EQ(ddim[2], 5); // construct a DDim from a vector std::vector vec({9, 1, 5}); - majel::DDim vddim = majel::make_ddim(vec); + paddle::framework::DDim vddim = paddle::framework::make_ddim(vec); EXPECT_EQ(ddim[0], 9); EXPECT_EQ(ddim[1], 1); EXPECT_EQ(ddim[2], 5); @@ -23,43 +23,43 @@ TEST(DDim, Equality) { // mutate a DDim ddim[1] = 2; EXPECT_EQ(ddim[1], 2); - majel::set(ddim, 0, 6); - EXPECT_EQ(majel::get(ddim, 0), 6); + paddle::framework::set(ddim, 0, 6); + EXPECT_EQ(paddle::framework::get(ddim, 0), 6); // vectorize a DDim - std::vector res_vec = majel::vectorize(vddim); + std::vector res_vec = paddle::framework::vectorize(vddim); EXPECT_EQ(res_vec[0], 9); EXPECT_EQ(res_vec[1], 1); EXPECT_EQ(res_vec[2], 5); - majel::Dim<3> d(3, 2, 1); - res_vec = majel::vectorize(majel::DDim(d)); + paddle::framework::Dim<3> d(3, 2, 1); + res_vec = paddle::framework::vectorize(paddle::framework::DDim(d)); EXPECT_EQ(res_vec[0], 3); EXPECT_EQ(res_vec[1], 2); EXPECT_EQ(res_vec[2], 1); // add two DDims - majel::DDim ddim_sum = ddim + vddim; + paddle::framework::DDim ddim_sum = ddim + vddim; EXPECT_EQ(ddim_sum[0], 15); EXPECT_EQ(ddim_sum[1], 3); EXPECT_EQ(ddim_sum[2], 10); // multiply two DDims - majel::DDim ddim_mul = ddim * vddim; + paddle::framework::DDim ddim_mul = ddim * vddim; EXPECT_EQ(ddim_mul[0], 54); EXPECT_EQ(ddim_mul[1], 2); EXPECT_EQ(ddim_mul[2], 25); // arity of a DDim - EXPECT_EQ(majel::arity(ddim), 3); + EXPECT_EQ(paddle::framework::arity(ddim), 3); // product of a DDim - EXPECT_EQ(majel::product(vddim), 45); + EXPECT_EQ(paddle::framework::product(vddim), 45); } TEST(DDim, Print) { // print a DDim std::stringstream ss; - majel::DDim ddim = majel::make_ddim({2, 3, 4}); + paddle::framework::DDim ddim = paddle::framework::make_ddim({2, 3, 4}); ss << ddim; EXPECT_EQ("2, 3, 4", ss.str()); } diff --git a/paddle/majel/dim.h b/paddle/framework/dim.h similarity index 96% rename from paddle/majel/dim.h rename to paddle/framework/dim.h index c4b0c6aea683384d4657dd5db6f419b9e1108704..bcde291d12d429a3f2cd41fa6d0ee606c7c9c92f 100644 --- a/paddle/majel/dim.h +++ b/paddle/framework/dim.h @@ -5,10 +5,11 @@ #include #include -#include "paddle/majel/detail/cuda_assert.h" -#include "paddle/majel/detail/hostdevice.h" +#include "paddle/platform/assert.h" +#include "paddle/platform/hostdevice.h" -namespace majel { +namespace paddle { +namespace framework { // Statically sized, statically indexed dimension template @@ -74,7 +75,7 @@ struct Dim<1> { throw std::invalid_argument("Index out of range."); } #else - MAJEL_ASSERT(idx < size.head); + PADDLE_ASSERT(idx < size.head); #endif } @@ -131,7 +132,7 @@ HOSTDEVICE int& indexer(Dim& dim, int idx) { throw std::invalid_argument("Tried to access a negative dimension"); } #else - MAJEL_ASSERT(idx >= 0); + PADDLE_ASSERT(idx >= 0); #endif if (idx == 0) { return dim.head; @@ -146,7 +147,7 @@ HOSTDEVICE int& indexer<1>(Dim<1>& dim, int idx) { throw std::invalid_argument("Invalid index"); } #else - MAJEL_ASSERT(idx == 0); + PADDLE_ASSERT(idx == 0); #endif return dim.head; } @@ -158,7 +159,7 @@ HOSTDEVICE int indexer(const Dim& dim, int idx) { throw std::invalid_argument("Tried to access a negative dimension"); } #else - MAJEL_ASSERT(idx >= 0); + PADDLE_ASSERT(idx >= 0); #endif if (idx == 0) { return dim.head; @@ -173,7 +174,7 @@ HOSTDEVICE int indexer<1>(const Dim<1>& dim, int idx) { throw std::invalid_argument("Invalid index"); } #else - MAJEL_ASSERT(idx == 0); + PADDLE_ASSERT(idx == 0); #endif return dim.head; } @@ -411,7 +412,7 @@ HOSTDEVICE Dim make_dim(Args... idxes) { // XXX For some reason, overloading fails to resolve this correctly template typename std::enable_if<(i > 1), std::ostream&>::type operator<<( - std::ostream& os, const majel::Dim& d) { + std::ostream& os, const Dim& d) { os << d.head << ", " << d.tail; return os; } @@ -420,7 +421,7 @@ typename std::enable_if<(i > 1), std::ostream&>::type operator<<( // XXX I wish this could be an overload instead of a template template typename std::enable_if<(i == 1), std::ostream&>::type operator<<( - std::ostream& os, const majel::Dim& d) { + std::ostream& os, const Dim& d) { os << d.head; return os; } @@ -448,4 +449,5 @@ HOSTDEVICE Dim linear_to_dimension(int linear_index, Dim extents) { return result; } -} // namespace majel +} // namespace framework +} // namespace paddle diff --git a/paddle/framework/dim_test.cu b/paddle/framework/dim_test.cu new file mode 100644 index 0000000000000000000000000000000000000000..809bf04826637195425a32c054c94e00ef940df9 --- /dev/null +++ b/paddle/framework/dim_test.cu @@ -0,0 +1,128 @@ +#include +#include + +#include "paddle/framework/dim.h" +#include "gtest/gtest.h" + +__global__ void test(paddle::framework::Dim<2>* o) { + o[0] = paddle::framework::make_dim(5, 6); +} + +__global__ void dyn_idx_gpu(int* o) { + auto d = paddle::framework::make_dim(5, 6); + o[0] = d[1]; +} + +TEST(Dim, Equality) { + // construct a Dim on the CPU + auto a = paddle::framework::make_dim(3, 4); + EXPECT_EQ(paddle::framework::get<0>(a), 3); + EXPECT_EQ(paddle::framework::get<1>(a), 4); + + // construct a Dim on the GPU + thrust::device_vector> t(2); + test<<<1,1>>>(thrust::raw_pointer_cast(t.data())); + a = t[0]; + EXPECT_EQ(paddle::framework::get<0>(a), 5); + EXPECT_EQ(paddle::framework::get<1>(a), 6); + + // linearization + auto b = paddle::framework::make_dim(7, 8); + EXPECT_EQ(paddle::framework::linearize(a, b), 83); + + // product + EXPECT_EQ(paddle::framework::product(a), 30); + + // mutate a Dim + paddle::framework::get<1>(b) = 10; + EXPECT_EQ(paddle::framework::get<0>(b), 7); + EXPECT_EQ(paddle::framework::get<1>(b), 10); + + // dynamic access + paddle::framework::get(b, 0) = 8; + b[1] = 11; + EXPECT_EQ(paddle::framework::get<0>(b), 8); + EXPECT_EQ(paddle::framework::get<1>(b), 11); + EXPECT_EQ(paddle::framework::get(b, 0), 8); + EXPECT_EQ(b[1], 11); + + // dynamic access on GPU + thrust::device_vector r(1); + dyn_idx_gpu<<<1,1>>>(thrust::raw_pointer_cast(r.data())); + int res = r[0]; + EXPECT_EQ(res, 6); + + // ex_prefix_mul + paddle::framework::Dim<3> c = paddle::framework::ex_prefix_mul(paddle::framework::Dim<3>(3, 4, 5)); + EXPECT_EQ(paddle::framework::get<0>(c), 1); + EXPECT_EQ(paddle::framework::get<1>(c), 3); + EXPECT_EQ(paddle::framework::get<2>(c), 12); + + // contiguous_strides + c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(10, 1, 10)); + EXPECT_EQ(paddle::framework::get<0>(c), 1); + EXPECT_EQ(paddle::framework::get<1>(c), 0); + EXPECT_EQ(paddle::framework::get<2>(c), 10); + c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(10, 10, 1)); + EXPECT_EQ(paddle::framework::get<0>(c), 1); + EXPECT_EQ(paddle::framework::get<1>(c), 10); + EXPECT_EQ(paddle::framework::get<2>(c), 0); + c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(1, 10, 10)); + EXPECT_EQ(paddle::framework::get<0>(c), 0); + EXPECT_EQ(paddle::framework::get<1>(c), 1); + EXPECT_EQ(paddle::framework::get<2>(c), 10); + c = paddle::framework::contiguous_strides(paddle::framework::Dim<3>(2, 3, 4)); + EXPECT_EQ(paddle::framework::get<0>(c), 1); + EXPECT_EQ(paddle::framework::get<1>(c), 2); + EXPECT_EQ(paddle::framework::get<2>(c), 6); + + // generate from an index + auto size = paddle::framework::make_dim(4, 5, 2); + c = paddle::framework::Dim<3>(14, size); + EXPECT_EQ(paddle::framework::get<0>(c), 2); + EXPECT_EQ(paddle::framework::get<1>(c), 3); + EXPECT_EQ(paddle::framework::get<2>(c), 0); + c = paddle::framework::Dim<3>(25, size); + EXPECT_EQ(paddle::framework::get<0>(c), 1); + EXPECT_EQ(paddle::framework::get<1>(c), 1); + EXPECT_EQ(paddle::framework::get<2>(c), 1); +} + +TEST(Dim, Bool) { + auto a = paddle::framework::make_dim(3, 4); + auto b = paddle::framework::make_dim(5, 6); + auto c = paddle::framework::make_dim(3, 4); + + // in_bounds check + EXPECT_TRUE(paddle::framework::contained(a, b)); + EXPECT_FALSE(paddle::framework::contained(b, a)); + + // comparison + EXPECT_TRUE(a == a); + EXPECT_FALSE(a == b); + EXPECT_TRUE(a == c); + + // contiguous check + int x = 4, y = 5, z = 2; + paddle::framework::Dim<3> sizef(x, y, z); + paddle::framework::Dim<3> stridea(1, x, x*y); + paddle::framework::Dim<3> strideb(2, 2*x, 2*x*y); + paddle::framework::Dim<3> stridec(1, x, 2*x*y); + EXPECT_TRUE(paddle::framework::contiguous(sizef, stridea)); + EXPECT_FALSE(paddle::framework::contiguous(sizef, strideb)); + EXPECT_FALSE(paddle::framework::contiguous(sizef, stridec)); +} + +TEST(Dim, Print) { + { + std::stringstream ss; + auto a = paddle::framework::make_dim(2, 3); + ss << a; + EXPECT_EQ(ss.str(), "2, 3"); + } + { + std::stringstream ss; + ss << paddle::framework::make_dim(8); + EXPECT_EQ(ss.str(), "8"); + } +} diff --git a/paddle/majel/README.md b/paddle/framework/tensor.md similarity index 100% rename from paddle/majel/README.md rename to paddle/framework/tensor.md diff --git a/paddle/majel/.gitignore b/paddle/majel/.gitignore deleted file mode 100644 index 1f5acdebb56971202b63d2485e2ac5042786f13c..0000000000000000000000000000000000000000 --- a/paddle/majel/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -build -third-party \ No newline at end of file diff --git a/paddle/majel/detail/cuda_assert.h b/paddle/majel/detail/cuda_assert.h deleted file mode 100644 index 9490d0ae3eff01bdb4403de710b7bfd878e87f03..0000000000000000000000000000000000000000 --- a/paddle/majel/detail/cuda_assert.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#define STRINGIFY(x) #x -#define TOSTRING(x) STRINGIFY(x) - -#if defined(__APPLE__) && defined(__CUDA_ARCH__) && !defined(NDEBUG) -#include -#define MAJEL_ASSERT(e) \ - do { \ - if (!(e)) { \ - printf( \ - "%s:%d Assertion `%s` failed.\n", __FILE__, __LINE__, TOSTRING(e)); \ - asm("trap;"); \ - } \ - } while (0) - -#define MAJEL_ASSERT_MSG(e, m) \ - do { \ - if (!(e)) { \ - printf("%s:%d Assertion `%s` failed (%s).\n", \ - __FILE__, \ - __LINE__, \ - TOSTRING(e), \ - m); \ - asm("trap;"); \ - } \ - } while (0) -#else -#include -#define MAJEL_ASSERT(e) assert(e) -#define MAJEL_ASSERT_MSG(e, m) assert((e) && (m)) -#endif diff --git a/paddle/majel/dim_test.cu b/paddle/majel/dim_test.cu deleted file mode 100644 index a7d81e595bea7fa6326ea350e2702e1ef8f5caa4..0000000000000000000000000000000000000000 --- a/paddle/majel/dim_test.cu +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include - -#include "paddle/majel/dim.h" -#include "gtest/gtest.h" - -__global__ void test(majel::Dim<2>* o) { - o[0] = majel::make_dim(5, 6); -} - -__global__ void dyn_idx_gpu(int* o) { - auto d = majel::make_dim(5, 6); - o[0] = d[1]; -} - -TEST(Dim, Equality) { - // construct a Dim on the CPU - auto a = majel::make_dim(3, 4); - EXPECT_EQ(majel::get<0>(a), 3); - EXPECT_EQ(majel::get<1>(a), 4); - - // construct a Dim on the GPU - thrust::device_vector> t(2); - test<<<1,1>>>(thrust::raw_pointer_cast(t.data())); - a = t[0]; - EXPECT_EQ(majel::get<0>(a), 5); - EXPECT_EQ(majel::get<1>(a), 6); - - // linearization - auto b = majel::make_dim(7, 8); - EXPECT_EQ(majel::linearize(a, b), 83); - - // product - EXPECT_EQ(majel::product(a), 30); - - // mutate a Dim - majel::get<1>(b) = 10; - EXPECT_EQ(majel::get<0>(b), 7); - EXPECT_EQ(majel::get<1>(b), 10); - - // dynamic access - majel::get(b, 0) = 8; - b[1] = 11; - EXPECT_EQ(majel::get<0>(b), 8); - EXPECT_EQ(majel::get<1>(b), 11); - EXPECT_EQ(majel::get(b, 0), 8); - EXPECT_EQ(b[1], 11); - - // dynamic access on GPU - thrust::device_vector r(1); - dyn_idx_gpu<<<1,1>>>(thrust::raw_pointer_cast(r.data())); - int res = r[0]; - EXPECT_EQ(res, 6); - - // ex_prefix_mul - majel::Dim<3> c = majel::ex_prefix_mul(majel::Dim<3>(3, 4, 5)); - EXPECT_EQ(majel::get<0>(c), 1); - EXPECT_EQ(majel::get<1>(c), 3); - EXPECT_EQ(majel::get<2>(c), 12); - - // contiguous_strides - c = majel::contiguous_strides(majel::Dim<3>(10, 1, 10)); - EXPECT_EQ(majel::get<0>(c), 1); - EXPECT_EQ(majel::get<1>(c), 0); - EXPECT_EQ(majel::get<2>(c), 10); - c = majel::contiguous_strides(majel::Dim<3>(10, 10, 1)); - EXPECT_EQ(majel::get<0>(c), 1); - EXPECT_EQ(majel::get<1>(c), 10); - EXPECT_EQ(majel::get<2>(c), 0); - c = majel::contiguous_strides(majel::Dim<3>(1, 10, 10)); - EXPECT_EQ(majel::get<0>(c), 0); - EXPECT_EQ(majel::get<1>(c), 1); - EXPECT_EQ(majel::get<2>(c), 10); - c = majel::contiguous_strides(majel::Dim<3>(2, 3, 4)); - EXPECT_EQ(majel::get<0>(c), 1); - EXPECT_EQ(majel::get<1>(c), 2); - EXPECT_EQ(majel::get<2>(c), 6); - - // generate from an index - auto size = majel::make_dim(4, 5, 2); - c = majel::Dim<3>(14, size); - EXPECT_EQ(majel::get<0>(c), 2); - EXPECT_EQ(majel::get<1>(c), 3); - EXPECT_EQ(majel::get<2>(c), 0); - c = majel::Dim<3>(25, size); - EXPECT_EQ(majel::get<0>(c), 1); - EXPECT_EQ(majel::get<1>(c), 1); - EXPECT_EQ(majel::get<2>(c), 1); -} - -TEST(Dim, Bool) { - auto a = majel::make_dim(3, 4); - auto b = majel::make_dim(5, 6); - auto c = majel::make_dim(3, 4); - - // in_bounds check - EXPECT_TRUE(majel::contained(a, b)); - EXPECT_FALSE(majel::contained(b, a)); - - // comparison - EXPECT_TRUE(a == a); - EXPECT_FALSE(a == b); - EXPECT_TRUE(a == c); - - // contiguous check - int x = 4, y = 5, z = 2; - majel::Dim<3> sizef(x, y, z); - majel::Dim<3> stridea(1, x, x*y); - majel::Dim<3> strideb(2, 2*x, 2*x*y); - majel::Dim<3> stridec(1, x, 2*x*y); - EXPECT_TRUE(majel::contiguous(sizef, stridea)); - EXPECT_FALSE(majel::contiguous(sizef, strideb)); - EXPECT_FALSE(majel::contiguous(sizef, stridec)); -} - -TEST(Dim, Print) { - { - std::stringstream ss; - auto a = majel::make_dim(2, 3); - ss << a; - EXPECT_EQ(ss.str(), "2, 3"); - } - { - std::stringstream ss; - ss << majel::make_dim(8); - EXPECT_EQ(ss.str(), "8"); - } -} diff --git a/paddle/majel/place.cc b/paddle/majel/place.cc deleted file mode 100644 index ca50b37843e0ba047f8f8b8d24a3d3c131587382..0000000000000000000000000000000000000000 --- a/paddle/majel/place.cc +++ /dev/null @@ -1,49 +0,0 @@ -#include "paddle/majel/place.h" - -namespace majel { - -namespace detail { - -class PlacePrinter : public boost::static_visitor<> { -private: - std::ostream& os_; - -public: - PlacePrinter(std::ostream& os) : os_(os) {} - - void operator()(const CpuPlace&) { os_ << "CpuPlace"; } - - void operator()(const GpuPlace& p) { os_ << "GpuPlace(" << p.device << ")"; } -}; - -} // namespace detail - -static Place the_default_place; - -void set_place(const Place& place) { the_default_place = place; } - -const Place& get_place() { return the_default_place; } - -const GpuPlace default_gpu() { return GpuPlace(0); } - -const CpuPlace default_cpu() { return CpuPlace(); } - -bool is_gpu_place(const Place& p) { - return boost::apply_visitor(IsGpuPlace(), p); -} - -bool is_cpu_place(const Place& p) { - return !boost::apply_visitor(IsGpuPlace(), p); -} - -bool places_are_same_class(const Place& p1, const Place& p2) { - return is_gpu_place(p1) == is_gpu_place(p2); -} - -std::ostream& operator<<(std::ostream& os, const majel::Place& p) { - majel::detail::PlacePrinter printer(os); - boost::apply_visitor(printer, p); - return os; -} - -} // namespace majel diff --git a/paddle/majel/place.h b/paddle/majel/place.h deleted file mode 100644 index ad3dc3fe0b80ac5dc10a59910c580d7912469cd4..0000000000000000000000000000000000000000 --- a/paddle/majel/place.h +++ /dev/null @@ -1,50 +0,0 @@ -#pragma once -#include -#include - -namespace majel { - -struct CpuPlace { - CpuPlace() {} // WORKAROUND: for some reason, omitting this constructor - // causes errors with boost 1.59 and OSX - // needed for variant equality comparison - inline bool operator==(const CpuPlace&) const { return true; } - - inline bool operator!=(const CpuPlace&) const { return false; } -}; - -struct GpuPlace { - GpuPlace(int d) : device(d) {} - - // needed for variant equality comparison - inline bool operator==(const GpuPlace& o) const { return device == o.device; } - - inline bool operator!=(const GpuPlace& o) const { return !(*this == o); } - - GpuPlace() : GpuPlace(0) {} - int device; -}; - -class IsGpuPlace : public boost::static_visitor { -public: - bool operator()(const CpuPlace&) const { return false; } - - bool operator()(const GpuPlace& gpu) const { return true; } -}; - -typedef boost::variant Place; - -void set_place(const Place&); - -const Place& get_place(); - -const GpuPlace default_gpu(); -const CpuPlace default_cpu(); - -bool is_gpu_place(const Place&); -bool is_cpu_place(const Place&); -bool places_are_same_class(const Place&, const Place&); - -std::ostream& operator<<(std::ostream&, const majel::Place&); - -} // namespace majel diff --git a/paddle/majel/place_test.cc b/paddle/majel/place_test.cc deleted file mode 100644 index 6a099ae6b6e4f63a6ce845ab17eaab6e12c2c0b0..0000000000000000000000000000000000000000 --- a/paddle/majel/place_test.cc +++ /dev/null @@ -1,40 +0,0 @@ -#include "paddle/majel/place.h" -#include -#include "gtest/gtest.h" - -TEST(Place, Equality) { - majel::CpuPlace cpu; - majel::GpuPlace g0(0), g1(1), gg0(0); - - EXPECT_EQ(cpu, cpu); - EXPECT_EQ(g0, g0); - EXPECT_EQ(g1, g1); - EXPECT_EQ(g0, gg0); - - EXPECT_NE(g0, g1); - - EXPECT_TRUE(majel::places_are_same_class(g0, gg0)); - EXPECT_FALSE(majel::places_are_same_class(g0, cpu)); -} - -TEST(Place, Default) { - EXPECT_TRUE(majel::is_gpu_place(majel::get_place())); - EXPECT_TRUE(majel::is_gpu_place(majel::default_gpu())); - EXPECT_TRUE(majel::is_cpu_place(majel::default_cpu())); - - majel::set_place(majel::CpuPlace()); - EXPECT_TRUE(majel::is_cpu_place(majel::get_place())); -} - -TEST(Place, Print) { - { - std::stringstream ss; - ss << majel::GpuPlace(1); - EXPECT_EQ("GpuPlace(1)", ss.str()); - } - { - std::stringstream ss; - ss << majel::CpuPlace(); - EXPECT_EQ("CpuPlace", ss.str()); - } -} diff --git a/paddle/platform/.clang-format b/paddle/platform/.clang-format new file mode 100644 index 0000000000000000000000000000000000000000..29282dc87e2c499988c17d90d47d44cd5cf7f115 --- /dev/null +++ b/paddle/platform/.clang-format @@ -0,0 +1,5 @@ +--- +Language: Cpp +BasedOnStyle: Google +Standard: Cpp11 +... diff --git a/paddle/majel/CMakeLists.txt b/paddle/platform/CMakeLists.txt similarity index 51% rename from paddle/majel/CMakeLists.txt rename to paddle/platform/CMakeLists.txt index 93e5e2c22f0eb5797c635efd8ca34ffb74c03311..c7d7b14518ebb8415014a78fc1a3bafa8c386191 100644 --- a/paddle/majel/CMakeLists.txt +++ b/paddle/platform/CMakeLists.txt @@ -1,8 +1,4 @@ +nv_test(cuda_test SRCS cuda_test.cu) + cc_library(place SRCS place.cc) cc_test(place_test SRCS place_test.cc DEPS place glog gflags) - -cc_library(ddim SRCS ddim.cc) -cc_test(ddim_test SRCS ddim_test.cc DEPS ddim) - -nv_test(cuda_test SRCS cuda_test.cu) -nv_test(dim_test SRCS dim_test.cu DEPS ddim) diff --git a/paddle/platform/assert.h b/paddle/platform/assert.h new file mode 100644 index 0000000000000000000000000000000000000000..70d3bf75062c5471ab54ee2c4c7637c679d9a8a3 --- /dev/null +++ b/paddle/platform/assert.h @@ -0,0 +1,29 @@ +#pragma once + +#define STRINGIFY(x) #x +#define TOSTRING(x) STRINGIFY(x) + +#if defined(__APPLE__) && defined(__CUDA_ARCH__) && !defined(NDEBUG) +#include +#define PADDLE_ASSERT(e) \ + do { \ + if (!(e)) { \ + printf("%s:%d Assertion `%s` failed.\n", __FILE__, __LINE__, \ + TOSTRING(e)); \ + asm("trap;"); \ + } \ + } while (0) + +#define PADDLE_ASSERT_MSG(e, m) \ + do { \ + if (!(e)) { \ + printf("%s:%d Assertion `%s` failed (%s).\n", __FILE__, __LINE__, \ + TOSTRING(e), m); \ + asm("trap;"); \ + } \ + } while (0) +#else +#include +#define PADDLE_ASSERT(e) assert(e) +#define PADDLE_ASSERT_MSG(e, m) assert((e) && (m)) +#endif diff --git a/paddle/majel/cuda_test.cu b/paddle/platform/cuda_test.cu similarity index 100% rename from paddle/majel/cuda_test.cu rename to paddle/platform/cuda_test.cu diff --git a/paddle/majel/detail/hostdevice.h b/paddle/platform/hostdevice.h similarity index 100% rename from paddle/majel/detail/hostdevice.h rename to paddle/platform/hostdevice.h diff --git a/paddle/platform/place.cc b/paddle/platform/place.cc new file mode 100644 index 0000000000000000000000000000000000000000..1afd03c01169d395b086c1da458ce25c66a12a51 --- /dev/null +++ b/paddle/platform/place.cc @@ -0,0 +1,46 @@ +#include "paddle/platform/place.h" + +namespace paddle { +namespace platform { + +namespace detail { + +class PlacePrinter : public boost::static_visitor<> { + public: + PlacePrinter(std::ostream &os) : os_(os) {} + void operator()(const CpuPlace &) { os_ << "CpuPlace"; } + void operator()(const GpuPlace &p) { os_ << "GpuPlace(" << p.device << ")"; } + + private: + std::ostream &os_; +}; + +} // namespace detail + +static Place the_default_place; + +void set_place(const Place &place) { the_default_place = place; } +const Place &get_place() { return the_default_place; } + +const GpuPlace default_gpu() { return GpuPlace(0); } +const CpuPlace default_cpu() { return CpuPlace(); } + +bool is_gpu_place(const Place &p) { + return boost::apply_visitor(IsGpuPlace(), p); +} +bool is_cpu_place(const Place &p) { + return !boost::apply_visitor(IsGpuPlace(), p); +} + +bool places_are_same_class(const Place &p1, const Place &p2) { + return is_gpu_place(p1) == is_gpu_place(p2); +} + +std::ostream &operator<<(std::ostream &os, const Place &p) { + detail::PlacePrinter printer(os); + boost::apply_visitor(printer, p); + return os; +} + +} // namespace platform +} // namespace paddle diff --git a/paddle/platform/place.h b/paddle/platform/place.h new file mode 100644 index 0000000000000000000000000000000000000000..489572c526e162500c8f747f0ec8df10da9d86a2 --- /dev/null +++ b/paddle/platform/place.h @@ -0,0 +1,49 @@ +#pragma once +#include +#include + +namespace paddle { +namespace platform { + +struct CpuPlace { + // WORKAROUND: for some reason, omitting this constructor + // causes errors with boost 1.59 and OSX + CpuPlace() {} + + // needed for variant equality comparison + inline bool operator==(const CpuPlace &) const { return true; } + inline bool operator!=(const CpuPlace &) const { return false; } +}; + +struct GpuPlace { + GpuPlace() : GpuPlace(0) {} + GpuPlace(int d) : device(d) {} + + // needed for variant equality comparison + inline bool operator==(const GpuPlace &o) const { return device == o.device; } + inline bool operator!=(const GpuPlace &o) const { return !(*this == o); } + + int device; +}; + +struct IsGpuPlace : public boost::static_visitor { + bool operator()(const CpuPlace &) const { return false; } + bool operator()(const GpuPlace &gpu) const { return true; } +}; + +typedef boost::variant Place; + +void set_place(const Place &); +const Place &get_place(); + +const GpuPlace default_gpu(); +const CpuPlace default_cpu(); + +bool is_gpu_place(const Place &); +bool is_cpu_place(const Place &); +bool places_are_same_class(const Place &, const Place &); + +std::ostream &operator<<(std::ostream &, const Place &); + +} // namespace platform +} // namespace paddle diff --git a/paddle/platform/place_test.cc b/paddle/platform/place_test.cc new file mode 100644 index 0000000000000000000000000000000000000000..73fccceedf6918148a26100f64cf322305c3ac20 --- /dev/null +++ b/paddle/platform/place_test.cc @@ -0,0 +1,40 @@ +#include "paddle/platform/place.h" +#include +#include "gtest/gtest.h" + +TEST(Place, Equality) { + paddle::platform::CpuPlace cpu; + paddle::platform::GpuPlace g0(0), g1(1), gg0(0); + + EXPECT_EQ(cpu, cpu); + EXPECT_EQ(g0, g0); + EXPECT_EQ(g1, g1); + EXPECT_EQ(g0, gg0); + + EXPECT_NE(g0, g1); + + EXPECT_TRUE(paddle::platform::places_are_same_class(g0, gg0)); + EXPECT_FALSE(paddle::platform::places_are_same_class(g0, cpu)); +} + +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())); + + paddle::platform::set_place(paddle::platform::CpuPlace()); + EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::get_place())); +} + +TEST(Place, Print) { + { + std::stringstream ss; + ss << paddle::platform::GpuPlace(1); + EXPECT_EQ("GpuPlace(1)", ss.str()); + } + { + std::stringstream ss; + ss << paddle::platform::CpuPlace(); + EXPECT_EQ("CpuPlace", ss.str()); + } +}