diff --git a/level_zero/api/core/ze_cmdlist_api_entrypoints.h b/level_zero/api/core/ze_cmdlist_api_entrypoints.h index b710e0b65a..8c2ccba922 100644 --- a/level_zero/api/core/ze_cmdlist_api_entrypoints.h +++ b/level_zero/api/core/ze_cmdlist_api_entrypoints.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2022 Intel Corporation + * Copyright (C) 2020-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -64,6 +64,36 @@ ze_result_t zeCommandListAppendQueryKernelTimestamps( return L0::CommandList::fromHandle(hCommandList)->appendQueryKernelTimestamps(numEvents, phEvents, dstptr, pOffsets, hSignalEvent, numWaitEvents, phWaitEvents); } +ze_result_t zeCommandListGetDeviceHandle( + ze_command_list_handle_t hCommandList, + ze_device_handle_t *phDevice) { + return L0::CommandList::fromHandle(hCommandList)->getDeviceHandle(phDevice); +} + +ze_result_t zeCommandListGetContextHandle( + ze_command_list_handle_t hCommandList, + ze_context_handle_t *phContext) { + return L0::CommandList::fromHandle(hCommandList)->getContextHandle(phContext); +} + +ze_result_t zeCommandListGetOrdinal( + ze_command_list_handle_t hCommandList, + uint32_t *pOrdinal) { + return L0::CommandList::fromHandle(hCommandList)->getOrdinal(pOrdinal); +} + +ze_result_t zeCommandListImmediateGetIndex( + ze_command_list_handle_t hCommandListImmediate, + uint32_t *pIndex) { + return L0::CommandList::fromHandle(hCommandListImmediate)->getImmediateIndex(pIndex); +} + +ze_result_t zeCommandListIsImmediate( + ze_command_list_handle_t hCommandList, + ze_bool_t *pIsImmediate) { + return L0::CommandList::fromHandle(hCommandList)->isImmediate(pIsImmediate); +} + } // namespace L0 extern "C" { @@ -122,4 +152,44 @@ ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListAppendWriteGlobalTimestamp( numWaitEvents, phWaitEvents); } -} \ No newline at end of file + +ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListGetDeviceHandle( + ze_command_list_handle_t hCommandList, + ze_device_handle_t *phDevice) { + return L0::zeCommandListGetDeviceHandle( + hCommandList, + phDevice); +} + +ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListGetContextHandle( + ze_command_list_handle_t hCommandList, + ze_context_handle_t *phContext) { + return L0::zeCommandListGetContextHandle( + hCommandList, + phContext); +} + +ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListGetOrdinal( + ze_command_list_handle_t hCommandList, + uint32_t *pOrdinal) { + return L0::zeCommandListGetOrdinal( + hCommandList, + pOrdinal); +} + +ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListImmediateGetIndex( + ze_command_list_handle_t hCommandListImmediate, + uint32_t *pIndex) { + return L0::zeCommandListImmediateGetIndex( + hCommandListImmediate, + pIndex); +} + +ZE_APIEXPORT ze_result_t ZE_APICALL zeCommandListIsImmediate( + ze_command_list_handle_t hCommandList, + ze_bool_t *pIsImmediate) { + return L0::zeCommandListIsImmediate( + hCommandList, + pIsImmediate); +} +} diff --git a/level_zero/core/source/cmdlist/cmdlist.h b/level_zero/core/source/cmdlist/cmdlist.h index b5dec7d0b8..53c36c414b 100644 --- a/level_zero/core/source/cmdlist/cmdlist.h +++ b/level_zero/core/source/cmdlist/cmdlist.h @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -170,6 +171,12 @@ struct CommandList : _ze_command_list_handle_t { uint64_t data) = 0; virtual ze_result_t hostSynchronize(uint64_t timeout) = 0; + virtual ze_result_t getDeviceHandle(ze_device_handle_t *phDevice) = 0; + virtual ze_result_t getContextHandle(ze_context_handle_t *phContext) = 0; + virtual ze_result_t getOrdinal(uint32_t *pOrdinal) = 0; + virtual ze_result_t getImmediateIndex(uint32_t *pIndex) = 0; + virtual ze_result_t isImmediate(ze_bool_t *pIsImmediate) = 0; + static CommandList *create(uint32_t productFamily, Device *device, NEO::EngineGroupType engineGroupType, ze_command_list_flags_t flags, ze_result_t &resultValue, bool internalUsage); @@ -188,6 +195,7 @@ struct CommandList : _ze_command_list_handle_t { return commandListPerThreadScratchSize[slotId]; } + void setOrdinal(uint32_t ord) { ordinal = ord; } void setCommandListPerThreadScratchSize(uint32_t slotId, uint32_t size) { UNRECOVERABLE_IF(slotId > 1); commandListPerThreadScratchSize[slotId] = size; @@ -381,6 +389,7 @@ struct CommandList : _ze_command_list_handle_t { NEO::PreemptionMode commandListPreemptionMode = NEO::PreemptionMode::Initial; NEO::EngineGroupType engineGroupType = NEO::EngineGroupType::maxEngineGroups; NEO::HeapAddressModel cmdListHeapAddressModel = NEO::HeapAddressModel::privateHeaps; + std::optional ordinal = std::nullopt; CommandListType cmdListType = CommandListType::typeRegular; uint32_t commandListPerThreadScratchSize[2]{}; diff --git a/level_zero/core/source/cmdlist/cmdlist_imp.cpp b/level_zero/core/source/cmdlist/cmdlist_imp.cpp index f8c8edd4e5..dbd2bc17f6 100644 --- a/level_zero/core/source/cmdlist/cmdlist_imp.cpp +++ b/level_zero/core/source/cmdlist/cmdlist_imp.cpp @@ -125,6 +125,33 @@ CommandList *CommandList::create(uint32_t productFamily, Device *device, NEO::En return commandList; } +ze_result_t CommandListImp::getDeviceHandle(ze_device_handle_t *phDevice) { + *phDevice = getDevice()->toHandle(); + return ZE_RESULT_SUCCESS; +} +ze_result_t CommandListImp::getContextHandle(ze_context_handle_t *phContext) { + *phContext = getCmdListContext(); + return ZE_RESULT_SUCCESS; +} + +ze_result_t CommandListImp::getOrdinal(uint32_t *pOrdinal) { + UNRECOVERABLE_IF(ordinal == std::nullopt); + *pOrdinal = ordinal.value(); + return ZE_RESULT_SUCCESS; +} + +ze_result_t CommandListImp::getImmediateIndex(uint32_t *pIndex) { + if (isImmediateType()) { + return cmdQImmediate->getIndex(pIndex); + } + return ZE_RESULT_ERROR_INVALID_ARGUMENT; +} + +ze_result_t CommandListImp::isImmediate(ze_bool_t *pIsImmediate) { + *pIsImmediate = isImmediateType(); + return ZE_RESULT_SUCCESS; +} + CommandList *CommandList::createImmediate(uint32_t productFamily, Device *device, const ze_command_queue_desc_t *desc, bool internalUsage, NEO::EngineGroupType engineGroupType, diff --git a/level_zero/core/source/cmdlist/cmdlist_imp.h b/level_zero/core/source/cmdlist/cmdlist_imp.h index 38cbde7b5f..a090432fed 100644 --- a/level_zero/core/source/cmdlist/cmdlist_imp.h +++ b/level_zero/core/source/cmdlist/cmdlist_imp.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2020-2023 Intel Corporation + * Copyright (C) 2020-2024 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -26,6 +26,13 @@ struct CommandListImp : public CommandList { ze_result_t appendMetricQueryBegin(zet_metric_query_handle_t hMetricQuery) override; ze_result_t appendMetricQueryEnd(zet_metric_query_handle_t hMetricQuery, ze_event_handle_t hSignalEvent, uint32_t numWaitEvents, ze_event_handle_t *phWaitEvents) override; + + ze_result_t getDeviceHandle(ze_device_handle_t *phDevice) override; + ze_result_t getContextHandle(ze_context_handle_t *phContext) override; + ze_result_t getOrdinal(uint32_t *pOrdinal) override; + ze_result_t getImmediateIndex(uint32_t *pIndex) override; + ze_result_t isImmediate(ze_bool_t *pIsImmediate) override; + virtual void appendMultiPartitionPrologue(uint32_t partitionDataSize) = 0; virtual void appendMultiPartitionEpilogue() = 0; diff --git a/level_zero/core/source/device/device_imp.cpp b/level_zero/core/source/device/device_imp.cpp index eb8268605f..5ed5def4df 100644 --- a/level_zero/core/source/device/device_imp.cpp +++ b/level_zero/core/source/device/device_imp.cpp @@ -226,7 +226,9 @@ ze_result_t DeviceImp::createCommandList(const ze_command_list_desc_t *desc, ze_result_t returnValue = ZE_RESULT_SUCCESS; auto createCommandList = getCmdListCreateFunc(desc); *commandList = createCommandList(productFamily, this, engineGroupType, desc->flags, returnValue, false); - + if (returnValue == ZE_RESULT_SUCCESS) { + CommandList::fromHandle(*commandList)->setOrdinal(desc->commandQueueGroupOrdinal); + } return returnValue; } @@ -238,7 +240,6 @@ ze_result_t DeviceImp::createInternalCommandList(const ze_command_list_desc_t *d ze_result_t returnValue = ZE_RESULT_SUCCESS; auto createCommandList = getCmdListCreateFunc(desc); *commandList = createCommandList(productFamily, this, engineGroupType, desc->flags, returnValue, true); - return returnValue; } @@ -256,6 +257,9 @@ ze_result_t DeviceImp::createCommandListImmediate(const ze_command_queue_desc_t auto productFamily = neoDevice->getHardwareInfo().platform.eProductFamily; ze_result_t returnValue = ZE_RESULT_SUCCESS; *phCommandList = CommandList::createImmediate(productFamily, this, &commandQueueDesc, false, engineGroupType, returnValue); + if (returnValue == ZE_RESULT_SUCCESS) { + CommandList::fromHandle(*phCommandList)->setOrdinal(desc->ordinal); + } return returnValue; } diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp index d478ba194f..2dacaeec83 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_1.cpp @@ -48,6 +48,10 @@ TEST_F(ContextCommandListCreate, whenCreatingCommandListFromContextThenSuccessIs EXPECT_EQ(Context::fromHandle(CommandList::fromHandle(hCommandList)->getCmdListContext()), context); L0::CommandList *commandList = L0::CommandList::fromHandle(hCommandList); + ze_context_handle_t hContext; + EXPECT_EQ(ZE_RESULT_SUCCESS, commandList->getContextHandle(&hContext)); + EXPECT_EQ(context, hContext); + commandList->destroy(); } @@ -111,7 +115,10 @@ TEST_F(CommandListCreate, whenCommandListIsCreatedThenItIsInitialized) { std::unique_ptr commandList(CommandList::create(productFamily, device, NEO::EngineGroupType::renderCompute, 0u, returnValue, false)); ASSERT_NE(nullptr, commandList); + ze_device_handle_t hDevice; EXPECT_EQ(device, commandList->getDevice()); + EXPECT_EQ(ZE_RESULT_SUCCESS, commandList->getDeviceHandle(&hDevice)); + EXPECT_EQ(device->toHandle(), hDevice); ASSERT_GT(commandList->getCmdContainer().getCmdBufferAllocations().size(), 0u); auto numAllocations = 0u; diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp index 4002a095ae..7681dbf74c 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_3.cpp @@ -89,6 +89,15 @@ TEST_F(CommandListCreateNegativeTest, whenDeviceAllocationFailsDuringCommandList ASSERT_EQ(nullptr, commandList); } +TEST_F(CommandListCreateNegativeTest, whenDeviceAllocationFailsDuringCreateCommandListFromDeviceThenOutOfDeviceMemoryErrorIsReturned) { + ze_command_list_handle_t commandList = nullptr; + ze_command_list_desc_t desc = {ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC}; + memoryManager->forceFailureInPrimaryAllocation = true; + auto returnValue = device->createCommandList(&desc, &commandList); + EXPECT_EQ(ZE_RESULT_ERROR_OUT_OF_DEVICE_MEMORY, returnValue); + ASSERT_EQ(nullptr, commandList); +} + using CommandListCreateNegativeStateBaseAddressTest = Test>; HWTEST2_F(CommandListCreateNegativeStateBaseAddressTest, diff --git a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_7.cpp b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_7.cpp index 14f389458c..65f96339fa 100644 --- a/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_7.cpp +++ b/level_zero/core/test/unit_tests/sources/cmdlist/test_cmdlist_7.cpp @@ -153,6 +153,67 @@ TEST_F(CommandListCreate, givenOrdinalBiggerThanAvailableEnginesWhenCreatingComm EXPECT_EQ(nullptr, commandList); } +TEST_F(CommandListCreate, givenCommandListWithValidOrdinalWhenCallingGetOrdinalThenSuccessIsReturned) { + auto availableEngineGroups = neoDevice->getRegularEngineGroups(); + ze_command_list_handle_t commandList = nullptr; + for (uint32_t engineGroup = 0; engineGroup < availableEngineGroups.size(); engineGroup++) { + ze_command_list_desc_t desc = {ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC}; + desc.commandQueueGroupOrdinal = engineGroup; + auto returnValue = device->createCommandList(&desc, &commandList); + EXPECT_EQ(ZE_RESULT_SUCCESS, returnValue); + EXPECT_NE(nullptr, commandList); + + auto whiteboxCommandList = static_cast(CommandList::fromHandle(commandList)); + uint32_t ordinal = 0; + whiteboxCommandList->getOrdinal(&ordinal); + EXPECT_EQ(engineGroup, ordinal); + + whiteboxCommandList->destroy(); + } +} + +TEST_F(CommandListCreate, givenCommandListWhenCallingImmediateCommandListIndexThenInvalidArgumentIsReturned) { + ze_command_list_desc_t desc = {ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC}; + ze_command_list_handle_t commandList = nullptr; + + auto returnValue = device->createCommandList(&desc, &commandList); + EXPECT_EQ(ZE_RESULT_SUCCESS, returnValue); + ASSERT_NE(nullptr, commandList); + + auto whiteboxCommandList = static_cast(CommandList::fromHandle(commandList)); + + uint32_t index = 4; + ze_bool_t isImmediate = true; + EXPECT_EQ(ZE_RESULT_SUCCESS, whiteboxCommandList->isImmediate(&isImmediate)); + EXPECT_FALSE(static_cast(isImmediate)); + EXPECT_EQ(ZE_RESULT_ERROR_INVALID_ARGUMENT, whiteboxCommandList->getImmediateIndex(&index)); + EXPECT_EQ(4u, index); + + whiteboxCommandList->destroy(); +} + +TEST_F(CommandListCreate, givenImmediateCommandListWhenCallingImmediateCommandListIndexThenValidIndexIsReturned) { + ze_command_queue_desc_t desc = {ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC}; + ze_command_list_handle_t commandList = nullptr; + + auto returnValue = device->createCommandListImmediate(&desc, &commandList); + EXPECT_EQ(ZE_RESULT_SUCCESS, returnValue); + ASSERT_NE(nullptr, commandList); + + auto whiteboxCommandList = static_cast(CommandList::fromHandle(commandList)); + + uint32_t index = 4, cmdQIndex = 2; + ze_bool_t isImmediate = true; + EXPECT_EQ(ZE_RESULT_SUCCESS, whiteboxCommandList->isImmediate(&isImmediate)); + EXPECT_TRUE(static_cast(isImmediate)); + EXPECT_EQ(ZE_RESULT_SUCCESS, whiteboxCommandList->getImmediateIndex(&index)); + EXPECT_NE(nullptr, whiteboxCommandList->cmdQImmediate); + EXPECT_EQ(ZE_RESULT_SUCCESS, whiteboxCommandList->cmdQImmediate->getIndex(&cmdQIndex)); + EXPECT_EQ(cmdQIndex, index); + + whiteboxCommandList->destroy(); +} + TEST_F(CommandListCreate, givenQueueFlagsWhenCreatingImmediateCommandListThenDontCopyFlags) { ze_command_queue_desc_t desc = {ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC}; desc.flags = ZE_COMMAND_QUEUE_FLAG_EXPLICIT_ONLY; @@ -1921,4 +1982,4 @@ TEST(CommandList, givenContextGroupEnabledWhenCreatingImmediateCommandListThenEa } } // namespace ult -} // namespace L0 \ No newline at end of file +} // namespace L0