Skip to content

Commit 1baed0b

Browse files
authored
Merge pull request #1771 from igchor/level_zero_v2_dispatcher
[L0] Initial version of queue dispatcher
2 parents 00ca0da + e6dc6d9 commit 1baed0b

18 files changed

+1764
-268
lines changed

scripts/generate_code.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,46 @@ def generate_tools(path, section, namespace, tags, version, specs, meta):
454454
loc += _mako_info_hpp(infodir, namespace, tags, version, specs, meta)
455455

456456
print("TOOLS Generated %s lines of code.\n" % loc)
457+
458+
"""
459+
Entry-point:
460+
generates API functions that accept queue for level_zero
461+
"""
462+
def generate_level_zero_queue_api(path, section, namespace, tags, version, specs, meta):
463+
template = "queue_api.cpp.mako"
464+
fin = os.path.join("templates", template)
465+
466+
name = "queue_api"
467+
filename = "queue_api.cpp"
468+
layer_dstpath = os.path.join(path, "adapters/level_zero")
469+
os.makedirs(layer_dstpath, exist_ok=True)
470+
fout = os.path.join(layer_dstpath, filename)
471+
472+
print("Generating %s..." % fout)
473+
474+
loc = util.makoWrite(
475+
fin, fout,
476+
ver=version,
477+
name = name,
478+
namespace=namespace,
479+
tags=tags,
480+
specs=specs,
481+
meta=meta)
482+
483+
template = "queue_api.hpp.mako"
484+
fin = os.path.join("templates", template)
485+
486+
filename = "queue_api.hpp"
487+
fout = os.path.join(layer_dstpath, filename)
488+
489+
print("Generating %s..." % fout)
490+
491+
loc += util.makoWrite(
492+
fin, fout,
493+
ver=version,
494+
name = name,
495+
namespace=namespace,
496+
tags=tags,
497+
specs=specs,
498+
meta=meta)
499+
print("QUEUE Generated %s lines of code.\n" % loc)

scripts/json2src.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def add_argument(parser, name, help, default=False):
3131
add_argument(parser, "adapters", "generation of null adapter files.", True)
3232
add_argument(parser, "common", "generation of common files.", True)
3333
add_argument(parser, "tools", "generation of common files.", True)
34+
add_argument(parser, "l0_queue", "generation of l0 queue abstractions.", True)
3435
parser.add_argument("--debug", action='store_true', help="dump intermediate data to disk.")
3536
parser.add_argument("--sections", type=list, default=None, help="Optional list of sections for which to generate source, default is all")
3637
parser.add_argument("--ver", type=str, default="1.0", help="specification version to generate.")
@@ -60,6 +61,8 @@ def add_argument(parser, name, help, default=False):
6061
generate_code.generate_common(srcpath, config['name'], config['namespace'], config['tags'], args.ver, specs, input['meta'])
6162
if args.tools:
6263
generate_code.generate_tools(toolspath, config['name'], config['namespace'], config['tags'], args.ver, specs, input['meta'])
64+
if args.l0_queue:
65+
generate_code.generate_level_zero_queue_api(srcpath, config['name'], config['namespace'], config['tags'], args.ver, specs, input['meta'])
6366

6467
if args.debug:
6568
util.makoFileListWrite("generated.json")

scripts/templates/helper.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,3 +1604,35 @@ def get_handle_create_get_retain_release_functions(specs, namespace, tags):
16041604
records.append(record)
16051605

16061606
return records
1607+
1608+
"""
1609+
Public:
1610+
returns a list of objects representing functions that accept $x_queue_handle_t as a first param
1611+
"""
1612+
def get_queue_related_functions(specs, namespace, tags):
1613+
funcs = []
1614+
for s in specs:
1615+
for obj in s['objects']:
1616+
if re.match(r"function", obj['type']):
1617+
if obj['params'] and obj['params'][0]['type'] == '$x_queue_handle_t':
1618+
funcs.append(obj)
1619+
return funcs
1620+
1621+
"""
1622+
Public:
1623+
transform a queue related function using following rules:
1624+
- remove $x prefix
1625+
- make first letter lowercase
1626+
- remove first param (queue)
1627+
"""
1628+
def transform_queue_related_function_name(namespace, tags, obj, format = ["name", "type"]):
1629+
function_name = make_func_name(namespace, tags, obj).replace(namespace,'')
1630+
function_name=function_name[0].lower() + function_name[1:]
1631+
1632+
if obj['params'][0]['type'] != '$x_queue_handle_t':
1633+
raise ValueError('First parameter is not a queue handle')
1634+
1635+
params = make_param_lines(namespace, tags, obj, format=format)
1636+
params = params[1:]
1637+
1638+
return "{}({})".format(function_name, ", ".join(params))

