Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 19 additions & 16 deletions source/adapters/level_zero/command_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//
//===----------------------------------------------------------------------===//
#include "command_buffer.hpp"
#include "event.hpp"
#include "logger/ur_logger.hpp"
#include "ur_level_zero.hpp"

Expand Down Expand Up @@ -268,7 +269,7 @@ ur_result_t createSyncPointAndGetZeEvents(

UR_CALL(getEventsFromSyncPoints(CommandBuffer, NumSyncPointsInWaitList,
SyncPointWaitList, ZeEventList));
ur_event_handle_t LaunchEvent;
ur_event_handle_legacy_t LaunchEvent;
UR_CALL(EventCreate(CommandBuffer->Context, nullptr, false, HostVisible,
&LaunchEvent, false, !CommandBuffer->IsProfilingEnabled));
LaunchEvent->CommandType = CommandType;
Expand Down Expand Up @@ -414,9 +415,9 @@ ur_exp_command_buffer_handle_t_::ur_exp_command_buffer_handle_t_(
: Context(Context), Device(Device), ZeComputeCommandList(CommandList),
ZeComputeCommandListTranslated(CommandListTranslated),
ZeCommandListResetEvents(CommandListResetEvents),
ZeCopyCommandList(CopyCommandList), SignalEvent(SignalEvent),
WaitEvent(WaitEvent), AllResetEvent(AllResetEvent), ZeFencesMap(),
ZeActiveFence(nullptr), SyncPoints(), NextSyncPoint(0),
ZeCopyCommandList(CopyCommandList), SignalEvent(Legacy(SignalEvent)),
WaitEvent(Legacy(WaitEvent)), AllResetEvent(Legacy(AllResetEvent)),
ZeFencesMap(), ZeActiveFence(nullptr), SyncPoints(), NextSyncPoint(0),
IsUpdatable(Desc ? Desc->isUpdatable : false),
IsProfilingEnabled(Desc ? Desc->enableProfiling : false),
IsInOrderCmdList(IsInOrderCmdList) {
Expand Down Expand Up @@ -523,7 +524,8 @@ ur_exp_command_buffer_command_handle_t_::
}

void ur_exp_command_buffer_handle_t_::registerSyncPoint(
ur_exp_command_buffer_sync_point_t SyncPoint, ur_event_handle_t Event) {
ur_exp_command_buffer_sync_point_t SyncPoint,
ur_event_handle_legacy_t Event) {
SyncPoints[SyncPoint] = Event;
NextSyncPoint++;
ZeEventsList.push_back(Event->ZeEvent);
Expand Down Expand Up @@ -633,9 +635,9 @@ urCommandBufferCreateExp(ur_context_handle_t Context, ur_device_handle_t Device,
UR_RESULT_ERROR_UNSUPPORTED_FEATURE);
}

ur_event_handle_t SignalEvent;
ur_event_handle_t WaitEvent;
ur_event_handle_t AllResetEvent;
ur_event_handle_legacy_t SignalEvent;
ur_event_handle_legacy_t WaitEvent;
ur_event_handle_legacy_t AllResetEvent;

UR_CALL(EventCreate(Context, nullptr, false, false, &SignalEvent, false,
!EnableProfiling));
Expand Down Expand Up @@ -1248,7 +1250,7 @@ ur_result_t getZeCommandQueue(ur_queue_handle_legacy_t Queue,
ur_result_t waitForDependencies(ur_exp_command_buffer_handle_t CommandBuffer,
ur_queue_handle_legacy_t Queue,
uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList) {
const ur_event_handle_legacy_t *EventWaitList) {
const bool UseCopyEngine = false;
bool MustSignalWaitEvent = true;
if (NumEventsInWaitList) {
Expand Down Expand Up @@ -1299,9 +1301,9 @@ ur_result_t waitForDependencies(ur_exp_command_buffer_handle_t CommandBuffer,
ur_result_t createUserEvent(ur_exp_command_buffer_handle_t CommandBuffer,
ur_queue_handle_legacy_t Queue,
ur_command_list_ptr_t SignalCommandList,
ur_event_handle_t &Event) {
ur_event_handle_legacy_t &Event) {
// Execution event for this enqueue of the UR command-buffer
ur_event_handle_t RetEvent{};
ur_event_handle_legacy_t RetEvent{};

UR_CALL(createEventAndAssociateQueue(Queue, &RetEvent,
UR_COMMAND_COMMAND_BUFFER_ENQUEUE_EXP,
Expand Down Expand Up @@ -1355,7 +1357,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
CommandBuffer->getFenceForQueue(ZeCommandQueue, ZeFence);

UR_CALL(waitForDependencies(CommandBuffer, Queue, NumEventsInWaitList,
EventWaitList));
Legacy(EventWaitList)));

// Submit reset events command-list. This command-list is of a batch
// command-list type, regardless of the UR Queue type. We therefore need to
Expand Down Expand Up @@ -1385,9 +1387,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(

// Create a command-list to signal the Event on completion
ur_command_list_ptr_t SignalCommandList{};
UR_CALL(Queue->Context->getAvailableCommandList(Queue, SignalCommandList,
false, NumEventsInWaitList,
EventWaitList, false));
UR_CALL(Queue->Context->getAvailableCommandList(
Queue, SignalCommandList, false, NumEventsInWaitList,
Legacy(EventWaitList), false));

// Reset the wait-event for the UR command-buffer that is signaled when its
// submission dependencies have been satisfied.
Expand All @@ -1399,7 +1401,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp(
(SignalCommandList->first, CommandBuffer->AllResetEvent->ZeEvent));

if (Event) {
UR_CALL(createUserEvent(CommandBuffer, Queue, SignalCommandList, *Event));
UR_CALL(createUserEvent(CommandBuffer, Queue, SignalCommandList,
*Legacy(Event)));
}

UR_CALL(Queue->executeCommandList(SignalCommandList, false, false));
Expand Down
11 changes: 6 additions & 5 deletions source/adapters/level_zero/command_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
const ur_exp_command_buffer_desc_t *Desc, const bool IsInOrderCmdList);

void registerSyncPoint(ur_exp_command_buffer_sync_point_t SyncPoint,
ur_event_handle_t Event);
ur_event_handle_legacy_t Event);

ur_exp_command_buffer_sync_point_t getNextSyncPoint() const {
return NextSyncPoint;
Expand Down Expand Up @@ -82,13 +82,13 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
ze_command_list_handle_t ZeCopyCommandList;
// Event which will signals the most recent execution of the command-buffer
// has finished
ur_event_handle_t SignalEvent = nullptr;
ur_event_handle_legacy_t SignalEvent = nullptr;
// Event which a command-buffer waits on until the wait-list dependencies
// passed to a command-buffer enqueue have been satisfied.
ur_event_handle_t WaitEvent = nullptr;
ur_event_handle_legacy_t WaitEvent = nullptr;
// Event which a command-buffer waits on until the main command-list event
// have been reset.
ur_event_handle_t AllResetEvent = nullptr;
ur_event_handle_legacy_t AllResetEvent = nullptr;
// This flag is must be set to false if at least one copy command has been
// added to `ZeCopyCommandList`
bool MCopyCommandListEmpty = true;
Expand All @@ -100,7 +100,8 @@ struct ur_exp_command_buffer_handle_t_ : public _ur_object {
// itself.
ze_fence_handle_t ZeActiveFence;
// Map of sync_points to ur_events
std::unordered_map<ur_exp_command_buffer_sync_point_t, ur_event_handle_t>
std::unordered_map<ur_exp_command_buffer_sync_point_t,
ur_event_handle_legacy_t>
SyncPoints;
// Next sync_point value (may need to consider ways to reuse values if 32-bits
// is not enough)
Expand Down
17 changes: 17 additions & 0 deletions source/adapters/level_zero/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,4 +512,21 @@ extern thread_local int32_t ErrorAdapterNativeCode;
ur_result_t ErrorCode,
int32_t AdapterErrorCode);

// Get specific implementation of UR type from the handle
template <typename UrType, typename UrHandle> UrType GetImpl(UrHandle Handle) {
if (!Handle)
return nullptr;
auto *H = dynamic_cast<UrType>(Handle);
if (!H) {
if constexpr (std::is_same_v<UrHandle, ur_queue_handle_t>) {
throw UR_RESULT_ERROR_INVALID_QUEUE;
} else if constexpr (std::is_same_v<UrHandle, ur_event_handle_t>) {
throw UR_RESULT_ERROR_INVALID_EVENT;
} else {
throw UR_RESULT_ERROR_UNKNOWN;
}
}
return H;
}

#define L0_DRIVER_INORDER_MIN_VERSION 29534
17 changes: 9 additions & 8 deletions source/adapters/level_zero/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ ur_result_t ur_context_handle_t_::getFreeSlotInExistingOrNewPool(
return UR_RESULT_SUCCESS;
}

ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
ur_event_handle_legacy_t ur_context_handle_t_::getEventFromContextCache(
bool HostVisible, bool WithProfiling, ur_device_handle_t Device,
bool CounterBasedEventEnabled) {
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
Expand All @@ -564,7 +564,7 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
return nullptr;

auto It = Cache->begin();
ur_event_handle_t Event = *It;
ur_event_handle_legacy_t Event = *It;
if (Event->CounterBasedEventsEnabled != CounterBasedEventEnabled) {
return nullptr;
}
Expand All @@ -574,7 +574,8 @@ ur_event_handle_t ur_context_handle_t_::getEventFromContextCache(
return Event;
}

void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
void ur_context_handle_t_::addEventToContextCache(
ur_event_handle_legacy_t Event) {
std::scoped_lock<ur_mutex> Lock(EventCacheMutex);
ur_device_handle_t Device = nullptr;

Expand All @@ -587,8 +588,8 @@ void ur_context_handle_t_::addEventToContextCache(ur_event_handle_t Event) {
Cache->emplace_back(Event);
}

ur_result_t
ur_context_handle_t_::decrementUnreleasedEventsInPool(ur_event_handle_t Event) {
ur_result_t ur_context_handle_t_::decrementUnreleasedEventsInPool(
ur_event_handle_legacy_t Event) {
std::shared_lock<ur_shared_mutex> EventLock(Event->Mutex, std::defer_lock);
std::scoped_lock<ur_mutex, std::shared_lock<ur_shared_mutex>> LockAll(
ZeEventPoolCacheMutex, EventLock);
Expand Down Expand Up @@ -648,14 +649,14 @@ static const size_t CmdListsCleanupThreshold = [] {
ur_result_t ur_context_handle_t_::getAvailableCommandList(
ur_queue_handle_legacy_t Queue, ur_command_list_ptr_t &CommandList,
bool UseCopyEngine, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList, bool AllowBatching,
const ur_event_handle_legacy_t *EventWaitList, bool AllowBatching,
ze_command_queue_handle_t *ForcedCmdQueue) {
// Immediate commandlists have been pre-allocated and are always available.
if (Queue->UsingImmCmdLists) {
CommandList = Queue->getQueueGroup(UseCopyEngine).getImmCmdList();
if (CommandList->second.EventList.size() >=
Queue->getImmdCmmdListsEventCleanupThreshold()) {
std::vector<ur_event_handle_t> EventListToCleanup;
std::vector<ur_event_handle_legacy_t> EventListToCleanup;
Queue->resetCommandList(CommandList, false, EventListToCleanup);
CleanupEventListFromResetCmdList(EventListToCleanup, true);
}
Expand Down Expand Up @@ -804,7 +805,7 @@ ur_result_t ur_context_handle_t_::getAvailableCommandList(
ze_result_t ZeResult =
ZE_CALL_NOCHECK(zeFenceQueryStatus, (it->second.ZeFence));
if (ZeResult == ZE_RESULT_SUCCESS) {
std::vector<ur_event_handle_t> EventListToCleanup;
std::vector<ur_event_handle_legacy_t> EventListToCleanup;
Queue->resetCommandList(it, false, EventListToCleanup);
CleanupEventListFromResetCmdList(EventListToCleanup,
true /* QueueLocked */);
Expand Down
20 changes: 10 additions & 10 deletions source/adapters/level_zero/context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ struct ur_context_handle_t_ : _ur_object {
ur_mutex EventCacheMutex;

// Caches for events.
using EventCache = std::vector<std::list<ur_event_handle_t>>;
using EventCache = std::vector<std::list<ur_event_handle_legacy_t>>;
EventCache EventCaches{4};
std::vector<std::unordered_map<ur_device_handle_t, size_t>>
EventCachesDeviceMap{4};
Expand Down Expand Up @@ -204,14 +204,14 @@ struct ur_context_handle_t_ : _ur_object {
bool CounterBasedEventEnabled,
bool UsingImmCmdList);

// Get ur_event_handle_t from cache.
ur_event_handle_t getEventFromContextCache(bool HostVisible,
bool WithProfiling,
ur_device_handle_t Device,
bool CounterBasedEventEnabled);
// Get ur_event_handle_legacy_t from cache.
ur_event_handle_legacy_t
getEventFromContextCache(bool HostVisible, bool WithProfiling,
ur_device_handle_t Device,
bool CounterBasedEventEnabled);

// Add ur_event_handle_t to cache.
void addEventToContextCache(ur_event_handle_t);
// Add ur_event_handle_legacy_t to cache.
void addEventToContextCache(ur_event_handle_legacy_t);

enum EventPoolCacheType {
HostVisibleCacheType,
Expand Down Expand Up @@ -271,7 +271,7 @@ struct ur_context_handle_t_ : _ur_object {

// Decrement number of events living in the pool upon event destroy
// and return the pool to the cache if there are no unreleased events.
ur_result_t decrementUnreleasedEventsInPool(ur_event_handle_t Event);
ur_result_t decrementUnreleasedEventsInPool(ur_event_handle_legacy_t Event);

// Retrieves a command list for executing on this device along with
// a fence to be used in tracking the execution of this command list.
Expand All @@ -296,7 +296,7 @@ struct ur_context_handle_t_ : _ur_object {
ur_result_t getAvailableCommandList(
ur_queue_handle_legacy_t Queue, ur_command_list_ptr_t &CommandList,
bool UseCopyEngine, uint32_t NumEventsInWaitList,
const ur_event_handle_t *EventWaitList, bool AllowBatching = false,
const ur_event_handle_legacy_t *EventWaitList, bool AllowBatching = false,
ze_command_queue_handle_t *ForcedCmdQueue = nullptr);

// Checks if Device is covered by this context.
Expand Down
Loading