From 1174b36668fffba5f9967e36c1b9e32fe79d0dad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A1bio?= Date: Fri, 18 Aug 2023 14:54:15 +0100 Subject: [PATCH] Add preview of the OpenCL adapter to the adapters branch (#809) --- CMakeLists.txt | 1 + README.md | 34 +++++------ source/adapters/CMakeLists.txt | 11 ++++ source/adapters/opencl/CMakeLists.txt | 86 +++++++++++++++++++++++++++ 4 files changed, 115 insertions(+), 17 deletions(-) create mode 100644 source/adapters/opencl/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 57a4ccec3d..fa0acc0bf1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,7 @@ option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" OFF) option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF) option(UR_BUILD_TOOLS "build ur tools" ON) option(UR_BUILD_ADAPTER_L0 "build level 0 adapter from SYCL" OFF) +option(UR_BUILD_ADAPTER_OPENCL "build opencl adapter from SYCL" OFF) option(UR_BUILD_ADAPTER_CUDA "build cuda adapter from SYCL" OFF) option(UR_BUILD_ADAPTER_HIP "build hip adapter from SYCL" OFF) diff --git a/README.md b/README.md index a213b15048..3dfe65c615 100644 --- a/README.md +++ b/README.md @@ -113,23 +113,23 @@ $ make List of options provided by CMake: -| Name | Description | Values | Default | -| - | - | - | - | -| UR_BUILD_TESTS | Build the tests | ON/OFF | ON | -| UR_BUILD_TOOLS | Build tools | ON/OFF | ON | -| UR_FORMAT_CPP_STYLE | Format code style | ON/OFF | OFF | -| UR_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF | -| UR_USE_ASAN | Enable AddressSanitizer | ON/OFF | OFF | -| UR_USE_TSAN | Enable ThreadSanitizer | ON/OFF | OFF | -| UR_USE_UBSAN | Enable UndefinedBehavior Sanitizer | ON/OFF | OFF | -| UR_USE_MSAN | Enable MemorySanitizer (clang only) | ON/OFF | OFF | -| UR_ENABLE_TRACING | Enable XPTI-based tracing layer | ON/OFF | OFF | -| UR_CONFORMANCE_TARGET_TRIPLES | SYCL triples to build CTS device binaries for | Comma-separated list | spir64 | -| UR_BUILD_TOOLS | Build tools | ON/OFF | ON | -| UR_BUILD_ADAPTER_L0 | Fetch and use level-zero adapter from SYCL | ON/OFF | OFF | -| UR_BUILD_ADAPTER_CUDA | Fetch and use cuda adapter from SYCL | ON/OFF | OFF | -| UR_BUILD_ADAPTER_HIP | Fetch and use hip adapter from SYCL | ON/OFF | OFF | -| UR_HIP_PLATFORM | Build hip adapter for AMD or NVIDIA platform | AMD/NVIDIA | AMD | +| Name | Description | Values | Default | +|-------------------------------|--------------------------------------------------------|----------------------|---------| +| UR_BUILD_TESTS | Build the tests | ON/OFF | ON | +| UR_BUILD_TOOLS | Build tools | ON/OFF | ON | +| UR_FORMAT_CPP_STYLE | Format code style | ON/OFF | OFF | +| UR_DEVELOPER_MODE | Treat warnings as errors and enables additional checks | ON/OFF | OFF | +| UR_USE_ASAN | Enable AddressSanitizer | ON/OFF | OFF | +| UR_USE_TSAN | Enable ThreadSanitizer | ON/OFF | OFF | +| UR_USE_UBSAN | Enable UndefinedBehavior Sanitizer | ON/OFF | OFF | +| UR_USE_MSAN | Enable MemorySanitizer (clang only) | ON/OFF | OFF | +| UR_ENABLE_TRACING | Enable XPTI-based tracing layer | ON/OFF | OFF | +| UR_CONFORMANCE_TARGET_TRIPLES | SYCL triples to build CTS device binaries for | Comma-separated list | spir64 | +| UR_BUILD_ADAPTER_L0 | Fetch and use level-zero adapter from SYCL | ON/OFF | OFF | +| UR_BUILD_ADAPTER_OPENCL | Fetch and use opencl adapter from SYCL | ON/OFF | OFF | +| UR_BUILD_ADAPTER_CUDA | Fetch and use cuda adapter from SYCL | ON/OFF | OFF | +| UR_BUILD_ADAPTER_HIP | Fetch and use hip adapter from SYCL | ON/OFF | OFF | +| UR_HIP_PLATFORM | Build hip adapter for AMD or NVIDIA platform | AMD/NVIDIA | AMD | ### Additional make targets To run automated code formatting, configure CMake with `UR_FORMAT_CPP_STYLE` option diff --git a/source/adapters/CMakeLists.txt b/source/adapters/CMakeLists.txt index 8de581ac68..5ff982c992 100644 --- a/source/adapters/CMakeLists.txt +++ b/source/adapters/CMakeLists.txt @@ -36,6 +36,13 @@ if(UR_BUILD_ADAPTER_L0 OR UR_BUILD_ADAPTER_CUDA OR UR_BUILD_ADAPTER_HIP) FetchSource(https://github.com/intel/llvm.git nightly-2023-08-01 "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR}) endif() +# Temporarily fetch the opencl adapter from a fork until the PR has been merged. +if(UR_BUILD_ADAPTER_OPENCL) + # fetch adapter sources from SYCL + set(SYCL_ADAPTER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/external") + FetchSource(https://github.com/fabiomestre/llvm.git opencl_adapter_unofficial "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR}) +endif() + if(UR_BUILD_ADAPTER_L0) add_subdirectory(level_zero) endif() @@ -47,3 +54,7 @@ endif() if(UR_BUILD_ADAPTER_HIP) add_subdirectory(hip) endif() + +if(UR_BUILD_ADAPTER_OPENCL) + add_subdirectory(opencl) +endif() diff --git a/source/adapters/opencl/CMakeLists.txt b/source/adapters/opencl/CMakeLists.txt new file mode 100644 index 0000000000..da9c988552 --- /dev/null +++ b/source/adapters/opencl/CMakeLists.txt @@ -0,0 +1,86 @@ +# Copyright (C) 2023 Intel Corporation +# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. +# See LICENSE.TXT +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +set(OPENCL_DIR "${SYCL_ADAPTER_DIR}/sycl/plugins/unified_runtime/ur/adapters/opencl" CACHE PATH "OpenCL adapter directory") + +set(TARGET_NAME ur_adapter_opencl) + +add_ur_adapter(${TARGET_NAME} + SHARED + ${OPENCL_DIR}/ur_interface_loader.cpp + ${OPENCL_DIR}/adapter.hpp + ${OPENCL_DIR}/adapter.cpp + ${OPENCL_DIR}/command_buffer.hpp + ${OPENCL_DIR}/command_buffer.cpp + ${OPENCL_DIR}/common.hpp + ${OPENCL_DIR}/common.cpp + ${OPENCL_DIR}/context.cpp + ${OPENCL_DIR}/context.hpp + ${OPENCL_DIR}/device.cpp + ${OPENCL_DIR}/device.hpp + ${OPENCL_DIR}/enqueue.cpp + ${OPENCL_DIR}/event.cpp + ${OPENCL_DIR}/image.cpp + ${OPENCL_DIR}/kernel.cpp + ${OPENCL_DIR}/memory.cpp + ${OPENCL_DIR}/platform.cpp + ${OPENCL_DIR}/platform.hpp + ${OPENCL_DIR}/program.cpp + ${OPENCL_DIR}/queue.cpp + ${OPENCL_DIR}/sampler.cpp + ${OPENCL_DIR}/usm.cpp + ${OPENCL_DIR}/usm_p2p.cpp + ${OPENCL_DIR}/../../ur.cpp + ${OPENCL_DIR}/../../ur.hpp +) + +set_target_properties(${TARGET_NAME} PROPERTIES + VERSION "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}" + SOVERSION "${PROJECT_VERSION_MAJOR}" +) + +find_package(Threads REQUIRED) + +if (NOT DEFINED OpenCL_LIBRARY OR NOT DEFINED OpenCL_INCLUDE_DIR) + message(WARNING "OpenCL_LIBRARY and OpenCL_INCLUDE_DIR are not set. Using find_package() to find an OpenCL installation in the system.") + find_package(OpenCL REQUIRED) +endif() + +message(STATUS "OpenCL_LIBRARY: ${OpenCL_LIBRARY}") +message(STATUS "OpenCL_INCLUDE_DIR: ${OpenCL_INCLUDE_DIR}") + +# Suppress a compiler message about undefined CL_TARGET_OPENCL_VERSION. +# Define all symbols up to OpenCL 3.0. +target_compile_definitions(ur_adapter_opencl PRIVATE CL_TARGET_OPENCL_VERSION=300) + +# Make imported library global to use it within the project. +add_library(OpenCL-ICD SHARED IMPORTED GLOBAL) + +if (WIN32) + set_target_properties( + OpenCL-ICD PROPERTIES + IMPORTED_IMPLIB ${OpenCL_LIBRARY} + INTERFACE_INCLUDE_DIRECTORIES ${OpenCL_INCLUDE_DIR} + ) +else() + set_target_properties( + OpenCL-ICD PROPERTIES + IMPORTED_LOCATION ${OpenCL_LIBRARY} + INTERFACE_INCLUDE_DIRECTORIES ${OpenCL_INCLUDE_DIR} + ) +endif() + +target_link_libraries(${TARGET_NAME} PRIVATE + ${PROJECT_NAME}::headers + ${PROJECT_NAME}::common + ${PROJECT_NAME}::unified_malloc_framework + Threads::Threads + OpenCL-ICD +) + +target_include_directories(${TARGET_NAME} PRIVATE + ${OPENCL_DIR}/../../../ + ${OpenCL_INCLUDE_DIR} +)