ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SYCLContext.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 <Logging.h>
11 
12 #include <array>
13 #include <cstdlib>
14 #include <sstream>
15 #include <sycl/sycl.hpp>
16 
18 
19 namespace cloudViewer {
20 namespace core {
21 namespace sy {
22 
23 CLOUDVIEWER_DLL_LOCAL std::string GetDeviceTypeName(const sycl::device &device);
24 
26  static thread_local SYCLContext instance;
27  return instance;
28 }
29 
30 bool SYCLContext::IsAvailable() { return devices_.size() > 0; }
31 
33  return devices_.find(device) != devices_.end();
34 }
35 std::vector<Device> SYCLContext::GetAvailableSYCLDevices() {
36  std::vector<Device> device_vec;
37  for (const auto &device : devices_) {
38  device_vec.push_back(device.first);
39  }
40  return device_vec;
41 }
42 
43 sycl::queue SYCLContext::GetDefaultQueue(const Device &device) {
44  return devices_.at(device).queue;
45 }
46 
47 SYCLDevice::SYCLDevice(const sycl::device &sycl_device) {
48  namespace sid = sycl::info::device;
49  device = sycl_device;
50  queue = sycl::queue(device);
51  name = device.get_info<sid::name>();
53  max_work_group_size = device.get_info<sid::max_work_group_size>();
54  auto aspects = device.get_info<sid::aspects>();
55  fp64 = std::find(aspects.begin(), aspects.end(), sycl::aspect::fp64) !=
56  aspects.end();
57  if (!fp64) {
59  "SYCL device {} does not support double precision. Use env "
60  "vars 'OverrideDefaultFP64Settings=1' "
61  "'IGC_EnableDPEmulation=1' to enable double precision "
62  "emulation on Intel GPUs.",
63  name);
64  }
66  std::find(aspects.begin(), aspects.end(),
67  sycl::aspect::usm_device_allocations) != aspects.end();
70  "SYCL device {} does not support USM device allocations. "
71  "CloudViewer SYCL support may not work.",
72  name);
73  }
74 }
75 
76 SYCLContext::SYCLContext() {
77  // SYCL GPU.
78  // TODO: Currently we only support one GPU device.
79  try {
80  const sycl::device &sycl_device = sycl::device(sycl::gpu_selector_v);
81  const Device open3d_device = Device("SYCL:0");
82  devices_.emplace(open3d_device, sycl_device);
83  } catch (const sycl::exception &e) {
84  utility::LogWarning("SYCL GPU unavailable: {}", e.what());
85  }
86 
87  // SYCL CPU fallback (last device).
88  try {
89  if (devices_.size() == 0) {
90  // This could happen if the Intel GPGPU driver is not installed or
91  // if your CPU does not have integrated GPU.
93  "SYCL GPU device is not available, falling back to SYCL "
94  "host device.");
95  }
96  const sycl::device &sycl_device = sycl::device(sycl::cpu_selector_v);
97  const Device open3d_device =
98  Device("SYCL:" + std::to_string(devices_.size()));
99  devices_.emplace(open3d_device, sycl_device);
100  } catch (const sycl::exception &e) {
101  utility::LogWarning("SYCL CPU unavailable: {}", e.what());
102  }
103 
104  if (devices_.size() == 0) {
105  utility::LogWarning("No SYCL device is available.");
106  }
107 }
108 
109 } // namespace sy
110 } // namespace core
111 } // namespace cloudViewer
std::string name
#define CLOUDVIEWER_DLL_LOCAL
Definition: Macro.h:23
SYCL queue manager.
Common SYCL utilities.
bool IsDeviceAvailable(const Device &device)
Returns true if the specified SYCL device is available.
Definition: SYCLContext.cpp:32
static SYCLContext & GetInstance()
Get singleton instance.
Definition: SYCLContext.cpp:25
bool IsAvailable()
Returns true if there is at least one SYCL devices.
Definition: SYCLContext.cpp:30
sycl::queue GetDefaultQueue(const Device &device)
Get the default SYCL queue given an CloudViewer device.
Definition: SYCLContext.cpp:43
std::vector< Device > GetAvailableSYCLDevices()
Returns a list of all available SYCL devices.
Definition: SYCLContext.cpp:35
#define LogWarning(...)
Definition: Logging.h:72
CLOUDVIEWER_DLL_LOCAL std::string GetDeviceTypeName(const sycl::device &device)
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20
bool fp64
Double precision support, else need to emulate.
Definition: SYCLContext.h:34
SYCLDevice(const sycl::device &sycl_device)
Definition: SYCLContext.cpp:47
std::string device_type
cpu, gpu, host, acc, custom, unknown.
Definition: SYCLContext.h:30
size_t max_work_group_size
Preferred work group size.
Definition: SYCLContext.h:33
sycl::device device
SYCL device.
Definition: SYCLContext.h:31
std::string name
Fiendlly / descriptive name of the device.
Definition: SYCLContext.h:29
sycl::queue queue
Default queue for this device.
Definition: SYCLContext.h:32