|
18 | 18 | #include <sycl/detail/handler_proxy.hpp>
|
19 | 19 | #include <sycl/detail/os_util.hpp>
|
20 | 20 | #include <sycl/event.hpp>
|
| 21 | +#include <sycl/ext/oneapi/device_global/device_global.hpp> |
21 | 22 | #include <sycl/ext/oneapi/kernel_properties/properties.hpp>
|
22 | 23 | #include <sycl/ext/oneapi/properties/properties.hpp>
|
23 | 24 | #include <sycl/ext/oneapi/properties/property.hpp>
|
@@ -2576,6 +2577,91 @@ class __SYCL_EXPORT handler {
|
2576 | 2577 | commonUSMFill2DFallbackKernel(Dest, DestPitch, Pattern, Width, Height);
|
2577 | 2578 | }
|
2578 | 2579 |
|
| 2580 | + /// Copies data from a USM memory region to a device_global. |
| 2581 | + /// Throws an exception if the copy operation intends to write outside the |
| 2582 | + /// memory range \param Dest, as specified through \param NumBytes and |
| 2583 | + /// \param DestOffset. |
| 2584 | + /// |
| 2585 | + /// \param Dest is the destination device_glboal. |
| 2586 | + /// \param Src is a USM pointer to the source memory. |
| 2587 | + /// \param NumBytes is a number of bytes to copy. |
| 2588 | + /// \param DestOffset is the offset into \param Dest to copy to. |
| 2589 | + template <typename T, typename PropertyListT> |
| 2590 | + void memcpy(ext::oneapi::experimental::device_global<T, PropertyListT> &Dest, |
| 2591 | + const void *Src, size_t NumBytes = sizeof(T), |
| 2592 | + size_t DestOffset = 0) { |
| 2593 | + if (sizeof(T) < DestOffset + NumBytes) |
| 2594 | + throw sycl::exception(make_error_code(errc::invalid), |
| 2595 | + "Copy to device_global is out of bounds."); |
| 2596 | + |
| 2597 | + constexpr bool IsDeviceImageScoped = PropertyListT::template has_property< |
| 2598 | + ext::oneapi::experimental::device_image_scope_key>(); |
| 2599 | + memcpyToDeviceGlobal(&Dest, Src, IsDeviceImageScoped, NumBytes, DestOffset); |
| 2600 | + } |
| 2601 | + |
| 2602 | + /// Copies data from a device_global to USM memory. |
| 2603 | + /// Throws an exception if the copy operation intends to read outside the |
| 2604 | + /// memory range \param Src, as specified through \param NumBytes and |
| 2605 | + /// \param SrcOffset. |
| 2606 | + /// |
| 2607 | + /// \param Dest is a USM pointer to copy to. |
| 2608 | + /// \param Src is the source device_global. |
| 2609 | + /// \param NumBytes is a number of bytes to copy. |
| 2610 | + /// \param SrcOffset is the offset into \param Src to copy from. |
| 2611 | + template <typename T, typename PropertyListT> |
| 2612 | + void |
| 2613 | + memcpy(void *Dest, |
| 2614 | + const ext::oneapi::experimental::device_global<T, PropertyListT> &Src, |
| 2615 | + size_t NumBytes = sizeof(T), size_t SrcOffset = 0) { |
| 2616 | + if (sizeof(T) < SrcOffset + NumBytes) |
| 2617 | + throw sycl::exception(make_error_code(errc::invalid), |
| 2618 | + "Copy from device_global is out of bounds."); |
| 2619 | + |
| 2620 | + constexpr bool IsDeviceImageScoped = PropertyListT::template has_property< |
| 2621 | + ext::oneapi::experimental::device_image_scope_key>(); |
| 2622 | + memcpyFromDeviceGlobal(Dest, &Src, IsDeviceImageScoped, NumBytes, |
| 2623 | + SrcOffset); |
| 2624 | + } |
| 2625 | + |
| 2626 | + /// Copies elements of type `std::remove_all_extents_t<T>` from a USM memory |
| 2627 | + /// region to a device_global. |
| 2628 | + /// Throws an exception if the copy operation intends to write outside the |
| 2629 | + /// memory range \param Dest, as specified through \param Count and |
| 2630 | + /// \param StartIndex. |
| 2631 | + /// |
| 2632 | + /// \param Src is a USM pointer to the source memory. |
| 2633 | + /// \param Dest is the destination device_glboal. |
| 2634 | + /// \param Count is a number of elements to copy. |
| 2635 | + /// \param StartIndex is the index of the first element in Dest to copy to. |
| 2636 | + template <typename T, typename PropertyListT> |
| 2637 | + void copy(const std::remove_all_extents_t<T> *Src, |
| 2638 | + ext::oneapi::experimental::device_global<T, PropertyListT> &Dest, |
| 2639 | + size_t Count = sizeof(T) / sizeof(std::remove_all_extents_t<T>), |
| 2640 | + size_t StartIndex = 0) { |
| 2641 | + this->memcpy(Dest, Src, Count * sizeof(std::remove_all_extents_t<T>), |
| 2642 | + StartIndex * sizeof(std::remove_all_extents_t<T>)); |
| 2643 | + } |
| 2644 | + |
| 2645 | + /// Copies elements of type `std::remove_all_extents_t<T>` from a |
| 2646 | + /// device_global to a USM memory region. |
| 2647 | + /// Throws an exception if the copy operation intends to write outside the |
| 2648 | + /// memory range \param Src, as specified through \param Count and |
| 2649 | + /// \param StartIndex. |
| 2650 | + /// |
| 2651 | + /// \param Src is the source device_global. |
| 2652 | + /// \param Dest is a USM pointer to copy to. |
| 2653 | + /// \param Count is a number of elements to copy. |
| 2654 | + /// \param StartIndex is the index of the first element in Src to copy from. |
| 2655 | + template <typename T, typename PropertyListT> |
| 2656 | + void |
| 2657 | + copy(const ext::oneapi::experimental::device_global<T, PropertyListT> &Src, |
| 2658 | + std::remove_all_extents_t<T> *Dest, |
| 2659 | + size_t Count = sizeof(T) / sizeof(std::remove_all_extents_t<T>), |
| 2660 | + size_t StartIndex = 0) { |
| 2661 | + this->memcpy(Dest, Src, Count * sizeof(std::remove_all_extents_t<T>), |
| 2662 | + StartIndex * sizeof(std::remove_all_extents_t<T>)); |
| 2663 | + } |
| 2664 | + |
2579 | 2665 | private:
|
2580 | 2666 | std::shared_ptr<detail::handler_impl> MImpl;
|
2581 | 2667 | std::shared_ptr<detail::queue_impl> MQueue;
|
@@ -2774,6 +2860,16 @@ class __SYCL_EXPORT handler {
|
2774 | 2860 | // Implementation of ext_oneapi_memset2d using command for native 2D memset.
|
2775 | 2861 | void ext_oneapi_memset2d_impl(void *Dest, size_t DestPitch, int Value,
|
2776 | 2862 | size_t Width, size_t Height);
|
| 2863 | + |
| 2864 | + // Implementation of memcpy to device_global. |
| 2865 | + void memcpyToDeviceGlobal(const void *DeviceGlobalPtr, const void *Src, |
| 2866 | + bool IsDeviceImageScoped, size_t NumBytes, |
| 2867 | + size_t Offset); |
| 2868 | + |
| 2869 | + // Implementation of memcpy from device_global. |
| 2870 | + void memcpyFromDeviceGlobal(void *Dest, const void *DeviceGlobalPtr, |
| 2871 | + bool IsDeviceImageScoped, size_t NumBytes, |
| 2872 | + size_t Offset); |
2777 | 2873 | };
|
2778 | 2874 | } // __SYCL_INLINE_VER_NAMESPACE(_V1)
|
2779 | 2875 | } // namespace sycl
|
0 commit comments