From 110a402221d7d719f8c0eb50cea8f1f3bfedee9d Mon Sep 17 00:00:00 2001 From: fengjiayi Date: Thu, 18 May 2017 09:28:49 +0000 Subject: [PATCH] make up missing 'majel::' and import majel/util.h --- paddle/majel/dim.h | 19 +++++++---------- paddle/majel/test/dim_test.cu | 18 ++++++++-------- paddle/majel/util.h | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 21 deletions(-) create mode 100644 paddle/majel/util.h diff --git a/paddle/majel/dim.h b/paddle/majel/dim.h index cf7682b6865..ba718ce5b76 100644 --- a/paddle/majel/dim.h +++ b/paddle/majel/dim.h @@ -4,14 +4,9 @@ #include #include #include -/* -#ifdef __CUDACC__ - #include -#endif -*/ -#include "hostdevice.h" -#include "paddle/utils/Logging.h" +#include "majel/hostdevice.h" +#include "majel/util.h" namespace majel { @@ -79,7 +74,7 @@ struct Dim<1> { throw std::invalid_argument("Index out of range."); } #else - CHECK(idx < size.head); + MAJEL_ASSERT(idx < size.head); #endif } @@ -136,7 +131,7 @@ HOSTDEVICE int& indexer(Dim& dim, int idx) { throw std::invalid_argument("Tried to access a negative dimension"); } #else - CHECK(idx >= 0); + MAJEL_ASSERT(idx >= 0); #endif if (idx == 0) { return dim.head; @@ -151,7 +146,7 @@ HOSTDEVICE int& indexer<1>(Dim<1>& dim, int idx) { throw std::invalid_argument("Invalid index"); } #else - CHECK(idx == 0); + MAJEL_ASSERT(idx == 0); #endif return dim.head; } @@ -163,7 +158,7 @@ HOSTDEVICE int indexer(const Dim& dim, int idx) { throw std::invalid_argument("Tried to access a negative dimension"); } #else - CHECK(idx >= 0); + MAJEL_ASSERT(idx >= 0); #endif if (idx == 0) { return dim.head; @@ -178,7 +173,7 @@ HOSTDEVICE int indexer<1>(const Dim<1>& dim, int idx) { throw std::invalid_argument("Invalid index"); } #else - CHECK(idx == 0); + MAJEL_ASSERT(idx == 0); #endif return dim.head; } diff --git a/paddle/majel/test/dim_test.cu b/paddle/majel/test/dim_test.cu index 380204531d8..ed7c34ffd1d 100644 --- a/paddle/majel/test/dim_test.cu +++ b/paddle/majel/test/dim_test.cu @@ -1,7 +1,7 @@ #include #include -#include "majel/dim.h" +#include "majel/dim.h" #include "gtest/gtest.h" __global__ void test(majel::Dim<2>* o) { @@ -16,22 +16,22 @@ __global__ void dyn_idx_gpu(int* o) { TEST(Dim, Equality) { // construct a Dim on the CPU auto a = majel::make_dim(3, 4); - EXPECT_EQ(get<0>(a), 3); - EXPECT_EQ(get<1>(a), 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(get<0>(a), 5); - EXPECT_EQ(get<1>(a), 6); + EXPECT_EQ(majel::get<0>(a), 5); + EXPECT_EQ(majel::get<1>(a), 6); // linearization - auto b = make_dim(7, 8); - EXPECT_EQ(linearize(a, b), 83); + auto b = majel::make_dim(7, 8); + EXPECT_EQ(majel::linearize(a, b), 83); // product - EXPECT_EQ(product(a), 30); + EXPECT_EQ(majel::product(a), 30); // mutate a Dim majel::get<1>(b) = 10; @@ -53,7 +53,7 @@ TEST(Dim, Equality) { EXPECT_EQ(res, 6); // ex_prefix_mul - majel::Dim<3> c = majel::ex_prefix_mul(Dim<3>(3, 4, 5)); + 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); diff --git a/paddle/majel/util.h b/paddle/majel/util.h new file mode 100644 index 00000000000..ab21e1869f7 --- /dev/null +++ b/paddle/majel/util.h @@ -0,0 +1,39 @@ +#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 + +namespace majel { +namespace detail { + +inline int div_up(int x, int y) { return (x + y - 1) / y; } +} +} -- GitLab