Skip to content

Commit

Permalink
Add program handle
Browse files Browse the repository at this point in the history
  • Loading branch information
omarahmed1111 committed Dec 11, 2023
1 parent 2b29c7f commit 528ca1a
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 67 deletions.
9 changes: 5 additions & 4 deletions source/adapters/opencl/enqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//

#include "common.hpp"
#include "program.hpp"

cl_map_flags convertURMapFlagsToCL(ur_map_flags_t URFlags) {
cl_map_flags CLFlags = 0;
Expand Down Expand Up @@ -453,7 +454,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite(
return UR_RESULT_ERROR_INVALID_OPERATION;

Res = F(cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_program>(hProgram), name, blockingWrite, count,
hProgram->get(), name, blockingWrite, count,
offset, pSrc, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent));
Expand Down Expand Up @@ -484,7 +485,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead(
return UR_RESULT_ERROR_INVALID_OPERATION;

Res = F(cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_program>(hProgram), name, blockingRead, count,
hProgram->get(), name, blockingRead, count,
offset, pDst, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent));
Expand Down Expand Up @@ -515,7 +516,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe(
if (FuncPtr) {
RetVal = mapCLErrorToUR(
FuncPtr(cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_program>(hProgram), pipe_symbol, blocking,
hProgram->get(), pipe_symbol, blocking,
pDst, size, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));
Expand Down Expand Up @@ -547,7 +548,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueWriteHostPipe(
if (FuncPtr) {
RetVal = mapCLErrorToUR(
FuncPtr(cl_adapter::cast<cl_command_queue>(hQueue),
cl_adapter::cast<cl_program>(hProgram), pipe_symbol, blocking,
hProgram->get(), pipe_symbol, blocking,
pSrc, size, numEventsInWaitList,
cl_adapter::cast<const cl_event *>(phEventWaitList),
cl_adapter::cast<cl_event *>(phEvent)));
Expand Down
3 changes: 2 additions & 1 deletion source/adapters/opencl/kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//===----------------------------------------------------------------------===//
#include "common.hpp"
#include "device.hpp"
#include "program.hpp"

#include <algorithm>
#include <memory>
Expand All @@ -19,7 +20,7 @@ urKernelCreate(ur_program_handle_t hProgram, const char *pKernelName,

cl_int CLResult;
*phKernel = cl_adapter::cast<ur_kernel_handle_t>(clCreateKernel(
cl_adapter::cast<cl_program>(hProgram), pKernelName, &CLResult));
hProgram->get(), pKernelName, &CLResult));
CL_RETURN_ON_FAILURE(CLResult);
return UR_RESULT_SUCCESS;
}
Expand Down
120 changes: 58 additions & 62 deletions source/adapters/opencl/program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,31 @@
#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<std::vector<cl_device_id>> &DevicesInProgram) {

cl_uint DeviceCount;
CL_RETURN_ON_FAILURE(clGetProgramInfo(cl_adapter::cast<cl_program>(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<std::vector<cl_device_id>>(DeviceCount);

CL_RETURN_ON_FAILURE(clGetProgramInfo(
cl_adapter::cast<cl_program>(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;
}

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<std::vector<cl_device_id>> 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(
Expand All @@ -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(
Expand All @@ -79,15 +69,17 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL(
}
}

*phProgram = cl_adapter::cast<ur_program_handle_t>(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"},
Expand All @@ -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<ur_program_handle_t>(
FuncPtr(hContext->get(), pIL, length, &Err));
CL_RETURN_ON_FAILURE(Err);
}

Expand All @@ -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<ur_program_handle_t>(clCreateProgramWithBinary(
cl_program Program = clCreateProgramWithBinary(
hContext->get(), cl_adapter::cast<cl_uint>(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);

Expand All @@ -140,7 +133,7 @@ urProgramCompile([[maybe_unused]] ur_context_handle_t hContext,
std::unique_ptr<std::vector<cl_device_id>> DevicesInProgram;
CL_RETURN_ON_FAILURE(getDevicesFromProgram(hProgram, DevicesInProgram));

CL_RETURN_ON_FAILURE(clCompileProgram(cl_adapter::cast<cl_program>(hProgram),
CL_RETURN_ON_FAILURE(clCompileProgram(hProgram->get(),
DevicesInProgram->size(),
DevicesInProgram->data(), pOptions, 0,
nullptr, nullptr, nullptr, nullptr));
Expand Down Expand Up @@ -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<cl_program>(hProgram),
auto ClResult = clGetProgramInfo(hProgram->get(),
mapURProgramInfoToCL(propName), propSize,
pPropValue, &CheckPropSize);
if (pPropValue && CheckPropSize != propSize) {
Expand All @@ -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<cl_program>(hProgram), DevicesInProgram->size(),
hProgram->get(), DevicesInProgram->size(),
DevicesInProgram->data(), pOptions, nullptr, nullptr));
return UR_RESULT_SUCCESS;
}
Expand All @@ -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<ur_program_handle_t>(
std::vector<cl_program> 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<cl_uint>(count),
cl_adapter::cast<const cl_program *>(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;
Expand Down Expand Up @@ -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<cl_program>(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<cl_program>(hProgram),
clGetProgramBuildInfo(hProgram->get(),
hDevice->get(), mapURProgramBuildInfoToCL(propName),
propSize, pPropValue, &CheckPropSize);
if (pPropValue && CheckPropSize != propSize) {
Expand All @@ -304,30 +302,32 @@ 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<cl_program>(hProgram)));
CL_RETURN_ON_FAILURE(clRetainProgram(hProgram->get()));
return UR_RESULT_SUCCESS;
}

UR_APIEXPORT ur_result_t UR_APICALL
urProgramRelease(ur_program_handle_t hProgram) {

CL_RETURN_ON_FAILURE(
clReleaseProgram(cl_adapter::cast<cl_program>(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<ur_native_handle_t>(hProgram);
*phNativeProgram = reinterpret_cast<ur_native_handle_t>(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<ur_program_handle_t>(hNativeProgram);
cl_program NativeHandle =
reinterpret_cast<cl_program>(hNativeProgram);
*phProgram = new ur_program_handle_t_(NativeHandle, hContext);
if (!pProperties || !pProperties->isNativeHandleOwned) {
return urProgramRetain(*phProgram);
}
Expand All @@ -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<cl_program>(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<std::vector<cl_device_id>> DevicesInCtx;
cl_adapter::getDevicesFromContext(cl_adapter::cast<ur_context_handle_t>(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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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<cl_program>(hProgram),
CL_PROGRAM_CONTEXT, sizeof(CLContext),
&CLContext, nullptr));
cl_context CLContext = hProgram->Context->get();

cl_ext::clGetDeviceFunctionPointer_fn FuncT = nullptr;

Expand All @@ -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<cl_program>(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<cl_program>(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
Expand All @@ -471,7 +467,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramGetFunctionPointer(
}

const cl_int CLResult =
FuncT(hDevice->get(), cl_adapter::cast<cl_program>(hProgram),
FuncT(hDevice->get(), hProgram->get(),
pFunctionName, reinterpret_cast<cl_ulong *>(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
Expand Down
26 changes: 26 additions & 0 deletions source/adapters/opencl/program.hpp
Original file line number Diff line number Diff line change
@@ -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 <vector>

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; }
};

0 comments on commit 528ca1a

Please sign in to comment.