DPC++ Runtime
Runtime libraries for oneAPI DPC++
pi_opencl.hpp
Go to the documentation of this file.
1 //==---------- pi_opencl.hpp - OpenCL Plugin -------------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
10 
16 
17 #ifndef PI_OPENCL_HPP
18 #define PI_OPENCL_HPP
19 
20 #include <climits>
21 #include <regex>
22 #include <string>
23 
24 // This version should be incremented for any change made to this file or its
25 // corresponding .cpp file.
26 #define _PI_OPENCL_PLUGIN_VERSION 1
27 
28 #define _PI_OPENCL_PLUGIN_VERSION_STRING \
29  _PI_PLUGIN_VERSION_STRING(_PI_OPENCL_PLUGIN_VERSION)
30 
31 namespace OCLV {
33 protected:
34  unsigned int ocl_major;
35  unsigned int ocl_minor;
36 
37 public:
39 
40  OpenCLVersion(unsigned int ocl_major, unsigned int ocl_minor)
42  if (!isValid())
43  ocl_major = ocl_minor = 0;
44  }
45 
46  OpenCLVersion(const char *version) : OpenCLVersion(std::string(version)) {}
47 
48  OpenCLVersion(const std::string &version) : ocl_major(0), ocl_minor(0) {
49  /* The OpenCL specification defines the full version string as
50  * 'OpenCL<space><ocl_major_version.ocl_minor_version><space><platform-specific
51  * information>' for platforms and as
52  * 'OpenCL<space><ocl_major_version.ocl_minor_version><space><vendor-specific
53  * information>' for devices.
54  */
55  std::regex rx("OpenCL ([0-9]+)\\.([0-9]+)");
56  std::smatch match;
57 
58  if (std::regex_search(version, match, rx) && (match.size() == 3)) {
59  ocl_major = strtoul(match[1].str().c_str(), nullptr, 10);
60  ocl_minor = strtoul(match[2].str().c_str(), nullptr, 10);
61 
62  if (!isValid())
63  ocl_major = ocl_minor = 0;
64  }
65  }
66 
67  bool operator==(const OpenCLVersion &v) const {
68  return ocl_major == v.ocl_major && ocl_minor == v.ocl_minor;
69  }
70 
71  bool operator!=(const OpenCLVersion &v) const { return !(*this == v); }
72 
73  bool operator<(const OpenCLVersion &v) const {
74  if (ocl_major == v.ocl_major)
75  return ocl_minor < v.ocl_minor;
76 
77  return ocl_major < v.ocl_major;
78  }
79 
80  bool operator>(const OpenCLVersion &v) const { return v < *this; }
81 
82  bool operator<=(const OpenCLVersion &v) const {
83  return (*this < v) || (*this == v);
84  }
85 
86  bool operator>=(const OpenCLVersion &v) const {
87  return (*this > v) || (*this == v);
88  }
89 
90  bool isValid() const {
91  switch (ocl_major) {
92  case 0:
93  return false;
94  case 1:
95  case 2:
96  return ocl_minor <= 2;
97  case UINT_MAX:
98  return false;
99  default:
100  return ocl_minor != UINT_MAX;
101  }
102  }
103 
104  int getMajor() const { return ocl_major; }
105  int getMinor() const { return ocl_minor; }
106 };
107 
108 inline const OpenCLVersion V1_0(1, 0);
109 inline const OpenCLVersion V1_1(1, 1);
110 inline const OpenCLVersion V1_2(1, 2);
111 inline const OpenCLVersion V2_0(2, 0);
112 inline const OpenCLVersion V2_1(2, 1);
113 inline const OpenCLVersion V2_2(2, 2);
114 inline const OpenCLVersion V3_0(3, 0);
115 
116 } // namespace OCLV
117 
118 #endif // PI_OPENCL_HPP
OCLV::V1_0
const OpenCLVersion V1_0(1, 0)
OCLV::OpenCLVersion::operator!=
bool operator!=(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:71
OCLV::V1_1
const OpenCLVersion V1_1(1, 1)
OCLV::OpenCLVersion::ocl_minor
unsigned int ocl_minor
Definition: pi_opencl.hpp:35
OCLV::OpenCLVersion::operator==
bool operator==(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:67
OCLV::OpenCLVersion::isValid
bool isValid() const
Definition: pi_opencl.hpp:90
OCLV::OpenCLVersion::operator<
bool operator<(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:73
OCLV::OpenCLVersion::OpenCLVersion
OpenCLVersion(const char *version)
Definition: pi_opencl.hpp:46
OCLV
Definition: pi_opencl.hpp:31
OCLV::V2_1
const OpenCLVersion V2_1(2, 1)
OCLV::OpenCLVersion::OpenCLVersion
OpenCLVersion(const std::string &version)
Definition: pi_opencl.hpp:48
OCLV::V2_0
const OpenCLVersion V2_0(2, 0)
OCLV::OpenCLVersion
Definition: pi_opencl.hpp:32
OCLV::OpenCLVersion::getMajor
int getMajor() const
Definition: pi_opencl.hpp:104
OCLV::OpenCLVersion::OpenCLVersion
OpenCLVersion()
Definition: pi_opencl.hpp:38
OCLV::OpenCLVersion::operator>
bool operator>(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:80
OCLV::OpenCLVersion::OpenCLVersion
OpenCLVersion(unsigned int ocl_major, unsigned int ocl_minor)
Definition: pi_opencl.hpp:40
std
Definition: accessor.hpp:3922
OCLV::OpenCLVersion::operator<=
bool operator<=(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:82
OCLV::V1_2
const OpenCLVersion V1_2(1, 2)
OCLV::OpenCLVersion::ocl_major
unsigned int ocl_major
Definition: pi_opencl.hpp:34
OCLV::V3_0
const OpenCLVersion V3_0(3, 0)
OCLV::OpenCLVersion::operator>=
bool operator>=(const OpenCLVersion &v) const
Definition: pi_opencl.hpp:86
OCLV::V2_2
const OpenCLVersion V2_2(2, 2)
OCLV::OpenCLVersion::getMinor
int getMinor() const
Definition: pi_opencl.hpp:105