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