From e47f6df9c01b16facc335c3731c3410ddaff7a0f Mon Sep 17 00:00:00 2001 From: jjohnnystwsss <33243683+jjohnnystwsss@users.noreply.github.com> Date: Sat, 21 Nov 2026 19:57:28 +0800 Subject: [PATCH 1/2] Use np.shares_memory to skip the copy back in BaseDeviate generate methods np.ascontiguousarray returns a view of a contiguous float array, but the .data buffer objects of the view and the original compare unequal, so the values were copied back into the array they were already in. Co-Authored-By: Claude Opus 5.5 --- galsim/random.py | 8 ++++---- tests/test_random.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/galsim/random.py b/galsim/random.py index 60f23d7270..8a048411d2 100644 --- a/galsim/random.py +++ b/galsim/random.py @@ -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') @@ -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') @@ -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') @@ -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') diff --git a/tests/test_random.py b/tests/test_random.py index 931c74dabd..801015b1de 100644 --- a/tests/test_random.py +++ b/tests/test_random.py @@ -2023,5 +2023,34 @@ 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) + + if __name__ == "__main__": runtests(__file__) From a554e50834bf466a65a8b5e8a3173615628889b4 Mon Sep 17 00:00:00 2001 From: Mike Jarvis Date: Sun, 22 Nov 2026 09:38:42 -0500 Subject: [PATCH 2/2] Update tests/test_random.py --- tests/test_random.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_random.py b/tests/test_random.py index 801015b1de..40a04ae112 100644 --- a/tests/test_random.py +++ b/tests/test_random.py @@ -2050,6 +2050,8 @@ def check(deviate, method, array, copies): # 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) + 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__":