Skip to content

Commit

Permalink
Fixes from review
Browse files Browse the repository at this point in the history
  • Loading branch information
Beanavil committed Dec 14, 2023
1 parent 43b8e34 commit 1511cbd
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 23 deletions.
10 changes: 4 additions & 6 deletions .github/workflows/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -568,14 +568,12 @@ jobs:
if: matrix.BIN != 'x86'
working-directory: ${{runner.workspace}}/OpenCL-SDK/build
run: |
$EXCLUDE_REGEX = 'externalmemory.*'
foreach ($Config in 'Release','Debug') {
& ctest `
--build-config ${Config} `
--output-on-failure `
--no-tests=error `
--parallel ${env:NUMBER_OF_PROCESSORS} `
--exclude-regex "$EXCLUDE_REGEX"
if ($LASTEXITCODE -ne 0) { throw "Running OpenCL-SDK tests in $Config failed." }
}
Expand Down Expand Up @@ -715,6 +713,7 @@ jobs:
# have been added because of Std compilation errors
echo "CXXFLAGS=-Wall -Wextra -pedantic -Wno-format -Werror -Wno-missing-field-initializers -Wno-deprecated-declarations" >> $GITHUB_ENV;
# Incompatible Vulkan Driver, so not configuring Vulkan samples.
- name: Configure
run: cmake
-G "${{matrix.GEN}}"
Expand All @@ -723,6 +722,7 @@ jobs:
-D BUILD_TESTING=ON
-D BUILD_EXAMPLES=ON
-D OPENCL_SDK_BUILD_SAMPLES=ON
-D OPENCL_SDK_BUILD_VULKAN_SAMPLES=OFF
-D CMAKE_C_STANDARD=${{matrix.STD.C}}
-D CMAKE_CXX_STANDARD=${{matrix.STD.CXX}}
-D CMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/install
Expand All @@ -737,10 +737,8 @@ jobs:
- name: Test
working-directory: ${{runner.workspace}}/OpenCL-SDK/build
run: |
# Incompatible Vulkan Driver
export EXCLUDE_REGEX="externalmemory.*"
OCL_ICD_VENDORS=/usr/local/etc/OpenCL/vendors ctest -C Debug --output-on-failure --no-tests=error --parallel `sysctl -n hw.logicalcpu` --exclude-regex "$EXCLUDE_REGEX"
OCL_ICD_VENDORS=/usr/local/etc/OpenCL/vendors ctest -C Release --output-on-failure --no-tests=error --parallel `sysctl -n hw.logicalcpu` --exclude-regex "$EXCLUDE_REGEX"
OCL_ICD_VENDORS=/usr/local/etc/OpenCL/vendors ctest -C Debug --output-on-failure --no-tests=error --parallel `sysctl -n hw.logicalcpu`"
OCL_ICD_VENDORS=/usr/local/etc/OpenCL/vendors ctest -C Release --output-on-failure --no-tests=error --parallel `sysctl -n hw.logicalcpu`"
- name: Install
run: |
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ project(OpenCL-SDK
include(CMakeDependentOption)
option(OPENCL_SDK_BUILD_SAMPLES "Build sample code" ON)
cmake_dependent_option(OPENCL_SDK_BUILD_OPENGL_SAMPLES "Build OpenCL-OpenGL interop sample code" ON OPENCL_SDK_BUILD_SAMPLES OFF)
cmake_dependent_option(OPENCL_SDK_BUILD_VULKAN_SAMPLES "Build OpenCL-Vulkan interop sample code" ON OPENCL_SDK_BUILD_SAMPLES OFF)
cmake_dependent_option(OPENCL_SDK_TEST_SAMPLES "Add CTest to samples (where applicable)" ON OPENCL_SDK_BUILD_SAMPLES OFF)

option(OPENCL_SDK_BUILD_CLINFO "Build clinfo utility" ON)
Expand Down
7 changes: 6 additions & 1 deletion cmake/Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ set(BUILD_SHARED_LIBS OFF CACHE BOOL "Global flag to cause add_library() to crea

# Fetch dependencies
if(OPENCL_SDK_BUILD_SAMPLES)
foreach(DEP IN ITEMS cargs TCLAP Stb Vulkan)
foreach(DEP IN ITEMS cargs TCLAP Stb)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/Dependencies/${DEP}")
include(${DEP})
endforeach()
Expand All @@ -45,6 +45,11 @@ if(OPENCL_SDK_BUILD_SAMPLES)
include(${DEP})
endforeach()
endif(OPENCL_SDK_BUILD_OPENGL_SAMPLES)

if(OPENCL_SDK_BUILD_VULKAN_SAMPLES)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/Dependencies/Vulkan")
include(Vulkan)
endif(OPENCL_SDK_BUILD_VULKAN_SAMPLES)
endif(OPENCL_SDK_BUILD_SAMPLES)

if(OPENCL_SDK_BUILD_CLINFO)
Expand Down
4 changes: 3 additions & 1 deletion samples/extensions/khr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

add_subdirectory(externalmemory)
add_subdirectory(histogram)
if(OPENCL_SDK_BUILD_OPENGL_SAMPLES)
add_subdirectory(conway)
add_subdirectory(nbody)
endif()
if(OPENCL_SDK_BUILD_VULKAN_SAMPLES)
add_subdirectory(externalmemory)
endif()
34 changes: 33 additions & 1 deletion samples/extensions/khr/externalmemory/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,19 @@ bool cl_check_external_memory_handle_type(
exit(EXIT_FAILURE);
}

// Vulkan function to export a:
// - POSIX file descriptor or
// - a Windows handle
// referencing the payload of a Vulkan device memory object.
const char* vk_get_memory_function_name =
#ifdef _WIN32
"vkGetMemoryWin32HandleKHR"

#else
"vkGetMemoryFdKHR"
#endif
;

int main(int argc, char* argv[])
{
cl_int error = CL_SUCCESS;
Expand Down Expand Up @@ -505,7 +518,7 @@ int main(int argc, char* argv[])
// from extension VK_KHR_external_memory_fd.
PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR =
(PFN_vkGetMemoryFdKHR)vkGetDeviceProcAddr(vk_device,
"vkGetMemoryFdKHR");
vk_get_memory_function_name);

VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_x, &fd_x));
VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_y, &fd_y));
Expand Down Expand Up @@ -552,6 +565,16 @@ int main(int argc, char* argv[])
OCLERROR_RET(clSetKernelArg(saxpy, 2, sizeof(cl_mem), &cl_buf_y), error,
clbufy);

// Acquire OpenCL memory objects created from Vulkan external memory
// handles.
cl_mem cl_mem_objects[] = { cl_buf_x, cl_buf_y };
clEnqueueAcquireExternalMemObjectsKHR_fn clEnqueueAcquireExternalMemObjects;
clEnqueueAcquireExternalMemObjects =
(clEnqueueAcquireExternalMemObjectsKHR_fn)
clGetExtensionFunctionAddressForPlatform(
cl_platform, "clEnqueueAcquireExternalMemObjectsKHR");
clEnqueueAcquireExternalMemObjects(queue, 2, cl_mem_objects, 0, NULL, NULL);

// Launch kernel.
if (diag_opts.verbose)
{
Expand All @@ -570,6 +593,15 @@ int main(int argc, char* argv[])
cl_ulong dev_time;
TIMER_DIFFERENCE(dev_time, dev_start, dev_end)

// Release OpenCL memory objects created from Vulkan external memory
// handles.
clEnqueueReleaseExternalMemObjectsKHR_fn
clEnqueueReleaseExternalMemObjects =
(clEnqueueReleaseExternalMemObjectsKHR_fn)
clGetExtensionFunctionAddressForPlatform(
cl_platform, "clEnqueueReleaseExternalMemObjectsKHR");
clEnqueueReleaseExternalMemObjects(queue, 2, cl_mem_objects, 0, NULL, NULL);

// Concurrently calculate reference saxpy.
if (diag_opts.verbose)
{
Expand Down
40 changes: 38 additions & 2 deletions samples/extensions/khr/externalmemory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ bool cl_check_external_memory_handle_type(
return it != supported_handle_types.end();
}

// Vulkan function to export a:
// - POSIX file descriptor or
// - a Windows handle
// referencing the payload of a Vulkan device memory object.
const std::string vk_get_memory_function_name =
#ifdef _WIN32
std::string{ "vkGetMemoryWin32HandleKHR" }

#else
std::string{ "vkGetMemoryFdKHR" }
#endif
;

int main(int argc, char* argv[])
{
try
Expand Down Expand Up @@ -423,8 +436,8 @@ int main(int argc, char* argv[])
// We need to get the pointer to the vkGetMemoryFdKHR function because
// it's from extension VK_KHR_external_memory_fd.
PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR =
(PFN_vkGetMemoryFdKHR)vkGetDeviceProcAddr(vk_device,
"vkGetMemoryFdKHR");
(PFN_vkGetMemoryFdKHR)vkGetDeviceProcAddr(
vk_device, vk_get_memory_function_name.c_str());

VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_x, &fd_x));
VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_y, &fd_y));
Expand Down Expand Up @@ -457,6 +470,18 @@ int main(int argc, char* argv[])
0 };
cl::CommandQueue queue{ cl_context, cl_device, *queue_props };

// Acquire OpenCL memory objects created from Vulkan external memory
// handles.
std::vector<cl_mem> cl_mem_objects = { cl_buf_x(), cl_buf_y() };
clEnqueueAcquireExternalMemObjectsKHR_fn
clEnqueueAcquireExternalMemObjects =
(clEnqueueAcquireExternalMemObjectsKHR_fn)
clGetExtensionFunctionAddressForPlatform(
cl_platform(), "clEnqueueAcquireExternalMemObjectsKHR");
clEnqueueAcquireExternalMemObjects(queue(), cl_mem_objects.size(),
cl_mem_objects.data(), 0, nullptr,
nullptr);

// Launch kernel.
if (diag_opts.verbose)
{
Expand All @@ -472,6 +497,17 @@ int main(int argc, char* argv[])
cl::WaitForEvents(kernel_run);
auto dev_end = std::chrono::high_resolution_clock::now();

// Release OpenCL memory objects created from Vulkan external memory
// handles.
clEnqueueReleaseExternalMemObjectsKHR_fn
clEnqueueReleaseExternalMemObjects =
(clEnqueueReleaseExternalMemObjectsKHR_fn)
clGetExtensionFunctionAddressForPlatform(
cl_platform(), "clEnqueueReleaseExternalMemObjectsKHR");
clEnqueueReleaseExternalMemObjects(queue(), cl_mem_objects.size(),
cl_mem_objects.data(), 0, nullptr,
nullptr);

// Concurrently calculate reference saxpy.
if (diag_opts.verbose)
{
Expand Down
24 changes: 12 additions & 12 deletions samples/extensions/khr/externalmemory/vulkan_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,24 +224,24 @@ find_suitable_device(VkInstance instance,
// Query OpenCL devices available.
cl_int error = CL_SUCCESS;
bool candidate_found = false;
cl_uint cl_platform_count = 0;
cl_uint platform_count = 0;
struct device_candidate found_candidate = {0};
OCLERROR_RET(clGetPlatformIDs(0, NULL, &cl_platform_count), error, ret);
OCLERROR_RET(clGetPlatformIDs(0, NULL, &platform_count), error, ret);

cl_platform_id* platforms =
(cl_platform_id*)malloc(cl_platform_count * sizeof(cl_platform_id));
OCLERROR_RET(clGetPlatformIDs(cl_platform_count, platforms, NULL), error,
(cl_platform_id*)malloc(platform_count * sizeof(cl_platform_id));
OCLERROR_RET(clGetPlatformIDs(platform_count, platforms, NULL), error,
platforms);

size_t cl_device_count = 0;
const char* uuid_khronos_extension[] = {
CL_KHR_DEVICE_UUID_EXTENSION_NAME
};
for (cl_uint cl_platform_id = 0; cl_platform_id < cl_platform_count;
++cl_platform_id)
for (cl_uint platform_id = 0; platform_id < platform_count;
++platform_id)
{
cl_uint cl_platform_devices_count = 0;
OCLERROR_RET(clGetDeviceIDs(platforms[cl_platform_id],
OCLERROR_RET(clGetDeviceIDs(platforms[platform_id],
CL_DEVICE_TYPE_ALL, 0, NULL,
&cl_platform_devices_count),
error, platforms);
Expand All @@ -250,7 +250,7 @@ find_suitable_device(VkInstance instance,
{
cl_device_id device;
OCLERROR_PAR(device = cl_util_get_device(
cl_platform_id, device_id, CL_DEVICE_TYPE_ALL, &error), error, platforms);
platform_id, device_id, CL_DEVICE_TYPE_ALL, &error), error, platforms);
cl_device_count +=
check_khronos_extensions(device, uuid_khronos_extension, 1);
}
Expand All @@ -264,11 +264,11 @@ find_suitable_device(VkInstance instance,
(struct cl_device_candidate*)malloc(
cl_device_count * sizeof(struct cl_device_candidate));
cl_device_count = 0;
for (cl_uint cl_platform_id = 0; cl_platform_id < cl_platform_count;
++cl_platform_id)
for (cl_uint platform_id = 0; platform_id < platform_count;
++platform_id)
{
cl_uint cl_platform_devices_count = 0;
OCLERROR_RET(clGetDeviceIDs(platforms[cl_platform_id],
OCLERROR_RET(clGetDeviceIDs(platforms[platform_id],
CL_DEVICE_TYPE_ALL, 0, NULL,
&cl_platform_devices_count),
error, candidates);
Expand All @@ -278,7 +278,7 @@ find_suitable_device(VkInstance instance,
++cl_candidate_id, ++cl_device_count)
{
cl_device_id device = cl_util_get_device(
cl_platform_id, cl_candidate_id, CL_DEVICE_TYPE_ALL, &error);
platform_id, cl_candidate_id, CL_DEVICE_TYPE_ALL, &error);
if (check_khronos_extensions(device, uuid_khronos_extension, 1))
{
cl_uchar vk_candidate_uuid[CL_UUID_SIZE_KHR];
Expand Down

0 comments on commit 1511cbd

Please sign in to comment.