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 #if CUDA_VERSION >= 12040
16 #include <nvFatbin.h>
17 #endif
18 
19 #include <ostream>
20 #include <utility>
21 #include <limits>
22 
23 
24 namespace cuda {
25 
31 using combined_version_t = int;
32 
39 struct version_t {
41  int major;
42  int minor;
44 
46  static version_t from_single_number(combined_version_t combined_version) noexcept
47  {
48  return { combined_version / 1000, (combined_version % 100) / 10 };
49  }
50 
52  operator ::std::pair<int, int>() const noexcept { return { major, minor }; }
54 };
55 
57 inline ::std::ostream& operator<<(::std::ostream& os, version_t v)
58 {
59  return os << v.major << '.' << v.minor;
60 }
61 
62 // Note: All of comparison operators in this can be made constexpr in C++14
63 
64 inline bool operator==(const version_t& lhs, const version_t& rhs) noexcept
65 {
66  return lhs.operator ::std::pair<int, int>() == rhs.operator ::std::pair<int, int>();
67 }
68 
69 inline bool operator!=(const version_t& lhs, const version_t& rhs) noexcept
70 {
71  return lhs.operator ::std::pair<int, int>() != rhs.operator ::std::pair<int, int>();
72 }
73 
74 inline bool operator<(const version_t& lhs, const version_t& rhs) noexcept
75 {
76  return lhs.operator ::std::pair<int, int>() < rhs.operator ::std::pair<int, int>();
77 }
78 
79 inline bool operator<=(const version_t& lhs, const version_t& rhs) noexcept
80 {
81  return lhs.operator ::std::pair<int, int>() <= rhs.operator ::std::pair<int, int>();
82 }
83 
84 inline bool operator>(const version_t& lhs, const version_t& rhs) noexcept
85 {
86  return lhs.operator ::std::pair<int, int>() > rhs.operator ::std::pair<int, int>();
87 }
88 
89 inline bool operator>=(const version_t& lhs, const version_t& rhs) noexcept
90 {
91  return lhs.operator ::std::pair<int, int>() >= rhs.operator ::std::pair<int, int>();
92 }
93 
94 // comparison with single integers - as major versions
95 
96 inline bool operator==(const version_t& lhs, int rhs) noexcept { return lhs == version_t::from_single_number(rhs); }
97 inline bool operator!=(const version_t& lhs, int rhs) noexcept { return lhs != version_t::from_single_number(rhs); }
98 inline bool operator< (const version_t& lhs, int rhs) noexcept { return lhs < version_t::from_single_number(rhs); }
99 inline bool operator> (const version_t& lhs, int rhs) noexcept { return lhs > version_t::from_single_number(rhs); }
100 inline bool operator<=(const version_t& lhs, int rhs) noexcept { return lhs <= version_t::from_single_number(rhs); }
101 inline bool operator>=(const version_t& lhs, int rhs) noexcept { return lhs >= version_t::from_single_number(rhs); }
102 
104 
105 
106 namespace version_numbers {
107 
114 constexpr version_t none() noexcept
115 {
116  return { 0, 0 };
117 }
118 
124 inline version_t make(combined_version_t combined_version) noexcept
125 {
126  return version_t::from_single_number(combined_version);
127 }
128 
133 inline version_t make(int major, int minor) noexcept
134 {
135  return { major, minor };
136 }
137 
150 inline version_t driver() {
151  combined_version_t version;
152  auto status = cuDriverGetVersion(&version);
153  throw_if_error_lazy(status, "Failed obtaining the CUDA driver version");
154  return version_t::from_single_number(version);
155 }
156 
163 inline version_t runtime() {
164  combined_version_t version;
165  auto status = cudaRuntimeGetVersion(&version);
166  throw_if_error_lazy(status, "Failed obtaining the CUDA runtime version");
167  return version_t::from_single_number(version);
168 }
169 
170 #if CUDA_VERSION >= 12040
171 
172 inline version_t fatbin() {
173  unsigned int major { 0 }, minor { 0 };
174 
175  auto status = nvFatbinVersion(&major, &minor);
176  throw_if_error_lazy(status, "Failed obtaining the nvfatbin library version");
177 #ifndef NDEBUG
178  if ((major == 0) or (major > ::std::numeric_limits<int>::max())
179  or (minor == 0) or (minor > ::std::numeric_limits<int>::max())) {
180  throw ::std::logic_error("Invalid version encountered: ("
181  + ::std::to_string(major) + ", " + ::std::to_string(minor) + ')' );
182  }
183 #endif
184  return version_t{ static_cast<int>(major), static_cast<int>(minor) };
185 }
186 #endif // CUDA_VERSION >= 12040
187 
188 } // namespace version_numbers
189 } // namespace cuda
190 
191 #endif // CUDA_API_WRAPPERS_VERSIONS_HPP_
Definitions and functionality wrapping CUDA APIs.
Definition: array.hpp:22
int combined_version_t
A combination of the major and minor version numbers for a CUDA release into a single integer...
Definition: versions.hpp:31
version_t make(combined_version_t combined_version) noexcept
Convert an integer representing a major and minor number (e.g.
Definition: versions.hpp:124
#define throw_if_error_lazy(status__,...)
A macro for only throwing an error if we&#39;ve failed - which also ensures no string is constructed unle...
Definition: error.hpp:316
bool operator==(const context_t &lhs, const context_t &rhs) noexcept
Definition: context.hpp:762
static version_t from_single_number(combined_version_t combined_version) noexcept
Parse the combined single-number representation, separating it.
Definition: versions.hpp:46
Facilities for exception-based handling of Runtime and Driver API errors, including a basic exception...
A structure representing a CUDA release version.
Definition: versions.hpp:39
version_t driver()
Obtains the maximum version of the CUDA Runtime supported by the driver currently loaded by the opera...
Definition: versions.hpp:150
version_t runtime()
Obtains the CUDA Runtime version.
Definition: versions.hpp:163