Loading [MathJax]/extensions/TeX/AMSsymbols.js
Open3D (C++ API)  0.14.1
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 #include <string>
29 #include <vector>
30 
31 #include "open3d/utility/Helper.h"
32 #include "open3d/utility/Logging.h"
33 
34 namespace open3d {
35 namespace core {
36 
39 class Device {
40 public:
42  enum class DeviceType { CPU = 0, CUDA = 1 };
43 
45  Device() = default;
46 
48  explicit Device(DeviceType device_type, int device_id)
49  : device_type_(device_type), device_id_(device_id) {
51  }
52 
54  explicit Device(const std::string& device_type, int device_id)
55  : Device(device_type + ":" + std::to_string(device_id)) {}
56 
58  explicit Device(const std::string& type_colon_id)
59  : device_type_(StringToDeviceType(type_colon_id)),
60  device_id_(StringToDeviceId(type_colon_id)) {
62  }
63 
64  bool operator==(const Device& other) const {
65  return this->device_type_ == other.device_type_ &&
66  this->device_id_ == other.device_id_;
67  }
68 
69  bool operator!=(const Device& other) const { return !operator==(other); }
70 
71  bool operator<(const Device& other) const {
72  return ToString() < other.ToString();
73  }
74 
75  std::string ToString() const {
76  std::string str = "";
77  switch (device_type_) {
78  case DeviceType::CPU:
79  str += "CPU";
80  break;
81  case DeviceType::CUDA:
82  str += "CUDA";
83  break;
84  default:
85  utility::LogError("Unsupported device type");
86  }
87  str += ":" + std::to_string(device_id_);
88  return str;
89  }
90 
91  DeviceType GetType() const { return device_type_; }
92 
93  int GetID() const { return device_id_; }
94 
95 protected:
97  if (device_type_ == DeviceType::CPU && device_id_ != 0) {
98  utility::LogError("CPU has device_id {}, but it must be 0.",
99  device_id_);
100  }
101  }
102 
103  static DeviceType StringToDeviceType(const std::string& type_colon_id) {
104  std::vector<std::string> tokens =
105  utility::SplitString(type_colon_id, ":", true);
106  if (tokens.size() == 2) {
107  std::string device_name_lower = utility::ToLower(tokens[0]);
108  if (device_name_lower == "cpu") {
109  return DeviceType::CPU;
110  } else if (device_name_lower == "cuda") {
111  return DeviceType::CUDA;
112  } else {
113  utility::LogError("Invalid device string {}.", type_colon_id);
114  }
115  } else {
116  utility::LogError("Invalid device string {}.", type_colon_id);
117  }
118  }
119 
120  static int StringToDeviceId(const std::string& type_colon_id) {
121  std::vector<std::string> tokens =
122  utility::SplitString(type_colon_id, ":", true);
123  if (tokens.size() == 2) {
124  return std::stoi(tokens[1]);
125  } else {
126  utility::LogError("Invalid device string {}.", type_colon_id);
127  }
128  }
129 
130 protected:
132  int device_id_ = 0;
133 };
134 
135 } // namespace core
136 } // namespace open3d
137 
138 namespace std {
139 template <>
140 struct hash<open3d::core::Device> {
142  return std::hash<std::string>{}(device.ToString());
143  }
144 };
145 } // namespace std
std::size_t operator()(const open3d::core::Device &device) const
Definition: Device.h:141
bool operator!=(const Device &other) const
Definition: Device.h:69
std::vector< std::string > SplitString(const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:45
Definition: Device.h:138
DeviceType GetType() const
Definition: Device.h:91
bool operator==(const Device &other) const
Definition: Device.h:64
int device_id_
Definition: Device.h:132
Device(const std::string &type_colon_id)
Constructor from string, e.g. "CUDA:0".
Definition: Device.h:58
DeviceType device_type_
Definition: Device.h:131
static int StringToDeviceId(const std::string &type_colon_id)
Definition: Device.h:120
DeviceType
Type for device.
Definition: Device.h:42
void AssertCPUDeviceIDIsZero()
Definition: Device.h:96
Definition: Device.h:39
bool operator<(const Device &other) const
Definition: Device.h:71
Device()=default
Default constructor.
static DeviceType StringToDeviceType(const std::string &type_colon_id)
Definition: Device.h:103
Definition: PinholeCameraIntrinsic.cpp:35
Device(DeviceType device_type, int device_id)
Constructor with device specified.
Definition: Device.h:48
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:724
std::string ToString() const
Definition: Device.h:75
Device(const std::string &device_type, int device_id)
Constructor from device type string and device id.
Definition: Device.h:54
#define LogError(...)
Definition: Logging.h:72
int GetID() const
Definition: Device.h:93
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:102