Skip to content
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

Implement `__cuda_array_interface__` #1144

Merged
merged 3 commits into from Aug 14, 2018
Merged

Implement `__cuda_array_interface__` #1144

merged 3 commits into from Aug 14, 2018

Conversation

@seibert
Copy link
Contributor

seibert commented Apr 13, 2018

This implements a special CUDA array interface attribute, analogous to the __array_interface__ attribute on NumPy arrays. This attribute is specifically for advertising CUDA device-compatible pointers (actual device allocations, or managed memory) and associated metadata.

This allows CuPy arrays to be used with Numba-compiled CUDA functions when combined with numba/numba#2860.

@seibert seibert force-pushed the seibert:array_interface branch from f3b8234 to 37ecde8 May 15, 2018
@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented May 15, 2018

(Rebasing this PR on master to see if that fixes the flake8 failures in unrelated code.)

@Crissman

This comment has been minimized.

Copy link
Member

Crissman commented May 22, 2018

@seibert Thanks for the PR! If this is ready for review, please remove the [WIP] label.

Thanks!

@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented May 22, 2018

Oh right! I forgot to change the title. Two questions I do have, now that I reread it:

  • This PR assumes that there is no such thing as a read-only CuPy array. Is that true?
  • I'm not sure where I should add a unit test for this attribute. Can you point me in the right direction?

BTW, the dev docs for Numba describe the meaning of the attributes:

http://numba.pydata.org/numba-doc/dev/cuda/cuda_array_interface.html

@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented May 29, 2018

@Crissman: Thoughts on my questions above?

@Crissman

This comment has been minimized.

Copy link
Member

Crissman commented May 31, 2018

There is no such thing as an immutable CuPy array at this time. Can't promise that won't change in the future, but we'd review the code if we added it.

Please add the unit test here: tests/cupy_tests/core_tests/test_ndarray.py

When you're ready for us to review, please remove the [WIP].

Thanks!

@seibert seibert force-pushed the seibert:array_interface branch from a7c65e0 to 20a1c8e Jul 23, 2018
@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented Jul 23, 2018

Sorry for the delay here. I've rewritten the patch and added tests. This is ready for review.

@seibert seibert changed the title [WIP] __cuda_array_interface__ implementation __cuda_array_interface__ implementation Jul 23, 2018
@okuta okuta added the cat:feature label Jul 25, 2018
@okuta okuta self-assigned this Jul 25, 2018
@okuta

This comment has been minimized.

Copy link
Member

okuta commented Jul 25, 2018

I will review this.

@@ -14,6 +14,7 @@ cdef class ndarray:
readonly object dtype
readonly memory.MemoryPointer data
readonly ndarray base
readonly object _cuda_array_descr

This comment has been minimized.

Copy link
@okuta

okuta Jul 27, 2018

Member

Does this feature need cache? Creating dict is low cost. And, dict is a mutable object. Perhaps, user edit this value though return value of __cuda_array_interface__.

This comment has been minimized.

Copy link
@seibert

seibert Aug 2, 2018

Author Contributor

It saves to 0.3 - 0.5 microseconds per array per kernel launch, which is probably not important unless you are passing many arrays to a function. I've reverted back to the safer, uncached approach.

set(['shape', 'typestr', 'data', 'version', 'descr']))
self.assertEqual(iface['shape'], (2,3))
self.assertEqual(iface['typestr'], '<f8')
self.assertIsInstance(iface['data'], tuple)

This comment has been minimized.

Copy link
@okuta

okuta Jul 27, 2018

Member

