Open3D (C++ API)  0.17.0
LinalgUtils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2023 www.open3d.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <string>
11 
12 #include "open3d/core/Device.h"
13 #include "open3d/core/Dtype.h"
17 #include "open3d/utility/Logging.h"
18 
19 namespace open3d {
20 namespace core {
21 
22 #define DISPATCH_LINALG_DTYPE_TO_TEMPLATE(DTYPE, ...) \
23  [&] { \
24  if (DTYPE == open3d::core::Float32) { \
25  using scalar_t = float; \
26  return __VA_ARGS__(); \
27  } else if (DTYPE == open3d::core::Float64) { \
28  using scalar_t = double; \
29  return __VA_ARGS__(); \
30  } else { \
31  utility::LogError("Unsupported data type."); \
32  } \
33  }()
34 
36  const std::string& msg) {
37  if (info < 0) {
38  utility::LogError("{}: {}-th parameter is invalid.", msg, -info);
39  } else if (info > 0) {
40  utility::LogError("{}: singular condition detected.", msg);
41  }
42 }
43 
44 #ifdef BUILD_CUDA_MODULE
45 inline void OPEN3D_CUBLAS_CHECK(cublasStatus_t status, const std::string& msg) {
46  if (CUBLAS_STATUS_SUCCESS != status) {
47  utility::LogError("{}", msg);
48  }
49 }
50 
51 inline void OPEN3D_CUSOLVER_CHECK(cusolverStatus_t status,
52  const std::string& msg) {
53  if (CUSOLVER_STATUS_SUCCESS != status) {
54  utility::LogError("{}", msg);
55  }
56 }
57 
58 inline void OPEN3D_CUSOLVER_CHECK_WITH_DINFO(cusolverStatus_t status,
59  const std::string& msg,
60  int* dinfo,
61  const Device& device) {
62  int hinfo;
63  MemoryManager::MemcpyToHost(&hinfo, dinfo, device, sizeof(int));
64  if (status != CUSOLVER_STATUS_SUCCESS || hinfo != 0) {
65  if (hinfo < 0) {
66  utility::LogError("{}: {}-th parameter is invalid.", msg, -hinfo);
67  } else if (hinfo > 0) {
68  utility::LogError("{}: singular condition detected.", msg);
69  } else {
70  utility::LogError("{}: status error code = {}.", msg, status);
71  }
72  }
73 }
74 
75 class CuSolverContext {
76 public:
77  static CuSolverContext& GetInstance();
78 
79  CuSolverContext(const CuSolverContext&) = delete;
80  CuSolverContext& operator=(const CuSolverContext&) = delete;
81  ~CuSolverContext();
82 
83  cusolverDnHandle_t& GetHandle(const Device& device);
84 
85 private:
86  CuSolverContext();
87  std::unordered_map<Device, cusolverDnHandle_t> map_device_to_handle_;
88 };
89 
90 class CuBLASContext {
91 public:
92  static CuBLASContext& GetInstance();
93 
94  CuBLASContext(const CuBLASContext&) = delete;
95  CuBLASContext& operator=(const CuBLASContext&) = delete;
96  ~CuBLASContext();
97 
98  cublasHandle_t& GetHandle(const Device& device);
99 
100 private:
101  CuBLASContext();
102  std::unordered_map<Device, cublasHandle_t> map_device_to_handle_;
103 };
104 #endif
105 } // namespace core
106 } // namespace open3d
#define OPEN3D_CPU_LINALG_INT
Definition: LinalgHeadersCPU.h:23
#define LogError(...)
Definition: Logging.h:48
static void MemcpyToHost(void *host_ptr, const void *src_ptr, const Device &src_device, size_t num_bytes)
Same as Memcpy, but with host (CPU:0) as default dst_device.
Definition: MemoryManager.cpp:85
void OPEN3D_LAPACK_CHECK(OPEN3D_CPU_LINALG_INT info, const std::string &msg)
Definition: LinalgUtils.h:35
Definition: PinholeCameraIntrinsic.cpp:16