OmniSciDB  d2f719934e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
get_nvidia_compute_capability.h
Go to the documentation of this file.
1 // Copyright (c) 2021 OmniSci, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <cassert>
18 #include <stdexcept>
19 #include <string>
20 #include <vector>
21 
22 #include <cuda_runtime.h>
23 
24 inline std::vector<size_t> get_nvidia_compute_capability() {
25  using namespace std::string_literals;
26  std::vector<size_t> ret;
27 
28  int deviceCount = 0;
29  cudaError_t error_id = cudaGetDeviceCount(&deviceCount);
30 
31  if (error_id != cudaSuccess) {
32  throw std::runtime_error("cudaGetDeviceCount failed: "s + std::to_string(error_id) +
33  ": "s + cudaGetErrorString(error_id));
34  }
35 
36  for (int dev = 0; dev < deviceCount; ++dev) {
37  cudaSetDevice(dev);
38  cudaDeviceProp deviceProp;
39  cudaGetDeviceProperties(&deviceProp, dev);
40 
41  if (deviceProp.major <= 0) {
42  throw std::runtime_error("unexpected cuda compute capability: major "s +
43  std::to_string(deviceProp.major));
44  }
45  if (deviceProp.minor < 0) {
46  throw std::runtime_error("unexpected cuda compute capability: minor "s +
47  std::to_string(deviceProp.minor));
48  }
49 
50  if (deviceProp.major >= 10) {
51  throw std::runtime_error("unexpected cuda compute capability: major "s +
52  std::to_string(deviceProp.major));
53  }
54  if (deviceProp.minor >= 10) {
55  throw std::runtime_error("unexpected cuda compute capability: minor "s +
56  std::to_string(deviceProp.minor));
57  }
58 
59  ret.push_back((deviceProp.major * 10U) + deviceProp.minor);
60  }
61 
62  return ret;
63 }
std::string to_string(char const *&&v)
std::vector< size_t > get_nvidia_compute_capability()