Skip to content

Feature/memory zero copy #152

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 50 commits into from
Oct 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
0714fee
Memory class exposes __sycl_usm_array_interface__
oleksandr-pavlyk Oct 21, 2020
906d77b
added memory.py to expose dpclt.memory module
oleksandr-pavlyk Oct 21, 2020
dfc0ac8
Pickling should preserve type of Python object
oleksandr-pavlyk Oct 22, 2020
eaf8574
Added comment, fixed black madness
oleksandr-pavlyk Oct 22, 2020
63b9b07
Added DPPLaligned_alloc_* functions to backend
oleksandr-pavlyk Oct 22, 2020
ec742a0
MemoryUSM* classes have have alignment option
oleksandr-pavlyk Oct 22, 2020
5df0bf6
Added DPPLQueue_Prefetch and DPPLQueue_MemAdvise
oleksandr-pavlyk Oct 22, 2020
e46f6ff
Implemented SyclQueue.prefetch, SyclQueue.mem_advise
oleksandr-pavlyk Oct 22, 2020
5a2bcf4
use numpy buffer as intermediary in copy_via_host routine
oleksandr-pavlyk Oct 23, 2020
242eb3a
added DPPLUSM_GetPointerDevice
oleksandr-pavlyk Oct 23, 2020
cebc064
Introduced tests/test_sycl_usm_interface.cpp
oleksandr-pavlyk Oct 23, 2020
2f6a01c
implemented cdef Memory.get_pointer_device
oleksandr-pavlyk Oct 23, 2020
9dbe493
Merge remote-tracking branch 'origin/master' into feature/memory-zero…
oleksandr-pavlyk Oct 23, 2020
ce554ac
Special case of zero-copy from another Memory object
oleksandr-pavlyk Oct 26, 2020
f3d1899
Implements #120
oleksandr-pavlyk Oct 26, 2020
f977746
corrected __dppl_take -> __dppl_keep in Memcpy
oleksandr-pavlyk Oct 26, 2020
5479b54
spacing fixed
oleksandr-pavlyk Oct 26, 2020
b7ab952
Added DPPLQueueMgr_GetQueueFromContextAndDevice to backend
oleksandr-pavlyk Oct 26, 2020
e9a9633
Exposed SyclQueue staticmethod to construct from context & dev.
oleksandr-pavlyk Oct 26, 2020
51fc1e6
Get buffer function now raises exception trying to access device pointer
oleksandr-pavlyk Oct 26, 2020
a4b5b1f
Fixed black
oleksandr-pavlyk Oct 26, 2020
fc5a4ed
Zero copy constructors check USM kind.
oleksandr-pavlyk Oct 26, 2020
153631f
cleaned trailing whitespace
oleksandr-pavlyk Oct 26, 2020
8367c6b
removed black trailing zero
oleksandr-pavlyk Oct 26, 2020
a95d2f2
tabs to spaces to address formatting concerns in PR
oleksandr-pavlyk Oct 26, 2020
59b078b
added doxygen @params
oleksandr-pavlyk Oct 26, 2020
7007eb9
untabified tabs to spaces
oleksandr-pavlyk Oct 26, 2020
34dfadf
fixed grammar, added note that allocation return nullptr on failure
oleksandr-pavlyk Oct 26, 2020
5bb48f2
formatting to stay within 80 chars
oleksandr-pavlyk Oct 26, 2020
3951ee6
added missing _Delete
oleksandr-pavlyk Oct 26, 2020
e73c817
deleted obsolete FIXME comment, added explanation for what the code does
oleksandr-pavlyk Oct 26, 2020
7b41388
formatting change
oleksandr-pavlyk Oct 26, 2020
efe65c4
Fixed syntax error in test_sycl_usm_interface.h
oleksandr-pavlyk Oct 26, 2020
1b3c7e4
fixed reference to filename in comment headers
oleksandr-pavlyk Oct 27, 2020
50e8503
Fixed a lapse in validation logic of `__sycl_usm_array_interface__``.
oleksandr-pavlyk Oct 27, 2020
3b06ba8
Renamed Memory class to _Memor yto reflect that it is not intended fo…
oleksandr-pavlyk Oct 27, 2020
9cd6bd8
Moved _memory.pyx, _memory.pxd to dpctl.memory submodule
oleksandr-pavlyk Oct 27, 2020
1da34fc
formatting changes per review
oleksandr-pavlyk Oct 27, 2020
8f634ec
Delete context in common_test_body
oleksandr-pavlyk Oct 27, 2020
ca6dcfb
formating: func (args) -> func(args)
oleksandr-pavlyk Oct 27, 2020
62dcdd2
added MemoryUSM* work to changelog
oleksandr-pavlyk Oct 27, 2020
692cb41
changes for CI's black to lighten up
oleksandr-pavlyk Oct 27, 2020
a7d6356
skip test_sycl_usm_array_interface for Host memory on windows
oleksandr-pavlyk Oct 28, 2020
a7ba748
with black around, forecast is overcast
oleksandr-pavlyk Oct 27, 2020
c5a31f5
Adjusted path in bld.bat
oleksandr-pavlyk Oct 28, 2020
948a67b
filled in description for @param CRef in doxygen comments for GetPoin…
oleksandr-pavlyk Oct 28, 2020
e654c1f
Work on build_for_develop.bat to bring it on par with Linux shell script
oleksandr-pavlyk Oct 28, 2020
b7fdc53
fixed type in GTEST_INCLUDE
oleksandr-pavlyk Oct 28, 2020
a05327d
further changes to build_for_devel.bat - fixed typo
oleksandr-pavlyk Oct 28, 2020
6d7375e
fixed dpcpp compilation warning on Windows
oleksandr-pavlyk Oct 28, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
### Added
- Device descriptors "max_compute_units", "max_work_item_dimensions", "max_work_item_sizes", "max_work_group_size", "max_num_sub_groups" and "aspects" for int64 atomics inside dpctl C API and inside the dpctl.SyclDevice class.
- MemoryUSM* classes moved to `dpctl.memory` module, added support for aligned allocation, added support for `prefetch` and `mem_advise` (sychronous) methods, implemented `copy_to_host`, `copy_from_host` and `copy_from_device` methods, pickling support, and zero-copy interoperability with Python objects which implement `__sycl_usm_array_inerface__` protocol.

### Removed
- The Legacy OpenCL interface.
Expand Down
11 changes: 11 additions & 0 deletions backends/include/dppl_sycl_device_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,15 @@ DPPLDevice_GetVendorName (__dppl_keep const DPPLSyclDeviceRef DRef);
DPPL_API
bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef);

/*!
* @brief Checks if two DPPLSyclDeviceRef objects point to the same
* sycl::device.
*
* @param DevRef1 First opaque pointer to the sycl device.
* @param DevRef2 Second opaque pointer to the sycl device.
* @return True if the underlying sycl::device are same, false otherwise.
*/
DPPL_API
bool DPPLDevice_AreEq (__dppl_keep const DPPLSyclDeviceRef DevRef1,
__dppl_keep const DPPLSyclDeviceRef DevRef2);
DPPL_C_EXTERN_C_END
26 changes: 26 additions & 0 deletions backends/include/dppl_sycl_queue_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,30 @@ DPPL_API
void DPPLQueue_Memcpy (__dppl_keep const DPPLSyclQueueRef QRef,
void *Dest, const void *Src, size_t Count);

/*!
* @brief C-API wrapper for sycl::queue::prefetch, the function waits on an event
* till the prefetch operation completes.
*
* @param QRef An opaque pointer to the sycl queue.
* @param Ptr An USM pointer to memory.
* @param Count A number of bytes to prefetch.
*/
DPPL_API
void DPPLQueue_Prefetch (__dppl_keep DPPLSyclQueueRef QRef,
const void *Ptr, size_t Count);

/*!
* @brief C-API wrapper for sycl::queue::mem_advise, the function waits on an event
* till the operation completes.
*
* @param QRef An opaque pointer to the sycl queue.
* @param Ptr An USM pointer to memory.
* @param Count A number of bytes to prefetch.
* @param Advice Device-defined advice for the specified allocation.
* A value of 0 reverts the advice for Ptr to the default behavior.
*/
DPPL_API
void DPPLQueue_MemAdvise (__dppl_keep DPPLSyclQueueRef QRef,
const void *Ptr, size_t Count, int Advice);

DPPL_C_EXTERN_C_END
21 changes: 21 additions & 0 deletions backends/include/dppl_sycl_queue_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,25 @@ DPPLQueueMgr_PushQueue (DPPLSyclBackendType BETy,
DPPL_API
void DPPLQueueMgr_PopQueue ();


/*!
* @brief Creates a new instance of SYCL queue from SYCL context and
* SYCL device.
*
* The instance is not placed into queue manager. The user assumes
* ownership of the queue reference and should deallocate it using
* DPPLQueue_Delete.
*
* @param CRef Sycl context reference
* @param DRef Sycl device reference
*
* @return A copy of the sycl::queue created from given context and device
* references.
*/
DPPL_API
__dppl_give DPPLSyclQueueRef
DPPLQueueMgr_GetQueueFromContextAndDevice(__dppl_keep DPPLSyclContextRef CRef,
__dppl_keep DPPLSyclDeviceRef DRef);


DPPL_C_EXTERN_C_END
86 changes: 80 additions & 6 deletions backends/include/dppl_sycl_usm_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,94 @@
DPPL_C_EXTERN_C_BEGIN

/*!
* @brief Crete USM shared memory.
* @brief Create USM shared memory.
*
* @return The pointer to USM shared memory.
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM shared memory. On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Crete USM host memory.
* @brief Create USM shared memory.
*
* @param alignment Allocation's byte alignment
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM host memory.
* @return The pointer to USM shared memory with the requested alignment.
* On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_shared (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Create USM host memory.
*
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM host memory. On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Crete USM device memory.
* @brief Create USM host memory.
*
* @param alignment Allocation's byte alignment
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM device memory.
* @return The pointer to USM host memory with the requested alignment.
* On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_host (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Create USM device memory.
*
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM device memory. On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Create USM device memory.
*
* @param alignment Allocation's byte alignment
* @param size Number of bytes to allocate
* @param QRef Sycl queue reference to use in allocation
*
* @return The pointer to USM device memory with requested alignment.
* On failure, returns nullptr.
*/
DPPL_API
__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_device (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef);

/*!
* @brief Free USM memory.
*
* @param MRef USM pointer to free
* @param QRef Sycl queue reference to use.
*
* USM pointer must have been allocated using the same context as the one
* used to construct the queue.
*/
DPPL_API
void DPPLfree_with_queue (__dppl_take DPPLSyclUSMRef MRef,
Expand All @@ -79,11 +138,26 @@ void DPPLfree_with_context (__dppl_take DPPLSyclUSMRef MRef,
/*!
* @brief Get pointer type.
*
* @param MRef USM Memory
* @param CRef Sycl context reference associated with the pointer
*
* @return "host", "device", "shared" or "unknown"
*/
DPPL_API
const char *
DPPLUSM_GetPointerType (__dppl_keep const DPPLSyclUSMRef MRef,
__dppl_keep const DPPLSyclContextRef CRef);

/*!
* @brief Get the device associated with USM pointer.
*
* @param MRef USM pointer
* @param CRef Sycl context reference associated with the pointer
*
* @return A DPPLSyclDeviceRef pointer to the sycl device.
*/
DPPL_API
DPPLSyclDeviceRef
DPPLUSM_GetPointerDevice (__dppl_keep const DPPLSyclUSMRef MRef,
__dppl_keep const DPPLSyclContextRef CRef);
DPPL_C_EXTERN_C_END
9 changes: 9 additions & 0 deletions backends/source/dppl_sycl_device_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,12 @@ bool DPPLDevice_IsHostUnifiedMemory (__dppl_keep const DPPLSyclDeviceRef DRef)
}
return false;
}

bool DPPLDevice_AreEq(__dppl_keep const DPPLSyclDeviceRef DevRef1,
__dppl_keep const DPPLSyclDeviceRef DevRef2)
{
if(!(DevRef1 && DevRef2))
// \todo handle error
return false;
return (*unwrap(DevRef1) == *unwrap(DevRef2));
}
14 changes: 7 additions & 7 deletions backends/source/dppl_sycl_platform_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ get_set_of_non_hostbackends ()
{
std::set<DPPLSyclBackendType> be_set;
for (auto p : platform::get_platforms()) {
if(p.is_host())
if(p.is_host())
continue;
auto be = p.get_backend();
switch (be)
Expand Down Expand Up @@ -155,12 +155,12 @@ void DPPLPlatform_DumpInfo ()
*/
size_t DPPLPlatform_GetNumNonHostPlatforms ()
{
auto nNonHostPlatforms = 0ul;
for (auto &p : platform::get_platforms()) {
if (p.is_host())
continue;
++nNonHostPlatforms;
}
auto nNonHostPlatforms = 0ul;
for (auto &p : platform::get_platforms()) {
if (p.is_host())
continue;
++nNonHostPlatforms;
}
return nNonHostPlatforms;
}

Expand Down
20 changes: 19 additions & 1 deletion backends/source/dppl_sycl_queue_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,28 @@ DPPLQueue_Wait (__dppl_keep DPPLSyclQueueRef QRef)
SyclQueue->wait();
}

void DPPLQueue_Memcpy (__dppl_take const DPPLSyclQueueRef QRef,
void DPPLQueue_Memcpy (__dppl_keep const DPPLSyclQueueRef QRef,
void *Dest, const void *Src, size_t Count)
{
auto Q = unwrap(QRef);
auto event = Q->memcpy(Dest, Src, Count);
event.wait();
}

void
DPPLQueue_Prefetch (__dppl_keep DPPLSyclQueueRef QRef,
const void *Ptr, size_t Count)
{
auto Q = unwrap(QRef);
auto event = Q->prefetch(Ptr, Count);
event.wait();
}

void
DPPLQueue_MemAdvise (__dppl_keep DPPLSyclQueueRef QRef,
const void *Ptr, size_t Count, int Advice)
{
auto Q = unwrap(QRef);
auto event = Q->mem_advise(Ptr, Count, static_cast<pi_mem_advice>(Advice));
event.wait();
}
18 changes: 17 additions & 1 deletion backends/source/dppl_sycl_queue_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ namespace

// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef)

/*!
* @brief A helper class to support the DPPLSyclQueuemanager.
Expand Down Expand Up @@ -99,7 +101,7 @@ class QMgrHelper
{
QVec *active_queues;
try {
auto def_device = std::move(default_selector().select_device());
auto def_device { default_selector().select_device() };
auto BE = def_device.get_platform().get_backend();
auto DevTy = def_device.get_info<info::device::device_type>();

Expand Down Expand Up @@ -534,3 +536,17 @@ void DPPLQueueMgr_PopQueue ()
{
QMgrHelper::popSyclQueue();
}

/*!
* The function constructs a new SYCL queue instance from SYCL conext and
* SYCL device.
*/
DPPLSyclQueueRef
DPPLQueueMgr_GetQueueFromContextAndDevice (__dppl_keep DPPLSyclContextRef CRef,
__dppl_keep DPPLSyclDeviceRef DRef)
{
auto dev = unwrap(DRef);
auto ctx = unwrap(CRef);

return wrap(new queue(*ctx, *dev));
}
41 changes: 41 additions & 0 deletions backends/source/dppl_sycl_usm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
//===----------------------------------------------------------------------===//

#include "dppl_sycl_usm_interface.h"
#include "dppl_sycl_device_interface.h"
#include "Support/CBindingWrapping.h"

#include <CL/sycl.hpp> /* SYCL headers */
Expand All @@ -35,6 +36,7 @@ namespace
{
// Create wrappers for C Binding types (see CBindingWrapping.h).
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(queue, DPPLSyclQueueRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(device, DPPLSyclDeviceRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(context, DPPLSyclContextRef)
DEFINE_SIMPLE_CONVERSION_FUNCTIONS(void, DPPLSyclUSMRef)

Expand All @@ -48,6 +50,15 @@ DPPLmalloc_shared (size_t size, __dppl_keep const DPPLSyclQueueRef QRef)
return wrap(Ptr);
}

__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_shared (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef)
{
auto Q = unwrap(QRef);
auto Ptr = aligned_alloc_shared(alignment, size, *Q);
return wrap(Ptr);
}

__dppl_give DPPLSyclUSMRef
DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef)
{
Expand All @@ -56,6 +67,15 @@ DPPLmalloc_host (size_t size, __dppl_keep const DPPLSyclQueueRef QRef)
return wrap(Ptr);
}

__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_host (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef)
{
auto Q = unwrap(QRef);
auto Ptr = aligned_alloc_host(alignment, size, *Q);
return wrap(Ptr);
}

__dppl_give DPPLSyclUSMRef
DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef)
{
Expand All @@ -64,6 +84,15 @@ DPPLmalloc_device (size_t size, __dppl_keep const DPPLSyclQueueRef QRef)
return wrap(Ptr);
}

__dppl_give DPPLSyclUSMRef
DPPLaligned_alloc_device (size_t alignment, size_t size,
__dppl_keep const DPPLSyclQueueRef QRef)
{
auto Q = unwrap(QRef);
auto Ptr = aligned_alloc_device(alignment, size, *Q);
return wrap(Ptr);
}

void DPPLfree_with_queue (__dppl_take DPPLSyclUSMRef MRef,
__dppl_keep const DPPLSyclQueueRef QRef)
{
Expand Down Expand Up @@ -99,3 +128,15 @@ DPPLUSM_GetPointerType (__dppl_keep const DPPLSyclUSMRef MRef,
return "unknown";
}
}

DPPLSyclDeviceRef
DPPLUSM_GetPointerDevice (__dppl_keep const DPPLSyclUSMRef MRef,
__dppl_keep const DPPLSyclContextRef CRef)
{
auto Ptr = unwrap(MRef);
auto C = unwrap(CRef);

auto Dev = get_pointer_device(Ptr, *C);

return wrap(new device(Dev));
}
Loading