Open3D (C++ API)  0.12.0
LinalgUtils.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - Open3D: www.open3d.org -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #pragma once
28 
29 #include <memory>
30 #include <string>
31 
32 #include "open3d/core/Dtype.h"
36 #include "open3d/utility/Console.h"
37 
38 namespace open3d {
39 namespace core {
40 
41 #define DISPATCH_LINALG_DTYPE_TO_TEMPLATE(DTYPE, ...) \
42  [&] { \
43  if (DTYPE == open3d::core::Dtype::Float32) { \
44  using scalar_t = float; \
45  return __VA_ARGS__(); \
46  } else if (DTYPE == open3d::core::Dtype::Float64) { \
47  using scalar_t = double; \
48  return __VA_ARGS__(); \
49  } else { \
50  utility::LogError("Unsupported data type."); \
51  } \
52  }()
53 
55  const std::string& msg) {
56  if (info < 0) {
57  utility::LogError("{}: {}-th parameter is invalid.", msg, -info);
58  } else if (info > 0) {
59  utility::LogError("{}: singular condition detected.", msg);
60  }
61 }
62 
63 #ifdef BUILD_CUDA_MODULE
64 inline void OPEN3D_CUBLAS_CHECK(cublasStatus_t status, const std::string& msg) {
65  if (CUBLAS_STATUS_SUCCESS != status) {
66  utility::LogError("{}", msg);
67  }
68 }
69 
70 inline void OPEN3D_CUSOLVER_CHECK(cusolverStatus_t status,
71  const std::string& msg) {
72  if (CUSOLVER_STATUS_SUCCESS != status) {
73  utility::LogError("{}", msg);
74  }
75 }
76 
77 inline void OPEN3D_CUSOLVER_CHECK_WITH_DINFO(cusolverStatus_t status,
78  const std::string& msg,
79  int* dinfo,
80  const Device& device) {
81  int hinfo;
82  MemoryManager::MemcpyToHost(&hinfo, dinfo, device, sizeof(int));
83  if (status != CUSOLVER_STATUS_SUCCESS || hinfo != 0) {
84  if (hinfo < 0) {
85  utility::LogError("{}: {}-th parameter is invalid.", msg, -hinfo);
86  } else if (hinfo > 0) {
87  utility::LogError("{}: singular condition detected.", msg);
88  } else {
89  utility::LogError("{}: status error code = {}.", msg, status);
90  }
91  }
92 }
93 
94 class CuSolverContext {
95 public:
96  static std::shared_ptr<CuSolverContext> GetInstance();
97  CuSolverContext();
98  ~CuSolverContext();
99 
100  cusolverDnHandle_t& GetHandle() { return handle_; }
101 
102 private:
103  cusolverDnHandle_t handle_;
104 
105  static std::shared_ptr<CuSolverContext> instance_;
106 };
107 
108 class CuBLASContext {
109 public:
110  static std::shared_ptr<CuBLASContext> GetInstance();
111 
112  CuBLASContext();
113  ~CuBLASContext();
114 
115  cublasHandle_t& GetHandle() { return handle_; }
116 
117 private:
118  cublasHandle_t handle_;
119 
120  static std::shared_ptr<CuBLASContext> instance_;
121 };
122 #endif
123 } // namespace core
124 } // namespace open3d
void LogError(const char *format, const Args &... args)
Definition: Console.h:176
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:88
Definition: Device.h:39
Definition: PinholeCameraIntrinsic.cpp:35
void OPEN3D_LAPACK_CHECK(OPEN3D_CPU_LINALG_INT info, const std::string &msg)
Definition: LinalgUtils.h:54
#define OPEN3D_CPU_LINALG_INT
Definition: LinalgHeadersCPU.h:45