scripts/templates/queue_api.cpp.mako

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<%!
2+
import re
3+
from templates import helper as th
4+
%><%
5+
n=namespace
6+
N=n.upper()
7+
8+
x=tags['$x']
9+
X=x.upper()
10+
%>/*
11+
*
12+
* Copyright (C) 2024 Intel Corporation
13+
*
14+
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
15+
* Exceptions. See LICENSE.TXT
16+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
17+
*
18+
* @file ${name}.cpp
19+
*
20+
*/
21+
22+
#include "queue_api.hpp"
23+
24+
ur_queue_handle_t_::~ur_queue_handle_t_() {}
25+
26+
## FUNCTION ###################################################################
27+
%for obj in th.get_queue_related_functions(specs, n, tags):
28+
${X}_APIEXPORT ${x}_result_t ${X}_APICALL
29+
${th.make_func_name(n, tags, obj)}(
30+
%for line in th.make_param_lines(n, tags, obj, format=["name", "type", "delim"]):
31+
${line}
32+
%endfor
33+
)
34+
{
35+
return ${obj['params'][0]['name']}->${th.transform_queue_related_function_name(n, tags, obj, format=["name"])};
36+
}
37+
%endfor

scripts/templates/queue_api.hpp.mako

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<%!
2+
import re
3+
from templates import helper as th
4+
%><%
5+
n=namespace
6+
N=n.upper()
7+
8+
x=tags['$x']
9+
X=x.upper()
10+
%>/*
11+
*
12+
* Copyright (C) 2024 Intel Corporation
13+
*
14+
* Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM
15+
* Exceptions. See LICENSE.TXT
16+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
17+
*
18+
* @file ${name}.hpp
19+
*
20+
*/
21+
22+
#pragma once
23+
24+
#include <ur_api.h>
25+
26+
struct ur_queue_handle_t_ {
27+
virtual ~ur_queue_handle_t_();
28+
%for obj in th.get_queue_related_functions(specs, n, tags):
29+
virtual ${x}_result_t ${th.transform_queue_related_function_name(n, tags, obj, format=["type"])} = 0;
30+
%endfor
31+
};

