GalSim-developers / GalSim-developers/GalSim
random number generators perform unnecessary copy
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 272
- Forks
- 121
- PR merge metrics
- No merged PRs in 30d
Description
Taking generate as an example:
https://github.com/GalSim-developers/GalSim/blob/ee177aa50424dbbc5cec4a71cadd6297109c2aa1/galsim/random.py#L265-L271
The result of ascontinguousarray can share memory with the input array yet the if array_1d.data != array.data: check can fail resulting in an unnecessary copy.
The issue can be illustrated in the following code:
import numpy as np
array = np.empty((10, 20, 30), dtype=float)
array_1d = np.ascontiguousarray(array.ravel(),dtype=float)
# data is not the same
assert array.data != array_1d.data
# because array_1d is a view of array
assert array.data == array_1d.base.data # note the use of base here
# arrays share memory
assert np.shares_memory(array, array_1d)
# double check
array_1d[:] = 2
assert np.all(array == 2)
Replacing if array_1d.data != array.data: with if not np.shares_memory(array, array_1d): should fix the issue.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in galsim/random.py at generate, especially the linked lines 265-271, and reproduce the NumPy example from the issue to confirm that the flattened array can share memory with the input. Verify that the completed change avoids an unnecessary copy when memory is shared and preserves the expected array contents.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- numpy, python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100