Determine if Array is Mmap?

Is there a way to determine if an Array is memory mapped or standard?
For example, taken from the doc:

# Create a file for mmapping
# (you could alternatively use mmap to do this step, too)
using Mmap
A = rand(1:20, 5, 30)
s = open("/tmp/mmap.bin", "w+")
# We'll write the dimensions of the array as the first two Ints in the file
write(s, size(A,1))
write(s, size(A,2))
# Now write the data
write(s, A)
close(s)

# Test by reading it back in
s = open("/tmp/mmap.bin")   # default is read-only
m = read(s, Int)
n = read(s, Int)
A2 = mmap(s, Matrix{Int}, (m,n))

then

julia> A2
5×30 Matrix{Int64}:
 12  17   4  12  16  18  13   2  12  …  13   7   1   2   7  13   4  15  11
  7   8  13  15  17  12   7  14  18     14  19  15   8  12   4  12   1   4
  5   4   7   4   7   7  18   5  18      4  18   8   3   1   9  13  16  19
 16  19  18  13  10  10  19  20   5      3  18   4  18  11  15   1   7  10
 14  18  15  15  18  17   6  19  16     19  11  15   7  15  10  10   8   9

julia> typeof(A2)
Matrix{Int64} (alias for Array{Int64, 2})

but can I by just having A2 detemine if it is memory mapped or not?

Cheers, Jesper

Didn’t found anything explicitly but you can check if Mmap.sync! throws an error:

function ismmap(a)
	r=true
	try
		Mmap.sync!(a)
	catch e
		r=false
	end
	return r
end

julia> ismmap(A)
false

julia> ismmap(A2)
true

But I am not sure if this is always valid.

1 Like

Cheers, have just tried the solution with Julia 1.10.5. The A array returns true instead of false… and the documentation looks the same. Any workaround? Thanks in advance!

1 Like

This very old thread is still accurate: we don’t store this information at a high-level and it’s hard to recover at a low level. I don’t think the Mmap.sync! error check suggested above was ever reliable.

https://groups.google.com/g/julia-users/c/_OFCVa3HAZQ/m/j0WpevcuHzEJ

2 Likes

Thanks for the interesting link! I didn’t know about this discussion. I decided to wrap it in my own struct to add that possibility, as suggested by Tim in the link.

1 Like