mirror of
https://github.com/intel/compute-runtime.git
synced 2025-12-21 01:04:57 +08:00
Context implementation (1/N)
Change-Id: I3e0b3c3fdb7c9ab9ac10fccd3d61c7b394dbeee7 Signed-off: Jaime Arteaga <jaime.a.arteaga.molina@intel.com>
This commit is contained in:
@@ -10,6 +10,7 @@ set(L0_SRCS_API
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_cl_interop.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_cmdlist.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_cmdqueue.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_context.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_copy.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_core_loader.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/ze_device.cpp
|
||||
|
||||
34
level_zero/api/core/ze_context.cpp
Normal file
34
level_zero/api/core/ze_context.cpp
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright (C) 2019-2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "level_zero/core/source/context/context.h"
|
||||
#include "level_zero/core/source/driver/driver_handle.h"
|
||||
#include <level_zero/ze_api.h>
|
||||
|
||||
#include "third_party/level_zero/ze_api_ext.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
ZE_APIEXPORT ze_result_t ZE_APICALL
|
||||
zeContextCreate(
|
||||
ze_driver_handle_t hDriver,
|
||||
const ze_context_desc_t *desc,
|
||||
ze_context_handle_t *phContext) {
|
||||
return L0::DriverHandle::fromHandle(hDriver)->createContext(desc, phContext);
|
||||
}
|
||||
|
||||
ZE_APIEXPORT ze_result_t ZE_APICALL
|
||||
zeContextDestroy(ze_context_handle_t hContext) {
|
||||
return L0::Context::fromHandle(hContext)->destroy();
|
||||
}
|
||||
|
||||
ZE_APIEXPORT ze_result_t ZE_APICALL
|
||||
zeContextGetStatus(ze_context_handle_t hContext) {
|
||||
return L0::Context::fromHandle(hContext)->getStatus();
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
@@ -29,6 +29,9 @@ set(L0_RUNTIME_SOURCES
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/cmdqueue/cmdqueue_hw_base.inl
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/cmdqueue/cmdqueue_imp.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/cmdqueue/cmdqueue_extended${BRANCH_DIR_SUFFIX}/cmdqueue_extended.inl
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/context/context_imp.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/context/context_imp.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/context/context.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/debugger/debug_manager.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/device/device.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/device/device_imp.cpp
|
||||
|
||||
32
level_zero/core/source/context/context.h
Normal file
32
level_zero/core/source/context/context.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "level_zero/core/source/driver/driver_handle.h"
|
||||
#include <level_zero/ze_api.h>
|
||||
|
||||
#include "third_party/level_zero/ze_api_ext.h"
|
||||
|
||||
struct _ze_context_handle_t {
|
||||
virtual ~_ze_context_handle_t() = default;
|
||||
};
|
||||
|
||||
namespace L0 {
|
||||
struct DriverHandle;
|
||||
|
||||
struct Context : _ze_context_handle_t {
|
||||
virtual ~Context() = default;
|
||||
virtual ze_result_t destroy() = 0;
|
||||
virtual ze_result_t getStatus() = 0;
|
||||
virtual DriverHandle *getDriverHandle() = 0;
|
||||
|
||||
static Context *fromHandle(ze_context_handle_t handle) { return static_cast<Context *>(handle); }
|
||||
inline ze_context_handle_t toHandle() { return this; }
|
||||
};
|
||||
|
||||
} // namespace L0
|
||||
30
level_zero/core/source/context/context_imp.cpp
Normal file
30
level_zero/core/source/context/context_imp.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "level_zero/core/source/context/context_imp.h"
|
||||
|
||||
namespace L0 {
|
||||
|
||||
ze_result_t ContextImp::destroy() {
|
||||
delete this;
|
||||
|
||||
return ZE_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
ze_result_t ContextImp::getStatus() {
|
||||
return ZE_RESULT_ERROR_UNSUPPORTED_FEATURE;
|
||||
}
|
||||
|
||||
DriverHandle *ContextImp::getDriverHandle() {
|
||||
return this->driverHandle;
|
||||
}
|
||||
|
||||
ContextImp::ContextImp(DriverHandle *driverHandle) {
|
||||
this->driverHandle = driverHandle;
|
||||
}
|
||||
|
||||
} // namespace L0
|
||||
26
level_zero/core/source/context/context_imp.h
Normal file
26
level_zero/core/source/context/context_imp.h
Normal file
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "level_zero/core/source/context/context.h"
|
||||
#include "level_zero/core/source/driver/driver_handle_imp.h"
|
||||
|
||||
namespace L0 {
|
||||
|
||||
struct ContextImp : Context {
|
||||
ContextImp(DriverHandle *driverHandle);
|
||||
~ContextImp() override = default;
|
||||
ze_result_t destroy() override;
|
||||
ze_result_t getStatus() override;
|
||||
DriverHandle *getDriverHandle() override;
|
||||
|
||||
protected:
|
||||
DriverHandle *driverHandle = nullptr;
|
||||
};
|
||||
|
||||
} // namespace L0
|
||||
@@ -9,6 +9,7 @@
|
||||
|
||||
#include "shared/source/memory_manager/unified_memory_manager.h"
|
||||
|
||||
#include "level_zero/core/source/context/context.h"
|
||||
#include "level_zero/core/source/device/device.h"
|
||||
#include <level_zero/ze_api.h>
|
||||
|
||||
@@ -23,6 +24,8 @@ struct Device;
|
||||
struct L0EnvVariables;
|
||||
|
||||
struct DriverHandle : _ze_driver_handle_t {
|
||||
virtual ze_result_t createContext(const ze_context_desc_t *desc,
|
||||
ze_context_handle_t *phContext) = 0;
|
||||
virtual ze_result_t getDevice(uint32_t *pCount, ze_device_handle_t *phDevices) = 0;
|
||||
virtual ze_result_t getProperties(ze_driver_properties_t *properties) = 0;
|
||||
virtual ze_result_t getApiVersion(ze_api_version_t *version) = 0;
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "shared/source/memory_manager/memory_manager.h"
|
||||
#include "shared/source/os_interface/os_library.h"
|
||||
|
||||
#include "level_zero/core/source/context/context_imp.h"
|
||||
#include "level_zero/core/source/debugger/debugger_l0.h"
|
||||
#include "level_zero/core/source/device/device_imp.h"
|
||||
#include "level_zero/core/source/driver/driver_imp.h"
|
||||
@@ -26,6 +27,18 @@ namespace L0 {
|
||||
|
||||
struct DriverHandleImp *GlobalDriver;
|
||||
|
||||
ze_result_t DriverHandleImp::createContext(const ze_context_desc_t *desc,
|
||||
ze_context_handle_t *phContext) {
|
||||
ContextImp *context = new ContextImp(this);
|
||||
if (nullptr == context) {
|
||||
return ZE_RESULT_ERROR_OUT_OF_HOST_MEMORY;
|
||||
}
|
||||
|
||||
*phContext = context->toHandle();
|
||||
|
||||
return ZE_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
NEO::MemoryManager *DriverHandleImp::getMemoryManager() {
|
||||
return this->memoryManager;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ namespace L0 {
|
||||
|
||||
struct DriverHandleImp : public DriverHandle {
|
||||
~DriverHandleImp() override;
|
||||
ze_result_t createContext(const ze_context_desc_t *desc, ze_context_handle_t *phContext) override;
|
||||
ze_result_t getDevice(uint32_t *pCount, ze_device_handle_t *phDevices) override;
|
||||
ze_result_t getProperties(ze_driver_properties_t *properties) override;
|
||||
ze_result_t getApiVersion(ze_api_version_t *version) override;
|
||||
|
||||
43
level_zero/core/test/unit_tests/mocks/mock_context.h
Normal file
43
level_zero/core/test/unit_tests/mocks/mock_context.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "level_zero/core/source/context/context.h"
|
||||
#include "level_zero/core/source/driver/driver_handle.h"
|
||||
#include "level_zero/core/test/unit_tests/mock.h"
|
||||
#include "level_zero/core/test/unit_tests/white_box.h"
|
||||
|
||||
namespace L0 {
|
||||
namespace ult {
|
||||
|
||||
template <>
|
||||
struct WhiteBox<::L0::Context> : public ::L0::Context {};
|
||||
|
||||
using Context = WhiteBox<::L0::Context>;
|
||||
|
||||
template <>
|
||||
struct Mock<Context> : public Context {
|
||||
Mock();
|
||||
~Mock() override;
|
||||
|
||||
MOCK_METHOD(ze_result_t,
|
||||
destroy,
|
||||
(),
|
||||
(override));
|
||||
MOCK_METHOD(ze_result_t,
|
||||
getStatus,
|
||||
(),
|
||||
(override));
|
||||
MOCK_METHOD(DriverHandle *,
|
||||
getDriverHandle,
|
||||
(),
|
||||
(override));
|
||||
};
|
||||
|
||||
} // namespace ult
|
||||
} // namespace L0
|
||||
@@ -50,6 +50,11 @@ struct Mock<DriverHandle> : public DriverHandleImp {
|
||||
ze_memory_allocation_properties_t *pMemAllocProperties,
|
||||
ze_device_handle_t *phDevice),
|
||||
(override));
|
||||
MOCK_METHOD(ze_result_t,
|
||||
createContext,
|
||||
(const ze_context_desc_t *desc,
|
||||
ze_context_handle_t *phContext),
|
||||
(override));
|
||||
MOCK_METHOD(ze_result_t,
|
||||
getExtensionProperties,
|
||||
(uint32_t * pCount,
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#
|
||||
# Copyright (C) 2020 Intel Corporation
|
||||
#
|
||||
# SPDX-License-Identifier: MIT
|
||||
#
|
||||
|
||||
target_sources(${TARGET_NAME} PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/test_context.cpp
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#include "test.h"
|
||||
|
||||
#include "level_zero/core/source/context/context_imp.h"
|
||||
#include "level_zero/core/test/unit_tests/fixtures/device_fixture.h"
|
||||
#include "level_zero/core/test/unit_tests/mocks/mock_driver_handle.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
namespace L0 {
|
||||
namespace ult {
|
||||
|
||||
using ContextTest = Test<DeviceFixture>;
|
||||
|
||||
TEST_F(ContextTest, givenCallToContextGetStatusThenUnsupportedIsReturned) {
|
||||
ze_context_handle_t hContext;
|
||||
ze_context_desc_t desc;
|
||||
ze_result_t res = driverHandle->createContext(&desc, &hContext);
|
||||
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
|
||||
|
||||
Context *context = L0::Context::fromHandle(hContext);
|
||||
|
||||
res = context->getStatus();
|
||||
EXPECT_EQ(ZE_RESULT_ERROR_UNSUPPORTED_FEATURE, res);
|
||||
|
||||
context->destroy();
|
||||
}
|
||||
|
||||
TEST_F(ContextTest, whenCreatingAndDestroyingContextThenSuccessIsReturned) {
|
||||
ze_context_handle_t hContext;
|
||||
ze_context_desc_t desc;
|
||||
|
||||
ze_result_t res = driverHandle->createContext(&desc, &hContext);
|
||||
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
|
||||
|
||||
res = L0::Context::fromHandle(hContext)->destroy();
|
||||
EXPECT_EQ(ZE_RESULT_SUCCESS, res);
|
||||
}
|
||||
|
||||
} // namespace ult
|
||||
} // namespace L0
|
||||
Reference in New Issue
Block a user