ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
UnaryEW.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 
14 
15 namespace cloudViewer {
16 namespace core {
17 namespace kernel {
18 
19 void UnaryEW(const Tensor& src, Tensor& dst, UnaryEWOpCode op_code) {
20  // Check shape
22  utility::LogError("Shape {} can not be broadcasted to {}.",
23  src.GetShape(), dst.GetShape());
24  }
25 
26  // Check dtype compatibility
27  const auto float_only_ops = {UnaryEWOpCode::Sqrt, UnaryEWOpCode::Sin,
31  Dtype src_dtype = src.GetDtype();
32  if (std::find(float_only_ops.begin(), float_only_ops.end(), op_code) !=
33  float_only_ops.end() &&
34  src_dtype != core::Float32 && src_dtype != core::Float64) {
35  utility::LogError("Only supports Float32 and Float64, but {} is used.",
36  src_dtype.ToString());
37  }
38 
39  // Dispatch to device
40  Device src_device = src.GetDevice();
41  Device dst_device = dst.GetDevice();
42  if (src_device != dst_device) {
43  utility::LogError("Source device {} != destination device {}.",
44  src_device.ToString(), dst_device.ToString());
45  }
46 
47  if (src_device.IsCPU()) {
48  UnaryEWCPU(src, dst, op_code);
49  } else if (src_device.IsSYCL()) {
50 #ifdef BUILD_SYCL_MODULE
51  UnaryEWSYCL(src, dst, op_code);
52 #else
53  utility::LogError("Not compiled with SYCL, but SYCL device is used.");
54 #endif
55  } else if (src_device.IsCUDA()) {
56 #ifdef BUILD_CUDA_MODULE
57  UnaryEWCUDA(src, dst, op_code);
58 #else
59  utility::LogError("Not compiled with CUDA, but CUDA device is used.");
60 #endif
61  } else {
62  utility::LogError("UnaryEW Unimplemented device");
63  }
64 }
65 
66 void Copy(const Tensor& src, Tensor& dst) {
67  // Check shape
69  utility::LogError("Shape {} can not be broadcasted to {}.",
70  src.GetShape(), dst.GetShape());
71  }
72 
73  // Dispatch to device
74  Device src_device = src.GetDevice();
75  Device dst_device = dst.GetDevice();
76  if ((!src_device.IsCPU() && !src_device.IsCUDA() && !src_device.IsSYCL()) ||
77  (!dst_device.IsCPU() && !dst_device.IsCUDA() && !dst_device.IsSYCL())) {
78  utility::LogError("Copy: Unimplemented device");
79  }
80  if (src_device.IsCPU() && dst_device.IsCPU()) {
81  CopyCPU(src, dst);
82  } else if ((src_device.IsCPU() || src_device.IsCUDA()) &&
83  (dst_device.IsCPU() || dst_device.IsCUDA())) {
84 #ifdef BUILD_CUDA_MODULE
85  CopyCUDA(src, dst);
86 #else
87  utility::LogError("Not compiled with CUDA, but CUDA device is used.");
88 #endif
89  } else if ((src_device.IsCPU() || src_device.IsSYCL()) &&
90  (dst_device.IsCPU() || dst_device.IsSYCL())) {
91 #ifdef BUILD_SYCL_MODULE
92  CopySYCL(src, dst);
93 #else
94  utility::LogError("Not compiled with SYCL, but SYCL device is used.");
95 #endif
96  } else {
97  utility::LogError("Copy: SYCL <-> CUDA Unimplemented device");
98  }
99 }
100 
101 } // namespace kernel
102 } // namespace core
103 } // namespace cloudViewer
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
bool IsCPU() const
Returns true iff device type is CPU.
Definition: Device.h:46
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
std::string ToString() const
Definition: Dtype.h:65
Dtype GetDtype() const
Definition: Tensor.h:1164
Device GetDevice() const override
Definition: Tensor.cpp:1435
SizeVector GetShape() const
Definition: Tensor.h:1127
#define LogError(...)
Definition: Logging.h:60
void UnaryEWCPU(const Tensor &src, Tensor &dst, UnaryEWOpCode op_code)
Definition: UnaryEWCPU.cpp:217
void Copy(const Tensor &src, Tensor &dst)
Definition: UnaryEW.cpp:66
void UnaryEWSYCL(const Tensor &src, Tensor &dst, UnaryEWOpCode op_code)
void UnaryEW(const Tensor &src, Tensor &dst, UnaryEWOpCode op_code)
Definition: UnaryEW.cpp:19
void CopySYCL(const Tensor &src, Tensor &dst)
void CopyCPU(const Tensor &src, Tensor &dst)
Definition: UnaryEWCPU.cpp:174
bool CanBeBrocastedToShape(const SizeVector &src_shape, const SizeVector &dst_shape)
Returns true if src_shape can be brocasted to dst_shape.
Definition: ShapeUtil.cpp:90
const Dtype Float64
Definition: Dtype.cpp:43
const Dtype Float32
Definition: Dtype.cpp:42
Generic file read and write utility for python interface.