Skip to content

Implement __cuda_array_interface__#1144

Merged
okuta merged 3 commits intocupy:masterfrom
seibert:array_interface
Aug 14, 2018
Merged

Implement __cuda_array_interface__#1144
okuta merged 3 commits intocupy:masterfrom
seibert:array_interface

Conversation

@seibert
Copy link
Copy Markdown
Contributor

@seibert 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
Copy link
Copy Markdown
Contributor Author

seibert commented May 15, 2018

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

@Crissman
Copy link
Copy Markdown
Member

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

Thanks!

@seibert
Copy link
Copy Markdown
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
Copy link
Copy Markdown
Contributor Author

seibert commented May 29, 2018

@Crissman: Thoughts on my questions above?

@Crissman
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
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 New features/APIs label Jul 25, 2018
@okuta okuta self-assigned this Jul 25, 2018
@okuta
Copy link
Copy Markdown
Member

okuta commented Jul 25, 2018

I will review this.

Comment thread cupy/core/core.pxd Outdated
readonly object dtype
readonly memory.MemoryPointer data
readonly ndarray base
readonly object _cuda_array_descr
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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__.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added.

@okuta
Copy link
Copy Markdown
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
Copy link
Copy Markdown
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

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

Choose a reason for hiding this comment

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

Please add a white space.

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

@okuta
Copy link
Copy Markdown
Member

okuta commented Aug 5, 2018

jenkins, test this please.

@chainer-ci
Copy link
Copy Markdown
Member

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

@okuta
Copy link
Copy Markdown
Member

okuta commented Aug 5, 2018

LGTM except a comment.

@okuta okuta added the st:awaiting-author Awaiting response from author label Aug 5, 2018
@seibert
Copy link
Copy Markdown
Contributor Author

seibert commented Aug 10, 2018

whitespace change has been made

@okuta
Copy link
Copy Markdown
Member

okuta commented Aug 14, 2018

jenkins, test this please.

@chainer-ci
Copy link
Copy Markdown
Member

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
Copy link
Copy Markdown
Member

okuta commented Aug 14, 2018

jenkins, test this please.

@chainer-ci
Copy link
Copy Markdown
Member

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

@okuta
Copy link
Copy Markdown
Member

okuta commented Aug 14, 2018

LGTM!

@okuta okuta merged commit cda197b into cupy:master Aug 14, 2018
@kmaehashi kmaehashi changed the title __cuda_array_interface__ implementation Implement __cuda_array_interface__ Aug 22, 2018
@asi1024 asi1024 removed the st:awaiting-author Awaiting response from author label Jan 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:feature New features/APIs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants