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

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

Y
Yi Wang 已提交
17
#include <iostream>
Y
Yi Wang 已提交
18 19
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/variant.h"
Y
Yi Wang 已提交
20

Y
Yi Wang 已提交
21 22
namespace paddle {
namespace platform {
Y
Yi Wang 已提交
23

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

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

D
dzhwinter 已提交
34 35 36
struct CUDAPlace {
  CUDAPlace() : CUDAPlace(0) {}
  explicit CUDAPlace(int d) : device(d) {}
Y
Yi Wang 已提交
37

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

  int device;
};

C
chengduoZH 已提交
48 49 50 51 52 53 54 55
struct CUDAPinnedPlace {
  CUDAPinnedPlace() {}

  // needed for variant equality comparison
  inline bool operator==(const CUDAPinnedPlace &) const { return true; }
  inline bool operator!=(const CUDAPinnedPlace &) const { return false; }
};

D
dzhwinter 已提交
56
struct IsCUDAPlace : public boost::static_visitor<bool> {
57
  bool operator()(const CPUPlace &) const { return false; }
D
dzhwinter 已提交
58
  bool operator()(const CUDAPlace &gpu) const { return true; }
C
chengduoZH 已提交
59
  bool operator()(const CUDAPinnedPlace &) const { return false; }
T
tensor-tang 已提交
60 61
};

C
chengduoZH 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
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 已提交
75

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

Y
Yi Wang 已提交
78 79
void set_place(const Place &);
const Place &get_place();
Y
Yi Wang 已提交
80

D
dzhwinter 已提交
81
const CUDAPlace default_gpu();
82
const CPUPlace default_cpu();
C
chengduoZH 已提交
83
const CUDAPinnedPlace default_cuda_pinned();
Y
Yi Wang 已提交
84

Y
Yi Wang 已提交
85 86
bool is_gpu_place(const Place &);
bool is_cpu_place(const Place &);
C
chengduoZH 已提交
87
bool is_cuda_pinned_place(const Place &);
Y
Yi Wang 已提交
88
bool places_are_same_class(const Place &, const Place &);
89
bool is_same_place(const Place &, const Place &);
Y
Yi Wang 已提交
90

Y
Yu Yang 已提交
91 92 93 94 95 96 97 98 99 100 101 102
struct PlaceHash {
  std::size_t operator()(const Place &p) const {
    constexpr size_t num_dev_bits = 4;
    std::hash<int> ihash;
    size_t dev_id = 0;
    if (is_gpu_place(p)) {
      dev_id = boost::get<CUDAPlace>(p).device;
    }
    return ihash(dev_id << num_dev_bits | p.which());
  }
};

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

Y
Yang Yu 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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 已提交
123 124 125 126 127

  typename Visitor::result_type operator()(
      const CUDAPinnedPlace &cuda_pinned) const {
    return visitor_(cuda_pinned);
  }
Y
Yang Yu 已提交
128 129 130 131 132 133 134 135
};

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

Y
Yi Wang 已提交
136 137
}  // namespace platform
}  // namespace paddle