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