Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 659bc8b

Browse filesBrowse files
committed
Add support for building older OpenCL versions.
1 parent 666b2fd commit 659bc8b
Copy full SHA for 659bc8b

File tree

Expand file treeCollapse file tree

4 files changed

+80
-61
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+80
-61
lines changed

‎src/backend/opencl/CMakeLists.txt

Copy file name to clipboardExpand all lines: src/backend/opencl/CMakeLists.txt
+13-5Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,19 @@ file_to_string(
136136
NAMESPACE "arrayfire opencl"
137137
)
138138

139-
set(opencl_compile_definitions
140-
CL_TARGET_OPENCL_VERSION=300
141-
CL_HPP_TARGET_OPENCL_VERSION=300
142-
CL_HPP_MINIMUM_OPENCL_VERSION=110
143-
CL_HPP_ENABLE_EXCEPTIONS)
139+
if(OpenCL_VERSION_MAJOR LESS 3)
140+
set(opencl_compile_definitions
141+
CL_TARGET_OPENCL_VERSION=120
142+
CL_HPP_TARGET_OPENCL_VERSION=120
143+
CL_HPP_MINIMUM_OPENCL_VERSION=120
144+
CL_HPP_ENABLE_EXCEPTIONS)
145+
else()
146+
set(opencl_compile_definitions
147+
CL_TARGET_OPENCL_VERSION=300
148+
CL_HPP_TARGET_OPENCL_VERSION=300
149+
CL_HPP_MINIMUM_OPENCL_VERSION=110
150+
CL_HPP_ENABLE_EXCEPTIONS)
151+
endif()
144152

145153
include(kernel/scan_by_key/CMakeLists.txt)
146154
include(kernel/sort_by_key/CMakeLists.txt)

‎src/backend/opencl/device_manager.cpp

Copy file name to clipboardExpand all lines: src/backend/opencl/device_manager.cpp
+12-22Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#include <blas.hpp>
1616
#include <build_version.hpp>
1717
#include <clfft.hpp>
18+
#include <common/ArrayFireTypesIO.hpp>
1819
#include <common/DefaultMemoryManager.hpp>
1920
#include <common/Logger.hpp>
21+
#include <common/Version.hpp>
2022
#include <common/defines.hpp>
2123
#include <common/host_memory.hpp>
2224
#include <common/util.hpp>
@@ -264,30 +266,18 @@ DeviceManager::DeviceManager()
264266

265267
auto platform_version =
266268
mPlatforms.back().first->getInfo<CL_PLATFORM_VERSION>();
267-
ostringstream options;
268-
if (platform_version.substr(7).c_str()[0] >= '3') {
269-
auto device_versions =
270-
mDevices.back()->getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
271-
sort(begin(device_versions), end(device_versions),
272-
[](const auto& lhs, const auto& rhs) {
273-
return lhs.version < rhs.version;
274-
});
275-
cl_name_version max_version = device_versions.back();
276-
options << fmt::format(" -cl-std=CL{}.{}",
277-
CL_VERSION_MAJOR(max_version.version),
278-
CL_VERSION_MINOR(max_version.version));
279-
} else {
280-
auto device_version =
281-
mDevices.back()->getInfo<CL_DEVICE_OPENCL_C_VERSION>();
282-
options << fmt::format(" -cl-std=CL{}",
283-
device_version.substr(9, 3));
284-
}
285-
options << fmt::format(" -D dim_t={}",
286-
dtype_traits<dim_t>::getName());
269+
string options;
270+
common::Version version =
271+
getOpenCLCDeviceVersion(*mDevices[i]).back();
287272
#ifdef AF_WITH_FAST_MATH
288-
options << " -cl-fast-relaxed-math";
273+
options = fmt::format(
274+
" -cl-std=CL{:Mm} -D dim_t={} -cl-fast-relaxed-math", version,
275+
dtype_traits<dim_t>::getName());
276+
#else
277+
options = fmt::format(" -cl-std=CL{:Mm} -D dim_t={}", version,
278+
dtype_traits<dim_t>::getName());
289279
#endif
290-
mBaseBuildFlags.push_back(options.str());
280+
mBaseBuildFlags.push_back(options);
291281
} catch (const cl::Error& err) {
292282
AF_TRACE("Error creating context for device {} with error {}\n",
293283
devices[i]->getInfo<CL_DEVICE_NAME>(), err.what());

‎src/backend/opencl/platform.cpp

Copy file name to clipboardExpand all lines: src/backend/opencl/platform.cpp
+49-34Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#include <blas.hpp>
1616
#include <build_version.hpp>
1717
#include <clfft.hpp>
18+
#include <common/ArrayFireTypesIO.hpp>
1819
#include <common/DefaultMemoryManager.hpp>
1920
#include <common/Logger.hpp>
21+
#include <common/Version.hpp>
2022
#include <common/host_memory.hpp>
2123
#include <common/util.hpp>
2224
#include <device_manager.hpp>
@@ -69,6 +71,7 @@ using std::vector;
6971
using arrayfire::common::getEnvVar;
7072
using arrayfire::common::ltrim;
7173
using arrayfire::common::MemoryManagerBase;
74+
using arrayfire::common::Version;
7275
using arrayfire::opencl::Allocator;
7376
using arrayfire::opencl::AllocatorPinned;
7477

@@ -121,7 +124,7 @@ static string platformMap(string& platStr) {
121124
}
122125
}
123126

