Skip to content

Commit

Permalink
feature: implementation for creating image with external memory
Browse files Browse the repository at this point in the history
Related-To: NEO-6757
Signed-off-by: Warchulski, Jaroslaw <jaroslaw.warchulski@intel.com>
  • Loading branch information
jwarchul authored and Compute-Runtime-Automation committed Apr 28, 2023
1 parent cbce863 commit 9e44344
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 6 deletions.
4 changes: 3 additions & 1 deletion opencl/source/mem_obj/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2018-2022 Intel Corporation
# Copyright (C) 2018-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
Expand Down Expand Up @@ -30,10 +30,12 @@ set(RUNTIME_SRCS_MEM_OBJ
if(WIN32)
list(APPEND RUNTIME_SRCS_MEM_OBJ
${CMAKE_CURRENT_SOURCE_DIR}/buffer_windows.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image_windows.cpp
)
else()
list(APPEND RUNTIME_SRCS_MEM_OBJ
${CMAKE_CURRENT_SOURCE_DIR}/buffer_linux.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image_linux.cpp
)
endif()

Expand Down
16 changes: 15 additions & 1 deletion opencl/source/mem_obj/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "opencl/source/helpers/surface_formats.h"
#include "opencl/source/mem_obj/buffer.h"
#include "opencl/source/mem_obj/mem_obj_helper.h"
#include "opencl/source/sharings/unified/unified_image.h"

#include "igfxfmid.h"

Expand Down Expand Up @@ -1308,7 +1309,20 @@ cl_mem Image::validateAndCreateImage(cl_context context,
return nullptr;
}

auto image = Image::create(pContext, memoryProperties, flags, flagsIntel, surfaceFormat, imageDesc, hostPtr, errcodeRet);
Image *image = nullptr;
UnifiedSharingMemoryDescription extMem{};

if (memoryProperties.handle) {
if (validateHandleType(memoryProperties, extMem)) {
extMem.handle = reinterpret_cast<void *>(memoryProperties.handle);
image = UnifiedImage::createSharedUnifiedImage(pContext, flags, extMem, imageFormat, imageDesc, &errcodeRet);
} else {
errcodeRet = CL_INVALID_PROPERTY;
return nullptr;
}
} else {
image = Image::create(pContext, memoryProperties, flags, flagsIntel, surfaceFormat, imageDesc, hostPtr, errcodeRet);
}

if (errcodeRet == CL_SUCCESS) {
image->storeProperties(properties);
Expand Down
8 changes: 6 additions & 2 deletions opencl/source/mem_obj/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
#include "opencl/source/mem_obj/mem_obj.h"

namespace NEO {

class GfxCoreHelper;
class Gmm;
struct HardwareInfo;
class Image;
struct HardwareInfo;
struct KernelInfo;
struct SurfaceFormatInfo;
class GfxCoreHelper;
struct UnifiedSharingMemoryDescription;

using ImageCreateFunc = Image *(*)(Context *context,
const MemoryProperties &memoryProperties,
Expand Down Expand Up @@ -202,6 +204,8 @@ class Image : public MemObj {

void fillImageRegion(size_t *region) const;

static bool validateHandleType(MemoryProperties &memoryProperties, UnifiedSharingMemoryDescription &extMem);

protected:
Image(Context *context,
const MemoryProperties &memoryProperties,
Expand Down
21 changes: 21 additions & 0 deletions opencl/source/mem_obj/image_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "opencl/source/mem_obj/image.h"
#include "opencl/source/sharings/unified/unified_sharing_types.h"

namespace NEO {

bool Image::validateHandleType(MemoryProperties &memoryProperties, UnifiedSharingMemoryDescription &extMem) {
if (memoryProperties.handleType == static_cast<uint64_t>(UnifiedSharingHandleType::LinuxFd)) {
extMem.type = UnifiedSharingHandleType::LinuxFd;
return true;
}
return false;
}

} // namespace NEO
21 changes: 21 additions & 0 deletions opencl/source/mem_obj/image_windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "opencl/source/mem_obj/image.h"
#include "opencl/source/sharings/unified/unified_sharing_types.h"

namespace NEO {

bool Image::validateHandleType(MemoryProperties &memoryProperties, UnifiedSharingMemoryDescription &extMem) {
if (memoryProperties.handleType == static_cast<uint64_t>(UnifiedSharingHandleType::Win32Nt)) {
extMem.type = UnifiedSharingHandleType::Win32Nt;
return true;
}
return false;
}

} // namespace NEO
15 changes: 15 additions & 0 deletions opencl/source/sharings/unified/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (C) 2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#

if(UNIX)
set(RUNTIME_SRCS_SHARINGS_UNIFIED_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/unified_image_linux.cpp
)

target_sources(${NEO_STATIC_LIB_NAME} PRIVATE ${RUNTIME_SRCS_SHARINGS_UNIFIED_LINUX})
set_property(GLOBAL PROPERTY RUNTIME_SRCS_SHARINGS_UNIFIED_LINUX ${RUNTIME_SRCS_SHARINGS_UNIFIED_LINUX})
endif()
29 changes: 29 additions & 0 deletions opencl/source/sharings/unified/linux/unified_image_linux.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm.h"

#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/context/context.h"
#include "opencl/source/sharings/unified/unified_image.h"

namespace NEO {

void *UnifiedImage::swapGmm(GraphicsAllocation *graphicsAllocation, Context *context, ImageInfo *imgInfo) {
if (!graphicsAllocation->getDefaultGmm()) {
auto gmmHelper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper();
auto gmm = std::make_unique<Gmm>(gmmHelper, *imgInfo, StorageInfo{}, false);
gmm->updateImgInfoAndDesc(*imgInfo, 0);
delete graphicsAllocation->getDefaultGmm();
graphicsAllocation->setDefaultGmm(gmm.release());
}

return 0;
}

} // namespace NEO
2 changes: 2 additions & 0 deletions opencl/source/sharings/unified/unified_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Image *UnifiedImage::createSharedUnifiedImage(Context *context, cl_mem_flags fla
return nullptr;
}

swapGmm(graphicsAllocation, context, &imgInfo);

graphicsAllocation->getDefaultGmm()->updateOffsetsInImgInfo(imgInfo, 0u);

auto &memoryManager = *context->getMemoryManager();
Expand Down
3 changes: 3 additions & 0 deletions opencl/source/sharings/unified/unified_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
namespace NEO {
class Image;
class Context;
struct ImageInfo;

class UnifiedImage : public UnifiedSharing {
using UnifiedSharing::UnifiedSharing;

public:
static Image *createSharedUnifiedImage(Context *context, cl_mem_flags flags, UnifiedSharingMemoryDescription description,
const cl_image_format *imageFormat, const cl_image_desc *imageDesc, cl_int *errcodeRet);

static void *swapGmm(GraphicsAllocation *graphicsAllocation, Context *context, ImageInfo *imgInfo);
};
} // namespace NEO
15 changes: 15 additions & 0 deletions opencl/source/sharings/unified/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#
# Copyright (C) 2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#

if(WIN32)
set(RUNTIME_SRCS_SHARINGS_UNIFIED_WINDOWS
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/unified_image_windows.cpp
)

target_sources(${NEO_STATIC_LIB_NAME} PRIVATE ${RUNTIME_SRCS_SHARINGS_UNIFIED_WINDOWS})
set_property(GLOBAL PROPERTY RUNTIME_SRCS_SHARINGS_UNIFIED_WINDOWS ${RUNTIME_SRCS_SHARINGS_UNIFIED_WINDOWS})
endif()
30 changes: 30 additions & 0 deletions opencl/source/sharings/unified/windows/unified_image_windows.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm.h"
#include "shared/source/gmm_helper/resource_info.h"

#include "opencl/source/cl_device/cl_device.h"
#include "opencl/source/context/context.h"
#include "opencl/source/sharings/unified/unified_image.h"

namespace NEO {

void *UnifiedImage::swapGmm(GraphicsAllocation *graphicsAllocation, Context *context, ImageInfo *imgInfo) {
if (graphicsAllocation->getDefaultGmm()->gmmResourceInfo->peekGmmResourceInfo()->GetResourceType() == RESOURCE_BUFFER) {
auto gmmHelper = context->getDevice(0)->getRootDeviceEnvironment().getGmmHelper();
auto gmm = std::make_unique<Gmm>(gmmHelper, *imgInfo, StorageInfo{}, false);
gmm->updateImgInfoAndDesc(*imgInfo, 0);
delete graphicsAllocation->getDefaultGmm();
graphicsAllocation->setDefaultGmm(gmm.release());
}

return 0;
}

} // namespace NEO
3 changes: 2 additions & 1 deletion opencl/test/unit_test/mem_obj/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
Expand All @@ -8,6 +8,7 @@ if(UNIX)
set(IGDRCL_SRCS_tests_mem_obj_linux
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/buffer_linux_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image_linux_tests.cpp
)

target_sources(igdrcl_tests PRIVATE ${IGDRCL_SRCS_tests_mem_obj_linux})
Expand Down
63 changes: 63 additions & 0 deletions opencl/test/unit_test/mem_obj/linux/image_linux_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/

#include "shared/test/common/test_macros/test.h"

#include "opencl/source/mem_obj/image.h"
#include "opencl/source/sharings/unified/unified_sharing_types.h"
#include "opencl/test/unit_test/fixtures/cl_device_fixture.h"
#include "opencl/test/unit_test/mocks/mock_context.h"
#include "opencl/test/unit_test/sharings/unified/unified_sharing_fixtures.h"

using namespace NEO;

using ImageLinuxTests = Test<ClDeviceFixture>;

TEST_F(ImageLinuxTests, givenPropertiesWithNtHandleWhenValidateAndCreateImageThenInvalidPropertyIsSet) {
cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_OPAQUE_WIN32_KHR, 0x1234, 0};

cl_image_desc imageDesc = {};
cl_image_format imageFormat = {};
imageFormat.image_channel_data_type = CL_UNSIGNED_INT8;
imageFormat.image_channel_order = CL_R;

std::unique_ptr<MockContext> context;
context.reset(new MockContext(pClDevice));
cl_mem_flags flags = CL_MEM_READ_WRITE;
cl_int retVal = CL_SUCCESS;

auto image = ImageFunctions::validateAndCreateImage(context.get(), properties, flags, 0, &imageFormat, &imageDesc, nullptr, retVal);

EXPECT_EQ(retVal, CL_INVALID_PROPERTY);
EXPECT_EQ(nullptr, image);

clReleaseMemObject(image);
}

using UnifiedSharingImageTests = UnifiedSharingFixture<true, true>;

TEST_F(UnifiedSharingImageTests, givenPropertiesWithDmaBufWhenValidateAndCreateImageThenCorrectImageIsSet) {
cl_mem_properties properties[] = {CL_EXTERNAL_MEMORY_HANDLE_DMA_BUF_KHR, 0x1234, 0};

cl_image_desc imageDesc = {};
imageDesc.image_type = CL_MEM_OBJECT_IMAGE2D;
imageDesc.image_width = 64;
imageDesc.image_height = 64;
cl_image_format imageFormat = {};
imageFormat.image_channel_data_type = CL_UNSIGNED_INT8;
imageFormat.image_channel_order = CL_R;

cl_mem_flags flags = CL_MEM_READ_WRITE;
cl_int retVal = CL_INVALID_VALUE;

auto image = ImageFunctions::validateAndCreateImage(context.get(), properties, flags, 0, &imageFormat, &imageDesc, nullptr, retVal);

EXPECT_EQ(retVal, CL_SUCCESS);
EXPECT_NE(image, nullptr);

clReleaseMemObject(image);
}
3 changes: 2 additions & 1 deletion opencl/test/unit_test/mem_obj/windows/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright (C) 2022 Intel Corporation
# Copyright (C) 2022-2023 Intel Corporation
#
# SPDX-License-Identifier: MIT
#
Expand All @@ -8,6 +8,7 @@ if(WIN32)
set(IGDRCL_SRCS_tests_mem_obj_windows
${CMAKE_CURRENT_SOURCE_DIR}/CMakeLists.txt
${CMAKE_CURRENT_SOURCE_DIR}/buffer_windows_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/image_windows_tests.cpp
)

target_sources(igdrcl_tests PRIVATE ${IGDRCL_SRCS_tests_mem_obj_windows})
Expand Down
Loading

0 comments on commit 9e44344

Please sign in to comment.