0

There has been an old question: Is there a way to check if NumPy arrays share the same data?

However, all answers cannot detect memory sharing via mmap.

Code snippet:

import numpy as np
x = np.zeros(2)
np.save('/dev/shm/x', x)
y = np.load('/dev/shm/x.npy', mmap_mode = 'r+')
z = np.load('/dev/shm/x.npy', mmap_mode = 'r+')
assert y.base is not z.base
assert y.base != z.base
assert y.__array_interface__['data'][0] != z.__array_interface__['data'][0]
assert not np.may_share_memory(y, z)
assert not np.shares_memory(y, z)
y[0] = 1
assert y[0] == z[0] == 1 # actually share memory

YouJiacheng
  • 449
  • 3
  • 11

1 Answers1

1
def is_both_arrays_map_same_file(y,z):
    if hasattr(y,'filename') and hasattr(z,'filename'):
        return y.filename == z.filename
    else:
        return False

just a disclaimer, this is not a standard method, so don't be surprised if it doesn't work with some version of numpy in the future, sadly there is no standard method, and only the filename is saved.

Ahmed AEK
  • 8,584
  • 2
  • 7
  • 23
  • This method has two drawback: First, it can be false positive, if I remove the file and create a new one with the same name. – YouJiacheng Sep 23 '22 at 07:17
  • Second, it can be false negative, if I use `np.asarray` get a `np.ndarray` from `np.memmap`, but still share memory with `np.memmap`. – YouJiacheng Sep 23 '22 at 07:23
  • unfortunately, they don't even map to the same address in memory (ie: each array buffer is in a different place in memory which you can check with ctypes, even the OS doesn't know they map the same file), so there is no workaround here, you'd probably have to suggest a modification to numpy at their github – Ahmed AEK Sep 23 '22 at 08:22