ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ParallelForSYCL.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 <Logging.h>
11 
12 #include <cstdint>
13 #include <type_traits>
14 
18 
19 namespace cloudViewer {
20 namespace core {
21 
23 template <typename Functor, typename... FuncArgs>
24 void ParallelForSYCL(const Device& device,
26  FuncArgs... func_args) {
27  if (!device.IsSYCL()) {
28  utility::LogError("ParallelFor for SYCL cannot run on device {}.",
29  device.ToString());
30  }
31  int64_t n = indexer.NumWorkloads();
32  if (n == 0) {
33  return;
34  }
35  auto queue = sy::SYCLContext::GetInstance().GetDefaultQueue(device);
37  queue.parallel_for<Functor>(n, [indexer, func_args...](int64_t i) {
38  Functor ef(indexer, func_args...);
39  ef(i);
40  }).wait_and_throw();
41 }
42 
44 template <typename Functor, typename... FuncArgs>
45 void ParallelForSYCL(const Device& device,
46  int64_t num_workloads,
47  FuncArgs... func_args) {
48  if (!device.IsSYCL()) {
49  utility::LogError("ParallelFor for SYCL cannot run on device {}.",
50  device.ToString());
51  }
52  if (num_workloads == 0) {
53  return;
54  }
55  auto queue = sy::SYCLContext::GetInstance().GetDefaultQueue(device);
57  queue.parallel_for<Functor>(num_workloads, [func_args...](int64_t i) {
58  Functor ef(func_args...);
59  ef(i);
60  }).wait_and_throw();
61 }
62 
63 } // namespace core
64 } // namespace cloudViewer
Indexer indexer
SYCL queue manager.
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:89
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:52
int64_t NumWorkloads() const
Definition: Indexer.cpp:406
static SYCLContext & GetInstance()
Get singleton instance.
Definition: SYCLContext.cpp:25
sycl::queue GetDefaultQueue(const Device &device)
Get the default SYCL queue given an CloudViewer device.
Definition: SYCLContext.cpp:43
#define LogError(...)
Definition: Logging.h:60
void ParallelForSYCL(const Device &device, Indexer indexer, FuncArgs... func_args)
Run a function in parallel with SYCL.
Generic file read and write utility for python interface.