-
Notifications
You must be signed in to change notification settings - Fork 117
Open
Description
Taking generate as an example:
Lines 265 to 271 in ee177aa
| array_1d = np.ascontiguousarray(array.ravel(),dtype=float) | |
| #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: | |
| # array_1d is not a view into the original array. Need to copy back. | |
| np.copyto(array, array_1d.reshape(array.shape), casting='unsafe') |
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels