Skip to content

Commit 9a13afd

Browse files
committed
Merge branch 'adapters' into ewan/L0_internal_event_fix
2 parents 6523932 + 614e6d0 commit 9a13afd

31 files changed

+5330
-63
lines changed

.github/CODEOWNERS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,11 @@ source/adapters/level_zero @oneapi-src/unified-runtime-level-zero-write
66
# CUDA and HIP adapters
77
source/adapters/cuda @oneapi-src/unified-runtime-cuda-write
88
source/adapters/hip @oneapi-src/unified-runtime-hip-write
9+
10+
# OpenCL adapter
11+
source/adapters/opencl @oneapi-src/unified-runtime-opencl-write
12+
13+
# Command-buffer experimental feature
14+
source/adapters/**/command_buffer.* @oneapi-src/unified-runtime-command-buffer-write
15+
scripts/core/EXP-COMMAND-BUFFER.rst @oneapi-src/unified-runtime-command-buffer-write
16+
scripts/core/exp-command-buffer.yml @oneapi-src/unified-runtime-command-buffer-write

source/adapters/CMakeLists.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,5 @@ if(UR_BUILD_ADAPTER_HIP)
4545
endif()
4646

4747
if(UR_BUILD_ADAPTER_OPENCL)
48-
# Temporarily fetch the opencl adapter from a fork until the PR has been merged.
49-
set(SYCL_ADAPTER_DIR "${CMAKE_CURRENT_BINARY_DIR}/external/opencl")
50-
FetchSource(https://github.com/fabiomestre/llvm.git opencl_adapter_unofficial "sycl/plugins/unified_runtime/ur" ${SYCL_ADAPTER_DIR})
5148
add_subdirectory(opencl)
5249
endif()

source/adapters/hip/context.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName,
108108
UR_APIEXPORT ur_result_t UR_APICALL
109109
urContextRelease(ur_context_handle_t hContext) {
110110
if (hContext->decrementReferenceCount() == 0) {
111+
hContext->invokeExtendedDeleters();
111112
delete hContext;
112113
}
113114
return UR_RESULT_SUCCESS;

source/adapters/level_zero/usm.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,9 @@ static ur_result_t USMDeviceAllocImpl(void **ResultPtr,
192192
reinterpret_cast<std::uintptr_t>(*ResultPtr) % Alignment == 0,
193193
UR_RESULT_ERROR_INVALID_VALUE);
194194

195-
return USMAllocationMakeResident(USMDeviceAllocationForceResidency, Context,
196-
Device, *ResultPtr, Size);
195+
USMAllocationMakeResident(USMDeviceAllocationForceResidency, Context, Device,
196+
*ResultPtr, Size);
197+
return UR_RESULT_SUCCESS;
197198
}
198199

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

228+
USMAllocationMakeResident(USMSharedAllocationForceResidency, Context, Device,
229+
*ResultPtr, Size);
230+
227231
// TODO: Handle PI_MEM_ALLOC_DEVICE_READ_ONLY.
228-
return USMAllocationMakeResident(USMSharedAllocationForceResidency, Context,
229-
Device, *ResultPtr, Size);
232+
return UR_RESULT_SUCCESS;
230233
}
231234

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

247-
return USMAllocationMakeResident(USMHostAllocationForceResidency, Context,
248-
nullptr, *ResultPtr, Size);
250+
USMAllocationMakeResident(USMHostAllocationForceResidency, Context, nullptr,
251+
*ResultPtr, Size);
252+
return UR_RESULT_SUCCESS;
249253
}
250254

