Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions galsim/random.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def generate(self, array):
#assert(array_1d.strides[0] == array_1d.itemsize)
_a = array_1d.__array_interface__['data'][0]
self._rng.generate(len(array_1d), _a)
if array_1d.data != array.data:
if not np.shares_memory(array, array_1d):
# array_1d is not a view into the original array. Need to copy back.
np.copyto(array, array_1d.reshape(array.shape), casting='unsafe')

Expand All @@ -277,7 +277,7 @@ def add_generate(self, array):
#assert(array_1d.strides[0] == array_1d.itemsize)
_a = array_1d.__array_interface__['data'][0]
self._rng.add_generate(len(array_1d), _a)
if array_1d.data != array.data:
if not np.shares_memory(array, array_1d):
# array_1d is not a view into the original array. Need to copy back.
np.copyto(array, array_1d.reshape(array.shape), casting='unsafe')

Expand Down Expand Up @@ -401,7 +401,7 @@ def generate_from_variance(self, array):
#assert(array_1d.strides[0] == array_1d.itemsize)
_a = array_1d.__array_interface__['data'][0]
self._rng.generate_from_variance(len(array_1d), _a)
if array_1d.data != array.data:
if not np.shares_memory(array, array_1d):
# array_1d is not a view into the original array. Need to copy back.
np.copyto(array, array_1d.reshape(array.shape), casting='unsafe')

Expand Down Expand Up @@ -521,7 +521,7 @@ def generate_from_expectation(self, array):
#assert(array_1d.strides[0] == array_1d.itemsize)
_a = array_1d.__array_interface__['data'][0]
self._rng.generate_from_expectation(len(array_1d), _a)
if array_1d.data != array.data:
if not np.shares_memory(array, array_1d):
# array_1d is not a view into the original array. Need to copy back.
np.copyto(array, array_1d.reshape(array.shape), casting='unsafe')

Expand Down
31 changes: 31 additions & 0 deletions tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -2023,5 +2023,36 @@ def test_numpy_generator():
assert np.isclose(np.mean(a3), 17, rtol=1.e-3)
assert np.isclose(np.std(a3), 23, rtol=3.e-3)

@timer
def test_generate_in_place():
"""Check that the generate methods only copy back when they have to.

np.ascontiguousarray returns a view of a contiguous float array, but its .data buffer
object can still compare unequal to the original's, which used to cause a redundant copy.
"""
from unittest import mock

def check(deviate, method, array, copies):
expected = array.copy()
getattr(deviate.duplicate(), method)(expected)
with mock.patch("galsim.random.np.copyto", wraps=np.copyto) as copyto:
getattr(deviate, method)(array)
assert copyto.call_count == copies
np.testing.assert_array_equal(array, expected)

for shape in [(10,), (10, 20), (10, 20, 30)]:
# Contiguous float arrays are filled in place, without a copy.
check(galsim.UniformDeviate(1234), "generate", np.zeros(shape), 0)
check(galsim.UniformDeviate(1234), "add_generate", np.ones(shape), 0)
check(galsim.GaussianDeviate(1234), "generate_from_variance", np.full(shape, 2.), 0)
check(galsim.PoissonDeviate(1234), "generate_from_expectation", np.full(shape, 3.), 0)

# Arrays that ascontiguousarray has to copy still get the values copied back.
check(galsim.UniformDeviate(1234), "generate", np.zeros(shape, dtype=np.float32), 1)
check(galsim.UniformDeviate(1234), "add_generate", np.ones(shape, order="F")[..., ::2], 1)
Comment thread
rmjarvis marked this conversation as resolved.
Resolved
check(galsim.GaussianDeviate(1234), "generate_from_variance", np.full(shape, 2.)[..., ::-1], 1)
check(galsim.PoissonDeviate(1234), "generate_from_expectation", np.full(shape, 3.)[5:9:2,...], 1)


if __name__ == "__main__":
runtests(__file__)
Loading