YouJiacheng
YouJiacheng

Reputation: 687

Is there a reliable way to detect if NumPy arrays share memory via mmap?

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

Upvotes: 0

Views: 65

Answers (1)

Ahmed AEK
Ahmed AEK

Reputation: 17775

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.

Upvotes: 1

Related Questions