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 
8 #include "core/Device.h"
9 
11 #include "pybind/core/core.h"
12 #include "pybind/docstring.h"
13 
14 namespace cloudViewer {
15 namespace core {
16 
17 void pybind_core_device(py::module &m) {
18  py::class_<Device> device(
19  m, "Device",
20  "Device context specifying device type and device id.");
21  device.def(py::init<>());
22  device.def(py::init<Device::DeviceType, int>());
23  device.def(py::init<const std::string &, int>());
24  device.def(py::init<const std::string &>());
25  device.def("__eq__", &Device::operator==);
26  device.def("__ene__", &Device::operator!=);
27  device.def("__repr__", [](const Device &d) {
28  std::string device_type;
29  switch (d.GetType()) {
30  case Device::DeviceType::CPU:
31  device_type = "CPU";
32  break;
33  case Device::DeviceType::CUDA:
34  device_type = "CUDA";
35  break;
36  case Device::DeviceType::SYCL:
37  device_type = "SYCL";
38  break;
39  default:
40  utility::LogError("Unknown device type");
41  return d.ToString();
42  }
43  return fmt::format("Device(\"{}\", {})", device_type, d.GetID());
44  });
45  device.def("__str__", &Device::ToString);
46  device.def("get_type", &Device::GetType);
47  device.def("get_id", &Device::GetID);
48  device.def(py::pickle(
49  [](const Device &d) {
50  return py::make_tuple(d.GetType(), d.GetID());
51  },
52  [](py::tuple t) {
53  if (t.size() != 2) {
55  "Cannot unpickle Device! Expecting a tuple of size "
56  "2.");
57  }
58  return Device(t[0].cast<Device::DeviceType>(),
59  t[1].cast<int>());
60  }));
61 
62  py::native_enum<Device::DeviceType>(device, "DeviceType", "enum.Enum")
63  .value("CPU", Device::DeviceType::CPU)
64  .value("CUDA", Device::DeviceType::CUDA)
65  .value("SYCL", Device::DeviceType::SYCL)
66  .finalize();
67 }
68 
69 } // namespace core
70 } // namespace cloudViewer
filament::Texture::InternalFormat format
DeviceType GetType() const
Returns type of the device, e.g. DeviceType::CPU, DeviceType::CUDA.
Definition: Device.h:58
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
#define LogError(...)
Definition: Logging.h:60
void pybind_core_device(py::module &m)
Definition: device.cpp:17
Generic file read and write utility for python interface.