124-
afcl::platform getPlatformEnum(cl::Device dev) {
127+
afcl::platform getPlatformEnum(Device dev) {
125128
string pname = getPlatformName(dev);
126129
if (verify_present(pname, "AMD"))
127130
return AFCL_PLATFORM_AMD;
@@ -188,7 +191,7 @@ string getDeviceInfo() noexcept {
188191
return info.str();
189192
}
190193

191-
string getPlatformName(const cl::Device& device) {
194+
string getPlatformName(const Device& device) {
192195
const Platform platform(device.getInfo<CL_DEVICE_PLATFORM>());
193196
string platStr = platform.getInfo<CL_PLATFORM_NAME>();
194197
return platformMap(platStr);
@@ -295,7 +298,7 @@ CommandQueue& getQueue() {
295298
return *(devMngr.mQueues[get<1>(devId)]);
296299
}
297300

298-
const cl::Device& getDevice(int id) {
301+
const Device& getDevice(int id) {
299302
device_id_t& devId = tlocalActiveDeviceId();
300303

301304
if (id == -1) { id = get<1>(devId); }
@@ -317,6 +320,40 @@ const std::string& getActiveDeviceBaseBuildFlags(int id) {
317320
return devMngr.mBaseBuildFlags[id];
318321
}
319322

323+
vector<Version> getOpenCLCDeviceVersion(const Device& device) {
324+
Platform device_platform(device.getInfo<CL_DEVICE_PLATFORM>(), false);
325+
auto platform_version = device_platform.getInfo<CL_PLATFORM_VERSION>();
326+
vector<Version> out;
327+
328+
/// The ifdef allows us to support BUILDING ArrayFire with older versions of
329+
/// OpenCL where as the if condition in the ifdef allows us to support older
330+
/// versions of OpenCL at runtime
331+
#ifdef CL_DEVICE_OPENCL_C_ALL_VERSIONS
332+
if (platform_version.substr(7).c_str()[0] >= '3') {
333+
vector<cl_name_version> device_versions =
334+
device.getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
335+
sort(begin(device_versions), end(device_versions),
336+
[](const auto& lhs, const auto& rhs) {
337+
return lhs.version < rhs.version;
338+
});
339+
transform(begin(device_versions), end(device_versions),
340+
std::back_inserter(out), [](const cl_name_version& version) {
341+
return Version(CL_VERSION_MAJOR(version.version),
342+
CL_VERSION_MINOR(version.version),
343+
CL_VERSION_PATCH(version.version));
344+
});
345+
} else {
346+
#endif
347+
auto device_version = device.getInfo<CL_DEVICE_OPENCL_C_VERSION>();
348+
int major = atoi(device_version.substr(9, 1).c_str());
349+
int minor = atoi(device_version.substr(11, 1).c_str());
350+
out.emplace_back(major, minor);
351+
#ifdef CL_DEVICE_OPENCL_C_ALL_VERSIONS
352+
}
353+
#endif
354+
return out;
355+
}
356+
320357
size_t getDeviceMemorySize(int device) {
321358
DeviceManager& devMngr = DeviceManager::getInstance();
322359

@@ -498,39 +535,17 @@ void addDeviceContext(cl_device_id dev, cl_context ctx, cl_command_queue que) {
498535
devMngr.mQueues.push_back(move(tQueue));
499536
nDevices = static_cast<int>(devMngr.mDevices.size()) - 1;
500537

501-
auto device_versions =
502-
devMngr.mDevices.back()->getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
503-
sort(begin(device_versions), end(device_versions),
504-
[](const auto& lhs, const auto& rhs) {
505-
return lhs.version < rhs.version;
506-
});
507-
508-
auto platform_version =
509-
devMngr.mPlatforms.back().first->getInfo<CL_PLATFORM_VERSION>();
510-
ostringstream options;
511-
if (platform_version.substr(7).c_str()[0] >= '3') {
512-
auto device_versions =
513-
devMngr.mDevices.back()
514-
->getInfo<CL_DEVICE_OPENCL_C_ALL_VERSIONS>();
515-
sort(begin(device_versions), end(device_versions),
516-
[](const auto& lhs, const auto& rhs) {
517-
return lhs.version < rhs.version;
518-
});
519-
cl_name_version max_version = device_versions.back();
520-
options << fmt::format(" -cl-std=CL{}.{}",
521-
CL_VERSION_MAJOR(max_version.version),
522-
CL_VERSION_MINOR(max_version.version));
523-
} else {
524-
auto device_version =
525-
devMngr.mDevices.back()->getInfo<CL_DEVICE_OPENCL_C_VERSION>();
526-
options << fmt::format(" -cl-std=CL{}",
527-
device_version.substr(9, 3));
528-
}
529-
options << fmt::format(" -D dim_t={}", dtype_traits<dim_t>::getName());
538+
auto versions = getOpenCLCDeviceVersion(*(devMngr.mDevices.back()));
530539
#ifdef AF_WITH_FAST_MATH
531-
options << " -cl-fast-relaxed-math";
540+
std::string options =
541+
fmt::format(" -cl-std=CL{:Mm} -D dim_t={} -cl-fast-relaxed-math",
542+
versions.back(), dtype_traits<dim_t>::getName());
543+
#else
544+
std::string options =
545+
fmt::format(" -cl-std=CL{:Mm} -D dim_t={}", versions.back(),
546+
dtype_traits<dim_t>::getName());
532547
#endif
533-
devMngr.mBaseBuildFlags.push_back(options.str());
548+
devMngr.mBaseBuildFlags.push_back(options);
534549

535550
// cache the boost program_cache object, clean up done on program exit
536551
// not during removeDeviceContext

‎src/backend/opencl/platform.hpp

Copy file name to clipboardExpand all lines: src/backend/opencl/platform.hpp
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace common {
3535
class ForgeManager;
3636

3737
class MemoryManagerBase;
38+
39+
class Version;
3840
} // namespace common
3941
} // namespace arrayfire
4042

@@ -69,6 +71,10 @@ const cl::Device& getDevice(int id = -1);
6971

7072
const std::string& getActiveDeviceBaseBuildFlags(int id = -1);
7173

74+
/// Returns the set of all OpenCL C Versions the device supports. The values
75+
/// are sorted from oldest to latest.
76+
std::vector<common::Version> getOpenCLCDeviceVersion(const cl::Device& device);
77+
7278
size_t getDeviceMemorySize(int device);
7379

7480
size_t getHostMemorySize();

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.