source/adapters/level_zero/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,11 @@ add_ur_adapter(${TARGET_NAME}
110110
${CMAKE_CURRENT_SOURCE_DIR}/physical_mem.hpp
111111
${CMAKE_CURRENT_SOURCE_DIR}/platform.hpp
112112
${CMAKE_CURRENT_SOURCE_DIR}/program.hpp
113+
${CMAKE_CURRENT_SOURCE_DIR}/queue_api.hpp
113114
${CMAKE_CURRENT_SOURCE_DIR}/queue.hpp
114115
${CMAKE_CURRENT_SOURCE_DIR}/sampler.hpp
116+
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_immediate_in_order.hpp
117+
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_factory.hpp
115118
${CMAKE_CURRENT_SOURCE_DIR}/ur_level_zero.cpp
116119
${CMAKE_CURRENT_SOURCE_DIR}/common.cpp
117120
${CMAKE_CURRENT_SOURCE_DIR}/context.cpp
@@ -126,10 +129,12 @@ add_ur_adapter(${TARGET_NAME}
126129
${CMAKE_CURRENT_SOURCE_DIR}/physical_mem.cpp
127130
${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp
128131
${CMAKE_CURRENT_SOURCE_DIR}/program.cpp
132+
${CMAKE_CURRENT_SOURCE_DIR}/queue_api.cpp
129133
${CMAKE_CURRENT_SOURCE_DIR}/queue.cpp
130134
${CMAKE_CURRENT_SOURCE_DIR}/sampler.cpp
131135
${CMAKE_CURRENT_SOURCE_DIR}/image.cpp
132136
${CMAKE_CURRENT_SOURCE_DIR}/../../ur/ur.cpp
137+
${CMAKE_CURRENT_SOURCE_DIR}/v2/queue_immediate_in_order.cpp
133138
)
134139

135140
if(NOT WIN32)

source/adapters/level_zero/enqueue_native.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010

1111
#include <ur_api.h>
1212

13-
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueNativeCommandExp(
14-
ur_queue_handle_t, ur_exp_enqueue_native_command_function_t, void *,
15-
uint32_t, const ur_mem_handle_t *,
16-
const ur_exp_enqueue_native_command_properties_t *, uint32_t,
17-
const ur_event_handle_t *, ur_event_handle_t *) {
13+
#include "queue.hpp"
14+
15+
ur_result_t ur_queue_handle_legacy_t_::enqueueNativeCommandExp(
16+
ur_exp_enqueue_native_command_function_t, void *, uint32_t,
17+
const ur_mem_handle_t *, const ur_exp_enqueue_native_command_properties_t *,
18+
uint32_t, const ur_event_handle_t *, ur_event_handle_t *) {
1819
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
1920
}

source/adapters/level_zero/event.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ bool WaitListEmptyOrAllEventsFromSameQueue(
5959
return true;
6060
}
6161

62-
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(
63-
ur_queue_handle_t UrQueue, ///< [in] handle of the queue object
62+
ur_result_t ur_queue_handle_legacy_t_::enqueueEventsWait( ///< [in] handle of
63+
///< the queue object
6464
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
6565
const ur_event_handle_t
6666
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
@@ -72,7 +72,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait(
7272
*OutEvent ///< [in,out][optional] return an event object that identifies
7373
///< this particular command instance.
7474
) {
75-
auto Queue = Legacy(UrQueue);
75+
auto Queue = this;
7676
if (EventWaitList) {
7777
bool UseCopyEngine = false;
7878

@@ -152,8 +152,9 @@ static const bool InOrderBarrierBySignal = [] {
152152
return (UrRet ? std::atoi(UrRet) : true);
153153
}();
154154

155-
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
156-
ur_queue_handle_t UrQueue, ///< [in] handle of the queue object
155+
ur_result_t
156+
ur_queue_handle_legacy_t_::enqueueEventsWaitWithBarrier( ///< [in] handle of the
157+
///< queue object
157158
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
158159
const ur_event_handle_t
159160
*EventWaitList, ///< [in][optional][range(0, numEventsInWaitList)]
@@ -165,7 +166,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier(
165166
*OutEvent ///< [in,out][optional] return an event object that identifies
166167
///< this particular command instance.
167168
) {
168-
auto Queue = Legacy(UrQueue);
169+
auto Queue = this;
169170

170171
// Lock automatically releases when this goes out of scope.
171172
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
@@ -661,8 +662,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetProfilingInfo(
661662
return UR_RESULT_SUCCESS;
662663
}
663664

664-
UR_APIEXPORT ur_result_t UR_APICALL urEnqueueTimestampRecordingExp(
665-
ur_queue_handle_t UrQueue, ///< [in] handle of the queue object
665+
ur_result_t ur_queue_handle_legacy_t_::enqueueTimestampRecordingExp(
666666
bool Blocking, ///< [in] blocking or non-blocking enqueue
667667
uint32_t NumEventsInWaitList, ///< [in] size of the event wait list
668668
const ur_event_handle_t
@@ -676,7 +676,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueTimestampRecordingExp(
676676
*OutEvent ///< [in,out] return an event object that identifies
677677
///< this particular command instance.
678678
) {
679-
auto Queue = Legacy(UrQueue);
679+
auto Queue = this;
680680
// Lock automatically releases when this goes out of scope.
681681
std::scoped_lock<ur_shared_mutex> lock(Queue->Mutex);
682682

@@ -1022,7 +1022,6 @@ ur_result_t urEventReleaseInternal(ur_event_handle_t Event) {
10221022

10231023
// Save pointer to the queue before deleting/resetting event.
10241024
auto Queue = Legacy(Event->UrQueue);
1025-
auto URQueue = Event->UrQueue;
10261025

10271026
// If the event was a timestamp recording, we try to evict its entry in the
10281027
// queue.
@@ -1054,8 +1053,8 @@ ur_result_t urEventReleaseInternal(ur_event_handle_t Event) {
10541053
// created so that we can avoid ur_queue_handle_t is released before the
10551054
// associated ur_event_handle_t is released. Here we have to decrement it so
10561055
// ur_queue_handle_t can be released successfully.
1057-
if (URQueue) {
1058-
UR_CALL(urQueueReleaseInternal(URQueue));
1056+
if (Queue) {
1057+
UR_CALL(urQueueReleaseInternal(Queue));
10591058
}
10601059

10611060
return UR_RESULT_SUCCESS;

source/adapters/level_zero/image.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -748,14 +748,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesSampledImageCreateExp(
748748
return UR_RESULT_SUCCESS;
749749
}
750750

751-
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesImageCopyExp(
752-
ur_queue_handle_t hUrQueue, void *pDst, void *pSrc,
753-
const ur_image_format_t *pImageFormat, const ur_image_desc_t *pImageDesc,
754-
ur_exp_image_copy_flags_t imageCopyFlags, ur_rect_offset_t srcOffset,
755-
ur_rect_offset_t dstOffset, ur_rect_region_t copyExtent,
756-
ur_rect_region_t hostExtent, uint32_t numEventsInWaitList,
757-
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
758-
auto hQueue = Legacy(hUrQueue);
751+
ur_result_t ur_queue_handle_legacy_t_::bindlessImagesImageCopyExp(
752+
void *pDst, void *pSrc, const ur_image_format_t *pImageFormat,
753+
const ur_image_desc_t *pImageDesc, ur_exp_image_copy_flags_t imageCopyFlags,
754+
ur_rect_offset_t srcOffset, ur_rect_offset_t dstOffset,
755+
ur_rect_region_t copyExtent, ur_rect_region_t hostExtent,
756+
uint32_t numEventsInWaitList, const ur_event_handle_t *phEventWaitList,
757+
ur_event_handle_t *phEvent) {
758+
auto hQueue = this;
759759
std::scoped_lock<ur_shared_mutex> Lock(hQueue->Mutex);
760760

761761
UR_ASSERT(hQueue, UR_RESULT_ERROR_INVALID_NULL_HANDLE);
@@ -1108,11 +1108,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesDestroyExternalSemaphoreExp(
11081108
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
11091109
}
11101110

1111-
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesWaitExternalSemaphoreExp(
1112-
ur_queue_handle_t hQueue, ur_exp_interop_semaphore_handle_t hSemaphore,
1113-
bool hasValue, uint64_t waitValue, uint32_t numEventsInWaitList,
1111+
ur_result_t ur_queue_handle_legacy_t_::bindlessImagesWaitExternalSemaphoreExp(
1112+
ur_exp_interop_semaphore_handle_t hSemaphore, bool hasValue,
1113+
uint64_t waitValue, uint32_t numEventsInWaitList,
11141114
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
1115-
std::ignore = hQueue;
11161115
std::ignore = hSemaphore;
11171116
std::ignore = hasValue;
11181117
std::ignore = waitValue;
@@ -1124,11 +1123,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesWaitExternalSemaphoreExp(
11241123
return UR_RESULT_ERROR_UNSUPPORTED_FEATURE;
11251124
}
11261125

1127-
UR_APIEXPORT ur_result_t UR_APICALL urBindlessImagesSignalExternalSemaphoreExp(
1128-
ur_queue_handle_t hQueue, ur_exp_interop_semaphore_handle_t hSemaphore,
1129-
bool hasValue, uint64_t signalValue, uint32_t numEventsInWaitList,
1126+
ur_result_t ur_queue_handle_legacy_t_::bindlessImagesSignalExternalSemaphoreExp(
1127+
ur_exp_interop_semaphore_handle_t hSemaphore, bool hasValue,
1128+
uint64_t signalValue, uint32_t numEventsInWaitList,
11301129
const ur_event_handle_t *phEventWaitList, ur_event_handle_t *phEvent) {
1131-
std::ignore = hQueue;
11321130
std::ignore = hSemaphore;
11331131
std::ignore = hasValue;
11341132
std::ignore = signalValue;

0 commit comments

Comments
 (0)