Skip to content

Commit ccf3a88

Browse files
committed
[L0 v2] implement USM allocation functions using UMF
Use UMF L0 provider
1 parent c2c1bea commit ccf3a88

17 files changed

+1328
-1191
lines changed

cmake/FetchLevelZero.cmake

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# Copyright (C) 2024 Intel Corporation
2+
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
3+
# See LICENSE.TXT
4+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5+
6+
set(UR_LEVEL_ZERO_LOADER_LIBRARY "" CACHE FILEPATH "Path of the Level Zero Loader library")
7+
set(UR_LEVEL_ZERO_INCLUDE_DIR "" CACHE FILEPATH "Directory containing the Level Zero Headers")
8+
set(UR_LEVEL_ZERO_LOADER_REPO "" CACHE STRING "Github repo to get the Level Zero loader sources from")
9+
set(UR_LEVEL_ZERO_LOADER_TAG "" CACHE STRING " GIT tag of the Level Loader taken from github repo")
10+
11+
# Copy Level Zero loader/headers locally to the build to avoid leaking their path.
12+
set(LEVEL_ZERO_COPY_DIR ${CMAKE_CURRENT_BINARY_DIR}/level_zero_loader)
13+
if (NOT UR_LEVEL_ZERO_LOADER_LIBRARY STREQUAL "")
14+
get_filename_component(LEVEL_ZERO_LIB_NAME "${UR_LEVEL_ZERO_LOADER_LIBRARY}" NAME)
15+
set(LEVEL_ZERO_LIBRARY ${LEVEL_ZERO_COPY_DIR}/${LEVEL_ZERO_LIB_NAME})
16+
message(STATUS "Level Zero Adapter: Copying Level Zero loader to local build tree")
17+
file(COPY ${UR_LEVEL_ZERO_LOADER_LIBRARY} DESTINATION ${LEVEL_ZERO_COPY_DIR} FOLLOW_SYMLINK_CHAIN)
18+
endif()
19+
if (NOT UR_LEVEL_ZERO_INCLUDE_DIR STREQUAL "")
20+
set(LEVEL_ZERO_INCLUDE_DIR ${LEVEL_ZERO_COPY_DIR})
21+
message(STATUS "Level Zero Adapter: Copying Level Zero headers to local build tree")
22+
file(COPY ${UR_LEVEL_ZERO_INCLUDE_DIR}/ DESTINATION ${LEVEL_ZERO_COPY_DIR})
23+
endif()
24+
25+
if (NOT DEFINED LEVEL_ZERO_LIBRARY OR NOT DEFINED LEVEL_ZERO_INCLUDE_DIR)
26+
message(STATUS "Level Zero Adapter: Download Level Zero loader and headers from github.com")
27+
28+
# Workaround warnings/errors for Level Zero build
29+
set(CMAKE_CXX_FLAGS_BAK "${CMAKE_CXX_FLAGS}")
30+
if (UNIX)
31+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-variable")
32+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic")
33+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-truncation")
34+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
35+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-c++98-compat-extra-semi")
36+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option")
37+
endif()
38+
39+
if (UR_LEVEL_ZERO_LOADER_REPO STREQUAL "")
40+
set(UR_LEVEL_ZERO_LOADER_REPO "https://github.com/oneapi-src/level-zero.git")
41+
endif()
42+
if (UR_LEVEL_ZERO_LOADER_TAG STREQUAL "")
43+
set(UR_LEVEL_ZERO_LOADER_TAG v1.17.6)
44+
endif()
45+
46+
# Disable due to a bug https://github.com/oneapi-src/level-zero/issues/104
47+
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
48+
# Prevent L0 loader from exporting extra symbols
49+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
50+
51+
message(STATUS "Level Zero Adapter: Will fetch Level Zero Loader from ${UR_LEVEL_ZERO_LOADER_REPO}")
52+
include(FetchContent)
53+
FetchContent_Declare(level-zero-loader
54+
GIT_REPOSITORY ${UR_LEVEL_ZERO_LOADER_REPO}
55+
GIT_TAG ${UR_LEVEL_ZERO_LOADER_TAG}
56+
)
57+
if(MSVC)
58+
set(USE_Z7 ON)
59+
endif()
60+
FetchContent_MakeAvailable(level-zero-loader)
61+
FetchContent_GetProperties(level-zero-loader)
62+
63+
# Restore original flags
64+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BAK}")
65+
66+
target_compile_options(ze_loader PRIVATE
67+
$<$<IN_LIST:$<CXX_COMPILER_ID>,GNU;Clang;Intel;IntelLLVM>:-Wno-error>
68+
$<$<CXX_COMPILER_ID:MSVC>:/WX- /UUNICODE>
69+
)
70+
71+
set(LEVEL_ZERO_LIBRARY ze_loader)
72+
set(LEVEL_ZERO_INCLUDE_DIR
73+
${level-zero-loader_SOURCE_DIR}/include CACHE PATH "Path to Level Zero Headers")
74+
endif()
75+
76+
add_library (LevelZeroLoader INTERFACE)
77+
# The MSVC linker does not like / at the start of a path, so to work around this
78+
# we split it into a link library and a library path, where the path is allowed
79+
# to have leading /.
80+
get_filename_component(LEVEL_ZERO_LIBRARY_SRC "${LEVEL_ZERO_LIBRARY}" DIRECTORY)
81+
get_filename_component(LEVEL_ZERO_LIB_NAME "${LEVEL_ZERO_LIBRARY}" NAME)
82+
target_link_directories(LevelZeroLoader
83+
INTERFACE "${LEVEL_ZERO_LIBRARY_SRC}"
84+
)
85+
target_link_libraries(LevelZeroLoader
86+
INTERFACE "${LEVEL_ZERO_LIB_NAME}"
87+
)
88+
89+
add_library (LevelZeroLoader-Headers INTERFACE)
90+
target_include_directories(LevelZeroLoader-Headers
91+
INTERFACE "${LEVEL_ZERO_INCLUDE_DIR}"
92+
)

