Skip to content

Commit

Permalink
Fix opencl version check
Browse files Browse the repository at this point in the history
  • Loading branch information
Beanavil committed Oct 1, 2024
1 parent e362cc5 commit fb99610
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 78 deletions.
90 changes: 19 additions & 71 deletions samples/extensions/khr/externalmemory/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,13 @@ bool cl_check_external_memory_handle_type(
exit(EXIT_FAILURE);
}

cl_int opencl_version_contains(const char* dev_version,
const char* version_fragment)
{
char* found_version = strstr(dev_version, version_fragment);
return (found_version != NULL);
}

int main(int argc, char* argv[])
{
cl_int error = CL_SUCCESS;
Expand Down Expand Up @@ -430,6 +437,7 @@ int main(int argc, char* argv[])
buffer_info.size = sizeof(cl_float) * length;
buffer_info.usage =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
;
buffer_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;

VkBuffer vk_buf_x, vk_buf_y;
Expand Down Expand Up @@ -484,35 +492,6 @@ int main(int argc, char* argv[])
memcpy(vk_arr_x, arr_x, sizeof(cl_float) * length);
memcpy(vk_arr_y, arr_y, sizeof(cl_float) * length);

#ifdef _WIN32
// Get Vulkan external memory file descriptors for accessing external memory
// with OpenCL.
VkMemoryGetWin32HandleInfoKHR handle_info_x = { 0 };
handle_info_x.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR;
handle_info_x.pNext = NULL;
handle_info_x.memory = vk_buf_x_memory;
handle_info_x.handleType = vk_external_memory_handle_type;
HANDLE handle_x;

VkMemoryGetWin32HandleInfoKHR handle_info_y = { 0 };
handle_info_y.sType = VK_STRUCTURE_TYPE_MEMORY_GET_WIN32_HANDLE_INFO_KHR;
handle_info_y.pNext = NULL;
handle_info_y.memory = vk_buf_y_memory;
handle_info_y.handleType = vk_external_memory_handle_type;
HANDLE handle_y;

// We need to get the pointer to the
// vkGetMemoryFdKHR/vkGetMemoryWin32HandleKHR function because it's from
// extension VK_KHR_external_memory_fd. This Vulkan function exports a POSIX
// file descriptor/Windows handle referencing the payload of a Vulkan device
// memory object.
PFN_vkGetMemoryWin32HandleKHR vkGetMemoryWin32Handle;
*(PFN_vkGetMemoryWin32HandleKHR*)&vkGetMemoryWin32Handle =
(PFN_vkGetMemoryWin32HandleKHR)vkGetDeviceProcAddr(
vk_device, "vkGetMemoryWin32HandleKHR");
VK_CHECK(vkGetMemoryWin32Handle(vk_device, &handle_info_x, &handle_x));
VK_CHECK(vkGetMemoryWin32Handle(vk_device, &handle_info_y, &handle_y));
#else
// Get Vulkan external memory file descriptors for accessing external memory
// with OpenCL.
VkMemoryGetFdInfoKHR fd_info_x = { 0 };
Expand All @@ -529,43 +508,30 @@ int main(int argc, char* argv[])
fd_info_y.handleType = vk_external_memory_handle_type;
int fd_y;

// We need to get the pointer to the
// vkGetMemoryFdKHR/vkGetMemoryWin32HandleKHR function because it's from
// extension VK_KHR_external_memory_fd. This Vulkan function exports a POSIX
// file descriptor/Windows handle referencing the payload of a Vulkan device
// memory object.
PFN_vkGetMemoryFdKHR vkGetMemoryFd;
*(PFN_vkGetMemoryFdKHR*)&vkGetMemoryFd =
// 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");
VK_CHECK(vkGetMemoryFd(vk_device, &fd_info_x, &fd_x));
VK_CHECK(vkGetMemoryFd(vk_device, &fd_info_y, &fd_y));
#endif

VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_x, &fd_x));
VK_CHECK(vkGetMemoryFdKHR(vk_device, &fd_info_y, &fd_y));

