summaryrefslogtreecommitdiff
path: root/benchmarks/CUDA/CP/common/python
diff options
context:
space:
mode:
authorTor Aamodt <[email protected]>2010-07-15 18:09:46 -0800
committerTor Aamodt <[email protected]>2010-07-15 18:09:46 -0800
commit69f2911e04ffb1b19eef1fafb8c040af271f656e (patch)
tree231d3b6bdc3a202f7c255bfcf7bf2c36e32cee9e /benchmarks/CUDA/CP/common/python
creating branch for adding support for CUDA 3.x and Fermi
[git-p4: depot-paths = "//depot/gpgpu_sim_research/fermi/distribution/": change = 6829]
Diffstat (limited to 'benchmarks/CUDA/CP/common/python')
-rw-r--r--benchmarks/CUDA/CP/common/python/binaryfilecompare.py50
-rw-r--r--benchmarks/CUDA/CP/common/python/filecompare.py154
-rw-r--r--benchmarks/CUDA/CP/common/python/textfilecompare.py27
3 files changed, 231 insertions, 0 deletions
diff --git a/benchmarks/CUDA/CP/common/python/binaryfilecompare.py b/benchmarks/CUDA/CP/common/python/binaryfilecompare.py
new file mode 100644
index 0000000..b143b68
--- /dev/null
+++ b/benchmarks/CUDA/CP/common/python/binaryfilecompare.py
@@ -0,0 +1,50 @@
+# (c) Copyright 2007 The Board of Trustees of the University of Illinois.
+
+import struct
+
+def uint16(f):
+ """Read a 16-bit unsigned integer from f."""
+ [c, c2] = f.read(2)
+ return ord(c) + (ord(c2) << 8)
+
+def uint32(f):
+ """Read a 32-bit unsigned integer from f."""
+ [c, c2, c3, c4] = f.read(4)
+ return ord(c) + (ord(c2) << 8) + (ord(c3) << 16) + (ord(c4) << 24)
+
+def float(f):
+ """Read a floating-point number from f."""
+ s = f.read(4)
+ (n,) = struct.unpack("<f", s)
+ return n
+
+def many(reader, count):
+ """Create a reader function that reads a fixed-size sequence of
+ values from f."""
+ return lambda f: [reader(f) for n in range(count)]
+
+def many_uint16(count):
+ """Create a reader function that reads a fixed-size sequence of
+ 16-bit unsigned integers from f."""
+ def read(f):
+ s = f.read(2*count)
+ ns = struct.unpack("<%dH" % count, s)
+ return ns
+ return read
+
+def many_float(count):
+ """Create a reader function that reads a fixed-size sequence of
+ floats from f."""
+ def read(f):
+ s = f.read(4*count)
+ floats = struct.unpack("<%df" % count, s)
+ return floats
+ return read
+
+def eof(f):
+ """Read the end of file 'f'. A ValueError is raised if anything
+ other than EOF is read from the file."""
+ if f.readline() == "": return None
+ else: raise ValueError, "Expecting end of file"
+
+
diff --git a/benchmarks/CUDA/CP/common/python/filecompare.py b/benchmarks/CUDA/CP/common/python/filecompare.py
new file mode 100644
index 0000000..a9a5c57
--- /dev/null
+++ b/benchmarks/CUDA/CP/common/python/filecompare.py
@@ -0,0 +1,154 @@
+# (c) Copyright 2007 The Board of Trustees of the University of Illinois.
+
+import sys
+
+# A monad for comparing two files. Comparing proceeds until the first
+# mismatch occurs, at which point comparison stops and the error is
+# reported.
+class CompareMonad(object):
+ def checkType(x):
+ if not isinstance(x, CompareMonad):
+ raise TypeError, "Not a CompareMonad instance"
+ checkType = staticmethod(checkType)
+
+ def run(self, ref_file, out_file):
+ raise NotImplementedError
+
+# The >>= operator. Executes 'fst', then executes 'snd' with the result
+# of 'fst'.
+class Bind(CompareMonad):
+
+ def __init__(self, fst, snd):
+ CompareMonad.checkType(fst)
+ self.fst = fst
+ self.snd = snd
+
+ def run(self, ref_file, out_file):
+ (ok, value) = self.fst.run(ref_file, out_file)
+ if ok:
+ sndMonad = self.snd(value)
+ CompareMonad.checkType(sndMonad)
+ return sndMonad.run(ref_file, out_file)
+ return (False, None)
+
+# The >> operator. Executes 'fst', then executes 'snd'.
+class Then(CompareMonad):
+ def __init__(self, fst, snd):
+ CompareMonad.checkType(fst)
+ CompareMonad.checkType(snd)
+ self.fst = fst
+ self.snd = snd
+
+ def run(self, ref_file, out_file):
+ (ok, value) = self.fst.run(ref_file, out_file)
+ if ok:
+ return self.snd.run(ref_file, out_file)
+ return (False, None)
+
+# The 'return' operator. Returns a value that is accessible
+# within the monad.
+class Return(CompareMonad):
+ def __init__(self, value):
+ self.value = value
+
+ def run(self, ref_file, out_file):
+ return (True, self.value)
+
+# Run a list of monad instances and return their results as
+# a list.
+class Sequence(CompareMonad):
+ def __init__(self, ms):
+ self.ms = ms
+
+ def run(self, ref_file, out_file):
+ values = []
+ for m in self.ms:
+ CompareMonad.checkType(m)
+ (ok, value) = m.run(ref_file, out_file)
+ if not ok: return (False, None)
+ values.append(value)
+
+ return (True, values)
+
+# Run a list of monad instances, ignoring their results.
+class Sequence_(CompareMonad):
+ def __init__(self, ms):
+ self.ms = ms
+
+ def run(self, ref_file, out_file):
+ for m in self.ms:
+ CompareMonad.checkType(m)
+ (ok, value) = m.run(ref_file, out_file)
+ if not ok: return (False, None)
+
+ return (True, None)
+
+# The basic CompareMonad routine. This reads a value from both input
+# files, compares the result, and, if the comparison was successful,
+# returns the value.
+class Compare(CompareMonad):
+ """Read an item from both input files and compare it."""
+
+ def __init__(self,
+ read = file.read,
+ equal = lambda x, y: x == y,
+ message = "Output does not match the expected output"):
+ self.read = read
+ self.equal = equal
+ self.message = message
+
+ def run(self, ref_file, out_file):
+ try:
+ x = self.read(ref_file)
+ except ValueError, e:
+ sys.stderr.write("Malformed reference file!\n")
+ return (False, None)
+ except EOFError:
+ sys.stderr.write("Unexpected end of reference file!\n")
+ return (False, None)
+ try:
+ y = self.read(out_file)
+ except ValueError, e:
+ sys.stderr.write("Malformed output file;\n")
+ sys.stderr.write(str(e))
+ sys.stderr.write('\n')
+ return (False, None)
+ except EOFError:
+ sys.stderr.write("Unexpected end of output file\n")
+ return (False, None)
+
+ # Compare reference data to result data
+ if self.equal(x, y):
+ return (True, x)
+ else:
+ sys.stderr.write(self.message)
+ sys.stderr.write('\n')
+ return (False, None)
+
+def open_or_abort(filename):
+ try: f = file(filename, "r")
+ except:
+ sys.stderr.write("Cannot open file '" + filename + "'\n")
+ sys.exit(-1)
+ return f
+
+
+def default_main(comparison_routine):
+ """Default main() routine. Read file names from sys.argv
+ and compare the files."""
+
+ if len(sys.argv) != 3:
+ sys.stderr.write("Usage: compare-output <from-file> <to-file>\n")
+ sys.exit(-1)
+
+ ref = open_or_abort(sys.argv[1])
+ out = open_or_abort(sys.argv[2])
+
+ (ok, _) = comparison_routine.run(ref, out)
+ if ok:
+ sys.stdout.write("Pass\n")
+ sys.exit(0)
+ else:
+ sys.stdout.write("Mismatch\n")
+ sys.exit(-1)
+
diff --git a/benchmarks/CUDA/CP/common/python/textfilecompare.py b/benchmarks/CUDA/CP/common/python/textfilecompare.py
new file mode 100644
index 0000000..2f6ef68
--- /dev/null
+++ b/benchmarks/CUDA/CP/common/python/textfilecompare.py
@@ -0,0 +1,27 @@
+# (c) Copyright 2007 The Board of Trustees of the University of Illinois.
+
+from binaryfilecompare import eof, many
+
+# Rename the builtin 'float' variable to avoid name conflicts
+builtin_float = float
+
+def verbatim(f):
+ """Read a line of text from file 'f'."""
+ line = f.readline()
+ return line
+
+def float(f):
+ """Read a line of text from file 'f' as a single floating-point
+ number."""
+ words = f.readline().split()
+ if len(words) != 1:
+ raise ValueError, "Expecting line to contain a single number"
+ return builtin_float(words[0])
+
+def floats(f):
+ """Read a line of text from file 'f' as a list of floating-point
+ numbers."""
+ words = f.readline().split()
+ return [builtin_float(x) for x in words]
+
+