Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.16.0
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Device.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018-2021 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <string>
30 #include <vector>
31 
32 namespace open3d {
33 namespace core {
34 
37 class Device {
38 public:
40  enum class DeviceType {
41  CPU = 0,
42  CUDA = 1,
43  SYCL = 2, // SYCL gpu_selector().
44  };
45 
47  Device() = default;
48 
50  explicit Device(DeviceType device_type, int device_id);
51 
53  explicit Device(const std::string& device_type, int device_id);
54 
56  explicit Device(const std::string& type_colon_id);
57 
58  bool operator==(const Device& other) const;
59 
60  bool operator!=(const Device& other) const;
61 
62  bool operator<(const Device& other) const;
63 
65  inline bool IsCPU() const { return device_type_ == DeviceType::CPU; }
66 
68  inline bool IsCUDA() const { return device_type_ == DeviceType::CUDA; }
69 
71  inline bool IsSYCL() const { return device_type_ == DeviceType::SYCL; }
72 
74  std::string ToString() const;
75 
77  inline DeviceType GetType() const { return device_type_; }
78 
80  inline int GetID() const { return device_id_; }
81 
83  bool IsAvailable() const;
84 
86  static std::vector<Device> GetAvailableDevices();
87 
89  static std::vector<Device> GetAvailableCPUDevices();
90 
92  static std::vector<Device> GetAvailableCUDADevices();
93 
95  static std::vector<Device> GetAvailableSYCLDevices();
96 
98  static void PrintAvailableDevices();
99 
100 protected:
102  int device_id_ = 0;
103 };
104 
107 class IsDevice {
108 public:
109  IsDevice() = default;
110  virtual ~IsDevice() = default;
111 
112  virtual core::Device GetDevice() const = 0;
113 
114  inline bool IsCPU() const {
115  return GetDevice().GetType() == Device::DeviceType::CPU;
116  }
117 
118  inline bool IsCUDA() const {
119  return GetDevice().GetType() == Device::DeviceType::CUDA;
120  }
121 };
122 
123 } // namespace core
124 } // namespace open3d
125 
126 namespace std {
127 template <>
128 struct hash<open3d::core::Device> {
130  return std::hash<std::string>{}(device.ToString());
131  }
132 };
133 } // namespace std
std::size_t operator()(const open3d::core::Device &device) const
Definition: Device.h:129
bool IsAvailable() const
Returns true if the device is available.
Definition: Device.cpp:126
static std::vector< Device > GetAvailableCPUDevices()
Returns a vector of available CPU device.
Definition: Device.cpp:146
bool operator!=(const Device &other) const
Definition: Device.cpp:99
Definition: Device.h:126
DeviceType GetType() const
Returns type of the device, e.g. DeviceType::CPU, DeviceType::CUDA.
Definition: Device.h:77
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:68
static std::vector< Device > GetAvailableSYCLDevices()
Returns a vector of available SYCL device.
Definition: Device.cpp:158
bool operator==(const Device &other) const
Definition: Device.cpp:94
static void PrintAvailableDevices()
Print all available devices.
Definition: Device.cpp:162
int device_id_
Definition: Device.h:102
DeviceType device_type_
Definition: Device.h:101
bool IsCUDA() const
Definition: Device.h:118
DeviceType
Type for device.
Definition: Device.h:40
Definition: Device.h:107
Definition: Device.h:37
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:71
static std::vector< Device > GetAvailableDevices()
Returns a vector of available devices.
Definition: Device.cpp:135
bool operator<(const Device &other) const
Definition: Device.cpp:103
static std::vector< Device > GetAvailableCUDADevices()
Returns a vector of available CUDA device.
Definition: Device.cpp:150
bool IsCPU() const
Definition: Device.h:114
Device()=default
Default constructor -> "CPU:0".
Definition: PinholeCameraIntrinsic.cpp:35
bool IsCPU() const
Returns true iff device type is CPU.
Definition: Device.h:65
const char const char value recording_handle imu_sample recording_handle uint8_t size_t data_size k4a_record_configuration_t config target_format k4a_capture_t capture_handle k4a_imu_sample_t imu_sample playback_handle k4a_logging_message_cb_t void min_level device_handle k4a_imu_sample_t timeout_in_ms capture_handle capture_handle capture_handle image_handle temperature_c k4a_image_t image_handle uint8_t image_handle image_handle image_handle image_handle image_handle timestamp_usec white_balance image_handle k4a_device_configuration_t config device_handle char size_t serial_number_size bool int32_t int32_t int32_t int32_t k4a_color_control_mode_t default_mode value const const k4a_calibration_t calibration char size_t
Definition: K4aPlugin.cpp:734
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:107
int GetID() const
Returns the device index (within the same device type).
Definition: Device.h:80