ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
SVD.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 <unordered_map>
11 
13 
14 namespace cloudViewer {
15 namespace core {
16 
17 void SVD(const Tensor &A, Tensor &U, Tensor &S, Tensor &VT) {
19 
20  const Device device = A.GetDevice();
21  const Dtype dtype = A.GetDtype();
22 
23  // Check dimensions
24  SizeVector A_shape = A.GetShape();
25  if (A_shape.size() != 2) {
26  utility::LogError("Tensor must be 2D, but got {}D", A_shape.size());
27  }
28 
29  int64_t m = A_shape[0], n = A_shape[1];
30  if (m == 0 || n == 0) {
32  "Tensor shapes should not contain dimensions with zero.");
33  }
34  if (m < n) {
35  utility::LogError("Only support m >= n, but got {} and {} matrix", m,
36  n);
37  }
38 
39  Tensor A_T = A.T().Contiguous();
40  U = Tensor::Empty({m, m}, dtype, device);
41  S = Tensor::Empty({n}, dtype, device);
42  VT = Tensor::Empty({n, n}, dtype, device);
43 
44  void *A_data = A_T.GetDataPtr();
45  void *U_data = U.GetDataPtr();
46  void *S_data = S.GetDataPtr();
47  void *VT_data = VT.GetDataPtr();
48 
49  if (device.IsSYCL()) {
50 #ifdef BUILD_SYCL_MODULE
51  SVDSYCL(A_data, U_data, S_data, VT_data, m, n, dtype, device);
52 #else
53  utility::LogError("Unimplemented device.");
54 #endif
55  } else if (device.IsCUDA()) {
56 #ifdef BUILD_CUDA_MODULE
57  CUDAScopedDevice scoped_device(device);
58  Tensor superb = Tensor::Empty({std::min(m, n) - 1}, dtype, device);
59  void *superb_data = superb.GetDataPtr();
60  SVDCUDA(A_data, U_data, S_data, VT_data, superb_data, m, n, dtype,
61  device);
62 #else
63  utility::LogError("Unimplemented device.");
64 #endif
65  } else {
66  Tensor superb = Tensor::Empty({std::min(m, n) - 1}, dtype, device);
67  void *superb_data = superb.GetDataPtr();
68  SVDCPU(A_data, U_data, S_data, VT_data, superb_data, m, n, dtype,
69  device);
70  }
71  U = U.T();
72  VT = VT.T();
73 }
74 } // namespace core
75 } // namespace cloudViewer
Common CUDA utilities.
#define AssertTensorDtypes(tensor,...)
Definition: TensorCheck.h:33
When CUDA is not enabled, this is a dummy class.
Definition: CUDAUtils.h:214
Tensor Contiguous() const
Definition: Tensor.cpp:772
Dtype GetDtype() const
Definition: Tensor.h:1164
Device GetDevice() const override
Definition: Tensor.cpp:1435
static Tensor Empty(const SizeVector &shape, Dtype dtype, const Device &device=Device("CPU:0"))
Create a tensor with uninitialized values.
Definition: Tensor.cpp:400
SizeVector GetShape() const
Definition: Tensor.h:1127
Tensor T() const
Expects input to be <= 2-D Tensor by swapping dimension 0 and 1.
Definition: Tensor.cpp:1079
#define LogError(...)
Definition: Logging.h:60
int min(int a, int b)
Definition: cutil_math.h:53
void SVD(const Tensor &A, Tensor &U, Tensor &S, Tensor &VT)
Definition: SVD.cpp:17
void SVDCPU(const void *A_data, void *U_data, void *S_data, void *VT_data, void *superb_data, int64_t m, int64_t n, Dtype dtype, const Device &device)
Definition: SVDCPU.cpp:15
const Dtype Float64
Definition: Dtype.cpp:43
void SVDSYCL(const void *A_data, void *U_data, void *S_data, void *VT_data, int64_t m, int64_t n, Dtype dtype, const Device &device)
Definition: SVDSYCL.cpp:19
void SVDCUDA(const void *A_data, void *U_data, void *S_data, void *VT_data, void *superb_data, int64_t m, int64_t n, Dtype dtype, const Device &device)
Definition: SVDCUDA.cpp:16
const Dtype Float32
Definition: Dtype.cpp:42
Generic file read and write utility for python interface.