Merge pull request #2513 from wangkuiyi/platform
Move paddle/majel/* to paddle/{platform,framework/*gangliao-patch-1
commit
1e6f65503b
@ -0,0 +1,5 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
Standard: Cpp11
|
||||
...
|
@ -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)
|
@ -0,0 +1,128 @@
|
||||
#include <thrust/device_vector.h>
|
||||
#include <sstream>
|
||||
|
||||
#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<paddle::framework::Dim<2>> 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<int> 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");
|
||||
}
|
||||
}
|
@ -1,2 +0,0 @@
|
||||
build
|
||||
third-party
|
@ -1,32 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
|
||||
#if defined(__APPLE__) && defined(__CUDA_ARCH__) && !defined(NDEBUG)
|
||||
#include <stdio.h>
|
||||
#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 <assert.h>
|
||||
#define MAJEL_ASSERT(e) assert(e)
|
||||
#define MAJEL_ASSERT_MSG(e, m) assert((e) && (m))
|
||||
#endif
|
@ -1,128 +0,0 @@
|
||||
#include <thrust/device_vector.h>
|
||||
#include <sstream>
|
||||
|
||||
#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<majel::Dim<2>> 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<int> 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");
|
||||
}
|
||||
}
|
@ -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
|
@ -1,50 +0,0 @@
|
||||
#pragma once
|
||||
#include <boost/variant.hpp>
|
||||
#include <iostream>
|
||||
|
||||
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<bool> {
|
||||
public:
|
||||
bool operator()(const CpuPlace&) const { return false; }
|
||||
|
||||
bool operator()(const GpuPlace& gpu) const { return true; }
|
||||
};
|
||||
|
||||
typedef boost::variant<GpuPlace, CpuPlace> 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
|
@ -1,40 +0,0 @@
|
||||
#include "paddle/majel/place.h"
|
||||
#include <sstream>
|
||||
#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());
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
---
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
Standard: Cpp11
|
||||
...
|
@ -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)
|
@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
|
||||
#if defined(__APPLE__) && defined(__CUDA_ARCH__) && !defined(NDEBUG)
|
||||
#include <stdio.h>
|
||||
#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 <assert.h>
|
||||
#define PADDLE_ASSERT(e) assert(e)
|
||||
#define PADDLE_ASSERT_MSG(e, m) assert((e) && (m))
|
||||
#endif
|
@ -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
|
@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
#include <boost/variant.hpp>
|
||||
#include <iostream>
|
||||
|
||||
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> {
|
||||
bool operator()(const CpuPlace &) const { return false; }
|
||||
bool operator()(const GpuPlace &gpu) const { return true; }
|
||||
};
|
||||
|
||||
typedef boost::variant<GpuPlace, CpuPlace> 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
|
@ -0,0 +1,40 @@
|
||||
#include "paddle/platform/place.h"
|
||||
#include <sstream>
|
||||
#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());
|
||||
}
|
||||
}
|
Loading…
Reference in new issue