251255
UR_APIEXPORT ur_result_t UR_APICALL urUSMHostAlloc(

source/adapters/opencl/.clang-format

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
Language: Cpp
3+
BasedOnStyle: LLVM
4+
...

source/adapters/opencl/CMakeLists.txt

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,37 @@
33
# See LICENSE.TXT
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6-
set(OPENCL_DIR "${SYCL_ADAPTER_DIR}/sycl/plugins/unified_runtime/ur/adapters/opencl" CACHE PATH "OpenCL adapter directory")
6+
set(OPENCL_DIR "${CMAKE_CURRENT_SOURCE_DIR}" CACHE PATH "OpenCL adapter directory")
77

88
set(TARGET_NAME ur_adapter_opencl)
99

1010
add_ur_adapter(${TARGET_NAME}
1111
SHARED
12-
${OPENCL_DIR}/ur_interface_loader.cpp
13-
${OPENCL_DIR}/adapter.hpp
14-
${OPENCL_DIR}/adapter.cpp
15-
${OPENCL_DIR}/command_buffer.hpp
16-
${OPENCL_DIR}/command_buffer.cpp
17-
${OPENCL_DIR}/common.hpp
18-
${OPENCL_DIR}/common.cpp
19-
${OPENCL_DIR}/context.cpp
20-
${OPENCL_DIR}/context.hpp
21-
${OPENCL_DIR}/device.cpp
22-
${OPENCL_DIR}/device.hpp
23-
${OPENCL_DIR}/enqueue.cpp
24-
${OPENCL_DIR}/event.cpp
25-
${OPENCL_DIR}/image.cpp
26-
${OPENCL_DIR}/kernel.cpp
27-
${OPENCL_DIR}/memory.cpp
28-
${OPENCL_DIR}/platform.cpp
29-
${OPENCL_DIR}/platform.hpp
30-
${OPENCL_DIR}/program.cpp
31-
${OPENCL_DIR}/queue.cpp
32-
${OPENCL_DIR}/sampler.cpp
33-
${OPENCL_DIR}/usm.cpp
34-
${OPENCL_DIR}/usm_p2p.cpp
35-
${OPENCL_DIR}/../../ur.cpp
36-
${OPENCL_DIR}/../../ur.hpp
12+
${CMAKE_CURRENT_SOURCE_DIR}/ur_interface_loader.cpp
13+
${CMAKE_CURRENT_SOURCE_DIR}/adapter.hpp
14+
${CMAKE_CURRENT_SOURCE_DIR}/adapter.cpp
15+
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.hpp
16+
${CMAKE_CURRENT_SOURCE_DIR}/command_buffer.cpp
17+
${CMAKE_CURRENT_SOURCE_DIR}/common.hpp
18+
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
19+
${CMAKE_CURRENT_SOURCE_DIR}/context.hpp
20+
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp
21+
${CMAKE_CURRENT_SOURCE_DIR}/device.hpp
22+
${CMAKE_CURRENT_SOURCE_DIR}/device.cpp
23+
${CMAKE_CURRENT_SOURCE_DIR}/enqueue.cpp
24+
${CMAKE_CURRENT_SOURCE_DIR}/event.cpp
25+
${CMAKE_CURRENT_SOURCE_DIR}/image.cpp
26+
${CMAKE_CURRENT_SOURCE_DIR}/kernel.cpp
27+
${CMAKE_CURRENT_SOURCE_DIR}/memory.cpp
28+
${CMAKE_CURRENT_SOURCE_DIR}/platform.hpp
29+
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
30+
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
31+
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp
32+
${CMAKE_CURRENT_SOURCE_DIR}/sampler.cpp
33+
${CMAKE_CURRENT_SOURCE_DIR}/usm.cpp
34+
${CMAKE_CURRENT_SOURCE_DIR}/usm_p2p.cpp
35+
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
36+
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.hpp
3737
)
3838

3939
set_target_properties(${TARGET_NAME} PROPERTIES
@@ -43,44 +43,27 @@ set_target_properties(${TARGET_NAME} PROPERTIES
4343

4444
find_package(Threads REQUIRED)
4545

46-
if (NOT DEFINED OpenCL_LIBRARY OR NOT DEFINED OpenCL_INCLUDE_DIR)
47-
message(WARNING "OpenCL_LIBRARY and OpenCL_INCLUDE_DIR are not set. Using find_package() to find an OpenCL installation in the system.")
46+
# The OpenCL target can be set manually on upstream cmake to avoid using find_package().
47+
if (NOT UR_OPENCL_ICD_LOADER_LIBRARY)
4848
find_package(OpenCL REQUIRED)
49+
message(STATUS "OpenCL_LIBRARY: ${OpenCL_LIBRARY}")
50+
message(STATUS "OpenCL_INCLUDE_DIR: ${OpenCL_INCLUDE_DIR}")
51+
set(UR_OPENCL_ICD_LOADER_LIBRARY OpenCL::OpenCL)
4952
endif()
5053

51-
message(STATUS "OpenCL_LIBRARY: ${OpenCL_LIBRARY}")
52-
message(STATUS "OpenCL_INCLUDE_DIR: ${OpenCL_INCLUDE_DIR}")
53-
5454
# Suppress a compiler message about undefined CL_TARGET_OPENCL_VERSION.
5555
# Define all symbols up to OpenCL 3.0.
56-
target_compile_definitions(ur_adapter_opencl PRIVATE CL_TARGET_OPENCL_VERSION=300)
57-
58-
# Make imported library global to use it within the project.
59-
add_library(OpenCL-ICD SHARED IMPORTED GLOBAL)
60-
61-
if (WIN32)
62-
set_target_properties(
63-
OpenCL-ICD PROPERTIES
64-
IMPORTED_IMPLIB ${OpenCL_LIBRARY}
65-
INTERFACE_INCLUDE_DIRECTORIES ${OpenCL_INCLUDE_DIR}
66-
)
67-
else()
68-
set_target_properties(
69-
OpenCL-ICD PROPERTIES
70-
IMPORTED_LOCATION ${OpenCL_LIBRARY}
71-
INTERFACE_INCLUDE_DIRECTORIES ${OpenCL_INCLUDE_DIR}
72-
)
73-
endif()
56+
target_compile_definitions(ur_adapter_opencl PRIVATE CL_TARGET_OPENCL_VERSION=300 CL_USE_DEPRECATED_OPENCL_1_2_APIS)
7457

7558
target_link_libraries(${TARGET_NAME} PRIVATE
7659
${PROJECT_NAME}::headers
7760
${PROJECT_NAME}::common
7861
${PROJECT_NAME}::unified_malloc_framework
7962
Threads::Threads
80-
OpenCL-ICD
63+
${UR_OPENCL_ICD_LOADER_LIBRARY}
8164
)
8265

8366
target_include_directories(${TARGET_NAME} PRIVATE
84-
${OPENCL_DIR}/../../../
67+
"${CMAKE_CURRENT_SOURCE_DIR}/../../"
8568
${OpenCL_INCLUDE_DIR}
8669
)

source/adapters/opencl/adapter.cpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//===-------------- adapter.cpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Copyright (C) 2023 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
#include "common.hpp"
12+
13+
struct ur_adapter_handle_t_ {
14+
std::atomic<uint32_t> RefCount = 0;
15+
};
16+
17+
ur_adapter_handle_t_ adapter{};
18+
19+
UR_APIEXPORT ur_result_t UR_APICALL urInit(ur_device_init_flags_t,
20+
ur_loader_config_handle_t) {
21+
cl_ext::ExtFuncPtrCache = new cl_ext::ExtFuncPtrCacheT();
22+
return UR_RESULT_SUCCESS;
23+
}
24+
25+
UR_APIEXPORT ur_result_t UR_APICALL urTearDown(void *) {
26+
if (cl_ext::ExtFuncPtrCache) {
27+
delete cl_ext::ExtFuncPtrCache;
28+
cl_ext::ExtFuncPtrCache = nullptr;
29+
}
30+
return UR_RESULT_SUCCESS;
31+
}
32+
33+
UR_APIEXPORT ur_result_t UR_APICALL
34+
urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters,
35+
uint32_t *pNumAdapters) {
36+
if (NumEntries > 0 && phAdapters) {
37+
*phAdapters = &adapter;
38+
}
39+
40+
if (pNumAdapters) {
41+
*pNumAdapters = 1;
42+
}
43+
44+
return UR_RESULT_SUCCESS;
45+
}
46+
47+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
48+
++adapter.RefCount;
49+
return UR_RESULT_SUCCESS;
50+
}
51+
52+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) {
53+
--adapter.RefCount;
54+
return UR_RESULT_SUCCESS;
55+
}
56+
57+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetLastError(
58+
ur_adapter_handle_t, const char **ppMessage, int32_t *pError) {
59+
*ppMessage = cl_adapter::ErrorMessage;
60+
*pError = cl_adapter::ErrorMessageCode;
61+
62+
return UR_RESULT_SUCCESS;
63+
}
64+
65+
UR_APIEXPORT ur_result_t UR_APICALL urAdapterGetInfo(ur_adapter_handle_t,
66+
ur_adapter_info_t propName,
67+
size_t propSize,
68+
void *pPropValue,
69+
size_t *pPropSizeRet) {
70+
UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet);
71+
72+
switch (propName) {
73+
case UR_ADAPTER_INFO_BACKEND:
74+
return ReturnValue(UR_ADAPTER_BACKEND_CUDA);
75+
case UR_ADAPTER_INFO_REFERENCE_COUNT:
76+
return ReturnValue(adapter.RefCount.load());
77+
default:
78+
return UR_RESULT_ERROR_INVALID_ENUMERATION;
79+
}
80+
81+
return UR_RESULT_SUCCESS;
82+
}

source/adapters/opencl/adapter.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//===-------------- adapter.hpp - OpenCL Adapter ---------------------===//
2+
//
3+
// Copyright (C) 2023 Intel Corporation
4+
//
5+
// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
6+
// Exceptions. See LICENSE.TXT
7+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8+
//
9+
//===----------------------------------------------------------------------===//
10+
11+
struct ur_adapter_handle_t_;
12+
13+
extern ur_adapter_handle_t_ adapter;

0 commit comments

Comments
 (0)