place_test.cc 1.7 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
Y
Yi Wang 已提交
14 15
#include "paddle/platform/place.h"
#include <sstream>
Y
Yi Wang 已提交
16
#include "gtest/gtest.h"
Y
Yi Wang 已提交
17 18

TEST(Place, Equality) {
19
  paddle::platform::CPUPlace cpu;
D
dzhwinter 已提交
20
  paddle::platform::CUDAPlace g0(0), g1(1), gg0(0);
Y
Yi Wang 已提交
21 22 23 24 25 26 27 28

  EXPECT_EQ(cpu, cpu);
  EXPECT_EQ(g0, g0);
  EXPECT_EQ(g1, g1);
  EXPECT_EQ(g0, gg0);

  EXPECT_NE(g0, g1);

Y
Yi Wang 已提交
29 30
  EXPECT_TRUE(paddle::platform::places_are_same_class(g0, gg0));
  EXPECT_FALSE(paddle::platform::places_are_same_class(g0, cpu));
Y
Yi Wang 已提交
31 32 33
}

TEST(Place, Default) {
Y
Yi Wang 已提交
34 35 36
  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()));
Y
Yi Wang 已提交
37

Q
QI JUN 已提交
38
  EXPECT_FALSE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
39
  paddle::platform::set_place(paddle::platform::CPUPlace());
Y
Yi Wang 已提交
40
  EXPECT_TRUE(paddle::platform::is_cpu_place(paddle::platform::get_place()));
Y
Yi Wang 已提交
41 42 43 44 45
}

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