Skip to content

[SYCL][L0] initialize descriptor .stype and .pNext #4032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 21 additions & 24 deletions sycl/plugins/level_zero/pi_level_zero.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ _pi_context::getFreeSlotInExistingOrNewPool(ze_event_pool_handle_t &ZePool,
std::lock_guard<std::mutex> NumEventsLiveInEventPoolGuard(
NumEventsLiveInEventPoolMutex, std::adopt_lock);

ze_event_pool_desc_t ZeEventPoolDesc = {};
ZeEventPoolDesc.stype = ZE_STRUCTURE_TYPE_EVENT_POOL_DESC;
ZeStruct<ze_event_pool_desc_t> ZeEventPoolDesc;
ZeEventPoolDesc.count = MaxNumEventsPerPool;

// Make all events visible on the host.
Expand Down Expand Up @@ -585,7 +584,7 @@ pi_result _pi_context::initialize() {
// TODO: get rid of using Devices[0] for the context with multiple
// root-devices. We should somehow make the data initialized on all devices.
pi_device Device = SingleRootDevice ? SingleRootDevice : Devices[0];
ze_command_queue_desc_t ZeCommandQueueDesc = {};
ZeStruct<ze_command_queue_desc_t> ZeCommandQueueDesc;
ZeCommandQueueDesc.ordinal = Device->ZeComputeQueueGroupIndex;
ZeCommandQueueDesc.index = 0;
ZeCommandQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_SYNCHRONOUS;
Expand Down Expand Up @@ -733,10 +732,8 @@ pi_result _pi_context::getAvailableCommandList(
// Each command list is paired with an associated fence to track when the
// command list is available for reuse.
_pi_result pi_result = PI_OUT_OF_RESOURCES;
ze_command_list_desc_t ZeCommandListDesc = {};
ZeCommandListDesc.stype = ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
ze_fence_desc_t ZeFenceDesc = {};
ZeFenceDesc.stype = ZE_STRUCTURE_TYPE_FENCE_DESC;
ZeStruct<ze_command_list_desc_t> ZeCommandListDesc;
ZeStruct<ze_fence_desc_t> ZeFenceDesc;

ZeCommandListDesc.commandQueueGroupOrdinal =
(UseCopyEngine) ? Queue->Device->ZeCopyQueueGroupIndex
Expand Down Expand Up @@ -2219,7 +2216,9 @@ pi_result piContextCreate(const pi_context_properties *Properties,
PI_ASSERT(RetContext, PI_INVALID_VALUE);

pi_platform Platform = (*Devices)->Platform;
ze_context_desc_t ContextDesc = {ZE_STRUCTURE_TYPE_CONTEXT_DESC, nullptr, 0};
ZeStruct<ze_context_desc_t> ContextDesc;
ContextDesc.flags = 0;

ze_context_handle_t ZeContext;
ZE_CALL(zeContextCreate, (Platform->ZeDriver, &ContextDesc, &ZeContext));
try {
Expand Down Expand Up @@ -2384,7 +2383,7 @@ pi_result piQueueCreate(pi_context Context, pi_device Device,
PI_ASSERT(Device, PI_INVALID_DEVICE);

ZeDevice = Device->ZeDevice;
ze_command_queue_desc_t ZeCommandQueueDesc = {};
ZeStruct<ze_command_queue_desc_t> ZeCommandQueueDesc;
ZeCommandQueueDesc.ordinal = Device->ZeComputeQueueGroupIndex;
ZeCommandQueueDesc.index = 0;
ZeCommandQueueDesc.mode = ZE_COMMAND_QUEUE_MODE_ASYNCHRONOUS;
Expand Down Expand Up @@ -2835,7 +2834,7 @@ pi_result piMemImageCreate(pi_context Context, pi_mem_flags Flags,
return PI_INVALID_VALUE;
}

ze_image_desc_t ZeImageDesc = {};
ZeStruct<ze_image_desc_t> ZeImageDesc;
ZeImageDesc.arraylevels = ZeImageDesc.flags = 0;
ZeImageDesc.type = ZeImageType;
ZeImageDesc.format = ZeFormatDesc;
Expand Down Expand Up @@ -3335,7 +3334,7 @@ static pi_result compileOrBuild(pi_program Program, pi_uint32 NumDevices,
}

// Ask Level Zero to build and load the native code onto the device.
ze_module_desc_t ZeModuleDesc = {};
ZeStruct<ze_module_desc_t> ZeModuleDesc;
ZeModuleDesc.format = (Program->State == _pi_program::IL)
? ZE_MODULE_FORMAT_IL_SPIRV
: ZE_MODULE_FORMAT_NATIVE;
Expand Down Expand Up @@ -3510,7 +3509,7 @@ static pi_result copyModule(ze_context_handle_t ZeContext,
std::unique_ptr<uint8_t[]> Code(new uint8_t[Length]);
ZE_CALL(zeModuleGetNativeBinary, (SrcMod, &Length, Code.get()));

ze_module_desc_t ZeModuleDesc = {};
ZeStruct<ze_module_desc_t> ZeModuleDesc;
ZeModuleDesc.format = ZE_MODULE_FORMAT_NATIVE;
ZeModuleDesc.inputSize = Length;
ZeModuleDesc.pInputModule = Code.get();
Expand Down Expand Up @@ -3597,7 +3596,7 @@ pi_result piKernelCreate(pi_program Program, const char *KernelName,
return PI_INVALID_PROGRAM_EXECUTABLE;
}

ze_kernel_desc_t ZeKernelDesc = {};
ZeStruct<ze_kernel_desc_t> ZeKernelDesc;
ZeKernelDesc.flags = 0;
ZeKernelDesc.pKernelName = KernelName;

Expand Down Expand Up @@ -4062,7 +4061,7 @@ pi_result piEventCreate(pi_context Context, pi_event *RetEvent) {
return Res;

ze_event_handle_t ZeEvent;
ze_event_desc_t ZeEventDesc = {};
ZeStruct<ze_event_desc_t> ZeEventDesc;
// We have to set the SIGNAL flag as HOST scope because the
// Level-Zero plugin implementation waits for the events to complete
// on the host.
Expand Down Expand Up @@ -4465,7 +4464,7 @@ pi_result piSamplerCreate(pi_context Context,
pi_device Device = Context->Devices[0];

ze_sampler_handle_t ZeSampler;
ze_sampler_desc_t ZeSamplerDesc = {};
ZeStruct<ze_sampler_desc_t> ZeSamplerDesc;

// Set the default values for the ZeSamplerDesc.
ZeSamplerDesc.isNormalized = PI_TRUE;
Expand Down Expand Up @@ -5181,7 +5180,7 @@ pi_result piEnqueueMemBufferMap(pi_queue Queue, pi_mem Buffer,
// Use the version with reference counting
PI_CALL(piextUSMHostAlloc(RetMap, Queue->Context, nullptr, Size, 1));
} else {
ze_host_mem_alloc_desc_t ZeDesc = {};
ZeStruct<ze_host_mem_alloc_desc_t> ZeDesc;
ZeDesc.flags = 0;

ZE_CALL(zeMemAllocHost,
Expand Down Expand Up @@ -5711,14 +5710,13 @@ static pi_result USMDeviceAllocImpl(void **ResultPtr, pi_context Context,
PI_INVALID_VALUE);

// TODO: translate PI properties to Level Zero flags
ze_device_mem_alloc_desc_t ZeDesc = {};
ZeStruct<ze_device_mem_alloc_desc_t> ZeDesc;
ZeDesc.flags = 0;
ZeDesc.ordinal = 0;

ze_relaxed_allocation_limits_exp_desc_t RelaxedDesc = {};
ZeStruct<ze_relaxed_allocation_limits_exp_desc_t> RelaxedDesc;
if (Size > Device->ZeDeviceProperties.maxMemAllocSize) {
// Tell Level-Zero to accept Size > maxMemAllocSize
RelaxedDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
RelaxedDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
ZeDesc.pNext = &RelaxedDesc;
}
Expand All @@ -5745,16 +5743,15 @@ static pi_result USMSharedAllocImpl(void **ResultPtr, pi_context Context,
PI_INVALID_VALUE);

// TODO: translate PI properties to Level Zero flags
ze_host_mem_alloc_desc_t ZeHostDesc = {};
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
ZeHostDesc.flags = 0;
ze_device_mem_alloc_desc_t ZeDevDesc = {};
ZeStruct<ze_device_mem_alloc_desc_t> ZeDevDesc;
ZeDevDesc.flags = 0;
ZeDevDesc.ordinal = 0;

ze_relaxed_allocation_limits_exp_desc_t RelaxedDesc = {};
ZeStruct<ze_relaxed_allocation_limits_exp_desc_t> RelaxedDesc;
if (Size > Device->ZeDeviceProperties.maxMemAllocSize) {
// Tell Level-Zero to accept Size > maxMemAllocSize
RelaxedDesc.stype = ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
RelaxedDesc.flags = ZE_RELAXED_ALLOCATION_LIMITS_EXP_FLAG_MAX_SIZE;
ZeDevDesc.pNext = &RelaxedDesc;
}
Expand All @@ -5779,7 +5776,7 @@ static pi_result USMHostAllocImpl(void **ResultPtr, pi_context Context,
PI_INVALID_VALUE);

// TODO: translate PI properties to Level Zero flags
ze_host_mem_alloc_desc_t ZeHostDesc = {};
ZeStruct<ze_host_mem_alloc_desc_t> ZeHostDesc;
ZeHostDesc.flags = 0;
ZE_CALL(zeMemAllocHost,
(Context->ZeContext, &ZeHostDesc, Size, Alignment, ResultPtr));
Expand Down
58 changes: 57 additions & 1 deletion sycl/plugins/level_zero/pi_level_zero.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,62 @@ template <> uint32_t pi_cast(uint64_t Value) {
std::terminate();
}

// Returns the ze_structure_type_t to use in .stype of a structured descriptor.
// Intentionally not defined; will give an error if no proper specialization
template <class T> ze_structure_type_t getZeStructureType();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't yet use other structures in the plugin. Will extend specializations as needed.


template <> ze_structure_type_t getZeStructureType<ze_event_pool_desc_t>() {
return ZE_STRUCTURE_TYPE_EVENT_POOL_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_fence_desc_t>() {
return ZE_STRUCTURE_TYPE_FENCE_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_command_list_desc_t>() {
return ZE_STRUCTURE_TYPE_COMMAND_LIST_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_context_desc_t>() {
return ZE_STRUCTURE_TYPE_CONTEXT_DESC;
}
template <>
ze_structure_type_t
getZeStructureType<ze_relaxed_allocation_limits_exp_desc_t>() {
return ZE_STRUCTURE_TYPE_RELAXED_ALLOCATION_LIMITS_EXP_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_host_mem_alloc_desc_t>() {
return ZE_STRUCTURE_TYPE_HOST_MEM_ALLOC_DESC;
}
template <>
ze_structure_type_t getZeStructureType<ze_device_mem_alloc_desc_t>() {
return ZE_STRUCTURE_TYPE_DEVICE_MEM_ALLOC_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_command_queue_desc_t>() {
return ZE_STRUCTURE_TYPE_COMMAND_QUEUE_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_image_desc_t>() {
return ZE_STRUCTURE_TYPE_IMAGE_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_module_desc_t>() {
return ZE_STRUCTURE_TYPE_MODULE_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_kernel_desc_t>() {
return ZE_STRUCTURE_TYPE_KERNEL_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_event_desc_t>() {
return ZE_STRUCTURE_TYPE_EVENT_DESC;
}
template <> ze_structure_type_t getZeStructureType<ze_sampler_desc_t>() {
return ZE_STRUCTURE_TYPE_SAMPLER_DESC;
}

// The helper struct to properly default initialize Level-Zero descriptor
// structure.
template <class T> struct ZeStruct : public T {
ZeStruct() {
this->stype = getZeStructureType<T>();
this->pNext = nullptr;
}
};

// Base class to store common data
struct _pi_object {
_pi_object() : RefCount{1} {}
Expand Down Expand Up @@ -613,7 +669,7 @@ struct _pi_image final : _pi_mem {

#ifndef NDEBUG
// Keep the descriptor of the image (for debugging purposes)
ze_image_desc_t ZeImageDesc;
ZeStruct<ze_image_desc_t> ZeImageDesc;
#endif // !NDEBUG

// Level Zero image handle.
Expand Down