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

Feature/memory zero copy #152

merged 50 commits into from
Oct 28, 2020

Conversation

oleksandr-pavlyk
Copy link
Contributor

@oleksandr-pavlyk oleksandr-pavlyk commented Oct 21, 2020

Closes #43, closes #76, closes #45, closes #120.

Work on Memory class and its derivatives.

Classes MemoryUSMShared, MemoryUSMDevice, and MemoryUSMHost constructors allow to specify the number of bytes to allocate, and optionally the queue, or give a Python object expected to implement __sycl_usm_array_interface__ attribute to create the USM memory object pointing the same USM memory with zero copy.

Classes also implement several new features.

  • Method copy_from_host(pyobj) to copy content of Python object buffer to USM memory
  • Method copy_to_hist(pyobj=None) to copy content of USM memory to pre-allocated Python object buffer. If object does not implement the buffer protocol, bytearray of the appropriate size is allocated and populated. The method returns the populated object.
  • Method copy_from_device(sycl_usm_obj) to copy content of USM memory behind Python object that is expected to implement __sycl_usm_array_interface__ to the USM memory of the instance.

Classes are now pickleable, with it being implemented via copying to host and picking that.

@Alexander-Makaryev

@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch 2 times, most recently from 9eaf8a5 to d6e6483 Compare October 21, 2020 21:47
@diptorupd diptorupd mentioned this pull request Oct 22, 2020
4 tasks
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch from c261e9a to bd9f1f1 Compare October 22, 2020 14:42
@oleksandr-pavlyk
Copy link
Contributor Author

By the way, reconstruction of queue from a pointer and context could proceed as follows:

   if isinstance(syclobj, SyclContext):
        ctx = <SyclContext> syclobj
        dev = DPPLget_pointer_device(data_ptr, ctx)
        new_queue = DPPLqueue_from_device_and_conext(dev, ctx)

@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch 2 times, most recently from 75690ba to bc861ab Compare October 22, 2020 21:02
@oleksandr-pavlyk
Copy link
Contributor Author

Implement support for aligned memory allocation, exposing DPPL_aligned_alloc_shared and friends.

Changed constructor of MemoryUSMShared to require and new keyword alignment and old one queue be keyword arguments only.

Non-positive alignment values are ignored and malloc_* is silently used instead.

@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch 2 times, most recently from 2c07fab to 0a8da06 Compare October 23, 2020 18:37
1. Memory class exposes the interface
2. Memory variants constructors can consum objects exposing
   the said interface, and take over memory zero copy.
3. Class implements

      memInst.copy_to_host(pyobj=None)
         If `pyobj` supports Python's buffer protocol,
	 content of USM memory in the instance is copied
	 to the host buffer.

	 Otherwise, bytearray is allocated, populated
	 and returned

      memInst.copy_from_host(pyobj)
         Copies buffer of `pyobj` into USM memory of the
	 instance.

	 Raises exception if pyobj is not a byte array

      memInst.copy_from_device(sycl_usm_obj)
         Copies USM memory of sycl_usm_obj exposing
	 __sycl_usm_array_interface__ into USM memory
	 of the instance

4. Class is pickleable
5. Class implements tobytes method that produces bytes object
   populated by the content of USM memory.

Methods are currently not releasing GIL, but I think they should.
modularized test, + changes per black
Previously it would always produced shared memory on unpickling.
queue can no longer be specified via positional argument,
only through a keyword to allow a user to specify alignment
but not queue.

SYCL spec says that aligned allocation may return null pointer
when the requested alignment is not supported by the device.

Non-positive alignments silently go unused (i.e. DPPLmalloc_* is
instead of DPPL_aligned_alloc_*)
Added DPPLDevice_AreEq to check if two devices are pointer equal.
Used in the test.
@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch from 0a8da06 to cebc064 Compare October 23, 2020 18:57
1. In that case we can avoid making change of reference objects
   very long.

```
In [1]: import dpctl, dpctl.memory as dpmem

In [2]: m = dpmem.MemoryUSMShared(256)

In [3]: m2 = dpmem.MemoryUSMShared(m)

In [4]: m3 = dpmem.MemoryUSMShared(m2)

In [5]: m3.reference_obj is m
Out[5]: True

In [6]: m2.reference_obj is m
Out[6]: True

In [7]: m2._pointer
Out[7]: 94798596370432

In [8]: m3._pointer
Out[8]: 94798596370432

In [9]: m._pointer
Out[9]: 94798596370432
```
1. Removed dpctl.memory
2. Exposed MemoryUSMShared, MemoryUSMDevice, MemoryUSMHost to dpctl
3. When dpctl is cimported MemoryUSMShared, MemoryUSMHost, MemoryUSMDevice
   and Memory classes are exposed.
@oleksandr-pavlyk
Copy link
Contributor Author

@diptorupd The refactoring is complete. Lapse in logic on L109 of _memory.pyx fixed, Memory help class renamed _Memory, and dpctl._memory moved to dpctl.memory._memory with user-facing dpctl.memory module introduced.

+ formatting changes, + EXPECT_NO_FATAL_FAILURE on prefetch call.
Copy link
Contributor

@diptorupd diptorupd left a comment

Choose a reason for hiding this comment

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

Please take care of the minor formatting stuff and then merge.

@oleksandr-pavlyk
Copy link
Contributor Author

Windows build failure in CI is not related to changes in this PR at all - test environment fails to activate.

I was able to build the package on Windows manually, and test suite run was clean.

@oleksandr-pavlyk oleksandr-pavlyk force-pushed the feature/memory-zero-copy branch from 0a66066 to 948a67b Compare October 28, 2020 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants