place.cc 1.9 KB
Newer Older
L
liaogang 已提交
1 2
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

L
Luo Tao 已提交
3 4 5
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
L
liaogang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
L
liaogang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
L
liaogang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/platform/place.h"
Y
Yi Wang 已提交
16

Y
Yi Wang 已提交
17 18
namespace paddle {
namespace platform {
Y
Yi Wang 已提交
19 20 21

namespace detail {

L
liaogang 已提交
22
class PlacePrinter : public boost::static_visitor<> {
Y
Yi Wang 已提交
23
 public:
L
liaogang 已提交
24
  explicit PlacePrinter(std::ostream &os) : os_(os) {}
25
  void operator()(const CPUPlace &) { os_ << "CPUPlace"; }
D
dzhwinter 已提交
26 27 28
  void operator()(const CUDAPlace &p) {
    os_ << "CUDAPlace(" << p.device << ")";
  }
Y
Yi Wang 已提交
29

Y
Yi Wang 已提交
30
 private:
Y
Yi Wang 已提交
31
  std::ostream &os_;
Y
Yi Wang 已提交
32 33
};

Y
Yi Wang 已提交
34
}  // namespace detail
Y
Yi Wang 已提交
35 36 37

static Place the_default_place;

Y
Yi Wang 已提交
38 39
void set_place(const Place &place) { the_default_place = place; }
const Place &get_place() { return the_default_place; }
Y
Yi Wang 已提交
40

D
dzhwinter 已提交
41
const CUDAPlace default_gpu() { return CUDAPlace(0); }
42
const CPUPlace default_cpu() { return CPUPlace(); }
Y
Yi Wang 已提交
43

Y
Yi Wang 已提交
44
bool is_gpu_place(const Place &p) {
D
dzhwinter 已提交
45
  return boost::apply_visitor(IsCUDAPlace(), p);
Y
Yi Wang 已提交
46 47
}

Q
QI JUN 已提交
48
bool is_cpu_place(const Place &p) { return !is_gpu_place(p); }
T
tensor-tang 已提交
49

Y
Yi Wang 已提交
50
bool places_are_same_class(const Place &p1, const Place &p2) {
Y
Yu Yang 已提交
51
  return p1.which() == p2.which();
Y
Yi Wang 已提交
52 53
}

54 55 56 57 58 59 60 61 62 63 64 65
bool is_same_place(const Place &p1, const Place &p2) {
  if (places_are_same_class(p1, p2)) {
    if (is_cpu_place(p1)) {
      return true;
    } else {
      return boost::get<CUDAPlace>(p1) == boost::get<CUDAPlace>(p2);
    }
  } else {
    return false;
  }
}

Y
Yi Wang 已提交
66 67
std::ostream &operator<<(std::ostream &os, const Place &p) {
  detail::PlacePrinter printer(os);
L
liaogang 已提交
68 69
  boost::apply_visitor(printer, p);
  return os;
Y
Yi Wang 已提交
70 71
}

Y
Yi Wang 已提交
72 73
}  // namespace platform
}  // namespace paddle