Skip to content

Commit

Permalink
Merge branch 'adapters' into ewan/L0_internal_event_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
EwanC committed Oct 23, 2023
2 parents 6523932 + 614e6d0 commit 9a13afd
Show file tree
Hide file tree
Showing 31 changed files with 5,330 additions and 63 deletions.
8 changes: 8 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,11 @@ source/adapters/level_zero @oneapi-src/unified-runtime-level-zero-write
# CUDA and HIP adapters
source/adapters/cuda @oneapi-src/unified-runtime-cuda-write
source/adapters/hip @oneapi-src/unified-runtime-hip-write

# OpenCL adapter
source/adapters/opencl @oneapi-src/unified-runtime-opencl-write

# Command-buffer experimental feature
source/adapters/**/command_buffer.* @oneapi-src/unified-runtime-command-buffer-write
scripts/core/EXP-COMMAND-BUFFER.rst @oneapi-src/unified-runtime-command-buffer-write
scripts/core/exp-command-buffer.yml @oneapi-src/unified-runtime-command-buffer-write
3 changes: 0 additions & 3 deletions source/adapters/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,5 @@ if(UR_BUILD_ADAPTER_HIP)
endif()

if(UR_BUILD_ADAPTER_OPENCL)
# Temporarily fetch the opencl adapter from a fork until the PR has been merged.
set(SYCL_ADAPTER_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/opencl")
FetchSource(https://github.com/fabiomestre/llvm.git opencl_adapter_unofficial "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR})
add_subdirectory(opencl)
endif()
1 change: 1 addition & 0 deletions source/adapters/hip/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName,
UR_APIEXPORT ur_result_t UR_APICALL
urContextRelease(ur_context_handle_t hContext) {
if (hContext->decrementReferenceCount() == 0) {
hContext->invokeExtendedDeleters();
delete hContext;
}
return UR_RESULT_SUCCESS;
Expand Down
16 changes: 10 additions & 6 deletions source/adapters/level_zero/usm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ static ur_result_t USMDeviceAllocImpl(void **ResultPtr,
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
UR_RESULT_ERROR_INVALID_VALUE);

return USMAllocationMakeResident(USMDeviceAllocationForceResidency, Context,
Device, *ResultPtr, Size);
USMAllocationMakeResident(USMDeviceAllocationForceResidency, Context, Device,
*ResultPtr, Size);
return UR_RESULT_SUCCESS;
}

static ur_result_t USMSharedAllocImpl(void **ResultPtr,
Expand Down Expand Up @@ -224,9 +225,11 @@ static ur_result_t USMSharedAllocImpl(void **ResultPtr,
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
UR_RESULT_ERROR_INVALID_VALUE);

USMAllocationMakeResident(USMSharedAllocationForceResidency, Context, Device,
*ResultPtr, Size);

// TODO: Handle PI_MEM_ALLOC_DEVICE_READ_ONLY.
return USMAllocationMakeResident(USMSharedAllocationForceResidency, Context,
Device, *ResultPtr, Size);
return UR_RESULT_SUCCESS;
}

static ur_result_t USMHostAllocImpl(void **ResultPtr,
Expand All @@ -244,8 +247,9 @@ static ur_result_t USMHostAllocImpl(void **ResultPtr,
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
UR_RESULT_ERROR_INVALID_VALUE);

return USMAllocationMakeResident(USMHostAllocationForceResidency, Context,
nullptr, *ResultPtr, Size);
USMAllocationMakeResident(USMHostAllocationForceResidency, Context, nullptr,
*ResultPtr, Size);
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc(
Expand Down
4 changes: 4 additions & 0 deletions source/adapters/opencl/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
Language: Cpp
BasedOnStyle: LLVM
...
85 changes: 34 additions & 51 deletions source/adapters/opencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
# 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(OPENCL_DIR "${CMAKE_CURRENT_SOURCE_DIR}" 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
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.hpp
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/common.hpp
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
${CMAKE_CURRENT_SOURCE_DIR}/context.hpp
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp
${CMAKE_CURRENT_SOURCE_DIR}/device.hpp
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
${CMAKE_CURRENT_SOURCE_DIR}/enqueue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/event.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image.cpp
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/memory.cpp
${CMAKE_CURRENT_SOURCE_DIR}/platform.hpp
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp
${CMAKE_CURRENT_SOURCE_DIR}/sampler.cpp
${CMAKE_CURRENT_SOURCE_DIR}/usm.cpp
${CMAKE_CURRENT_SOURCE_DIR}/usm_p2p.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.hpp
)

set_target_properties(${TARGET_NAME} PROPERTIES
Expand All @@ -43,44 +43,27 @@ set_target_properties(${TARGET_NAME} PROPERTIES

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.")
# The OpenCL target can be set manually on upstream cmake to avoid using find_package().
if (NOT UR_OPENCL_ICD_LOADER_LIBRARY)
find_package(OpenCL REQUIRED)
message(STATUS "OpenCL_LIBRARY: ${OpenCL_LIBRARY}")
message(STATUS "OpenCL_INCLUDE_DIR: ${OpenCL_INCLUDE_DIR}")
set(UR_OPENCL_ICD_LOADER_LIBRARY OpenCL::OpenCL)
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_compile_definitions(ur_adapter_opencl PRIVATE CL_TARGET_OPENCL_VERSION=300 CL_USE_DEPRECATED_OPENCL_1_2_APIS)

target_link_libraries(${TARGET_NAME} PRIVATE
${PROJECT_NAME}::headers
${PROJECT_NAME}::common
${PROJECT_NAME}::unified_malloc_framework
Threads::Threads
OpenCL-ICD
${UR_OPENCL_ICD_LOADER_LIBRARY}
)

target_include_directories(${TARGET_NAME} PRIVATE
${OPENCL_DIR}/../../../
"${CMAKE_CURRENT_SOURCE_DIR}/../../"
${OpenCL_INCLUDE_DIR}
)
82 changes: 82 additions & 0 deletions source/adapters/opencl/adapter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//===-------------- adapter.cpp - OpenCL Adapter ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "common.hpp"

struct ur_adapter_handle_t_ {
std::atomic<uint32_t> RefCount = 0;
};

ur_adapter_handle_t_ adapter{};

UR_APIEXPORT ur_result_t UR_APICALL urInit(ur_device_init_flags_t,
ur_loader_config_handle_t) {
cl_ext::ExtFuncPtrCache = new cl_ext::ExtFuncPtrCacheT();
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urTearDown(void *) {
if (cl_ext::ExtFuncPtrCache) {
delete cl_ext::ExtFuncPtrCache;
cl_ext::ExtFuncPtrCache = nullptr;
}
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL
urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
uint32_t *pNumAdapters) {
if (NumEntries > 0 && phAdapters) {
*phAdapters = &adapter;
}

if (pNumAdapters) {
*pNumAdapters = 1;
}

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
++adapter.RefCount;
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
--adapter.RefCount;
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError(
ur_adapter_handle_t, const char **ppMessage, int32_t *pError) {
*ppMessage = cl_adapter::ErrorMessage;
*pError = cl_adapter::ErrorMessageCode;

return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
ur_adapter_info_t propName,
size_t propSize,
void *pPropValue,
size_t *pPropSizeRet) {
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);

switch (propName) {
case UR_ADAPTER_INFO_BACKEND:
return ReturnValue(UR_ADAPTER_BACKEND_CUDA);
case UR_ADAPTER_INFO_REFERENCE_COUNT:
return ReturnValue(adapter.RefCount.load());
default:
return UR_RESULT_ERROR_INVALID_ENUMERATION;
}

return UR_RESULT_SUCCESS;
}
13 changes: 13 additions & 0 deletions source/adapters/opencl/adapter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===-------------- adapter.hpp - OpenCL Adapter ---------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

struct ur_adapter_handle_t_;

extern ur_adapter_handle_t_ adapter;
Loading

0 comments on commit 9a13afd

Please sign in to comment.