ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
cuda.cc
Go to the documentation of this file.
1 // Copyright (c) 2018, ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)
31 
32 #include "util/cuda.h"
33 
34 #include <cuda_runtime.h>
35 
36 #include <algorithm>
37 #include <iostream>
38 #include <vector>
39 
40 #include "util/cudacc.h"
41 #include "util/logging.h"
42 
43 namespace colmap {
44 namespace {
45 
46 // Check whether the first Cuda device is better than the second.
47 bool CompareCudaDevice(const cudaDeviceProp& d1, const cudaDeviceProp& d2) {
48  bool result = (d1.major > d2.major) ||
49  ((d1.major == d2.major) && (d1.minor > d2.minor)) ||
50  ((d1.major == d2.major) && (d1.minor == d2.minor) &&
51  (d1.multiProcessorCount > d2.multiProcessorCount));
52  return result;
53 }
54 
55 } // namespace
56 
58  int num_cuda_devices = 0;
59  cudaError_t error = cudaGetDeviceCount(&num_cuda_devices);
60  if (error != cudaSuccess) {
61  // CUDA initialization failed or no devices available
62  return 0;
63  }
64  return num_cuda_devices;
65 }
66 
67 void SetBestCudaDevice(const int gpu_index) {
68  const int num_cuda_devices = GetNumCudaDevices();
69  CHECK_GT(num_cuda_devices, 0) << "No CUDA devices available";
70 
71  int selected_gpu_index = -1;
72  if (gpu_index >= 0) {
73  selected_gpu_index = gpu_index;
74  } else {
75  std::vector<cudaDeviceProp> all_devices(num_cuda_devices);
76  for (int device_id = 0; device_id < num_cuda_devices; ++device_id) {
77  cudaGetDeviceProperties(&all_devices[device_id], device_id);
78  }
79  std::sort(all_devices.begin(), all_devices.end(), CompareCudaDevice);
81  cudaChooseDevice(&selected_gpu_index, all_devices.data()));
82  VLOG(2) << "Found " << num_cuda_devices << " CUDA device(s), "
83  << "selected device " << selected_gpu_index << " with name "
84  << all_devices[selected_gpu_index].name;
85  }
86 
87  CHECK_GE(selected_gpu_index, 0);
88  CHECK_LT(selected_gpu_index, num_cuda_devices)
89  << "Invalid CUDA GPU selected";
90 
91  cudaDeviceProp device;
92  cudaGetDeviceProperties(&device, selected_gpu_index);
93  CUDA_SAFE_CALL(cudaSetDevice(selected_gpu_index));
94 }
95 
96 } // namespace colmap
core::Tensor result
Definition: VtkUtils.cpp:76
#define CUDA_SAFE_CALL(error)
Definition: cudacc.h:14
void SetBestCudaDevice(const int gpu_index)
Definition: cuda.cc:67
int GetNumCudaDevices()
Definition: cuda.cc:57