From 0fc41339afa353aa35b7af18edec4e9c9f2f7f6b Mon Sep 17 00:00:00 2001 From: Arvind Sudarsanam Date: Tue, 22 Jun 2021 08:39:24 -0700 Subject: [PATCH 1/3] Add support to detect host->device and device->host transfers for USM memory copy Signed-off-by: Arvind Sudarsanam --- sycl/plugins/level_zero/pi_level_zero.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index fb156d60f185d..7455b8113d65e 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -6129,6 +6129,20 @@ pi_result piextUSMEnqueueMemset(pi_queue Queue, void *Ptr, pi_int32 Value, Count, NumEventsInWaitlist, EventsWaitlist, Event); } +// Helper function to check if a pointer is a host pointer. +static bool IsHostPointer(pi_context Context, const void *Ptr) { + // Query the device of the allocation + ze_device_handle_t ZeDeviceHandle; + ze_memory_allocation_properties_t ZeMemoryAllocationProperties = {}; + + // Query memory type of the pointer + ZE_CALL(zeMemGetAllocProperties, + (Context->ZeContext, Ptr, &ZeMemoryAllocationProperties, + &ZeDeviceHandle)); + + return (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_HOST); +} + pi_result piextUSMEnqueueMemcpy(pi_queue Queue, pi_bool Blocking, void *DstPtr, const void *SrcPtr, size_t Size, pi_uint32 NumEventsInWaitlist, @@ -6140,11 +6154,12 @@ pi_result piextUSMEnqueueMemcpy(pi_queue Queue, pi_bool Blocking, void *DstPtr, } PI_ASSERT(Queue, PI_INVALID_QUEUE); - + bool PreferCopyEngine = IsHostPointer(Queue->Context, SrcPtr) || + IsHostPointer(Queue->Context, DstPtr); return enqueueMemCopyHelper( // TODO: do we need a new command type for this? PI_COMMAND_TYPE_MEM_BUFFER_COPY, Queue, DstPtr, Blocking, Size, SrcPtr, - NumEventsInWaitlist, EventsWaitlist, Event); + NumEventsInWaitlist, EventsWaitlist, Event, PreferCopyEngine); } /// Hint to migrate memory to the device From af8695d497ad30cc71b9ab62fa623f1cbf785080 Mon Sep 17 00:00:00 2001 From: Arvind Sudarsanam Date: Tue, 22 Jun 2021 13:21:54 -0700 Subject: [PATCH 2/3] Modifying check for device to device copies --- sycl/plugins/level_zero/pi_level_zero.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 7455b8113d65e..878b4706ca367 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -6129,9 +6129,8 @@ pi_result piextUSMEnqueueMemset(pi_queue Queue, void *Ptr, pi_int32 Value, Count, NumEventsInWaitlist, EventsWaitlist, Event); } -// Helper function to check if a pointer is a host pointer. -static bool IsHostPointer(pi_context Context, const void *Ptr) { - // Query the device of the allocation +// Helper function to check if a pointer is a device pointer. +static bool IsDevicePointer(pi_context Context, const void *Ptr) { ze_device_handle_t ZeDeviceHandle; ze_memory_allocation_properties_t ZeMemoryAllocationProperties = {}; @@ -6140,7 +6139,7 @@ static bool IsHostPointer(pi_context Context, const void *Ptr) { (Context->ZeContext, Ptr, &ZeMemoryAllocationProperties, &ZeDeviceHandle)); - return (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_HOST); + return (ZeMemoryAllocationProperties.type == ZE_MEMORY_TYPE_DEVICE); } pi_result piextUSMEnqueueMemcpy(pi_queue Queue, pi_bool Blocking, void *DstPtr, @@ -6154,8 +6153,11 @@ pi_result piextUSMEnqueueMemcpy(pi_queue Queue, pi_bool Blocking, void *DstPtr, } PI_ASSERT(Queue, PI_INVALID_QUEUE); - bool PreferCopyEngine = IsHostPointer(Queue->Context, SrcPtr) || - IsHostPointer(Queue->Context, DstPtr); + + // Device to Device copies are found to execute slower on copy engine + // (versus compute engine). + bool PreferCopyEngine = !IsDevicePointer(Queue->Context, SrcPtr) && + !IsDevicePointer(Queue->Context, DstPtr); return enqueueMemCopyHelper( // TODO: do we need a new command type for this? PI_COMMAND_TYPE_MEM_BUFFER_COPY, Queue, DstPtr, Blocking, Size, SrcPtr, From ffa7136f14333d2f544077c44f127c683944e10d Mon Sep 17 00:00:00 2001 From: Arvind Sudarsanam Date: Tue, 22 Jun 2021 13:28:02 -0700 Subject: [PATCH 3/3] Fix wrong boolean operation --- sycl/plugins/level_zero/pi_level_zero.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/plugins/level_zero/pi_level_zero.cpp b/sycl/plugins/level_zero/pi_level_zero.cpp index 878b4706ca367..ec6bccf05b6b9 100644 --- a/sycl/plugins/level_zero/pi_level_zero.cpp +++ b/sycl/plugins/level_zero/pi_level_zero.cpp @@ -6156,7 +6156,7 @@ pi_result piextUSMEnqueueMemcpy(pi_queue Queue, pi_bool Blocking, void *DstPtr, // Device to Device copies are found to execute slower on copy engine // (versus compute engine). - bool PreferCopyEngine = !IsDevicePointer(Queue->Context, SrcPtr) && + bool PreferCopyEngine = !IsDevicePointer(Queue->Context, SrcPtr) || !IsDevicePointer(Queue->Context, DstPtr); return enqueueMemCopyHelper( // TODO: do we need a new command type for this?