cuda-api-wrappers
Thin C++-flavored wrappers for the CUDA Runtime API
versions.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 #ifndef CUDA_API_WRAPPERS_VERSIONS_HPP_
11 #define CUDA_API_WRAPPERS_VERSIONS_HPP_
12 
13 #include "error.hpp"
14 
15 #include <ostream>
16 #include <utility>
17 
18 namespace cuda {
19 
20 using combined_version_t = int;
21 
27 struct version_t {
28  int major;
29  int minor;
30 
31  static version_t from_single_number(combined_version_t combined_version) noexcept
32  {
33  return { combined_version / 1000, (combined_version % 100) / 10 };
34  }
35 
36  operator ::std::pair<int, int>() const noexcept { return { major, minor }; }
37 
38 };
39 
41 inline ::std::ostream& operator<<(::std::ostream& os, version_t v)
42 {
43  return os << v.major << '.' << v.minor;
44 }
45 
46 // Note: All of comparison operators in this can be made constexpr in C++14
47 
48 inline bool operator==(const version_t& lhs, const version_t& rhs) noexcept
49 {
50  return lhs.operator ::std::pair<int, int>() == rhs.operator ::std::pair<int, int>();
51 }
52 
53 inline bool operator!=(const version_t& lhs, const version_t& rhs) noexcept
54 {
55  return lhs.operator ::std::pair<int, int>() != rhs.operator ::std::pair<int, int>();
56 }
57 
58 inline bool operator<(const version_t& lhs, const version_t& rhs) noexcept
59 {
60  return lhs.operator ::std::pair<int, int>() < rhs.operator ::std::pair<int, int>();
61 }
62 
63 inline bool operator<=(const version_t& lhs, const version_t& rhs) noexcept
64 {
65  return lhs.operator ::std::pair<int, int>() <= rhs.operator ::std::pair<int, int>();
66 }
67 
68 inline bool operator>(const version_t& lhs, const version_t& rhs) noexcept
69 {
70  return lhs.operator ::std::pair<int, int>() > rhs.operator ::std::pair<int, int>();
71 }
72 
73 inline bool operator>=(const version_t& lhs, const version_t& rhs) noexcept
74 {
75  return lhs.operator ::std::pair<int, int>() >= rhs.operator ::std::pair<int, int>();
76 }
77 
78 // comparison with single integers - as major versions
79 
80 inline bool operator==(const version_t& lhs, int rhs) noexcept { return lhs == version_t::from_single_number(rhs); }
81 inline bool operator!=(const version_t& lhs, int rhs) noexcept { return lhs != version_t::from_single_number(rhs); }
82 inline bool operator< (const version_t& lhs, int rhs) noexcept { return lhs < version_t::from_single_number(rhs); }
83 inline bool operator> (const version_t& lhs, int rhs) noexcept { return lhs > version_t::from_single_number(rhs); }
84 inline bool operator<=(const version_t& lhs, int rhs) noexcept { return lhs <= version_t::from_single_number(rhs); }
85 inline bool operator>=(const version_t& lhs, int rhs) noexcept { return lhs >= version_t::from_single_number(rhs); }
86 
88 
89 
90 namespace version_numbers {
91 
98 constexpr version_t none() noexcept
99 {
100  return { 0, 0 };
101 }
102 
108 inline version_t make(combined_version_t combined_version) noexcept
109 {
110  return version_t::from_single_number(combined_version);
111 }
112 
117 inline version_t make(int major, int minor) noexcept
118 {
119  return { major, minor };
120 }
121 
134 inline version_t driver() {
135  combined_version_t version;
136  auto status = cuDriverGetVersion(&version);
137  // The same value would be returned using cuDriverGetVersion()
138  throw_if_error_lazy(status, "Failed obtaining the CUDA driver version");
139  return version_t::from_single_number(version);
140 }
141 
148 inline version_t runtime() {
149  combined_version_t version;
150  auto status = cudaRuntimeGetVersion(&version);
151  throw_if_error_lazy(status, "Failed obtaining the CUDA runtime version");
152  return version_t::from_single_number(version);
153 }
154 
155 } // namespace version_numbers
156 } // namespace cuda
157 
158 #endif // CUDA_API_WRAPPERS_VERSIONS_HPP_
All definitions and functionality wrapping the CUDA Runtime API.
Definition: array.hpp:22
version_t make(combined_version_t combined_version) noexcept
Convert an integer representing a major and minor number (e.g.
Definition: versions.hpp:108
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
CUDA Runtime version.
Definition: versions.hpp:27
version_t driver()
Obtains the maximum version of the CUDA Runtime supported by the driver currently loaded by the opera...
Definition: versions.hpp:134
version_t runtime()
Obtains the CUDA Runtime version.
Definition: versions.hpp:148