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

Improve performance of host to device memory copy #1367

Merged
merged 5 commits into from Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 12 additions & 9 deletions cupy/core/core.pyx
Expand Up @@ -3,13 +3,13 @@
from __future__ import division
import sys

import ctypes
import numpy
import six

import cupy
from cupy.core import flags
from cupy.cuda import device
from cupy.cuda import stream

try:
from cupy.cuda import thrust
Expand Down Expand Up @@ -2120,6 +2120,7 @@ cpdef ndarray array(obj, dtype=None, bint copy=True, str order='K',
# TODO(beam2d): Support subok options
cdef Py_ssize_t nvidem
cdef ndarray a, src
cdef size_t nbytes
if subok:
raise NotImplementedError
if isinstance(obj, ndarray):
Expand All @@ -2139,24 +2140,26 @@ cpdef ndarray array(obj, dtype=None, bint copy=True, str order='K',
a = a.view()
a.shape = (1,) * (ndmin - ndim) + a.shape
else:
if order == 'K':
order = 'A'
if order is not None and len(order) == 1 and order in 'KAka':
Copy link
Member

Choose a reason for hiding this comment

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

Order needs to be normalized to accept longer names (ANY, KEEP).

if isinstance(obj, numpy.ndarray) and obj.flags.f_contiguous:
order = 'F'
else:
order = 'C'
a_cpu = numpy.array(obj, dtype=dtype, copy=False, order=order,
ndmin=ndmin)
order = 'C' if a_cpu.flags.c_contiguous else 'F'
a_dtype = a_cpu.dtype
if a_dtype.char not in '?bhilqBHILQefdFD':
raise ValueError('Unsupported dtype %s' % a_dtype)
a = ndarray(a_cpu.shape, dtype=a_dtype, order=order)
if a_cpu.ndim == 0:
a.fill(a_cpu[()])
return a
mem = pinned_memory.alloc_pinned_memory(a.nbytes)
src_cpu = numpy.frombuffer(mem, a_cpu.dtype,
a_cpu.size).reshape(a_cpu.shape)
src_cpu[...] = a_cpu
nbytes = a.nbytes
mem = pinned_memory.alloc_pinned_memory(nbytes)
src_cpu = numpy.frombuffer(mem, a_dtype, a_cpu.size)
src_cpu[:] = a_cpu.ravel(order)
stream = stream_module.get_current_stream()
a.set(src_cpu, stream)
a.data.copy_from_host_async(ctypes.c_void_p(mem.ptr), nbytes)
pinned_memory._add_to_watch_list(stream.record(), mem)
return a

Expand Down
4 changes: 2 additions & 2 deletions cupy/logic/type_test.py
Expand Up @@ -86,7 +86,7 @@ def isfortran(a):
>>> cupy.isfortran(a)
False

>>> b = cupy.array([[1, 2, 3], [4, 5, 6]], order='FORTRAN')
>>> b = cupy.array([[1, 2, 3], [4, 5, 6]], order='F')
>>> b
array([[1, 2, 3],
[4, 5, 6]])
Expand All @@ -111,7 +111,7 @@ def isfortran(a):

C-ordered arrays evaluate as False even if they are also FORTRAN-ordered.

>>> cupy.isfortran(np.array([1, 2], order='FORTRAN'))
>>> cupy.isfortran(np.array([1, 2], order='F'))
False

"""
Expand Down
9 changes: 9 additions & 0 deletions tests/cupy_tests/creation_tests/test_from_data.py
Expand Up @@ -22,6 +22,15 @@ def test_array_from_numpy(self, xp, dtype, order):
a = testing.shaped_arange((2, 3, 4), numpy, dtype)
return xp.array(a, order=order)

@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.with_requires('numpy>=1.10')
@testing.numpy_cupy_array_equal()
def test_array_from_numpy_broad_cast(self, xp, dtype, order):
a = testing.shaped_arange((2, 1, 4), numpy, dtype)
a = numpy.broadcast_to(a, (2, 3, 4))
return xp.array(a, order=order)

@testing.for_orders('CFAK')
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
Expand Down