ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
LinalgUtils.h
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 
8 #pragma once
9 
10 #include <Logging.h>
11 
12 #include <string>
13 
15 #include "cloudViewer/core/Dtype.h"
19 
20 namespace cloudViewer {
21 namespace core {
22 
23 #define DISPATCH_LINALG_DTYPE_TO_TEMPLATE(DTYPE, ...) \
24  [&] { \
25  if (DTYPE == cloudViewer::core::Float32) { \
26  using scalar_t = float; \
27  return __VA_ARGS__(); \
28  } else if (DTYPE == cloudViewer::core::Float64) { \
29  using scalar_t = double; \
30  return __VA_ARGS__(); \
31  } else { \
32  utility::LogError("Unsupported data type."); \
33  } \
34  }()
35 
37  const std::string& msg) {
38  if (info < 0) {
39  utility::LogError("{}: {}-th parameter is invalid.", msg, -info);
40  } else if (info > 0) {
41  utility::LogError("{}: singular condition detected.", msg);
42  }
43 }
44 
45 #ifdef BUILD_CUDA_MODULE
46 inline void OPEN3D_CUBLAS_CHECK(cublasStatus_t status, const std::string& msg) {
47  if (CUBLAS_STATUS_SUCCESS != status) {
48  utility::LogError("{}", msg);
49  }
50 }
51 
52 inline void OPEN3D_CUSOLVER_CHECK(cusolverStatus_t status,
53  const std::string& msg) {
54  if (CUSOLVER_STATUS_SUCCESS != status) {
55  utility::LogError("{}", msg);
56  }
57 }
58 
59 inline void OPEN3D_CUSOLVER_CHECK_WITH_DINFO(cusolverStatus_t status,
60  const std::string& msg,
61  int* dinfo,
62  const Device& device) {
63  int hinfo;
64  MemoryManager::MemcpyToHost(&hinfo, dinfo, device, sizeof(int));
65  if (status != CUSOLVER_STATUS_SUCCESS || hinfo != 0) {
66  if (hinfo < 0) {
67  utility::LogError("{}: {}-th parameter is invalid.", msg, -hinfo);
68  } else if (hinfo > 0) {
69  utility::LogError("{}: singular condition detected.", msg);
70  } else {
71  utility::LogError("{}: status error code = {}.", msg, status);
72  }
73  }
74 }
75 
76 class CuSolverContext {
77 public:
78  static CuSolverContext& GetInstance();
79 
80  CuSolverContext(const CuSolverContext&) = delete;
81  CuSolverContext& operator=(const CuSolverContext&) = delete;
82  ~CuSolverContext();
83 
84  cusolverDnHandle_t& GetHandle(const Device& device);
85 
86 private:
87  CuSolverContext();
88  std::unordered_map<Device, cusolverDnHandle_t> map_device_to_handle_;
89 };
90 
91 class CuBLASContext {
92 public:
93  static CuBLASContext& GetInstance();
94 
95  CuBLASContext(const CuBLASContext&) = delete;
96  CuBLASContext& operator=(const CuBLASContext&) = delete;
97  ~CuBLASContext();
98 
99  cublasHandle_t& GetHandle(const Device& device);
100 
101 private:
102  CuBLASContext();
103  std::unordered_map<Device, cublasHandle_t> map_device_to_handle_;
104 };
105 #endif
106 } // namespace core
107 } // namespace cloudViewer
#define CLOUDVIEWER_CPU_LINALG_INT
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.
#define LogError(...)
Definition: Logging.h:60
ccGuiPythonInstance * GetInstance() noexcept
Definition: Runtime.cpp:72
void OPEN3D_LAPACK_CHECK(CLOUDVIEWER_CPU_LINALG_INT info, const std::string &msg)
Definition: LinalgUtils.h:36
Generic file read and write utility for python interface.