coredevice: use device endian for kernel and RPC

This commit is contained in:
pca006132 2021-01-21 10:30:55 +08:00 committed by David Nadlinger
parent 3832b261b1
commit ec72eeda46
2 changed files with 30 additions and 21 deletions

View File

@ -68,13 +68,13 @@ def _receive_list(kernel, embedding_map):
return list(buffer) return list(buffer)
elif tag == "i": elif tag == "i":
buffer = kernel._read(4 * length) buffer = kernel._read(4 * length)
return list(struct.unpack(">%sl" % length, buffer)) return list(struct.unpack(kernel.endian + "%sl" % length, buffer))
elif tag == "I": elif tag == "I":
buffer = kernel._read(8 * length) buffer = kernel._read(8 * length)
return list(struct.unpack(">%sq" % length, buffer)) return list(struct.unpack(kernel.endian + "%sq" % length, buffer))
elif tag == "f": elif tag == "f":
buffer = kernel._read(8 * length) buffer = kernel._read(8 * length)
return list(struct.unpack(">%sd" % length, buffer)) return list(struct.unpack(kernel.endian + "%sd" % length, buffer))
else: else:
fn = receivers[tag] fn = receivers[tag]
elems = [] elems = []
@ -98,13 +98,13 @@ def _receive_array(kernel, embedding_map):
elems = numpy.ndarray((length, ), 'B', buffer) elems = numpy.ndarray((length, ), 'B', buffer)
elif tag == "i": elif tag == "i":
buffer = kernel._read(4 * length) buffer = kernel._read(4 * length)
elems = numpy.ndarray((length, ), '>i4', buffer) elems = numpy.ndarray((length, ), kernel.endian + 'i4', buffer)
elif tag == "I": elif tag == "I":
buffer = kernel._read(8 * length) buffer = kernel._read(8 * length)
elems = numpy.ndarray((length, ), '>i8', buffer) elems = numpy.ndarray((length, ), kernel.endian + 'i8', buffer)
elif tag == "f": elif tag == "f":
buffer = kernel._read(8 * length) buffer = kernel._read(8 * length)
elems = numpy.ndarray((length, ), '>d', buffer) elems = numpy.ndarray((length, ), kernel.endian + 'd', buffer)
else: else:
fn = receivers[tag] fn = receivers[tag]
elems = [] elems = []
@ -173,21 +173,22 @@ class CommKernelDummy:
class CommKernel: class CommKernel:
warned_of_mismatch = False warned_of_mismatch = False
def __init__(self, host, port=1381): def __init__(self, host, endian='>', port=1381):
self.endian = endian
self._read_type = None self._read_type = None
self.host = host self.host = host
self.port = port self.port = port
self.read_buffer = bytearray() self.read_buffer = bytearray()
self.write_buffer = bytearray() self.write_buffer = bytearray()
self.unpack_int32 = struct.Struct(">l").unpack self.unpack_int32 = struct.Struct(endian + "l").unpack
self.unpack_int64 = struct.Struct(">q").unpack self.unpack_int64 = struct.Struct(endian + "q").unpack
self.unpack_float64 = struct.Struct(">d").unpack self.unpack_float64 = struct.Struct(endian + "d").unpack
self.pack_header = struct.Struct(">lB").pack self.pack_header = struct.Struct(endian + "lB").pack
self.pack_int32 = struct.Struct(">l").pack self.pack_int32 = struct.Struct(endian + "l").pack
self.pack_int64 = struct.Struct(">q").pack self.pack_int64 = struct.Struct(endian + "q").pack
self.pack_float64 = struct.Struct(">d").pack self.pack_float64 = struct.Struct(endian + "d").pack
def open(self): def open(self):
if hasattr(self, "socket"): if hasattr(self, "socket"):
@ -466,11 +467,14 @@ class CommKernel:
if tag_element == "b": if tag_element == "b":
self._write(bytes(value)) self._write(bytes(value))
elif tag_element == "i": elif tag_element == "i":
self._write(struct.pack(">%sl" % len(value), *value)) self._write(struct.pack(self.endian + "%sl" %
len(value), *value))
elif tag_element == "I": elif tag_element == "I":
self._write(struct.pack(">%sq" % len(value), *value)) self._write(struct.pack(self.endian + "%sq" %
len(value), *value))
elif tag_element == "f": elif tag_element == "f":
self._write(struct.pack(">%sd" % len(value), *value)) self._write(struct.pack(self.endian + "%sd" %
len(value), *value))
else: else:
for elt in value: for elt in value:
tags_copy = bytearray(tags) tags_copy = bytearray(tags)
@ -488,13 +492,16 @@ class CommKernel:
if tag_element == "b": if tag_element == "b":
self._write(value.reshape((-1,), order="C").tobytes()) self._write(value.reshape((-1,), order="C").tobytes())
elif tag_element == "i": elif tag_element == "i":
array = value.reshape((-1,), order="C").astype('>i4') array = value.reshape(
(-1,), order="C").astype(self.endian + 'i4')
self._write(array.tobytes()) self._write(array.tobytes())
elif tag_element == "I": elif tag_element == "I":
array = value.reshape((-1,), order="C").astype('>i8') array = value.reshape(
(-1,), order="C").astype(self.endian + 'i8')
self._write(array.tobytes()) self._write(array.tobytes())
elif tag_element == "f": elif tag_element == "f":
array = value.reshape((-1,), order="C").astype('>d') array = value.reshape(
(-1,), order="C").astype(self.endian + 'd')
self._write(array.tobytes()) self._write(array.tobytes())
else: else:
for elt in value.reshape((-1,), order="C"): for elt in value.reshape((-1,), order="C"):

View File

@ -76,15 +76,17 @@ class Core:
self.ref_multiplier = ref_multiplier self.ref_multiplier = ref_multiplier
if target == "or1k": if target == "or1k":
self.target_cls = OR1KTarget self.target_cls = OR1KTarget
endian = ">"
elif target == "cortexa9": elif target == "cortexa9":
self.target_cls = CortexA9Target self.target_cls = CortexA9Target
endian = "<"
else: else:
raise ValueError("Unsupported target") raise ValueError("Unsupported target")
self.coarse_ref_period = ref_period*ref_multiplier self.coarse_ref_period = ref_period*ref_multiplier
if host is None: if host is None:
self.comm = CommKernelDummy() self.comm = CommKernelDummy()
else: else:
self.comm = CommKernel(host) self.comm = CommKernel(host, endian)
self.first_run = True self.first_run = True
self.dmgr = dmgr self.dmgr = dmgr