Skip to content

Commit

Permalink
Improve host to device memory copy
Browse files Browse the repository at this point in the history
  • Loading branch information
okuta committed Jun 14, 2018
1 parent faf0b0b commit 222b16d
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 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 @@ -2101,6 +2101,8 @@ 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 tuple a_shape
cdef size_t nbytes
if subok:
raise NotImplementedError
if isinstance(obj, ndarray):
Expand All @@ -2124,20 +2126,26 @@ cpdef ndarray array(obj, dtype=None, bint copy=True, str order='K',
order = 'A'
a_cpu = numpy.array(obj, dtype=dtype, copy=False, order=order,
ndmin=ndmin)
order = 'C' if a_cpu.flags.c_contiguous else 'F'
order = None 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)
a_shape = a_cpu.shape
a = ndarray(a_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)
nbytes = a.nbytes
mem = pinned_memory.alloc_pinned_memory(nbytes)
src_cpu = numpy.frombuffer(mem, a_dtype, a_cpu.size)
if order is None:
src_cpu.shape = a_shape
else:
src_cpu.shape = a_shape[::-1]
src_cpu = src_cpu.T
src_cpu[...] = a_cpu
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

0 comments on commit 222b16d

Please sign in to comment.