source/adapters/level_zero/CMakeLists.txt

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

6-
set(UR_LEVEL_ZERO_LOADER_LIBRARY "" CACHE FILEPATH "Path of the Level Zero Loader library")
7-
set(UR_LEVEL_ZERO_INCLUDE_DIR "" CACHE FILEPATH "Directory containing the Level Zero Headers")
8-
set(UR_LEVEL_ZERO_LOADER_REPO "" CACHE STRING "Github repo to get the Level Zero loader sources from")
9-
set(UR_LEVEL_ZERO_LOADER_TAG "" CACHE STRING " GIT tag of the Level Loader taken from github repo")
10-
11-
# Copy Level Zero loader/headers locally to the build to avoid leaking their path.
12-
set(LEVEL_ZERO_COPY_DIR ${CMAKE_CURRENT_BINARY_DIR}/level_zero_loader)
13-
if (NOT UR_LEVEL_ZERO_LOADER_LIBRARY STREQUAL "")
14-
get_filename_component(LEVEL_ZERO_LIB_NAME "${UR_LEVEL_ZERO_LOADER_LIBRARY}" NAME)
15-
set(LEVEL_ZERO_LIBRARY ${LEVEL_ZERO_COPY_DIR}/${LEVEL_ZERO_LIB_NAME})
16-
message(STATUS "Level Zero Adapter: Copying Level Zero loader to local build tree")
17-
file(COPY ${UR_LEVEL_ZERO_LOADER_LIBRARY} DESTINATION ${LEVEL_ZERO_COPY_DIR} FOLLOW_SYMLINK_CHAIN)
18-
endif()
19-
if (NOT UR_LEVEL_ZERO_INCLUDE_DIR STREQUAL "")
20-
set(LEVEL_ZERO_INCLUDE_DIR ${LEVEL_ZERO_COPY_DIR})
21-
message(STATUS "Level Zero Adapter: Copying Level Zero headers to local build tree")
22-
file(COPY ${UR_LEVEL_ZERO_INCLUDE_DIR}/ DESTINATION ${LEVEL_ZERO_COPY_DIR})
23-
endif()
24-
25-
if (NOT DEFINED LEVEL_ZERO_LIBRARY OR NOT DEFINED LEVEL_ZERO_INCLUDE_DIR)
26-
message(STATUS "Level Zero Adapter: Download Level Zero loader and headers from github.com")
27-
28-
# Workaround warnings/errors for Level Zero build
29-
set(CMAKE_CXX_FLAGS_BAK "${CMAKE_CXX_FLAGS}")
30-
if (UNIX)
31-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-but-set-variable")
32-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-pedantic")
33-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-stringop-truncation")
34-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter")
35-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-c++98-compat-extra-semi")
36-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unknown-warning-option")
37-
endif()
38-
39-
if (UR_LEVEL_ZERO_LOADER_REPO STREQUAL "")
40-
set(UR_LEVEL_ZERO_LOADER_REPO "https://github.com/oneapi-src/level-zero.git")
41-
endif()
42-
if (UR_LEVEL_ZERO_LOADER_TAG STREQUAL "")
43-
set(UR_LEVEL_ZERO_LOADER_TAG v1.17.6)
44-
endif()
45-
46-
# Disable due to a bug https://github.com/oneapi-src/level-zero/issues/104
47-
set(CMAKE_INCLUDE_CURRENT_DIR OFF)
48-
# Prevent L0 loader from exporting extra symbols
49-
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS OFF)
50-
51-
message(STATUS "Level Zero Adapter: Will fetch Level Zero Loader from ${UR_LEVEL_ZERO_LOADER_REPO}")
52-
include(FetchContent)
53-
FetchContent_Declare(level-zero-loader
54-
GIT_REPOSITORY ${UR_LEVEL_ZERO_LOADER_REPO}
55-
GIT_TAG ${UR_LEVEL_ZERO_LOADER_TAG}
56-
)
57-
if(MSVC)
58-
set(USE_Z7 ON)
59-
endif()
60-
FetchContent_MakeAvailable(level-zero-loader)
61-
FetchContent_GetProperties(level-zero-loader)
62-
63-
# Restore original flags
64-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS_BAK}")
65-
66-
target_compile_options(ze_loader PRIVATE
67-
$<$<IN_LIST:$<CXX_COMPILER_ID>,GNU;Clang;Intel;IntelLLVM>:-Wno-error>
68-
$<$<CXX_COMPILER_ID:MSVC>:/WX- /UUNICODE>
69-
)
70-
71-
set(LEVEL_ZERO_LIBRARY ze_loader)
72-
set(LEVEL_ZERO_INCLUDE_DIR
73-
${level-zero-loader_SOURCE_DIR}/include CACHE PATH "Path to Level Zero Headers")
74-
endif()
75-
76-
add_library (LevelZeroLoader INTERFACE)
77-
# The MSVC linker does not like / at the start of a path, so to work around this
78-
# we split it into a link library and a library path, where the path is allowed
79-
# to have leading /.
80-
get_filename_component(LEVEL_ZERO_LIBRARY_SRC "${LEVEL_ZERO_LIBRARY}" DIRECTORY)
81-
get_filename_component(LEVEL_ZERO_LIB_NAME "${LEVEL_ZERO_LIBRARY}" NAME)
82-
target_link_directories(LevelZeroLoader
83-
INTERFACE "${LEVEL_ZERO_LIBRARY_SRC}"
84-
)
85-
target_link_libraries(LevelZeroLoader
86-
INTERFACE "${LEVEL_ZERO_LIB_NAME}"
87-
)
88-
89-
add_library (LevelZeroLoader-Headers INTERFACE)
90-
target_include_directories(LevelZeroLoader-Headers
91-
INTERFACE "${LEVEL_ZERO_INCLUDE_DIR}"
92-
)
93-
946
if(UR_BUILD_ADAPTER_L0)
957
add_ur_adapter(ur_adapter_level_zero
968
SHARED

source/adapters/level_zero/v2/api.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,6 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice,
253253
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
254254
}
255255

