From 528ca1a07d21697c0b2c4e0c8e78c1ff9d7d14dc Mon Sep 17 00:00:00 2001 From: omarahmed1111 Date: Mon, 11 Dec 2023 13:53:03 +0000 Subject: [PATCH] Add program handle --- source/adapters/opencl/enqueue.cpp | 9 ++- source/adapters/opencl/kernel.cpp | 3 +- source/adapters/opencl/program.cpp | 120 ++++++++++++++--------------- source/adapters/opencl/program.hpp | 26 +++++++ 4 files changed, 91 insertions(+), 67 deletions(-) create mode 100644 source/adapters/opencl/program.hpp diff --git a/source/adapters/opencl/enqueue.cpp b/source/adapters/opencl/enqueue.cpp index 24d60e62f5..80edec01f0 100644 --- a/source/adapters/opencl/enqueue.cpp +++ b/source/adapters/opencl/enqueue.cpp @@ -9,6 +9,7 @@ //===----------------------------------------------------------------------===// #include "common.hpp" +#include "program.hpp" cl_map_flags convertURMapFlagsToCL(ur_map_flags_t URFlags) { cl_map_flags CLFlags = 0; @@ -453,7 +454,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite( return UR_RESULT_ERROR_INVALID_OPERATION; Res = F(cl_adapter::cast(hQueue), - cl_adapter::cast(hProgram), name, blockingWrite, count, + hProgram->get(), name, blockingWrite, count, offset, pSrc, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent)); @@ -484,7 +485,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead( return UR_RESULT_ERROR_INVALID_OPERATION; Res = F(cl_adapter::cast(hQueue), - cl_adapter::cast(hProgram), name, blockingRead, count, + hProgram->get(), name, blockingRead, count, offset, pDst, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent)); @@ -515,7 +516,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe( if (FuncPtr) { RetVal = mapCLErrorToUR( FuncPtr(cl_adapter::cast(hQueue), - cl_adapter::cast(hProgram), pipe_symbol, blocking, + hProgram->get(), pipe_symbol, blocking, pDst, size, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); @@ -547,7 +548,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueWriteHostPipe( if (FuncPtr) { RetVal = mapCLErrorToUR( FuncPtr(cl_adapter::cast(hQueue), - cl_adapter::cast(hProgram), pipe_symbol, blocking, + hProgram->get(), pipe_symbol, blocking, pSrc, size, numEventsInWaitList, cl_adapter::cast(phEventWaitList), cl_adapter::cast(phEvent))); diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index adf87279d0..71a728a506 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -9,6 +9,7 @@ //===----------------------------------------------------------------------===// #include "common.hpp" #include "device.hpp" +#include "program.hpp" #include #include @@ -19,7 +20,7 @@ urKernelCreate(ur_program_handle_t hProgram, const char *pKernelName, cl_int CLResult; *phKernel = cl_adapter::cast(clCreateKernel( - cl_adapter::cast(hProgram), pKernelName, &CLResult)); + hProgram->get(), pKernelName, &CLResult)); CL_RETURN_ON_FAILURE(CLResult); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index 7e599e8e15..94d13d48a2 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -12,26 +12,20 @@ #include "context.hpp" #include "device.hpp" #include "platform.hpp" +#include "program.hpp" static ur_result_t getDevicesFromProgram( ur_program_handle_t hProgram, std::unique_ptr> &DevicesInProgram) { - cl_uint DeviceCount; - CL_RETURN_ON_FAILURE(clGetProgramInfo(cl_adapter::cast(hProgram), - CL_PROGRAM_NUM_DEVICES, sizeof(cl_uint), - &DeviceCount, nullptr)); - - if (DeviceCount < 1) { - return UR_RESULT_ERROR_INVALID_CONTEXT; + if (!hProgram->Context || !hProgram->Context->DeviceCount) { + return UR_RESULT_ERROR_INVALID_PROGRAM; } - + cl_uint DeviceCount = hProgram->Context->DeviceCount; DevicesInProgram = std::make_unique>(DeviceCount); - - CL_RETURN_ON_FAILURE(clGetProgramInfo( - cl_adapter::cast(hProgram), CL_PROGRAM_DEVICES, - DeviceCount * sizeof(cl_device_id), (*DevicesInProgram).data(), nullptr)); - + for (uint32_t i = 0; i < DeviceCount; i++) { + (*DevicesInProgram)[i] = hProgram->Context->Devices[i]->get(); + } return UR_RESULT_SUCCESS; } @@ -39,15 +33,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( ur_context_handle_t hContext, const void *pIL, size_t length, const ur_program_properties_t *, ur_program_handle_t *phProgram) { - std::unique_ptr> DevicesInCtx; - CL_RETURN_ON_FAILURE_AND_SET_NULL( - cl_adapter::getDevicesFromContext(hContext, DevicesInCtx), phProgram); - - cl_platform_id CurPlatform; - CL_RETURN_ON_FAILURE_AND_SET_NULL( - clGetDeviceInfo((*DevicesInCtx)[0], CL_DEVICE_PLATFORM, - sizeof(cl_platform_id), &CurPlatform, nullptr), - phProgram); + if (!hContext->DeviceCount || !hContext->Devices[0]->Platform) { + return UR_RESULT_ERROR_INVALID_CONTEXT; + } + cl_platform_id CurPlatform = hContext->Devices[0]->Platform->get(); oclv::OpenCLVersion PlatVer; CL_RETURN_ON_FAILURE_AND_SET_NULL( @@ -57,7 +46,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( if (PlatVer >= oclv::V2_1) { /* Make sure all devices support CL 2.1 or newer as well. */ - for (cl_device_id Dev : *DevicesInCtx) { + for (ur_device_handle_t URDev : hContext->Devices) { + cl_device_id Dev = URDev->get(); oclv::OpenCLVersion DevVer; CL_RETURN_ON_FAILURE_AND_SET_NULL( @@ -79,15 +69,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( } } - *phProgram = cl_adapter::cast(clCreateProgramWithIL( - hContext->get(), pIL, length, &Err)); + cl_program Program = clCreateProgramWithIL(hContext->get(), pIL, length, &Err); CL_RETURN_ON_FAILURE(Err); + + *phProgram = new ur_program_handle_t_(Program, hContext); } else { /* If none of the devices conform with CL 2.1 or newer make sure they all * support the cl_khr_il_program extension. */ - for (cl_device_id Dev : *DevicesInCtx) { + for (ur_device_handle_t URDev : hContext->Devices) { + cl_device_id Dev = URDev->get(); bool Supported = false; CL_RETURN_ON_FAILURE_AND_SET_NULL( cl_adapter::checkDeviceExtensions(Dev, {"cl_khr_il_program"}, @@ -106,9 +98,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( CurPlatform, "clCreateProgramWithILKHR")); assert(FuncPtr != nullptr); + cl_program Program = FuncPtr(hContext->get(), pIL, length, &Err); + *phProgram = new ur_program_handle_t_(Program, hContext); - *phProgram = cl_adapter::cast( - FuncPtr(hContext->get(), pIL, length, &Err)); CL_RETURN_ON_FAILURE(Err); } @@ -124,9 +116,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary( const size_t Lengths[1] = {size}; cl_int BinaryStatus[1]; cl_int CLResult; - *phProgram = cl_adapter::cast(clCreateProgramWithBinary( + cl_program Program = clCreateProgramWithBinary( hContext->get(), cl_adapter::cast(1u), - Devices, Lengths, &pBinary, BinaryStatus, &CLResult)); + Devices, Lengths, &pBinary, BinaryStatus, &CLResult); + *phProgram = new ur_program_handle_t_(Program, hContext); CL_RETURN_ON_FAILURE(BinaryStatus[0]); CL_RETURN_ON_FAILURE(CLResult); @@ -140,7 +133,7 @@ urProgramCompile([[maybe_unused]] ur_context_handle_t hContext, std::unique_ptr> DevicesInProgram; CL_RETURN_ON_FAILURE(getDevicesFromProgram(hProgram, DevicesInProgram)); - CL_RETURN_ON_FAILURE(clCompileProgram(cl_adapter::cast(hProgram), + CL_RETURN_ON_FAILURE(clCompileProgram(hProgram->get(), DevicesInProgram->size(), DevicesInProgram->data(), pOptions, 0, nullptr, nullptr, nullptr, nullptr)); @@ -178,7 +171,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, size_t propSize, void *pPropValue, size_t *pPropSizeRet) { size_t CheckPropSize = 0; - auto ClResult = clGetProgramInfo(cl_adapter::cast(hProgram), + auto ClResult = clGetProgramInfo(hProgram->get(), mapURProgramInfoToCL(propName), propSize, pPropValue, &CheckPropSize); if (pPropValue && CheckPropSize != propSize) { @@ -199,7 +192,7 @@ urProgramBuild([[maybe_unused]] ur_context_handle_t hContext, CL_RETURN_ON_FAILURE(getDevicesFromProgram(hProgram, DevicesInProgram)); CL_RETURN_ON_FAILURE(clBuildProgram( - cl_adapter::cast(hProgram), DevicesInProgram->size(), + hProgram->get(), DevicesInProgram->size(), DevicesInProgram->data(), pOptions, nullptr, nullptr)); return UR_RESULT_SUCCESS; } @@ -210,11 +203,16 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count, ur_program_handle_t *phProgram) { cl_int CLResult; - *phProgram = cl_adapter::cast( + std::vector CLPrograms(count); + for (uint32_t i = 0; i < count; i++) { + CLPrograms[i] = phPrograms[i]->get(); + } + cl_program Program = clLinkProgram(hContext->get(), 0, nullptr, pOptions, cl_adapter::cast(count), - cl_adapter::cast(phPrograms), nullptr, - nullptr, &CLResult)); + CLPrograms.data(), nullptr, + nullptr, &CLResult); + *phProgram = new ur_program_handle_t_(Program, hContext); CL_RETURN_ON_FAILURE(CLResult); return UR_RESULT_SUCCESS; @@ -280,14 +278,14 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UrReturnHelper ReturnValue(propSize, pPropValue, pPropSizeRet); cl_program_binary_type BinaryType; CL_RETURN_ON_FAILURE(clGetProgramBuildInfo( - cl_adapter::cast(hProgram), hDevice->get(), + hProgram->get(), hDevice->get(), mapURProgramBuildInfoToCL(propName), sizeof(cl_program_binary_type), &BinaryType, nullptr)); return ReturnValue(mapCLBinaryTypeToUR(BinaryType)); } size_t CheckPropSize = 0; cl_int ClErr = - clGetProgramBuildInfo(cl_adapter::cast(hProgram), + clGetProgramBuildInfo(hProgram->get(), hDevice->get(), mapURProgramBuildInfoToCL(propName), propSize, pPropValue, &CheckPropSize); if (pPropValue && CheckPropSize != propSize) { @@ -304,7 +302,7 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - CL_RETURN_ON_FAILURE(clRetainProgram(cl_adapter::cast(hProgram))); + CL_RETURN_ON_FAILURE(clRetainProgram(hProgram->get())); return UR_RESULT_SUCCESS; } @@ -312,22 +310,24 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { CL_RETURN_ON_FAILURE( - clReleaseProgram(cl_adapter::cast(hProgram))); + clReleaseProgram(hProgram->get())); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramGetNativeHandle( ur_program_handle_t hProgram, ur_native_handle_t *phNativeProgram) { - *phNativeProgram = reinterpret_cast(hProgram); + *phNativeProgram = reinterpret_cast(hProgram->get()); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle( - ur_native_handle_t hNativeProgram, ur_context_handle_t, + ur_native_handle_t hNativeProgram, ur_context_handle_t hContext, const ur_program_native_properties_t *pProperties, ur_program_handle_t *phProgram) { - *phProgram = reinterpret_cast(hNativeProgram); + cl_program NativeHandle = + reinterpret_cast(hNativeProgram); + *phProgram = new ur_program_handle_t_(NativeHandle, hContext); if (!pProperties || !pProperties->isNativeHandleOwned) { return urProgramRetain(*phProgram); } @@ -338,20 +338,19 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants( ur_program_handle_t hProgram, uint32_t count, const ur_specialization_constant_info_t *pSpecConstants) { - cl_program CLProg = cl_adapter::cast(hProgram); - cl_context Ctx = nullptr; - size_t RetSize = 0; - - CL_RETURN_ON_FAILURE(clGetProgramInfo(CLProg, CL_PROGRAM_CONTEXT, sizeof(Ctx), - &Ctx, &RetSize)); + cl_program CLProg = hProgram->get(); + if (!hProgram->Context) { + return UR_RESULT_ERROR_INVALID_PROGRAM; + } + ur_context_handle_t Ctx = hProgram->Context; + if (!Ctx->DeviceCount || !Ctx->Devices[0]->Platform) { + return UR_RESULT_ERROR_INVALID_CONTEXT; + } std::unique_ptr> DevicesInCtx; - cl_adapter::getDevicesFromContext(cl_adapter::cast(Ctx), - DevicesInCtx); + cl_adapter::getDevicesFromContext(Ctx, DevicesInCtx); - cl_platform_id CurPlatform; - clGetDeviceInfo((*DevicesInCtx)[0], CL_DEVICE_PLATFORM, - sizeof(cl_platform_id), &CurPlatform, nullptr); + cl_platform_id CurPlatform = Ctx->Devices[0]->Platform->get(); oclv::OpenCLVersion PlatVer; cl_adapter::getPlatformVersion(CurPlatform, PlatVer); @@ -383,7 +382,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramSetSpecializationConstants( SetProgramSpecializationConstant = nullptr; const ur_result_t URResult = cl_ext::getExtFuncFromContext< decltype(SetProgramSpecializationConstant)>( - Ctx, cl_ext::ExtFuncPtrCache->clSetProgramSpecializationConstantCache, + Ctx->get(), cl_ext::ExtFuncPtrCache->clSetProgramSpecializationConstantCache, cl_ext::SetProgramSpecializationConstantName, &SetProgramSpecializationConstant); @@ -430,10 +429,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer( ur_device_handle_t hDevice, ur_program_handle_t hProgram, const char *pFunctionName, void **ppFunctionPointer) { - cl_context CLContext = nullptr; - CL_RETURN_ON_FAILURE(clGetProgramInfo(cl_adapter::cast(hProgram), - CL_PROGRAM_CONTEXT, sizeof(CLContext), - &CLContext, nullptr)); + cl_context CLContext = hProgram->Context->get(); cl_ext::clGetDeviceFunctionPointer_fn FuncT = nullptr; @@ -453,14 +449,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer( // throws exceptions. *ppFunctionPointer = 0; size_t Size; - CL_RETURN_ON_FAILURE(clGetProgramInfo(cl_adapter::cast(hProgram), + CL_RETURN_ON_FAILURE(clGetProgramInfo(hProgram->get(), CL_PROGRAM_KERNEL_NAMES, 0, nullptr, &Size)); std::string KernelNames(Size, ' '); CL_RETURN_ON_FAILURE(clGetProgramInfo( - cl_adapter::cast(hProgram), CL_PROGRAM_KERNEL_NAMES, + hProgram->get(), CL_PROGRAM_KERNEL_NAMES, KernelNames.size(), &KernelNames[0], nullptr)); // Get rid of the null terminator and search for the kernel name. If the @@ -471,7 +467,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer( } const cl_int CLResult = - FuncT(hDevice->get(), cl_adapter::cast(hProgram), + FuncT(hDevice->get(), hProgram->get(), pFunctionName, reinterpret_cast(ppFunctionPointer)); // GPU runtime sometimes returns CL_INVALID_ARG_VALUE if the function address // cannot be found but the kernel exists. As the kernel does exist, return diff --git a/source/adapters/opencl/program.hpp b/source/adapters/opencl/program.hpp new file mode 100644 index 0000000000..d8e8197331 --- /dev/null +++ b/source/adapters/opencl/program.hpp @@ -0,0 +1,26 @@ +//===--------- program.hpp - OpenCL Adapter ---------------------------===// +// +// Copyright (C) 2023 Intel Corporation +// +// Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM +// Exceptions. See LICENSE.TXT +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +#pragma once + +#include "common.hpp" + +#include + +struct ur_program_handle_t_ { + using native_type = cl_program; + native_type Program; + ur_context_handle_t Context; + + ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx) : Program(Prog), Context(Ctx) {} + + ~ur_program_handle_t_() {} + + native_type get() { return Program; } +};