ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Gpu.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 
8 #include "Gpu.h"
9 
10 #include <Logging.h>
11 #include <memory.h>
12 
13 #include <sstream>
14 
15 #ifdef BUILD_CUDA_MODULE
16 #include <cuda_runtime.h>
17 #endif
18 
19 namespace cloudViewer {
20 namespace gpu {
21 
22 bool gpuSupportCUDA(int minComputeCapabilityMajor,
23  int minComputeCapabilityMinor,
24  int minTotalDeviceMemory) {
25 #ifdef BUILD_CUDA_MODULE
26  int nbDevices = 0;
27  cudaError_t success;
28  success = cudaGetDeviceCount(&nbDevices);
29  if (success != cudaSuccess) {
30  utility::LogError("cudaGetDeviceCount failed: ",
31  cudaGetErrorString(success));
32  nbDevices = 0;
33  }
34 
35  if (nbDevices > 0) {
36  for (int i = 0; i < nbDevices; ++i) {
37  cudaDeviceProp deviceProperties;
38 
39  if (cudaGetDeviceProperties(&deviceProperties, i) != cudaSuccess) {
41  "Cannot get properties for CUDA gpu device {}", i);
42  continue;
43  }
44 
45  if ((deviceProperties.major > minComputeCapabilityMajor ||
46  (deviceProperties.major == minComputeCapabilityMajor &&
47  deviceProperties.minor >= minComputeCapabilityMinor)) &&
48  deviceProperties.totalGlobalMem >=
49  static_cast<std::size_t>(minTotalDeviceMemory * 1024 *
50  1024)) {
51  utility::LogInfo("Supported CUDA-Enabled GPU detected.");
52  return true;
53  } else {
55  "CUDA-Enabled GPU detected, but the compute "
56  "capabilities is not enough.\n - Device {} : {}.{}, "
57  "global memory: {} MB\n - Requirements: {}.{}, global "
58  "memory: {} MB\n",
59  i, deviceProperties.major, deviceProperties.minor,
60  int(deviceProperties.totalGlobalMem / (1024 * 1024)),
61  minComputeCapabilityMajor, minComputeCapabilityMinor,
62  minTotalDeviceMemory);
63  }
64  }
65  utility::LogInfo("CUDA-Enabled GPU not supported.");
66  } else {
67  utility::LogInfo("Can't find CUDA-Enabled GPU.");
68  }
69 #endif
70  return false;
71 }
72 
73 std::string gpuInformationCUDA() {
74  std::string information;
75 #ifdef BUILD_CUDA_MODULE
76  int nbDevices = 0;
77  if (cudaGetDeviceCount(&nbDevices) != cudaSuccess) {
79  "Could not determine number of CUDA cards in this system");
80  nbDevices = 0;
81  }
82 
83  if (nbDevices > 0) {
84  information = "CUDA-Enabled GPU.\n";
85  for (int i = 0; i < nbDevices; ++i) {
86  cudaDeviceProp deviceProperties;
87  if (cudaGetDeviceProperties(&deviceProperties, i) != cudaSuccess) {
89  "Cannot get properties for CUDA gpu device {}", i);
90  continue;
91  }
92 
93  if (cudaSetDevice(i) != cudaSuccess) {
94  utility::LogError("Device with number {} does not exist", i);
95  continue;
96  }
97 
98  std::size_t avail;
99  std::size_t total;
100  if (cudaMemGetInfo(&avail, &total) != cudaSuccess) {
101  // if the card does not provide this information.
102  avail = 0;
103  total = 0;
105  "Cannot get available memory information for CUDA gpu "
106  "device {}.",
107  i);
108  }
109  std::stringstream deviceSS;
110 
111  deviceSS << "Device information:" << std::endl
112  << "\t- id: " << i << std::endl
113  << "\t- name: " << deviceProperties.name
114  << std::endl
115  << "\t- compute capability: "
116  << deviceProperties.major << "." << deviceProperties.minor
117  << std::endl
118  << "\t- total device memory: "
119  << deviceProperties.totalGlobalMem / (1024 * 1024)
120  << " MB " << std::endl
121  << "\t- device memory available: " << avail / (1024 * 1024)
122  << " MB " << std::endl
123  << "\t- per-block shared memory: "
124  << deviceProperties.sharedMemPerBlock << std::endl
125  << "\t- warp size: "
126  << deviceProperties.warpSize << std::endl
127  << "\t- max threads per block: "
128  << deviceProperties.maxThreadsPerBlock << std::endl
129  << "\t- max threads per SM(X): "
130  << deviceProperties.maxThreadsPerMultiProcessor
131  << std::endl
132  << "\t- max block sizes: "
133  << "{" << deviceProperties.maxThreadsDim[0] << ","
134  << deviceProperties.maxThreadsDim[1] << ","
135  << deviceProperties.maxThreadsDim[2] << "}" << std::endl
136  << "\t- max grid sizes: "
137  << "{" << deviceProperties.maxGridSize[0] << ","
138  << deviceProperties.maxGridSize[1] << ","
139  << deviceProperties.maxGridSize[2] << "}" << std::endl
140  << "\t- max 2D array texture: "
141  << "{" << deviceProperties.maxTexture2D[0] << ","
142  << deviceProperties.maxTexture2D[1] << "}" << std::endl
143  << "\t- max 3D array texture: "
144  << "{" << deviceProperties.maxTexture3D[0] << ","
145  << deviceProperties.maxTexture3D[1] << ","
146  << deviceProperties.maxTexture3D[2] << "}" << std::endl
147  << "\t- max 2D linear texture: "
148  << "{" << deviceProperties.maxTexture2DLinear[0] << ","
149  << deviceProperties.maxTexture2DLinear[1] << ","
150  << deviceProperties.maxTexture2DLinear[2] << "}"
151  << std::endl
152  << "\t- max 2D layered texture: "
153  << "{" << deviceProperties.maxTexture2DLayered[0] << ","
154  << deviceProperties.maxTexture2DLayered[1] << ","
155  << deviceProperties.maxTexture2DLayered[2] << "}"
156  << std::endl
157  << "\t- number of SM(x)s: "
158  << deviceProperties.multiProcessorCount << std::endl
159  << "\t- registers per SM(x): "
160  << deviceProperties.regsPerMultiprocessor << std::endl
161  << "\t- registers per block: "
162  << deviceProperties.regsPerBlock << std::endl
163  << "\t- concurrent kernels: "
164  << (deviceProperties.concurrentKernels ? "yes" : "no")
165  << std::endl
166  << "\t- mapping host memory: "
167  << (deviceProperties.canMapHostMemory ? "yes" : "no")
168  << std::endl
169  << "\t- unified addressing: "
170  << (deviceProperties.unifiedAddressing ? "yes" : "no")
171  << std::endl
172  << "\t- texture alignment: "
173  << deviceProperties.textureAlignment << " byte"
174  << std::endl
175  << "\t- pitch alignment: "
176  << deviceProperties.texturePitchAlignment << " byte"
177  << std::endl;
178 
179  information += deviceSS.str();
180  }
181  } else {
182  information = "No CUDA-Enabled GPU.";
183  }
184 #else
185  information = "AliceVision built without CUDA support.";
186 #endif
187  return information;
188 }
189 
190 } // namespace gpu
191 } // namespace cloudViewer
#define LogWarning(...)
Definition: Logging.h:72
#define LogInfo(...)
Definition: Logging.h:81
#define LogError(...)
Definition: Logging.h:60
QTextStream & endl(QTextStream &stream)
Definition: QtCompat.h:718
bool gpuSupportCUDA(int minComputeCapabilityMajor, int minComputeCapabilityMinor, int minTotalDeviceMemory)
Check if the system support CUDA with the given parameters.
Definition: Gpu.cpp:22
std::string gpuInformationCUDA()
gpuInformationCUDA
Definition: Gpu.cpp:73
Generic file read and write utility for python interface.