256-
ur_result_t UR_APICALL urKernelSetExecInfo(
257-
ur_kernel_handle_t hKernel, ur_kernel_exec_info_t propName, size_t propSize,
258-
const ur_kernel_exec_info_properties_t *pProperties,
259-
const void *pPropValue) {
260-
logger::error("{} function not implemented!", __FUNCTION__);
261-
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
262-
}
263-
264256
ur_result_t UR_APICALL
265257
urKernelSetArgSampler(ur_kernel_handle_t hKernel, uint32_t argIndex,
266258
const ur_kernel_arg_sampler_properties_t *pProperties,

source/adapters/level_zero/v2/context.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ ur_context_handle_t_::ur_context_handle_t_(ze_context_handle_t hContext,
2727
return std::make_unique<v2::provider_normal>(
2828
context, device, v2::EVENT_COUNTER,
2929
v2::QUEUE_IMMEDIATE);
30-
}) {}
30+
}),
31+
defaultUSMPool(this, nullptr) {}
3132

3233
ur_result_t ur_context_handle_t_::retain() {
3334
RefCount.increment();
@@ -60,6 +61,10 @@ bool ur_context_handle_t_::isValidDevice(ur_device_handle_t hDevice) const {
6061
return false;
6162
}
6263

64+
ur_usm_pool_handle_t ur_context_handle_t_::getDefaultUSMPool() {
65+
return &defaultUSMPool;
66+
}
67+
6368
UR_APIEXPORT ur_result_t UR_APICALL
6469
urContextCreate(uint32_t deviceCount, const ur_device_handle_t *phDevices,
6570
const ur_context_properties_t *pProperties,

source/adapters/level_zero/v2/context.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "command_list_cache.hpp"
1616
#include "common.hpp"
1717
#include "event_pool_cache.hpp"
18+
#include "usm.hpp"
1819

1920
struct ur_context_handle_t_ : _ur_object {
2021
ur_context_handle_t_(ze_context_handle_t hContext, uint32_t numDevices,
@@ -26,6 +27,7 @@ struct ur_context_handle_t_ : _ur_object {
2627
inline ze_context_handle_t getZeHandle() const { return hContext.get(); }
2728
ur_platform_handle_t getPlatform() const;
2829
const std::vector<ur_device_handle_t> &getDevices() const;
30+
ur_usm_pool_handle_t getDefaultUSMPool();
2931

3032
// Checks if Device is covered by this context.
3133
// For that the Device or its root devices need to be in the context.
@@ -35,4 +37,5 @@ struct ur_context_handle_t_ : _ur_object {
3537
const std::vector<ur_device_handle_t> hDevices;
3638
v2::command_list_cache_t commandListCache;
3739
v2::event_pool_cache eventPoolCache;
40+
ur_usm_pool_handle_t_ defaultUSMPool;
3841
};

source/adapters/level_zero/v2/kernel.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,48 @@ ur_program_handle_t ur_kernel_handle_t_::getProgramHandle() const {
196196
return hProgram;
197197
}
198198

199+
ur_result_t ur_kernel_handle_t_::setExecInfo(ur_kernel_exec_info_t propName,
200+
const void *pPropValue) {
201+
std::scoped_lock<ur_shared_mutex> Guard(Mutex);
202+
203+
for (auto &kernel : deviceKernels) {
204+
if (!kernel.has_value())
205+
continue;
206+
if (propName == UR_KERNEL_EXEC_INFO_USM_INDIRECT_ACCESS &&
207+
*(static_cast<const ur_bool_t *>(pPropValue)) == true) {
208+
// The whole point for users really was to not need to know anything
209+
// about the types of allocations kernel uses. So in DPC++ we always
210+
// just set all 3 modes for each kernel.
211+
ze_kernel_indirect_access_flags_t indirectFlags =
212+
ZE_KERNEL_INDIRECT_ACCESS_FLAG_HOST |
213+
ZE_KERNEL_INDIRECT_ACCESS_FLAG_DEVICE |
214+
ZE_KERNEL_INDIRECT_ACCESS_FLAG_SHARED;
215+
ZE2UR_CALL(zeKernelSetIndirectAccess,
216+
(kernel->hKernel.get(), indirectFlags));
217+
} else if (propName == UR_KERNEL_EXEC_INFO_CACHE_CONFIG) {
218+
ze_cache_config_flag_t zeCacheConfig{};
219+
auto cacheConfig =
220+
*(static_cast<const ur_kernel_cache_config_t *>(pPropValue));
221+
if (cacheConfig == UR_KERNEL_CACHE_CONFIG_LARGE_SLM)
222+
zeCacheConfig = ZE_CACHE_CONFIG_FLAG_LARGE_SLM;
223+
else if (cacheConfig == UR_KERNEL_CACHE_CONFIG_LARGE_DATA)
224+
zeCacheConfig = ZE_CACHE_CONFIG_FLAG_LARGE_DATA;
225+
else if (cacheConfig == UR_KERNEL_CACHE_CONFIG_DEFAULT)
226+
zeCacheConfig = static_cast<ze_cache_config_flag_t>(0);
227+
else
228+
// Unexpected cache configuration value.
229+
return UR_RESULT_ERROR_INVALID_VALUE;
230+
ZE2UR_CALL(zeKernelSetCacheConfig,
231+
(kernel->hKernel.get(), zeCacheConfig););
232+
} else {
233+
logger::error("urKernelSetExecInfo: unsupported ParamName");
234+
return UR_RESULT_ERROR_INVALID_VALUE;
235+
}
236+
}
237+
238+
return UR_RESULT_SUCCESS;
239+
}
240+
199241
UR_APIEXPORT ur_result_t UR_APICALL
200242
urKernelCreate(ur_program_handle_t hProgram, const char *pKernelName,
201243
ur_kernel_handle_t *phKernel) {
@@ -246,3 +288,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgPointer(
246288
TRACK_SCOPE_LATENCY("ur_kernel_handle_t_::setArgPointer");
247289
return hKernel->setArgPointer(argIndex, pProperties, pArgValue);
248290
}
291+
292+
UR_APIEXPORT ur_result_t UR_APICALL urKernelSetExecInfo(
293+
ur_kernel_handle_t hKernel, ///< [in] handle of the kernel object
294+
ur_kernel_exec_info_t propName, ///< [in] name of the execution attribute
295+
size_t propSize, ///< [in] size in byte the attribute value
296+
const ur_kernel_exec_info_properties_t
297+
*pProperties, ///< [in][optional] pointer to execution info properties
298+
const void *pPropValue ///< [in][range(0, propSize)] pointer to memory
299+
///< location holding the property value.
300+
) {
301+
std::ignore = propSize;
302+
std::ignore = pProperties;
303+
304+
return hKernel->setExecInfo(propName, pPropValue);
305+
}

source/adapters/level_zero/v2/kernel.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ struct ur_kernel_handle_t_ : _ur_object {
5757
const ur_kernel_arg_pointer_properties_t *pProperties,
5858
const void *pArgValue);
5959

60+
// Implementation of urKernelSetExecInfo.
61+
ur_result_t setExecInfo(ur_kernel_exec_info_t propName,
62+
const void *pPropValue);
63+
6064
// Perform cleanup.
6165
ur_result_t release();
6266

0 commit comments

Comments
 (0)