ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
LinalgUtils.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 
11 
12 namespace cloudViewer {
13 namespace core {
14 
15 CuSolverContext& CuSolverContext::GetInstance() {
16  static CuSolverContext instance;
17  return instance;
18 }
19 
20 CuSolverContext::CuSolverContext() {
21  for (const Device& device : Device::GetAvailableCUDADevices()) {
22  CUDAScopedDevice scoped_device(device);
23  cusolverDnHandle_t handle;
24  if (cusolverDnCreate(&handle) != CUSOLVER_STATUS_SUCCESS) {
25  utility::LogError("Unable to create cuSolver handle for {}.",
26  device.ToString());
27  }
28  map_device_to_handle_[device] = handle;
29  }
30 }
31 
32 CuSolverContext::~CuSolverContext() {
33  // Destroy map_device_to_handle_
34  for (auto& item : map_device_to_handle_) {
35  if (cusolverDnDestroy(item.second) != CUSOLVER_STATUS_SUCCESS) {
37  "Unable to destroy cuSolver handle for device {}.",
38  item.first.ToString());
39  }
40  }
41 }
42 
43 cusolverDnHandle_t& CuSolverContext::GetHandle(const Device& device) {
44  if (device.GetType() != Device::DeviceType::CUDA) {
45  utility::LogError("cuSolver is only available on CUDA devices");
46  }
47  if (map_device_to_handle_.count(device) == 0) {
48  utility::LogError("cuSolver handle not found for device: {}",
49  device.ToString());
50  }
51  return map_device_to_handle_.at(device);
52 }
53 
54 CuBLASContext& CuBLASContext::GetInstance() {
55  static CuBLASContext instance;
56  return instance;
57 }
58 
59 CuBLASContext::CuBLASContext() {
60  for (const Device& device : Device::GetAvailableCUDADevices()) {
61  CUDAScopedDevice scoped_device(device);
62  cublasHandle_t handle;
63  if (cublasCreate(&handle) != CUBLAS_STATUS_SUCCESS) {
64  utility::LogError("Unable to create cublas handle for {}.",
65  device.ToString());
66  }
67  map_device_to_handle_[device] = handle;
68  }
69 }
70 
71 CuBLASContext::~CuBLASContext() {
72  // Destroy map_device_to_handle_
73  for (auto& item : map_device_to_handle_) {
74  if (cublasDestroy(item.second) != CUBLAS_STATUS_SUCCESS) {
75  utility::LogError("Unable to destroy cublas handle for device {}.",
76  item.first.ToString());
77  }
78  }
79 }
80 
81 cublasHandle_t& CuBLASContext::GetHandle(const Device& device) {
82  if (device.GetType() != Device::DeviceType::CUDA) {
83  utility::LogError("cuBLAS is only available on CUDA devices");
84  }
85  if (map_device_to_handle_.count(device) == 0) {
86  utility::LogError("cuBLAS handle not found for device: {}",
87  device.ToString());
88  }
89  return map_device_to_handle_.at(device);
90 }
91 
92 } // namespace core
93 } // namespace cloudViewer
Common CUDA utilities.
static std::vector< Device > GetAvailableCUDADevices()
Returns a vector of available CUDA device.
Definition: Device.cpp:132
#define LogError(...)
Definition: Logging.h:60
ccGuiPythonInstance * GetInstance() noexcept
Definition: Runtime.cpp:72
Generic file read and write utility for python interface.