[MATRIX-SIG] Pickling arrays on a DEC Alpha
Konrad Hinsen
hinsen@ibs.ibs.fr
Tue, 3 Feb 1998 11:50:06 +0100
Here's a quick hack for those who want to exchange pickle files between
DEC Alphas and other machines (with 32-bit longs). If you replace the
functions LoadArray() and DumpArray() in Numeric.py, arrays of longs
will always be saved and retrieved in 32-bit format, even on Alphas.
The modification doesn't make any difference on non-Alpha machines.
---------------------------------------------------------------------------
def DumpArray(m, fp):
s = m.shape
if LittleEndian: endian = "L"
else: endian = "B"
typecode = m.typecode()
# Save 64-int arrays (DEC Alpha) as 32 bit for compatibility
if typecode == 'l' and m.itemsize() == 8:
m = m.astype('i')
fp.write("A%s%s%d " % (typecode, endian, m.itemsize()))
for d in s:
fp.write("%d "% d)
fp.write('\n')
fp.write(m.tostring())
SizeOfLong = zeros((1,), 'l').itemsize()
def LoadArray(fp):
ln = string.split(fp.readline())
if ln[0][0] == 'A': ln[0] = ln[0][1:] # Nasty hack showing my ignorance of pickle
typecode = ln[0][0]
endian = ln[0][1]
shape = map(lambda x: string.atoi(x), ln[1:])
itemsize = string.atoi(ln[0][2:])
sz = reduce(multiply, shape)*itemsize
data = fp.read(sz)
# Read 32-bit arrays as int (not long) on DEC Alphas
if typecode == 'l' and itemsize == 4 and SizeOfLong == 8:
m = fromstring(data, 'i').astype('l')
else:
m = fromstring(data, typecode)
m = reshape(m, shape)
if (LittleEndian and endian == 'B') or (not LittleEndian and endian == 'L'):
return m.byteswapped()
else:
return m
---------------------------------------------------------------------------
--
-------------------------------------------------------------------------------
Konrad Hinsen | E-Mail: hinsen@ibs.ibs.fr
Laboratoire de Dynamique Moleculaire | Tel.: +33-4.76.88.99.28
Institut de Biologie Structurale | Fax: +33-4.76.88.54.94
41, av. des Martyrs | Deutsch/Esperanto/English/
38027 Grenoble Cedex 1, France | Nederlands/Francais
-------------------------------------------------------------------------------
_______________
MATRIX-SIG - SIG on Matrix Math for Python
send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org
_______________