place.h 5.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13

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
#pragma once
15

S
sneaxiy 已提交
16
#include <functional>
Y
Yi Wang 已提交
17
#include <iostream>
18 19
#include <vector>

Y
Yi Wang 已提交
20 21
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/variant.h"
Y
Yi Wang 已提交
22

Y
Yi Wang 已提交
23 24
namespace paddle {
namespace platform {
Y
Yi Wang 已提交
25

26
struct CPUPlace {
Y
Yi Wang 已提交
27 28
  // WORKAROUND: for some reason, omitting this constructor
  // causes errors with boost 1.59 and OSX
29
  CPUPlace() {}
Y
Yi Wang 已提交
30

Y
Yi Wang 已提交
31
  // needed for variant equality comparison
32 33
  inline bool operator==(const CPUPlace &) const { return true; }
  inline bool operator!=(const CPUPlace &) const { return false; }
34
  inline bool operator<(const CPUPlace &) const { return false; }
Y
Yi Wang 已提交
35 36
};

D
dzhwinter 已提交
37 38 39
struct CUDAPlace {
  CUDAPlace() : CUDAPlace(0) {}
  explicit CUDAPlace(int d) : device(d) {}
Y
Yi Wang 已提交
40

Y
Yu Yang 已提交
41
  inline int GetDeviceId() const { return device; }
Y
Yi Wang 已提交
42
  // needed for variant equality comparison
D
dzhwinter 已提交
43 44 45 46
  inline bool operator==(const CUDAPlace &o) const {
    return device == o.device;
  }
  inline bool operator!=(const CUDAPlace &o) const { return !(*this == o); }
47
  inline bool operator<(const CUDAPlace &o) const { return device < o.device; }
Y
Yi Wang 已提交
48 49 50 51

  int device;
};

C
chengduoZH 已提交
52 53 54 55 56 57
struct CUDAPinnedPlace {
  CUDAPinnedPlace() {}

  // needed for variant equality comparison
  inline bool operator==(const CUDAPinnedPlace &) const { return true; }
  inline bool operator!=(const CUDAPinnedPlace &) const { return false; }
58
  inline bool operator<(const CUDAPinnedPlace &) const { return false; }
C
chengduoZH 已提交
59 60
};

D
dzhwinter 已提交
61
struct IsCUDAPlace : public boost::static_visitor<bool> {
62
  bool operator()(const CPUPlace &) const { return false; }
D
dzhwinter 已提交
63
  bool operator()(const CUDAPlace &gpu) const { return true; }
C
chengduoZH 已提交
64
  bool operator()(const CUDAPinnedPlace &) const { return false; }
T
tensor-tang 已提交
65 66
};

C
chengduoZH 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79
struct IsCPUPlace : public boost::static_visitor<bool> {
  bool operator()(const CPUPlace &cpu) const { return true; }
  bool operator()(const CUDAPlace &) const { return false; }
  bool operator()(const CUDAPinnedPlace &) const { return false; }
};

struct IsCUDAPinnedPlace : public boost::static_visitor<bool> {
  bool operator()(const CPUPlace &) const { return false; }
  bool operator()(const CUDAPlace &) const { return false; }
  bool operator()(const CUDAPinnedPlace &cuda_pinned) const { return true; }
};

typedef boost::variant<CUDAPlace, CPUPlace, CUDAPinnedPlace> Place;
Y
Yi Wang 已提交
80

Y
Yang Yu 已提交
81 82
using PlaceList = std::vector<Place>;

Y
Yi Wang 已提交
83 84
void set_place(const Place &);
const Place &get_place();
Y
Yi Wang 已提交
85

D
dzhwinter 已提交
86
const CUDAPlace default_gpu();
87
const CPUPlace default_cpu();
C
chengduoZH 已提交
88
const CUDAPinnedPlace default_cuda_pinned();
Y
Yi Wang 已提交
89

Y
Yi Wang 已提交
90 91
bool is_gpu_place(const Place &);
bool is_cpu_place(const Place &);
C
chengduoZH 已提交
92
bool is_cuda_pinned_place(const Place &);
Y
Yi Wang 已提交
93
bool places_are_same_class(const Place &, const Place &);
94
bool is_same_place(const Place &, const Place &);
Y
Yi Wang 已提交
95

Y
Yi Wang 已提交
96
std::ostream &operator<<(std::ostream &, const Place &);
Y
Yi Wang 已提交
97

Y
Yang Yu 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
template <typename Visitor>
struct PlaceVisitorWrapper
    : public boost::static_visitor<typename Visitor::result_type> {
  const Visitor &visitor_;
  explicit PlaceVisitorWrapper(const Visitor &visitor) : visitor_(visitor) {}

  typename Visitor::result_type operator()(const CPUPlace &cpu) const {
    return visitor_(cpu);
  }

  typename Visitor::result_type operator()(const CUDAPlace &cuda) const {
#ifdef PADDLE_WITH_CUDA
    return visitor_(cuda);
#else
    PADDLE_THROW("Paddle is not compiled with CUDA. Cannot visit cuda device");
    return typename Visitor::result_type();
#endif
  }
C
chengduoZH 已提交
116 117 118

  typename Visitor::result_type operator()(
      const CUDAPinnedPlace &cuda_pinned) const {
C
chengduoZH 已提交
119
#ifdef PADDLE_WITH_CUDA
C
chengduoZH 已提交
120
    return visitor_(cuda_pinned);
C
chengduoZH 已提交
121 122 123 124
#else
    PADDLE_THROW("Paddle is not compiled with CUDA. Cannot visit cuda_pinned");
    return typename Visitor::result_type();
#endif
C
chengduoZH 已提交
125
  }
Y
Yang Yu 已提交
126 127 128 129 130 131 132 133
};

template <typename Visitor>
typename Visitor::result_type VisitPlace(const Place &place,
                                         const Visitor &visitor) {
  return boost::apply_visitor(PlaceVisitorWrapper<Visitor>(visitor), place);
}

S
sneaxiy 已提交
134 135 136 137 138 139 140
struct PlaceHashVisitor : public boost::static_visitor<size_t> {
  template <typename Place>
  inline size_t operator()(const Place &place) const {
    return place.hash();
  }
};

Y
Yi Wang 已提交
141 142
}  // namespace platform
}  // namespace paddle
S
sneaxiy 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195

namespace std {

template <>
struct hash<::paddle::platform::CPUPlace> {
  using argument_type = ::paddle::platform::CPUPlace;
  using result_type = size_t;

  constexpr inline result_type operator()(const argument_type &place) const {
    return static_cast<result_type>(-1);
  }
};

template <>
struct hash<::paddle::platform::CUDAPlace> {
  using argument_type = ::paddle::platform::CUDAPlace;
  using result_type = size_t;

  inline result_type operator()(const argument_type &place) const {
    return static_cast<result_type>(place.device);
  }
};

template <>
struct hash<::paddle::platform::CUDAPinnedPlace> {
  using argument_type = ::paddle::platform::CUDAPinnedPlace;
  using result_type = size_t;

  constexpr inline result_type operator()(const argument_type &place) const {
    return static_cast<result_type>(-2);
  }
};

namespace {  // NOLINT
struct PlaceHashVisitor : public boost::static_visitor<size_t> {
  template <typename Place>
  inline size_t operator()(const Place &place) const {
    return std::hash<Place>()(place);
  }
};
}

template <>
struct hash<::paddle::platform::Place> {
  using argument_type = ::paddle::platform::Place;
  using result_type = size_t;

  inline result_type operator()(const argument_type &place) const {
    return boost::apply_visitor(PlaceHashVisitor(), place);
  }
};

}  // namespace std