ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Device.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Helper.h>
11 #include <Logging.h>
12 
13 #include <string>
14 #include <vector>
15 
18 
19 namespace cloudViewer {
20 namespace core {
21 
22 static Device::DeviceType StringToDeviceType(const std::string& type_colon_id) {
23  const std::vector<std::string> tokens =
24  utility::SplitString(type_colon_id, ":", true);
25  if (tokens.size() == 2) {
26  std::string device_type_lower = utility::ToLower(tokens[0]);
27  if (device_type_lower == "cpu") {
29  } else if (device_type_lower == "cuda") {
31  } else if (device_type_lower == "sycl") {
33  } else {
35  "Invalid device string {}. Valid device strings are like "
36  "\"CPU:0\", \"CUDA:1\" or \"SYCL:0\"",
37  type_colon_id);
38  }
39  } else {
41  "Invalid device string {}. Valid device strings are like "
42  "\"CPU:0\", \"CUDA:1\" or \"SYCL:0\"",
43  type_colon_id);
44  }
45 }
46 
47 static int StringToDeviceId(const std::string& type_colon_id) {
48  const std::vector<std::string> tokens =
49  utility::SplitString(type_colon_id, ":", true);
50  if (tokens.size() == 2) {
51  return std::stoi(tokens[1]);
52  } else {
54  "Invalid device string {}. Valid device strings are like "
55  "\"CPU:0\", \"CUDA:1\" or \"SYCL:0\"",
56  type_colon_id);
57  }
58 }
59 
60 Device::Device(DeviceType device_type, int device_id)
61  : device_type_(device_type), device_id_(device_id) {
62  // Sanity checks.
63  if (device_type_ == DeviceType::CPU && device_id_ != 0) {
64  utility::LogError("CPU has device_id {}, but it must be 0.",
65  device_id_);
66  }
67 }
68 
69 Device::Device(const std::string& device_type, int device_id)
70  : Device(device_type + ":" + std::to_string(device_id)) {}
71 
72 Device::Device(const std::string& type_colon_id)
73  : Device(StringToDeviceType(type_colon_id),
74  StringToDeviceId(type_colon_id)) {}
75 
76 bool Device::operator==(const Device& other) const {
77  return this->device_type_ == other.device_type_ &&
78  this->device_id_ == other.device_id_;
79 }
80 
81 bool Device::operator!=(const Device& other) const {
82  return !operator==(other);
83 }
84 
85 bool Device::operator<(const Device& other) const {
86  return ToString() < other.ToString();
87 }
88 
89 std::string Device::ToString() const {
90  std::string str = "";
91  switch (device_type_) {
92  case DeviceType::CPU:
93  str += "CPU";
94  break;
95  case DeviceType::CUDA:
96  str += "CUDA";
97  break;
98  case DeviceType::SYCL:
99  str += "SYCL";
100  break;
101  default:
102  utility::LogError("Unsupported device type");
103  }
104  str += ":" + std::to_string(device_id_);
105  return str;
106 }
107 
108 bool Device::IsAvailable() const {
109  for (const Device& device : GetAvailableDevices()) {
110  if (device == *this) {
111  return true;
112  }
113  }
114  return false;
115 }
116 
117 std::vector<Device> Device::GetAvailableDevices() {
118  const std::vector<Device> cpu_devices = GetAvailableCPUDevices();
119  const std::vector<Device> cuda_devices = GetAvailableCUDADevices();
120  const std::vector<Device> sycl_devices = GetAvailableSYCLDevices();
121  std::vector<Device> devices;
122  devices.insert(devices.end(), cpu_devices.begin(), cpu_devices.end());
123  devices.insert(devices.end(), cuda_devices.begin(), cuda_devices.end());
124  devices.insert(devices.end(), sycl_devices.begin(), sycl_devices.end());
125  return devices;
126 }
127 
128 std::vector<Device> Device::GetAvailableCPUDevices() {
129  return {Device(DeviceType::CPU, 0)};
130 }
131 
132 std::vector<Device> Device::GetAvailableCUDADevices() {
133  std::vector<Device> devices;
134  for (int i = 0; i < cuda::DeviceCount(); i++) {
135  devices.push_back(Device(DeviceType::CUDA, i));
136  }
137  return devices;
138 }
139 
140 std::vector<Device> Device::GetAvailableSYCLDevices() {
142 }
143 
145  for (const auto& device : GetAvailableDevices()) {
146  utility::LogInfo("Device(\"{}\")", device.ToString());
147  }
148 }
149 
150 } // namespace core
151 } // namespace cloudViewer
Common CUDA utilities.
Common SYCL utilities.
static std::vector< Device > GetAvailableSYCLDevices()
Returns a vector of available SYCL device.
Definition: Device.cpp:140
Device()=default
Default constructor -> "CPU:0".
static std::vector< Device > GetAvailableCUDADevices()
Returns a vector of available CUDA device.
Definition: Device.cpp:132
DeviceType device_type_
Definition: Device.h:82
static std::vector< Device > GetAvailableDevices()
Returns a vector of available devices.
Definition: Device.cpp:117
DeviceType
Type for device.
Definition: Device.h:21
bool operator==(const Device &other) const
Definition: Device.cpp:76
static std::vector< Device > GetAvailableCPUDevices()
Returns a vector of available CPU device.
Definition: Device.cpp:128
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:89
bool operator!=(const Device &other) const
Definition: Device.cpp:81
static void PrintAvailableDevices()
Print all available devices.
Definition: Device.cpp:144
bool operator<(const Device &other) const
Definition: Device.cpp:85
bool IsAvailable() const
Returns true if the device is available.
Definition: Device.cpp:108
#define LogInfo(...)
Definition: Logging.h:81
#define LogError(...)
Definition: Logging.h:60
Helper functions for the ml ops.
std::vector< Device > GetAvailableSYCLDevices()
Return a list of available SYCL devices.
Definition: SYCLUtils.cpp:230
static Device::DeviceType StringToDeviceType(const std::string &type_colon_id)
Definition: Device.cpp:22
static int StringToDeviceId(const std::string &type_colon_id)
Definition: Device.cpp:47
void SplitString(std::vector< std::string > &tokens, const std::string &str, const std::string &delimiters=" ", bool trim_empty_str=true)
Definition: Helper.cpp:197
std::string ToLower(const std::string &s)
Convert string to the lower case.
Definition: Helper.cpp:242
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20
Definition: Eigen.h:85