Please add self.assertEqual(iface['data'], arr.data.ptr`)

This comment has been minimized.

Copy link
@seibert

seibert Aug 2, 2018

Author Contributor

Added.

@okuta

This comment has been minimized.

Copy link
Member

okuta commented Jul 27, 2018

And please fix style.

--- original/./tests/cupy_tests/core_tests/test_ndarray.py
+++ fixed/./tests/cupy_tests/core_tests/test_ndarray.py
@@ -135,11 +135,11 @@
 class TestNdarrayCudaInterface(unittest.TestCase):
 
     def test_cuda_array_interface(self):
-        arr = cupy.zeros(shape=(2,3), dtype=cupy.float64)
+        arr = cupy.zeros(shape=(2, 3), dtype=cupy.float64)
         iface = arr.__cuda_array_interface__
         self.assertEqual(set(iface.keys()),
                          set(['shape', 'typestr', 'data', 'version', 'descr']))
-        self.assertEqual(iface['shape'], (2,3))
+        self.assertEqual(iface['shape'], (2, 3))
         self.assertEqual(iface['typestr'], '<f8')
         self.assertIsInstance(iface['data'], tuple)
         self.assertEqual(len(iface['data']), 2)
@@ -148,13 +148,13 @@
         self.assertEqual(iface['descr'], [('', '<f8')])
 
     def test_cuda_array_interface_view(self):
-        arr = cupy.zeros(shape=(10,20), dtype=cupy.float64)
-        view = arr[::2,::5]
+        arr = cupy.zeros(shape=(10, 20), dtype=cupy.float64)
+        view = arr[::2, ::5]
         iface = view.__cuda_array_interface__
         self.assertEqual(set(iface.keys()),
                          set(['shape', 'typestr', 'data', 'version',
                               'strides', 'descr']))
-        self.assertEqual(iface['shape'], (5,4))
+        self.assertEqual(iface['shape'], (5, 4))
         self.assertEqual(iface['typestr'], '<f8')
         self.assertIsInstance(iface['data'], tuple)
         self.assertEqual(len(iface['data']), 2)```
@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented Aug 2, 2018

Thanks for the review! I have updated the PR with your suggestions.

@okuta okuta added this to the v5.0.0b4 milestone Aug 5, 2018
@okuta
okuta approved these changes Aug 5, 2018

def test_cuda_array_interface_view(self):
arr = cupy.zeros(shape=(10, 20), dtype=cupy.float64)
view = arr[::2,::5]

This comment has been minimized.

Copy link
@okuta

okuta Aug 5, 2018

Member

Please add a white space.

-        view = arr[::2,::5]
+        view = arr[::2, ::5]
@okuta

This comment has been minimized.

Copy link
Member

okuta commented Aug 5, 2018

jenkins, test this please.

@chainer-ci

This comment has been minimized.

Copy link
Collaborator

chainer-ci commented Aug 5, 2018

Jenkins CI test (for commit 53bd090, target branch master) succeeded!

@okuta

This comment has been minimized.

Copy link
Member

okuta commented Aug 5, 2018

LGTM except a comment.

@seibert

This comment has been minimized.

Copy link
Contributor Author

seibert commented Aug 10, 2018

whitespace change has been made

@okuta
okuta approved these changes Aug 14, 2018
@okuta

This comment has been minimized.

Copy link
Member

okuta commented Aug 14, 2018

jenkins, test this please.

@chainer-ci

This comment has been minimized.

Copy link
Collaborator

chainer-ci commented Aug 14, 2018

Jenkins CI test (for commit 7aebd05, target branch master) failed with status FAILURE.
(For contributors, please wait until the reviewer confirms the details of the error.)

@okuta

This comment has been minimized.

Copy link
Member

okuta commented Aug 14, 2018

jenkins, test this please.

@chainer-ci

This comment has been minimized.

Copy link
Collaborator

chainer-ci commented Aug 14, 2018

Jenkins CI test (for commit 7aebd05, target branch master) succeeded!

@okuta

This comment has been minimized.

Copy link
Member

okuta commented Aug 14, 2018

LGTM!

@okuta okuta merged commit cda197b into cupy:master Aug 14, 2018
3 of 4 checks passed
3 of 4 checks passed
coverage/coveralls Coverage decreased (-10.9%) to 81.794%
Details
codecov/patch Coverage not affected when comparing a8c2f35...7aebd05
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@kmaehashi kmaehashi changed the title __cuda_array_interface__ implementation Implement `__cuda_array_interface__` Aug 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.