ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Device.h
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 
8 #pragma once
9 
10 #include <string>
11 #include <vector>
12 
13 namespace cloudViewer {
14 namespace core {
15 
18 class Device {
19 public:
21  enum class DeviceType {
22  CPU = 0,
23  CUDA = 1,
24  SYCL = 2, // SYCL gpu_selector_v.
25  };
26 
28  Device() = default;
29 
31  explicit Device(DeviceType device_type, int device_id);
32 
34  explicit Device(const std::string& device_type, int device_id);
35 
37  explicit Device(const std::string& type_colon_id);
38 
39  bool operator==(const Device& other) const;
40 
41  bool operator!=(const Device& other) const;
42 
43  bool operator<(const Device& other) const;
44 
46  inline bool IsCPU() const { return device_type_ == DeviceType::CPU; }
47 
49  inline bool IsCUDA() const { return device_type_ == DeviceType::CUDA; }
50 
52  inline bool IsSYCL() const { return device_type_ == DeviceType::SYCL; }
53 
55  std::string ToString() const;
56 
58  inline DeviceType GetType() const { return device_type_; }
59 
61  inline int GetID() const { return device_id_; }
62 
64  bool IsAvailable() const;
65 
67  static std::vector<Device> GetAvailableDevices();
68 
70  static std::vector<Device> GetAvailableCPUDevices();
71 
73  static std::vector<Device> GetAvailableCUDADevices();
74 
76  static std::vector<Device> GetAvailableSYCLDevices();
77 
79  static void PrintAvailableDevices();
80 
81 protected:
83  int device_id_ = 0;
84 };
85 
88 class IsDevice {
89 public:
90  IsDevice() = default;
91  virtual ~IsDevice() = default;
92 
93  virtual core::Device GetDevice() const = 0;
94 
95  inline bool IsCPU() const {
97  }
98 
99  inline bool IsCUDA() const {
101  }
102 
103  inline bool IsSYCL() const {
105  }
106 };
107 
108 } // namespace core
109 } // namespace cloudViewer
110 
111 namespace std {
112 template <>
113 struct hash<cloudViewer::core::Device> {
114  std::size_t operator()(const cloudViewer::core::Device& device) const {
115  return std::hash<std::string>{}(device.ToString());
116  }
117 };
118 
119 template <>
120 struct less<cloudViewer::core::Device> {
122  const cloudViewer::core::Device& rhs) const {
123  return lhs.ToString() < rhs.ToString();
124  }
125 };
126 } // namespace std
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
static std::vector< Device > GetAvailableSYCLDevices()
Returns a vector of available SYCL device.
Definition: Device.cpp:140
DeviceType GetType() const
Returns type of the device, e.g. DeviceType::CPU, DeviceType::CUDA.
Definition: Device.h:58
Device()=default
Default constructor -> "CPU:0".
static std::vector< Device > GetAvailableCUDADevices()
Returns a vector of available CUDA device.
Definition: Device.cpp:132
bool IsCPU() const
Returns true iff device type is CPU.
Definition: Device.h:46
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
int GetID() const
Returns the device index (within the same device type).
Definition: Device.h:61
bool operator!=(const Device &other) const
Definition: Device.cpp:81
static void PrintAvailableDevices()
Print all available devices.
Definition: Device.cpp:144
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:52
bool operator<(const Device &other) const
Definition: Device.cpp:85
bool IsAvailable() const
Returns true if the device is available.
Definition: Device.cpp:108
bool IsCUDA() const
Definition: Device.h:99
bool IsCPU() const
Definition: Device.h:95
virtual ~IsDevice()=default
virtual core::Device GetDevice() const =0
Generic file read and write utility for python interface.
Definition: Eigen.h:85
std::size_t operator()(const cloudViewer::core::Device &device) const
Definition: Device.h:114
bool operator()(const cloudViewer::core::Device &lhs, const cloudViewer::core::Device &rhs) const
Definition: Device.h:121