diff --git a/source/adapters/opencl/adapter.cpp b/source/adapters/opencl/adapter.cpp index 763c6d532d..8ae1e77755 100644 --- a/source/adapters/opencl/adapter.cpp +++ b/source/adapters/opencl/adapter.cpp @@ -22,10 +22,10 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, uint32_t *pNumAdapters) { if (NumEntries > 0 && phAdapters) { std::lock_guard Lock{adapter.Mutex}; - // adapter.RefCount++; if (adapter.RefCount++ == 0) { cl_ext::ExtFuncPtrCache = std::make_unique(); } + *phAdapters = &adapter; } @@ -43,7 +43,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) { UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t) { std::lock_guard Lock{adapter.Mutex}; - // --adapter.RefCount; if (--adapter.RefCount == 0) { cl_ext::ExtFuncPtrCache.reset(); } diff --git a/source/adapters/opencl/command_buffer.cpp b/source/adapters/opencl/command_buffer.cpp index 79db8b88a0..d015548f01 100644 --- a/source/adapters/opencl/command_buffer.cpp +++ b/source/adapters/opencl/command_buffer.cpp @@ -324,7 +324,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferEnqueueExp( NumberOfQueues, &CLQueue, hCommandBuffer->CLCommandBuffer, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/context.cpp b/source/adapters/opencl/context.cpp index 07207009f9..5dc18dc49e 100644 --- a/source/adapters/opencl/context.cpp +++ b/source/adapters/opencl/context.cpp @@ -45,8 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate( cl_context Ctx = clCreateContext( nullptr, cl_adapter::cast(DeviceCount), CLDevices.data(), nullptr, nullptr, cl_adapter::cast(&Ret)); - - *phContext = new ur_context_handle_t_(Ctx, DeviceCount, phDevices); + auto URContext = + std::make_unique(Ctx, DeviceCount, phDevices); + *phContext = URContext.release(); return mapCLErrorToUR(Ret); } @@ -142,7 +143,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreateWithNativeHandle( ur_context_handle_t *phContext) { cl_context NativeHandle = reinterpret_cast(hNativeContext); - *phContext = new ur_context_handle_t_(NativeHandle, numDevices, phDevices); + auto URContext = std::make_unique( + NativeHandle, numDevices, phDevices); + *phContext = URContext.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/device.cpp b/source/adapters/opencl/device.cpp index 629e205b1a..98080e4e3e 100644 --- a/source/adapters/opencl/device.cpp +++ b/source/adapters/opencl/device.cpp @@ -79,6 +79,7 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType, default: return UR_RESULT_ERROR_INVALID_ENUMERATION; } + UR_RETURN_ON_FAILURE(hPlatform->InitDevices()); try { uint32_t AllDevicesNum = hPlatform->Devices.size(); uint32_t DeviceNumIter = 0; @@ -86,7 +87,7 @@ urDeviceGet(ur_platform_handle_t hPlatform, ur_device_type_t DeviceType, cl_device_type DeviceType = hPlatform->Devices[i]->Type; if (DeviceType == Type || Type == CL_DEVICE_TYPE_ALL) { if (phDevices) { - phDevices[DeviceNumIter] = hPlatform->Devices[i]; + phDevices[DeviceNumIter] = hPlatform->Devices[i].get(); } DeviceNumIter++; } @@ -999,8 +1000,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( CLNumDevicesRet, CLSubDevices.data(), nullptr)); for (uint32_t i = 0; i < NumDevices; i++) { - phSubDevices[i] = - new ur_device_handle_t_(CLSubDevices[i], hDevice->Platform, hDevice); + auto URSubDevice = std::make_unique( + CLSubDevices[i], hDevice->Platform, hDevice); + phSubDevices[i] = URSubDevice.release(); } } @@ -1033,7 +1035,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceCreateWithNativeHandle( ur_native_handle_t hNativeDevice, ur_platform_handle_t hPlatform, const ur_device_native_properties_t *, ur_device_handle_t *phDevice) { cl_device_id NativeHandle = reinterpret_cast(hNativeDevice); - *phDevice = new ur_device_handle_t_(NativeHandle, hPlatform, nullptr); + auto URDevice = + std::make_unique(NativeHandle, hPlatform, nullptr); + *phDevice = URDevice.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/enqueue.cpp b/source/adapters/opencl/enqueue.cpp index 0da75663a5..13e952d1a4 100644 --- a/source/adapters/opencl/enqueue.cpp +++ b/source/adapters/opencl/enqueue.cpp @@ -45,7 +45,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueKernelLaunch( pGlobalWorkOffset, pGlobalWorkSize, pLocalWorkSize, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -61,7 +63,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWait( CL_RETURN_ON_FAILURE(clEnqueueMarkerWithWaitList( hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -77,7 +81,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueEventsWaitWithBarrier( CL_RETURN_ON_FAILURE(clEnqueueBarrierWithWaitList( hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -95,7 +101,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferRead( hQueue->get(), hBuffer->get(), blockingRead, offset, size, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -113,7 +121,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWrite( hQueue->get(), hBuffer->get(), blockingWrite, offset, size, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -139,7 +149,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferReadRect( Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -165,7 +177,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferWriteRect( Region, bufferRowPitch, bufferSlicePitch, hostRowPitch, hostSlicePitch, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -184,7 +198,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopy( hQueue->get(), hBufferSrc->get(), hBufferDst->get(), srcOffset, dstOffset, size, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -209,7 +225,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferCopyRect( Region, srcRowPitch, srcSlicePitch, dstRowPitch, dstSlicePitch, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -231,7 +249,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill( hQueue->get(), hBuffer->get(), pPattern, patternSize, offset, size, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -271,7 +291,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferFill( } if (phEvent) { - *phEvent = new ur_event_handle_t_(WriteEvent, hQueue->Context, hQueue); + auto UREvent = std::make_unique( + WriteEvent, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } else { CL_RETURN_ON_FAILURE(clReleaseEvent(WriteEvent)); } @@ -295,7 +317,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageRead( hQueue->get(), hImage->get(), blockingRead, Origin, Region, rowPitch, slicePitch, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -316,7 +340,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageWrite( hQueue->get(), hImage->get(), blockingWrite, Origin, Region, rowPitch, slicePitch, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -339,7 +365,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemImageCopy( hQueue->get(), hImageSrc->get(), hImageDst->get(), SrcOrigin, DstOrigin, Region, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -360,7 +388,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemBufferMap( numEventsInWaitList, CLWaitEvents.data(), &Event, &Err); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return mapCLErrorToUR(Err); } @@ -378,7 +408,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueMemUnmap( pMappedPtr, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -406,7 +438,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableWrite( Res = F(hQueue->get(), hProgram->get(), name, blockingWrite, count, offset, pSrc, numEventsInWaitList, CLWaitEvents.data(), &Event); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return mapCLErrorToUR(Res); } @@ -434,7 +468,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueDeviceGlobalVariableRead( Res = F(hQueue->get(), hProgram->get(), name, blockingRead, count, offset, pDst, numEventsInWaitList, CLWaitEvents.data(), &Event); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return mapCLErrorToUR(Res); } @@ -463,7 +499,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueReadHostPipe( blocking, pDst, size, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } } @@ -494,7 +532,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueWriteHostPipe( blocking, pSrc, size, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } } diff --git a/source/adapters/opencl/event.cpp b/source/adapters/opencl/event.cpp index 5e1d2fabe4..a41561f06c 100644 --- a/source/adapters/opencl/event.cpp +++ b/source/adapters/opencl/event.cpp @@ -115,7 +115,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventCreateWithNativeHandle( const ur_event_native_properties_t *pProperties, ur_event_handle_t *phEvent) { cl_event NativeHandle = reinterpret_cast(hNativeEvent); - *phEvent = new ur_event_handle_t_(NativeHandle, hContext, nullptr); + auto UREvent = + std::make_unique(NativeHandle, hContext, nullptr); + *phEvent = UREvent.release(); if (!pProperties || !pProperties->isNativeHandleOwned) { return urEventRetain(*phEvent); } diff --git a/source/adapters/opencl/kernel.cpp b/source/adapters/opencl/kernel.cpp index 8c7434b1ad..440c981030 100644 --- a/source/adapters/opencl/kernel.cpp +++ b/source/adapters/opencl/kernel.cpp @@ -393,11 +393,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgMemObj( ur_kernel_handle_t hKernel, uint32_t argIndex, const ur_kernel_arg_mem_obj_properties_t *, ur_mem_handle_t hArgValue) { - cl_mem CLArgValue = hArgValue->get(); - cl_int RetErr = clSetKernelArg(cl_adapter::cast(hKernel), - cl_adapter::cast(argIndex), - sizeof(hArgValue), &CLArgValue); - CL_RETURN_ON_FAILURE(RetErr); + cl_mem CLArgValue = hArgValue ? hArgValue->get() : nullptr; + CL_RETURN_ON_FAILURE(clSetKernelArg(cl_adapter::cast(hKernel), + cl_adapter::cast(argIndex), + sizeof(CLArgValue), &CLArgValue)); return UR_RESULT_SUCCESS; } @@ -408,7 +407,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelSetArgSampler( cl_sampler CLArgSampler = hArgValue->get(); cl_int RetErr = clSetKernelArg(cl_adapter::cast(hKernel), cl_adapter::cast(argIndex), - sizeof(hArgValue), &CLArgSampler); + sizeof(CLArgSampler), &CLArgSampler); CL_RETURN_ON_FAILURE(RetErr); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/memory.cpp b/source/adapters/opencl/memory.cpp index 0d3fecd305..4a5f8c6b39 100644 --- a/source/adapters/opencl/memory.cpp +++ b/source/adapters/opencl/memory.cpp @@ -266,7 +266,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate( cl_mem Buffer = FuncPtr( CLContext, PropertiesIntel.data(), static_cast(flags), size, pProperties->pHost, cl_adapter::cast(&RetErr)); - *phBuffer = new ur_mem_handle_t_(Buffer, hContext); + auto URMem = std::make_unique(Buffer, hContext); + *phBuffer = URMem.release(); return mapCLErrorToUR(RetErr); } } @@ -276,7 +277,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreate( clCreateBuffer(hContext->get(), static_cast(flags), size, HostPtr, cl_adapter::cast(&RetErr)); CL_RETURN_ON_FAILURE(RetErr); - *phBuffer = new ur_mem_handle_t_(Buffer, hContext); + auto URMem = std::make_unique(Buffer, hContext); + *phBuffer = URMem.release(); return UR_RESULT_SUCCESS; } @@ -296,7 +298,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreate( clCreateImage(hContext->get(), MapFlags, &ImageFormat, &ImageDesc, pHost, cl_adapter::cast(&RetErr)); CL_RETURN_ON_FAILURE(RetErr); - *phMem = new ur_mem_handle_t_(Mem, hContext); + auto URMem = std::make_unique(Mem, hContext); + *phMem = URMem.release(); return UR_RESULT_SUCCESS; } @@ -321,10 +324,11 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferPartition( BufferRegion.origin = pRegion->origin; BufferRegion.size = pRegion->size; - *phMem = reinterpret_cast(clCreateSubBuffer( + cl_mem Buffer = clCreateSubBuffer( hBuffer->get(), static_cast(flags), BufferCreateType, - &BufferRegion, cl_adapter::cast(&RetErr))); - + &BufferRegion, cl_adapter::cast(&RetErr)); + auto URMem = std::make_unique(Buffer, hBuffer->Context); + *phMem = URMem.release(); if (RetErr == CL_INVALID_VALUE) { size_t BufferSize = 0; CL_RETURN_ON_FAILURE(clGetMemObjectInfo( @@ -344,7 +348,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemBufferCreateWithNativeHandle( ur_native_handle_t hNativeMem, ur_context_handle_t hContext, const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) { cl_mem NativeHandle = reinterpret_cast(hNativeMem); - *phMem = new ur_mem_handle_t_(NativeHandle, hContext); + auto URMem = std::make_unique(NativeHandle, hContext); + *phMem = URMem.release(); if (!pProperties || !pProperties->isNativeHandleOwned) { return urMemRetain(*phMem); } @@ -357,7 +362,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageCreateWithNativeHandle( [[maybe_unused]] const ur_image_desc_t *pImageDesc, const ur_mem_native_properties_t *pProperties, ur_mem_handle_t *phMem) { cl_mem NativeHandle = reinterpret_cast(hNativeMem); - *phMem = new ur_mem_handle_t_(NativeHandle, hContext); + auto URMem = std::make_unique(NativeHandle, hContext); + *phMem = URMem.release(); if (!pProperties || !pProperties->isNativeHandleOwned) { return urMemRetain(*phMem); } diff --git a/source/adapters/opencl/platform.cpp b/source/adapters/opencl/platform.cpp index 2526c14a51..89bb16d7e9 100644 --- a/source/adapters/opencl/platform.cpp +++ b/source/adapters/opencl/platform.cpp @@ -87,7 +87,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries, ur_platform_handle_t *phPlatforms, uint32_t *pNumPlatforms) { - static std::vector URPlatforms; + static std::vector> URPlatforms; static std::once_flag InitFlag; static uint32_t NumPlatforms = 0; cl_int Result = CL_SUCCESS; @@ -107,7 +107,8 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries, } URPlatforms.resize(NumPlatforms); for (uint32_t i = 0; i < NumPlatforms; i++) { - URPlatforms[i] = new ur_platform_handle_t_(CLPlatforms[i]); + URPlatforms[i] = + std::make_unique(CLPlatforms[i]); } return Result; }, @@ -125,7 +126,7 @@ urPlatformGet(ur_adapter_handle_t *, uint32_t, uint32_t NumEntries, } if (NumEntries && phPlatforms) { for (uint32_t i = 0; i < NumEntries; i++) { - phPlatforms[i] = URPlatforms[i]; + phPlatforms[i] = URPlatforms[i].get(); } } return mapCLErrorToUR(Result); @@ -142,7 +143,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urPlatformCreateWithNativeHandle( ur_platform_handle_t *phPlatform) { cl_platform_id NativeHandle = reinterpret_cast(hNativePlatform); - *phPlatform = new ur_platform_handle_t_(NativeHandle); + auto URPlatform = std::make_unique(NativeHandle); + *phPlatform = URPlatform.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/platform.hpp b/source/adapters/opencl/platform.hpp index 667b998970..0957f4562d 100644 --- a/source/adapters/opencl/platform.hpp +++ b/source/adapters/opencl/platform.hpp @@ -17,172 +17,16 @@ namespace cl_adapter { ur_result_t getPlatformVersion(cl_platform_id Plat, oclv::OpenCLVersion &Version); - -// Older versions of GCC don't like "const" here -#if defined(__GNUC__) && (__GNUC__ < 7 || (__GNU__C == 7 && __GNUC_MINOR__ < 2)) -#define CONSTFIX constexpr -#else -#define CONSTFIX const -#endif - -// Names of USM functions that are queried from OpenCL -CONSTFIX char HostMemAllocName[] = "clHostMemAllocINTEL"; -CONSTFIX char DeviceMemAllocName[] = "clDeviceMemAllocINTEL"; -CONSTFIX char SharedMemAllocName[] = "clSharedMemAllocINTEL"; -CONSTFIX char MemBlockingFreeName[] = "clMemBlockingFreeINTEL"; -CONSTFIX char CreateBufferWithPropertiesName[] = - "clCreateBufferWithPropertiesINTEL"; -CONSTFIX char SetKernelArgMemPointerName[] = "clSetKernelArgMemPointerINTEL"; -CONSTFIX char EnqueueMemFillName[] = "clEnqueueMemFillINTEL"; -CONSTFIX char EnqueueMemcpyName[] = "clEnqueueMemcpyINTEL"; -CONSTFIX char GetMemAllocInfoName[] = "clGetMemAllocInfoINTEL"; -CONSTFIX char SetProgramSpecializationConstantName[] = - "clSetProgramSpecializationConstant"; -CONSTFIX char GetDeviceFunctionPointerName[] = - "clGetDeviceFunctionPointerINTEL"; -CONSTFIX char EnqueueWriteGlobalVariableName[] = - "clEnqueueWriteGlobalVariableINTEL"; -CONSTFIX char EnqueueReadGlobalVariableName[] = - "clEnqueueReadGlobalVariableINTEL"; -// Names of host pipe functions queried from OpenCL -CONSTFIX char EnqueueReadHostPipeName[] = "clEnqueueReadHostPipeINTEL"; -CONSTFIX char EnqueueWriteHostPipeName[] = "clEnqueueWriteHostPipeINTEL"; -// Names of command buffer functions queried from OpenCL -CONSTFIX char CreateCommandBufferName[] = "clCreateCommandBufferKHR"; -CONSTFIX char RetainCommandBufferName[] = "clRetainCommandBufferKHR"; -CONSTFIX char ReleaseCommandBufferName[] = "clReleaseCommandBufferKHR"; -CONSTFIX char FinalizeCommandBufferName[] = "clFinalizeCommandBufferKHR"; -CONSTFIX char CommandNRRangeKernelName[] = "clCommandNDRangeKernelKHR"; -CONSTFIX char CommandCopyBufferName[] = "clCommandCopyBufferKHR"; -CONSTFIX char CommandCopyBufferRectName[] = "clCommandCopyBufferRectKHR"; -CONSTFIX char CommandFillBufferName[] = "clCommandFillBufferKHR"; -CONSTFIX char EnqueueCommandBufferName[] = "clEnqueueCommandBufferKHR"; - -#undef CONSTFIX - -using clGetDeviceFunctionPointer_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_device_id device, cl_program program, - const char *FuncName, cl_ulong *ret_ptr); - -using clEnqueueWriteGlobalVariable_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool, - size_t, size_t, const void *, cl_uint, const cl_event *, - cl_event *); - -using clEnqueueReadGlobalVariable_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_queue, cl_program, const char *, cl_bool, - size_t, size_t, void *, cl_uint, const cl_event *, - cl_event *); - -using clSetProgramSpecializationConstant_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_program program, cl_uint spec_id, size_t spec_size, - const void *spec_value); - -using clEnqueueReadHostPipeINTEL_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program, - const char *pipe_symbol, cl_bool blocking, void *ptr, - size_t size, cl_uint num_events_in_waitlist, - const cl_event *events_waitlist, cl_event *event); - -using clEnqueueWriteHostPipeINTEL_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_queue queue, cl_program program, - const char *pipe_symbol, cl_bool blocking, - const void *ptr, size_t size, - cl_uint num_events_in_waitlist, - const cl_event *events_waitlist, cl_event *event); - -using clCreateCommandBufferKHR_fn = CL_API_ENTRY cl_command_buffer_khr( - CL_API_CALL *)(cl_uint num_queues, const cl_command_queue *queues, - const cl_command_buffer_properties_khr *properties, - cl_int *errcode_ret); - -using clRetainCommandBufferKHR_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); - -using clReleaseCommandBufferKHR_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); - -using clFinalizeCommandBufferKHR_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_command_buffer_khr command_buffer); - -using clCommandNDRangeKernelKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( - cl_command_buffer_khr command_buffer, cl_command_queue command_queue, - const cl_ndrange_kernel_command_properties_khr *properties, - cl_kernel kernel, cl_uint work_dim, const size_t *global_work_offset, - const size_t *global_work_size, const size_t *local_work_size, - cl_uint num_sync_points_in_wait_list, - const cl_sync_point_khr *sync_point_wait_list, - cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); - -using clCommandCopyBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( - cl_command_buffer_khr command_buffer, cl_command_queue command_queue, - cl_mem src_buffer, cl_mem dst_buffer, size_t src_offset, size_t dst_offset, - size_t size, cl_uint num_sync_points_in_wait_list, - const cl_sync_point_khr *sync_point_wait_list, - cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); - -using clCommandCopyBufferRectKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( - cl_command_buffer_khr command_buffer, cl_command_queue command_queue, - cl_mem src_buffer, cl_mem dst_buffer, const size_t *src_origin, - const size_t *dst_origin, const size_t *region, size_t src_row_pitch, - size_t src_slice_pitch, size_t dst_row_pitch, size_t dst_slice_pitch, - cl_uint num_sync_points_in_wait_list, - const cl_sync_point_khr *sync_point_wait_list, - cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); - -using clCommandFillBufferKHR_fn = CL_API_ENTRY cl_int(CL_API_CALL *)( - cl_command_buffer_khr command_buffer, cl_command_queue command_queue, - cl_mem buffer, const void *pattern, size_t pattern_size, size_t offset, - size_t size, cl_uint num_sync_points_in_wait_list, - const cl_sync_point_khr *sync_point_wait_list, - cl_sync_point_khr *sync_point, cl_mutable_command_khr *mutable_handle); - -using clEnqueueCommandBufferKHR_fn = CL_API_ENTRY -cl_int(CL_API_CALL *)(cl_uint num_queues, cl_command_queue *queues, - cl_command_buffer_khr command_buffer, - cl_uint num_events_in_wait_list, - const cl_event *event_wait_list, cl_event *event); - -struct ExtFuncPtrT { - clHostMemAllocINTEL_fn clHostMemAllocINTELCache; - clDeviceMemAllocINTEL_fn clDeviceMemAllocINTELCache; - clSharedMemAllocINTEL_fn clSharedMemAllocINTELCache; - clGetDeviceFunctionPointer_fn clGetDeviceFunctionPointerCache; - clCreateBufferWithPropertiesINTEL_fn clCreateBufferWithPropertiesINTELCache; - clMemBlockingFreeINTEL_fn clMemBlockingFreeINTELCache; - clSetKernelArgMemPointerINTEL_fn clSetKernelArgMemPointerINTELCache; - clEnqueueMemFillINTEL_fn clEnqueueMemFillINTELCache; - clEnqueueMemcpyINTEL_fn clEnqueueMemcpyINTELCache; - clGetMemAllocInfoINTEL_fn clGetMemAllocInfoINTELCache; - clEnqueueWriteGlobalVariable_fn clEnqueueWriteGlobalVariableCache; - clEnqueueReadGlobalVariable_fn clEnqueueReadGlobalVariableCache; - clEnqueueReadHostPipeINTEL_fn clEnqueueReadHostPipeINTELCache; - clEnqueueWriteHostPipeINTEL_fn clEnqueueWriteHostPipeINTELCache; - clSetProgramSpecializationConstant_fn clSetProgramSpecializationConstantCache; - clCreateCommandBufferKHR_fn clCreateCommandBufferKHRCache; - clRetainCommandBufferKHR_fn clRetainCommandBufferKHRCache; - clReleaseCommandBufferKHR_fn clReleaseCommandBufferKHRCache; - clFinalizeCommandBufferKHR_fn clFinalizeCommandBufferKHRCache; - clCommandNDRangeKernelKHR_fn clCommandNDRangeKernelKHRCache; - clCommandCopyBufferKHR_fn clCommandCopyBufferKHRCache; - clCommandCopyBufferRectKHR_fn clCommandCopyBufferRectKHRCache; - clCommandFillBufferKHR_fn clCommandFillBufferKHRCache; - clEnqueueCommandBufferKHR_fn clEnqueueCommandBufferKHRCache; -}; } // namespace cl_adapter struct ur_platform_handle_t_ { using native_type = cl_platform_id; native_type Platform = nullptr; - std::unique_ptr ExtFuncPtr; - std::vector Devices; + std::vector> Devices; - ur_platform_handle_t_(native_type Plat) : Platform(Plat) { - ExtFuncPtr = std::make_unique(); - InitDevices(); - } + ur_platform_handle_t_(native_type Plat) : Platform(Plat) {} - ~ur_platform_handle_t_() { ExtFuncPtr.reset(); } + ~ur_platform_handle_t_() {} template ur_result_t getExtFunc(T CachedExtFunc, const char *FuncName, T *Fptr) { @@ -201,17 +45,20 @@ struct ur_platform_handle_t_ { native_type get() { return Platform; } ur_result_t InitDevices() { - cl_uint DeviceNum = 0; - CL_RETURN_ON_FAILURE( - clGetDeviceIDs(Platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &DeviceNum)); - - std::vector CLDevices(DeviceNum); - CL_RETURN_ON_FAILURE(clGetDeviceIDs(Platform, CL_DEVICE_TYPE_ALL, DeviceNum, - CLDevices.data(), nullptr)); - - Devices = std::vector(DeviceNum); - for (size_t i = 0; i < DeviceNum; i++) { - Devices[i] = new ur_device_handle_t_(CLDevices[i], this, nullptr); + if (Devices.empty()) { + cl_uint DeviceNum = 0; + CL_RETURN_ON_FAILURE( + clGetDeviceIDs(Platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &DeviceNum)); + + std::vector CLDevices(DeviceNum); + CL_RETURN_ON_FAILURE(clGetDeviceIDs( + Platform, CL_DEVICE_TYPE_ALL, DeviceNum, CLDevices.data(), nullptr)); + + Devices.resize(DeviceNum); + for (size_t i = 0; i < DeviceNum; i++) { + Devices[i] = + std::make_unique(CLDevices[i], this, nullptr); + } } return UR_RESULT_SUCCESS; diff --git a/source/adapters/opencl/program.cpp b/source/adapters/opencl/program.cpp index fc1ca40cbe..a647cd64be 100644 --- a/source/adapters/opencl/program.cpp +++ b/source/adapters/opencl/program.cpp @@ -72,8 +72,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( cl_program Program = clCreateProgramWithIL(hContext->get(), pIL, length, &Err); CL_RETURN_ON_FAILURE(Err); - - *phProgram = new ur_program_handle_t_(Program, hContext); + auto URProgram = std::make_unique(Program, hContext); + *phProgram = URProgram.release(); } else { /* If none of the devices conform with CL 2.1 or newer make sure they all @@ -100,7 +100,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithIL( assert(FuncPtr != nullptr); cl_program Program = FuncPtr(hContext->get(), pIL, length, &Err); - *phProgram = new ur_program_handle_t_(Program, hContext); + auto URProgram = std::make_unique(Program, hContext); + *phProgram = URProgram.release(); CL_RETURN_ON_FAILURE(Err); } @@ -120,7 +121,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithBinary( cl_program Program = clCreateProgramWithBinary( hContext->get(), cl_adapter::cast(1u), Devices, Lengths, &pBinary, BinaryStatus, &CLResult); - *phProgram = new ur_program_handle_t_(Program, hContext); + + auto URProgram = std::make_unique(Program, hContext); + *phProgram = URProgram.release(); CL_RETURN_ON_FAILURE(BinaryStatus[0]); CL_RETURN_ON_FAILURE(CLResult); @@ -186,6 +189,10 @@ urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, cl_uint DeviceCount = hProgram->Context->DeviceCount; return ReturnValue(DeviceCount); } + case UR_PROGRAM_INFO_DEVICES: { + return ReturnValue(&hProgram->Context->Devices[0], + hProgram->Context->DeviceCount); + } default: { size_t CheckPropSize = 0; auto ClResult = clGetProgramInfo(hProgram->get(), CLPropName, propSize, @@ -230,7 +237,8 @@ urProgramLink(ur_context_handle_t hContext, uint32_t count, hContext->get(), 0, nullptr, pOptions, cl_adapter::cast(count), CLPrograms.data(), nullptr, nullptr, &CLResult); CL_RETURN_ON_FAILURE(CLResult); - *phProgram = new ur_program_handle_t_(Program, hContext); + auto URProgram = std::make_unique(Program, hContext); + *phProgram = URProgram.release(); return UR_RESULT_SUCCESS; } @@ -340,7 +348,10 @@ UR_APIEXPORT ur_result_t UR_APICALL urProgramCreateWithNativeHandle( const ur_program_native_properties_t *pProperties, ur_program_handle_t *phProgram) { cl_program NativeHandle = reinterpret_cast(hNativeProgram); - *phProgram = new ur_program_handle_t_(NativeHandle, hContext); + + auto URProgram = + std::make_unique(NativeHandle, hContext); + *phProgram = URProgram.release(); if (!pProperties || !pProperties->isNativeHandleOwned) { return urProgramRetain(*phProgram); } diff --git a/source/adapters/opencl/queue.cpp b/source/adapters/opencl/queue.cpp index 41ff096acc..477bcb2015 100644 --- a/source/adapters/opencl/queue.cpp +++ b/source/adapters/opencl/queue.cpp @@ -95,7 +95,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate( clCreateCommandQueue(hContext->get(), hDevice->get(), CLProperties & SupportByOpenCL, &RetErr); CL_RETURN_ON_FAILURE(RetErr); - *phQueue = new ur_queue_handle_t_(Queue, hContext, hDevice); + auto URQueue = + std::make_unique(Queue, hContext, hDevice); + *phQueue = URQueue.release(); return UR_RESULT_SUCCESS; } @@ -105,7 +107,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreate( cl_command_queue Queue = clCreateCommandQueueWithProperties( hContext->get(), hDevice->get(), CreationFlagProperties, &RetErr); CL_RETURN_ON_FAILURE(RetErr); - *phQueue = new ur_queue_handle_t_(Queue, hContext, hDevice); + auto URQueue = std::make_unique(Queue, hContext, hDevice); + *phQueue = URQueue.release(); return UR_RESULT_SUCCESS; } @@ -167,7 +170,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueCreateWithNativeHandle( cl_command_queue NativeHandle = reinterpret_cast(hNativeQueue); - *phQueue = new ur_queue_handle_t_(NativeHandle, hContext, hDevice); + auto URQueue = + std::make_unique(NativeHandle, hContext, hDevice); + *phQueue = URQueue.release(); cl_int RetErr = clRetainCommandQueue(cl_adapter::cast(hNativeQueue)); diff --git a/source/adapters/opencl/sampler.cpp b/source/adapters/opencl/sampler.cpp index 51457535ec..6a59fe7eaf 100644 --- a/source/adapters/opencl/sampler.cpp +++ b/source/adapters/opencl/sampler.cpp @@ -148,7 +148,8 @@ ur_result_t urSamplerCreate(ur_context_handle_t hContext, cl_sampler Sampler = clCreateSampler( hContext->get(), static_cast(pDesc->normalizedCoords), AddressingMode, FilterMode, cl_adapter::cast(&ErrorCode)); - *phSampler = new ur_sampler_handle_t_(Sampler, hContext); + auto URSampler = std::make_unique(Sampler, hContext); + *phSampler = URSampler.release(); return mapCLErrorToUR(ErrorCode); } @@ -200,6 +201,8 @@ UR_APIEXPORT ur_result_t UR_APICALL urSamplerCreateWithNativeHandle( ur_native_handle_t hNativeSampler, ur_context_handle_t hContext, const ur_sampler_native_properties_t *, ur_sampler_handle_t *phSampler) { cl_sampler NativeHandle = reinterpret_cast(hNativeSampler); - *phSampler = new ur_sampler_handle_t_(NativeHandle, hContext); + auto URSampler = + std::make_unique(NativeHandle, hContext); + *phSampler = URSampler.release(); return UR_RESULT_SUCCESS; } diff --git a/source/adapters/opencl/usm.cpp b/source/adapters/opencl/usm.cpp index 684004e126..241936a6ad 100644 --- a/source/adapters/opencl/usm.cpp +++ b/source/adapters/opencl/usm.cpp @@ -252,7 +252,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill( patternSize, size, numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; } @@ -287,9 +289,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill( } cl_event CopyEvent = nullptr; - CL_RETURN_ON_FAILURE(USMMemcpy( - hQueue->get(), false, ptr, HostBuffer, size, numEventsInWaitList, - cl_adapter::cast(phEventWaitList), &CopyEvent)); + std::vector CLWaitEvents(numEventsInWaitList); + for (uint32_t i = 0; i < numEventsInWaitList; i++) { + CLWaitEvents[i] = phEventWaitList[i]->get(); + } + CL_RETURN_ON_FAILURE(USMMemcpy(hQueue->get(), false, ptr, HostBuffer, size, + numEventsInWaitList, CLWaitEvents.data(), + &CopyEvent)); struct DeleteCallbackInfo { DeleteCallbackInfo(clMemBlockingFreeINTEL_fn USMFree, cl_context CLContext, @@ -326,7 +332,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMFill( CL_RETURN_ON_FAILURE(ClErr); } if (phEvent) { - *phEvent = new ur_event_handle_t_(CopyEvent, hQueue->Context, hQueue); + auto UREvent = std::make_unique( + CopyEvent, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } else { CL_RETURN_ON_FAILURE(clReleaseEvent(CopyEvent)); } @@ -357,7 +365,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy( numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } } @@ -378,7 +388,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMPrefetch( CL_RETURN_ON_FAILURE(clEnqueueMarkerWithWaitList( hQueue->get(), numEventsInWaitList, CLWaitEvents.data(), &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; /* @@ -411,7 +423,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMAdvise( CL_RETURN_ON_FAILURE( clEnqueueMarkerWithWaitList(hQueue->get(), 0, nullptr, &Event)); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } return UR_RESULT_SUCCESS; /* @@ -491,7 +505,9 @@ UR_APIEXPORT ur_result_t UR_APICALL urEnqueueUSMMemcpy2D( ClResult = clEnqueueBarrierWithWaitList(hQueue->get(), Events.size(), Events.data(), &Event); if (phEvent) { - *phEvent = new ur_event_handle_t_(Event, hQueue->Context, hQueue); + auto UREvent = + std::make_unique(Event, hQueue->Context, hQueue); + *phEvent = UREvent.release(); } } for (const auto &E : Events) {