ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Tri.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 
13 
14 namespace cloudViewer {
15 namespace core {
16 
17 static void CheckInput(const Tensor& A, const int diagonal) {
18  // Check dimensions.
19  SizeVector A_shape = A.GetShape();
20  if (A_shape.size() != 2) {
21  utility::LogError("Tensor must be 2D, but got {}D.", A_shape.size());
22  }
23  if (A_shape[0] == 0 || A_shape[1] == 0) {
25  "Tensor shapes should not contain dimensions with zero.");
26  }
27  if (diagonal <= -1 * A_shape[0] || diagonal >= A_shape[1]) {
29  "Diagonal parameter must be between [-{}, {}] for a matrix "
30  "with shape {}, but got {}.",
31  A_shape[0], A_shape[1], A.GetShape().ToString(), diagonal);
32  }
33 }
34 
35 void Triu(const Tensor& A, Tensor& output, const int diagonal) {
36  CheckInput(A, diagonal);
37  core::Device device = A.GetDevice();
38  output = core::Tensor::Zeros(A.GetShape(), A.GetDtype(), device);
39  if (device.IsCUDA()) {
40 #ifdef BUILD_CUDA_MODULE
41  CUDAScopedDevice scoped_device(device);
42  TriuCUDA(A.Contiguous(), output, diagonal);
43 #else
44  utility::LogError("Unimplemented device.");
45 #endif
46  } else if (device.IsSYCL()) {
47 #ifdef BUILD_SYCL_MODULE
48  TriuSYCL(A.Contiguous(), output, diagonal);
49 #else
50  utility::LogError("Unimplemented device.");
51 #endif
52  } else {
53  TriuCPU(A.Contiguous(), output, diagonal);
54  }
55 }
56 
57 void Tril(const Tensor& A, Tensor& output, const int diagonal) {
58  CheckInput(A, diagonal);
59  core::Device device = A.GetDevice();
60  output = core::Tensor::Zeros(A.GetShape(), A.GetDtype(), device);
61  if (device.IsCUDA()) {
62 #ifdef BUILD_CUDA_MODULE
63  CUDAScopedDevice scoped_device(device);
64  TrilCUDA(A.Contiguous(), output, diagonal);
65 #else
66  utility::LogError("Unimplemented device.");
67 #endif
68  } else if (device.IsSYCL()) {
69 #ifdef BUILD_SYCL_MODULE
70  TrilSYCL(A.Contiguous(), output, diagonal);
71 #else
72  utility::LogError("Unimplemented device.");
73 #endif
74  } else {
75  TrilCPU(A.Contiguous(), output, diagonal);
76  }
77 }
78 
79 void Triul(const Tensor& A, Tensor& upper, Tensor& lower, const int diagonal) {
80  CheckInput(A, diagonal);
81  core::Device device = A.GetDevice();
82  upper = core::Tensor::Zeros(A.GetShape(), A.GetDtype(), device);
83  lower = core::Tensor::Zeros(A.GetShape(), A.GetDtype(), device);
84  if (device.IsCUDA()) {
85 #ifdef BUILD_CUDA_MODULE
86  CUDAScopedDevice scoped_device(device);
87  TriulCUDA(A.Contiguous(), upper, lower, diagonal);
88 #else
89  utility::LogError("Unimplemented device.");
90 #endif
91  } else if (device.IsSYCL()) {
92 #ifdef BUILD_SYCL_MODULE
93  TriulSYCL(A.Contiguous(), upper, lower, diagonal);
94 #else
95  utility::LogError("Unimplemented device.");
96 #endif
97  } else {
98  TriulCPU(A.Contiguous(), upper, lower, diagonal);
99  }
100 }
101 
102 } // namespace core
103 } // namespace cloudViewer
Common CUDA utilities.
When CUDA is not enabled, this is a dummy class.
Definition: CUDAUtils.h:214
bool IsCUDA() const
Returns true iff device type is CUDA.
Definition: Device.h:49
bool IsSYCL() const
Returns true iff device type is SYCL GPU.
Definition: Device.h:52
std::string ToString() const
Definition: SizeVector.cpp:132
Tensor Contiguous() const
Definition: Tensor.cpp:772
Dtype GetDtype() const
Definition: Tensor.h:1164
static Tensor Zeros(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor fill with zeros.
Definition: Tensor.cpp:406
Device GetDevice() const override
Definition: Tensor.cpp:1435
SizeVector GetShape() const
Definition: Tensor.h:1127
#define LogError(...)
Definition: Logging.h:60
void TrilCPU(const Tensor &A, Tensor &output, const int diagonal)
Definition: TriCPU.cpp:35
void TrilSYCL(const Tensor &A, Tensor &output, const int diagonal)
Definition: TriSYCL.cpp:34
void Triu(const Tensor &A, Tensor &output, const int diagonal)
Definition: Tri.cpp:35
void Triul(const Tensor &A, Tensor &upper, Tensor &lower, const int diagonal)
Definition: Tri.cpp:79
void Tril(const Tensor &A, Tensor &output, const int diagonal)
Definition: Tri.cpp:57
void TriulSYCL(const Tensor &A, Tensor &upper, Tensor &lower, const int diagonal)
Definition: TriSYCL.cpp:52
void TriulCPU(const Tensor &A, Tensor &upper, Tensor &lower, const int diagonal)
Definition: TriCPU.cpp:53
void TriuCPU(const Tensor &A, Tensor &output, const int diagonal)
Definition: TriCPU.cpp:17
void TriuSYCL(const Tensor &A, Tensor &output, const int diagonal)
Definition: TriSYCL.cpp:16
static void CheckInput(const Tensor &A, const int diagonal)
Definition: Tri.cpp:17
Generic file read and write utility for python interface.