place_test.cc 1.6 KB
Newer Older
Y
Yi Wang 已提交
1 2
#include "paddle/platform/place.h"
#include <sstream>
Y
Yi Wang 已提交
3
#include "gtest/gtest.h"
Y
Yi Wang 已提交
4 5

TEST(Place, Equality) {
6 7
  paddle::platform::CPUPlace cpu;
  paddle::platform::GPUPlace g0(0), g1(1), gg0(0);
8
  paddle::platform::CUDNNPlace d0(0), d1(1), dd0(0);
Y
Yi Wang 已提交
9 10 11 12 13

  EXPECT_EQ(cpu, cpu);
  EXPECT_EQ(g0, g0);
  EXPECT_EQ(g1, g1);
  EXPECT_EQ(g0, gg0);
14
  EXPECT_EQ(d0, dd0);
Y
Yi Wang 已提交
15 16

  EXPECT_NE(g0, g1);
17
  EXPECT_NE(d0, d1);
Y
Yi Wang 已提交
18

Y
Yi Wang 已提交
19 20
  EXPECT_TRUE(paddle::platform::places_are_same_class(g0, gg0));
  EXPECT_FALSE(paddle::platform::places_are_same_class(g0, cpu));
21 22 23

  EXPECT_TRUE(paddle::platform::is_gpu_place(d0));
  EXPECT_FALSE(paddle::platform::places_are_same_class(g0, d0));
Y
Yi Wang 已提交
24 25 26
}

TEST(Place, Default) {
Y
Yi Wang 已提交
27 28 29
  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()));
T
tensor-tang 已提交
30 31
  EXPECT_TRUE(
      paddle::platform::is_mkldnn_place(paddle::platform::default_mkldnn()));
Y
Yi Wang 已提交
32

33
  paddle::platform::set_place(paddle::platform::CPUPlace());
Y
Yi Wang 已提交
34
  EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
T
tensor-tang 已提交
35 36 37 38

  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()));
Y
Yi Wang 已提交
39 40 41 42 43
}

TEST(Place, Print) {
  {
    std::stringstream ss;
44 45
    ss << paddle::platform::GPUPlace(1);
    EXPECT_EQ("GPUPlace(1)", ss.str());
Y
Yi Wang 已提交
46 47
  }
  {
L
liaogang 已提交
48
    std::stringstream ss;
49 50
    ss << paddle::platform::CPUPlace();
    EXPECT_EQ("CPUPlace", ss.str());
Y
Yi Wang 已提交
51 52
  }
}