Skip to content

Commit

Permalink
Merge 79538c2 into fa373e6
Browse files Browse the repository at this point in the history
  • Loading branch information
okuta committed Jul 9, 2017
2 parents fa373e6 + 79538c2 commit b0df112
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
6 changes: 5 additions & 1 deletion cupy/manipulation/basic.py
Expand Up @@ -47,7 +47,11 @@ def copyto(dst, src, casting='same_kind', where=None):
if _can_memcpy(dst, src):
dst.data.copy_from(src.data, src.nbytes)
else:
core.elementwise_copy(src, dst)
device = dst.device
with device:
if src.device != device:
src = src.copy()
core.elementwise_copy(src, dst)
else:
core.elementwise_copy_where(src, where, dst)

Expand Down
52 changes: 52 additions & 0 deletions tests/cupy_tests/manipulation_tests/test_basic.py
@@ -1,5 +1,10 @@
import itertools
import six
import unittest

import numpy

import cupy
from cupy import cuda
from cupy import testing

Expand Down Expand Up @@ -42,6 +47,37 @@ def test_copyto_where(self, xp, dtype):
xp.copyto(a, b, where=c)
return a

def _check_copyto_where_multigpu_raises(self, dtype, ngpus):
def get_numpy():
a = testing.shaped_arange((2, 3, 4), numpy, dtype)
b = testing.shaped_reverse_arange((2, 3, 4), numpy, dtype)
c = testing.shaped_arange((2, 3, 4), numpy, '?')
numpy.copyto(a, b, where=c)
return a

for dev1, dev2, dev3, dev4 in itertools.product(*[range(ngpus)] * 4):
if dev1 == dev2 == dev3 == dev4:
continue
if not dev1 <= dev2 <= dev3 <= dev4:
continue

with cuda.Device(dev1):
a = testing.shaped_arange((2, 3, 4), cupy, dtype)
with cuda.Device(dev2):
b = testing.shaped_reverse_arange((2, 3, 4), cupy, dtype)
with cuda.Device(dev3):
c = testing.shaped_arange((2, 3, 4), cupy, '?')
with cuda.Device(dev4):
with six.assertRaisesRegex(
self, ValueError,
'^Array device must be same as the current device'):
cupy.copyto(a, b, where=c)

@testing.multi_gpu(2)
@testing.for_all_dtypes()
def test_copyto_where_multigpu_raises(self, dtype):
self._check_copyto_where_multigpu_raises(dtype, 2)

@testing.multi_gpu(2)
@testing.for_all_dtypes()
@testing.numpy_cupy_array_equal()
Expand All @@ -53,6 +89,22 @@ def test_copyto_multigpu(self, xp, dtype):
xp.copyto(b, a)
return b

@testing.multi_gpu(2)
@testing.for_all_dtypes()
def test_copyto_multigpu_noncontinguous(self, dtype):
with cuda.Device(0):
src = testing.shaped_arange((2, 3, 4), cupy, dtype)
src = src.swapaxes(0, 1)
with cuda.Device(1):
dst = cupy.empty_like(src)
cupy.copyto(dst, src)

expected = testing.shaped_arange((2, 3, 4), numpy, dtype)
expected = expected.swapaxes(0, 1)

testing.assert_array_equal(expected, src.get())
testing.assert_array_equal(expected, dst.get())


@testing.parameterize(
*testing.product(
Expand Down

0 comments on commit b0df112

Please sign in to comment.