Skip to content

Add clblast patch to handle custom context with multiple devices #2967

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeModules/build_CLBlast.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ find_program(GIT git)
set(prefix ${PROJECT_BINARY_DIR}/third_party/CLBlast)
set(CLBlast_location ${prefix}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}clblast${CMAKE_STATIC_LIBRARY_SUFFIX})

set(CLBLAST_PATCH_COMMAND ${GIT} apply --whitespace=fix ${ArrayFire_SOURCE_DIR}/CMakeModules/clblast_program_getIR.patch)

if(WIN32 AND CMAKE_GENERATOR_PLATFORM AND NOT CMAKE_GENERATOR MATCHES "Ninja")
set(extproj_gen_opts "-G${CMAKE_GENERATOR}" "-A${CMAKE_GENERATOR_PLATFORM}")
else()
Expand All @@ -31,6 +33,7 @@ ExternalProject_Add(
PREFIX "${prefix}"
INSTALL_DIR "${prefix}"
UPDATE_COMMAND ""
PATCH_COMMAND "${CLBLAST_PATCH_COMMAND}"
BUILD_BYPRODUCTS ${CLBlast_location}
CONFIGURE_COMMAND ${CMAKE_COMMAND} ${extproj_gen_opts}
-Wno-dev <SOURCE_DIR>
Expand Down
44 changes: 44 additions & 0 deletions CMakeModules/clblast_program_getIR.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
diff --git a/src/clpp11.hpp b/src/clpp11.hpp
index 4ed157ea..2a25606c 100644
--- a/src/clpp11.hpp
+++ b/src/clpp11.hpp
@@ -509,12 +509,35 @@ class Program {

// Retrieves a binary or an intermediate representation of the compiled program
std::string GetIR() const {
- auto bytes = size_t{0};
- CheckError(clGetProgramInfo(program_, CL_PROGRAM_BINARY_SIZES, sizeof(size_t), &bytes, nullptr));
+ cl_uint num_devices = 0;
+ CheckError(clGetProgramInfo(program_, CL_PROGRAM_NUM_DEVICES,
+ sizeof(cl_uint), &num_devices, nullptr));
+
+ std::vector<size_t> binSizesInBytes(num_devices, 0);
+ CheckError(clGetProgramInfo(program_, CL_PROGRAM_BINARY_SIZES,
+ num_devices * sizeof(size_t), binSizesInBytes.data(), nullptr));
+
+ auto bytes = size_t{0};
+ auto binSizeIter = size_t{0};
+ // Loop over the program binary sizes to find a binary whose size is > 0.
+ // The current logic assumes that there ever is only one valid program binary
+ // in a given cl_program. This should be the case unless the cl_program
+ // is built for all or a subset of devices associated to a given cl_program
+ for (; binSizeIter < binSizesInBytes.size(); ++binSizeIter) {
+ if (binSizesInBytes[binSizeIter] > 0) {
+ bytes = binSizesInBytes[binSizeIter];
+ break;
+ }
+ }
auto result = std::string{};
result.resize(bytes);
- auto result_ptr = result.data();
- CheckError(clGetProgramInfo(program_, CL_PROGRAM_BINARIES, sizeof(char*), &result_ptr, nullptr));
+
+ std::vector<char*> out(num_devices, nullptr);
+ out[binSizeIter] = const_cast<char*>(result.data());
+
+ CheckError(clGetProgramInfo(program_, CL_PROGRAM_BINARIES,
+ num_devices * sizeof(char*),
+ out.data(), nullptr));
return result;
}