ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
BinaryEW.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 <vector>
13 
16 
17 namespace cloudViewer {
18 namespace core {
19 namespace kernel {
20 
21 const std::unordered_set<BinaryEWOpCode, utility::hash_enum_class>
28  };
29 
30 void BinaryEW(const Tensor& lhs,
31  const Tensor& rhs,
32  Tensor& dst,
33  BinaryEWOpCode op_code) {
34  // lhs, rhs and dst must be on the same device.
35  for (auto device :
36  std::vector<Device>({rhs.GetDevice(), dst.GetDevice()})) {
37  if (lhs.GetDevice() != device) {
38  utility::LogError("Device mismatch {} != {}.",
39  lhs.GetDevice().ToString(), device.ToString());
40  }
41  }
42 
43  // broadcast(lhs.shape, rhs.shape) must be dst.shape.
44  const SizeVector broadcasted_input_shape =
46  if (broadcasted_input_shape != dst.GetShape()) {
48  "The broadcasted input shape {} does not match the output "
49  "shape {}.",
50  broadcasted_input_shape, dst.GetShape());
51  }
52 
53  if (lhs.IsCPU()) {
54  BinaryEWCPU(lhs, rhs, dst, op_code);
55  } else if (lhs.IsSYCL()) {
56 #ifdef BUILD_SYCL_MODULE
57  BinaryEWSYCL(lhs, rhs, dst, op_code);
58 #else
59  utility::LogError("Not compiled with SYCL, but SYCL device is used.");
60 #endif
61  } else if (lhs.IsCUDA()) {
62 #ifdef BUILD_CUDA_MODULE
63  BinaryEWCUDA(lhs, rhs, dst, op_code);
64 #else
65  utility::LogError("Not compiled with CUDA, but CUDA device is used.");
66 #endif
67  } else {
68  utility::LogError("BinaryEW: Unimplemented device");
69  }
70 }
71 
72 } // namespace kernel
73 } // namespace core
74 } // namespace cloudViewer
std::string ToString() const
Returns string representation of device, e.g. "CPU:0", "CUDA:0".
Definition: Device.cpp:89
bool IsCUDA() const
Definition: Device.h:99
bool IsCPU() const
Definition: Device.h:95
Device GetDevice() const override
Definition: Tensor.cpp:1435
SizeVector GetShape() const
Definition: Tensor.h:1127
#define LogError(...)
Definition: Logging.h:60
void BinaryEWCPU(const Tensor &lhs, const Tensor &rhs, Tensor &dst, BinaryEWOpCode op_code)
const std::unordered_set< BinaryEWOpCode, utility::hash_enum_class > s_boolean_binary_ew_op_codes
Definition: BinaryEW.cpp:22
void BinaryEW(const Tensor &lhs, const Tensor &rhs, Tensor &dst, BinaryEWOpCode op_code)
Definition: BinaryEW.cpp:30
void BinaryEWSYCL(const Tensor &lhs, const Tensor &rhs, Tensor &dst, BinaryEWOpCode op_code)
SizeVector BroadcastedShape(const SizeVector &l_shape, const SizeVector &r_shape)
Returns the broadcasted shape of two shapes.
Definition: ShapeUtil.cpp:56
Generic file read and write utility for python interface.