// Create OpenCL buffers from Vulkan external memory file descriptors.
cl_mem_properties ext_mem_props_x[] = {
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR,
#ifdef _WIN32
(cl_mem_properties)handle_x,
#else
(cl_mem_properties)fd_x,
#endif
(cl_mem_properties)CL_MEM_DEVICE_HANDLE_LIST_KHR,
(cl_mem_properties)CL_DEVICE_HANDLE_LIST_KHR,
(cl_mem_properties)(uintptr_t)cl_device,
CL_MEM_DEVICE_HANDLE_LIST_END_KHR,
CL_DEVICE_HANDLE_LIST_END_KHR,
0
};
cl_mem_properties ext_mem_props_y[] = {
(cl_mem_properties)CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_FD_KHR,
#ifdef _WIN32
(cl_mem_properties)handle_y,
#else
(cl_mem_properties)fd_y,
#endif
(cl_mem_properties)CL_MEM_DEVICE_HANDLE_LIST_KHR,
(cl_mem_properties)CL_DEVICE_HANDLE_LIST_KHR,
(cl_mem_properties)(uintptr_t)cl_device,
CL_MEM_DEVICE_HANDLE_LIST_END_KHR,
CL_DEVICE_HANDLE_LIST_END_KHR,
0
};
cl_mem cl_buf_x, cl_buf_y;
Expand Down Expand Up @@ -593,16 +559,6 @@ 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 =
(clEnqueueAcquireExternalMemObjectsKHR_fn)
clGetExtensionFunctionAddressForPlatform(
cl_platform, "clEnqueueAcquireExternalMemObjectsKHR");
clEnqueueAcquireExternalMemObjects(queue, 2, cl_mem_objects, 0, NULL, NULL);

// Launch kernel.
if (diag_opts.verbose)
{
Expand All @@ -621,15 +577,6 @@ 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 Expand Up @@ -671,7 +618,8 @@ int main(int argc, char* argv[])
printf("Kernel execution time as seen by host: %llu us.\n",
(unsigned long long)(dev_time + 500) / 1000);

printf("Kernel execution time as measured by device: %llu us.\n",
printf("Kernel execution time as measured by device:\n");
printf("\t%llu us.\n",
(unsigned long long)(cl_util_get_event_duration(
kernel_run, CL_PROFILING_COMMAND_START,
CL_PROFILING_COMMAND_END, &error)
Expand Down
25 changes: 18 additions & 7 deletions samples/extensions/khr/externalmemory/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ bool cl_check_external_memory_handle_type(
return it != supported_handle_types.end();
}

bool opencl_version_contains(const cl::string& dev_version,
const cl::string& version_fragment)
{
return dev_version.find(version_fragment) != cl::string::npos;
}

int main(int argc, char* argv[])
{
try
Expand Down Expand Up @@ -267,13 +273,18 @@ int main(int argc, char* argv[])

// The Khronos extension showcased requires OpenCL 3.0 version.
cl::string compiler_options = "";
#if CL_HPP_TARGET_OPENCL_VERSION >= 300
compiler_options += cl::string{ "-cl-std=CL3.0 " };
#else
sdt::cerr << "\nError: OpenCL version must be at least 3.0"
<< std::endl;
exit(EXIT_FAILURE);
#endif
const std::string dev_version =
cl_device.getInfo<CL_DEVICE_OPENCL_C_VERSION>();
if (opencl_version_contains(dev_version, "3."))
{
cl::string{ "-cl-std=CL3.0 " };
}
else
{
std::cerr << "\nError: OpenCL version must be at least 3.0"
<< std::endl;
exit(EXIT_FAILURE);
}

cl_program.build(cl_device, compiler_options.c_str());

Expand Down

0 comments on commit fb99610

Please sign in to comment.