我在 x32 win xp 上使用 python x32
有时程序上线失败
fp = np.memmap('C:/memmap_test', dtype='float32', mode='w+', shape=(rows,cols))
错误
memmap.py
Traceback (most recent call last):
fp = np.memmap('C:/memmap_test', dtype='float32', mode='w+', shape=(rows,cols)) File "C:\Python27\lib\site-packages\numpy\core\memmap.py", line 253, in __new__
mm = mmap.mmap(fid.fileno(), bytes, access=acc, offset=start)
OverflowError: cannot fit 'long' into an index-sized integer
所以我假设数组的大小有限制,那么数组的最大大小 maxN = rows*cols 是多少?
同样的问题 1. python x32 win x64 和 2. python x64 win x64。
更新:
#create array
rows= 250000
cols= 1000
fA= np.memmap('A.npy', dtype='float32', mode='w+', shape=(rows,cols))
# fA1= np.memmap('A1.npy', dtype='float32', mode='w+', shape=(rows,cols)) # can't create another one big memmap
print fA.nbytes/1024/1024 # 953 mb
所以看来不仅还有另一个限制<2Gb for single memmaped array.
还有 @Paul 提供的测试输出
working with 30000000 elements
number bytes required 0.240000 GB
works
working with 300000000 elements
number bytes required 2.400000 GB
OverflowError("cannot fit 'long' into an index-sized integer",)
working with 3000000000 elements
number bytes required 24.000000 GB
IOError(28, 'No space left on device')
working with 30000000000 elements
number bytes required 240.000000 GB
IOError(28, 'No space left on device')
working with 300000000000 elements
number bytes required 2400.000000 GB
IOError(28, 'No space left on device')
working with 3000000000000 elements
number bytes required 24000.000000 GB
IOError(22, 'Invalid argument')
这里有一些关于这个主题的讨论:内存映射文件可以有多大?和为什么Python的mmap不能处理大文件?
对于以下测试,我使用以下代码:
baseNumber = 3000000L
for powers in arange(1,7):
l1 = baseNumber*10**powers
print('working with %d elements'%(l1))
print('number bytes required %f GB'%(l1*8/1e9))
try:
fp = numpy.memmap('test.map',dtype='float64', mode='w+',shape=(1,l1))
#works
print('works')
del fp
except Exception as e:
print(repr(e))
Windows x32 上的 python x32 对于 32 位 Windows,文件大小的限制约为 2-3GB。 因此,由于操作系统限制,Windows 无法创建任何大于此文件大小的文件。 我无法访问 x32 位机器,但在达到文件大小限制后命令将失败
Windows x64 上的 python x32
在这种情况下,由于 python 是 32 位,我们无法达到 win64 上允许的文件大小。
%run -i scratch.py
python x32 win x64
working with 30000000 elements
number bytes required 0.240000 GB
works
working with 300000000 elements
number bytes required 2.400000 GB
OverflowError("cannot fit 'long' into an index-sized integer",)
working with 3000000000 elements
number bytes required 24.000000 GB
OverflowError("cannot fit 'long' into an index-sized integer",)
working with 30000000000 elements
number bytes required 240.000000 GB
IOError(28, 'No space left on device')
working with 300000000000 elements
number bytes required 2400.000000 GB
IOError(28, 'No space left on device')
working with 3000000000000 elements
number bytes required 24000.000000 GB
IOError(22, 'Invalid argument')
Windows x64 上的 python x64
在这种情况下,我们最初受到磁盘大小的限制,但是一旦我们的数组/字节大小足够大,就会出现一些溢出
%run -i scratch.py
working with 30000000 elements
number bytes required 0.240000 GB
works
working with 300000000 elements
number bytes required 2.400000 GB
works
working with 3000000000 elements
number bytes required 24.000000 GB
works
working with 30000000000 elements
number bytes required 240.000000 GB
IOError(28, 'No space left on device')
working with 300000000000 elements
number bytes required 2400.000000 GB
IOError(28, 'No space left on device')
working with 3000000000000 elements
number bytes required 24000.000000 GB
IOError(22, 'Invalid argument')
总结一下: 对于 Windows x64,阵列将失败的精确点将取决于最初的磁盘大小
pythonx32 Windows x64 最初我们遇到了您所看到的类型错误,然后是磁盘大小限制,但在某些时候会出现无效参数错误
pythonx64 窗口 x64
最初我们有磁盘大小限制,但在某些时候会出现其他错误。
有趣的是,这些错误似乎与 2^64 大小问题无关,如 3000000000000*8 < 2^64 in the same way that these errors manifested themselves on win32.
如果磁盘足够大,那么我们可能不会看到无效参数错误,并且我们可以达到 2^64 限制,尽管我没有足够大的磁盘来测试这个:)