place.h 2.8 KB
Newer Older
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
#pragma once
16

Y
Yi Wang 已提交
17
#include <iostream>
Y
Yu Yang 已提交
18

Y
Yu Yang 已提交
19
#include "paddle/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
};

T
tensor-tang 已提交
34
struct MKLDNNPlace {
T
tensor-tang 已提交
35 36 37 38 39 40 41
  MKLDNNPlace() {}

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

D
dzhwinter 已提交
42 43 44
struct CUDAPlace {
  CUDAPlace() : CUDAPlace(0) {}
  explicit CUDAPlace(int d) : device(d) {}
Y
Yi Wang 已提交
45

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

  int device;
};

D
dzhwinter 已提交
56 57 58
struct CUDNNPlace : public CUDAPlace {
  CUDNNPlace() : CUDAPlace() {}
  explicit CUDNNPlace(int d) : CUDAPlace(d) {}
D
dzhwinter 已提交
59 60
};

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

T
tensor-tang 已提交
68 69 70
struct IsMKLDNNPlace : public boost::static_visitor<bool> {
  bool operator()(const MKLDNNPlace &) const { return true; }
  bool operator()(const CPUPlace &) const { return false; }
D
dzhwinter 已提交
71
  bool operator()(const CUDAPlace &) const { return false; }
D
dzhwinter 已提交
72
  bool operator()(const CUDNNPlace &) const { return false; }
T
tensor-tang 已提交
73 74
};

D
dzhwinter 已提交
75
typedef boost::variant<CUDNNPlace, CUDAPlace, CPUPlace, MKLDNNPlace> Place;
Y
Yi Wang 已提交
76

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

D
dzhwinter 已提交
80
const CUDAPlace default_gpu();
81
const CPUPlace default_cpu();
T
tensor-tang 已提交
82
const MKLDNNPlace default_mkldnn();
Y
Yi Wang 已提交
83

Y
Yi Wang 已提交
84 85
bool is_gpu_place(const Place &);
bool is_cpu_place(const Place &);
T
tensor-tang 已提交
86
bool is_mkldnn_place(const Place &);
Y
Yi Wang 已提交
87
bool places_are_same_class(const Place &, const Place &);
Y
Yi Wang 已提交
88

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

Y
Yi Wang 已提交
91 92
}  // namespace platform
}  // namespace paddle