place.cc 1.9 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 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 已提交
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"; }
T
tensor-tang 已提交
26
  void operator()(const MKLDNNPlace &) { os_ << "MKLDNNPlace"; }
27
  void operator()(const GPUPlace &p) { os_ << "GPUPlace(" << p.device << ")"; }
Y
Yi Wang 已提交
28

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

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

static Place the_default_place;

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

40 41
const GPUPlace default_gpu() { return GPUPlace(0); }
const CPUPlace default_cpu() { return CPUPlace(); }
T
tensor-tang 已提交
42
const MKLDNNPlace default_mkldnn() { return MKLDNNPlace(); }
Y
Yi Wang 已提交
43

Y
Yi Wang 已提交
44
bool is_gpu_place(const Place &p) {
45
  return boost::apply_visitor(IsGPUPlace(), p);
Y
Yi Wang 已提交
46
}
Y
Yi Wang 已提交
47
bool is_cpu_place(const Place &p) {
T
tensor-tang 已提交
48
  return !is_gpu_place(p) && !is_mkldnn_place(p);
Y
Yi Wang 已提交
49 50
}

T
tensor-tang 已提交
51 52 53 54
bool is_mkldnn_place(const Place &p) {
  return boost::apply_visitor(IsMKLDNNPlace(), p);
}

Y
Yi Wang 已提交
55
bool places_are_same_class(const Place &p1, const Place &p2) {
Y
Yu Yang 已提交
56
  return p1.which() == p2.which();
Y
Yi Wang 已提交
57 58
}

Y
Yi Wang 已提交
59 60
std::ostream &operator<<(std::ostream &os, const Place &p) {
  detail::PlacePrinter printer(os);
L
liaogang 已提交
61 62
  boost::apply_visitor(printer, p);
  return os;
Y
Yi Wang 已提交
63 64
}

Y
Yi Wang 已提交
65 66
}  // namespace platform
}  // namespace paddle