Open3D (C++ API)  0.11.0
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 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/Console.h"
32 #include "open3d/utility/Helper.h"
33 
34 namespace open3d {
35 namespace core {
36 
39 class Device {
40 public:
42  enum class DeviceType { CPU = 0, CUDA = 1 };
43 
47  }
48 
50  Device(DeviceType device_type, int device_id)
51  : device_type_(device_type), device_id_(device_id) {
53  }
54 
56  Device(const std::string& device_type, int device_id)
57  : Device(device_type + ":" + std::to_string(device_id)) {}
58 
60  Device(const std::string& type_colon_id)
61  : device_type_(StringToDeviceType(type_colon_id)),
62  device_id_(StringToDeviceId(type_colon_id)) {
64  }
65 
66  bool operator==(const Device& other) const {
67  return this->device_type_ == other.device_type_ &&
68  this->device_id_ == other.device_id_;
69  }
70 
71  bool operator!=(const Device& other) const { return !operator==(other); }
72 
73  std::string ToString() const {
74  std::string str = "";
75  switch (device_type_) {
76  case DeviceType::CPU:
77  str += "CPU";
78  break;
79  case DeviceType::CUDA:
80  str += "CUDA";
81  break;
82  default:
83  utility::LogError("Unsupported device type");
84  }
85  str += ":" + std::to_string(device_id_);
86  return str;
87  }
88 
89  DeviceType GetType() const { return device_type_; }
90 
91  int GetID() const { return device_id_; }
92 
93 protected:
95  if (device_type_ == DeviceType::CPU && device_id_ != 0) {
96  utility::LogError("CPU has device_id {}, but it must be 0.",
97  device_id_);
98  }
99  }
100 
101  static DeviceType StringToDeviceType(const std::string& type_colon_id) {
102  std::vector<std::string> tokens;
103  utility::SplitString(tokens, type_colon_id, ":", true);
104  if (tokens.size() == 2) {
105  std::string device_name_lower = utility::ToLower(tokens[0]);
106  if (device_name_lower == "cpu") {
107  return DeviceType::CPU;
108  } else if (device_name_lower == "cuda") {
109  return DeviceType::CUDA;
110  } else {
111  utility::LogError("Invalid device string {}.", type_colon_id);
112  }
113  } else {
114  utility::LogError("Invalid device string {}.", type_colon_id);
115  }
116  }
117 
118  static int StringToDeviceId(const std::string& type_colon_id) {
119  std::vector<std::string> tokens;
120  utility::SplitString(tokens, type_colon_id, ":", true);
121  if (tokens.size() == 2) {
122  return std::stoi(tokens[1]);
123  } else {
124  utility::LogError("Invalid device string {}.", type_colon_id);
125  }
126  }
127 
128 protected:
131 };
132 
133 } // namespace core
134 } // namespace open3d
Device()
Defalut constructor.
Definition: Device.h:45
bool operator!=(const Device &other) const
Definition: Device.h:71
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
Definition: Optional.h:922
DeviceType GetType() const
Definition: Device.h:89
bool operator==(const Device &other) const
Definition: Device.h:66
int device_id_
Definition: Device.h:130
Device(const std::string &type_colon_id)
Constructor from string, e.g. "CUDA:0".
Definition: Device.h:60
DeviceType device_type_
Definition: Device.h:129
static int StringToDeviceId(const std::string &type_colon_id)
Definition: Device.h:118
DeviceType
Type for device.
Definition: Device.h:42
void AssertCPUDeviceIDIsZero()
Definition: Device.h:94
Definition: Device.h:39
static DeviceType StringToDeviceType(const std::string &type_colon_id)
Definition: Device.h:101
Definition: PinholeCameraIntrinsic.cpp:35
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters, bool trim_empty_str)
Definition: Helper.cpp:43
Device(DeviceType device_type, int device_id)
Constructor with device specified.
Definition: Device.h:50
std::string ToString() const
Definition: Device.h:73
Device(const std::string &device_type, int device_id)
Constructor from device type string and device id.
Definition: Device.h:56
int GetID() const
Definition: Device.h:91
std::string ToLower(const std::string &str)
Convert string to the lower case.
Definition: Helper.cpp:72