diff options
author | Stan Seibert <stan@mtrr.org> | 2013-03-13 08:47:11 -0400 |
---|---|---|
committer | tlatorre <tlatorre@uchicago.edu> | 2021-05-09 08:42:39 -0700 |
commit | 485cb10a4033f2044de82ad9ea1e1b74e6e23786 (patch) | |
tree | 1e8b63152cb6175b13caf6cb4ccd9d1a1480cd85 /test/matrix_test.py | |
parent | 14b36d2d8b8c0c1ad4d1b8a3fdf6af91b613873f (diff) | |
download | chroma-485cb10a4033f2044de82ad9ea1e1b74e6e23786.tar.gz chroma-485cb10a4033f2044de82ad9ea1e1b74e6e23786.tar.bz2 chroma-485cb10a4033f2044de82ad9ea1e1b74e6e23786.zip |
Convert test script to one big unit test. Needs further refactoring.
Diffstat (limited to 'test/matrix_test.py')
-rw-r--r-- | test/matrix_test.py | 412 |
1 files changed, 207 insertions, 205 deletions
diff --git a/test/matrix_test.py b/test/matrix_test.py index 3499945..bfc49ad 100644 --- a/test/matrix_test.py +++ b/test/matrix_test.py @@ -42,287 +42,289 @@ minusmatrix = mod.get_function('minusmatrix') size = {'block': (1,1,1), 'grid': (1,1)} -for i in range(1): - a = np.random.random_sample(size=9).astype(np.float32) - b = np.random.random_sample(size=9).astype(np.float32) - dest = np.empty(1, dtype=np.float32) - c = np.int32(np.random.random_sample()) +# FIXME: Need to refactor this into proper unit tests +def test_matrix(): + for i in range(1): + a = np.random.random_sample(size=9).astype(np.float32) + b = np.random.random_sample(size=9).astype(np.float32) + dest = np.empty(1, dtype=np.float32) + c = np.int32(np.random.random_sample()) - print 'testing det...', + print 'testing det...', - det(cuda.In(a), cuda.Out(dest), **size) + det(cuda.In(a), cuda.Out(dest), **size) - if not np.allclose(np.float32(np.linalg.det(a.reshape(3,3))), dest[0]): - print 'fail' - print np.float32(np.linalg.det(a.reshape(3,3))) - print dest[0] - else: - print 'success' + if not np.allclose(np.float32(np.linalg.det(a.reshape(3,3))), dest[0]): + print 'fail' + print np.float32(np.linalg.det(a.reshape(3,3))) + print dest[0] + else: + print 'success' - print 'testing inv...', + print 'testing inv...', - dest = np.empty(9, dtype=np.float32) + dest = np.empty(9, dtype=np.float32) - inv(cuda.In(a), cuda.Out(dest), **size) + inv(cuda.In(a), cuda.Out(dest), **size) - if not np.allclose(np.linalg.inv(a.reshape(3,3)).flatten().astype(np.float32), dest): - print 'fail' - print np.linalg.inv(a.reshape(3,3)).flatten().astype(np.float32) - print dest - else: - print 'success' + if not np.allclose(np.linalg.inv(a.reshape(3,3)).flatten().astype(np.float32), dest): + print 'fail' + print np.linalg.inv(a.reshape(3,3)).flatten().astype(np.float32) + print dest + else: + print 'success' - print 'testing matrixadd...', + print 'testing matrixadd...', - matrixadd(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) + matrixadd(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) - if not np.allclose(a+b, dest): - print 'fail' - print a+b - print dest - else: - print 'success' + if not np.allclose(a+b, dest): + print 'fail' + print a+b + print dest + else: + print 'success' - print 'testing matrixsub...', + print 'testing matrixsub...', - matrixsub(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) + matrixsub(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) - if not np.allclose(a-b, dest): - print 'fail' - print a-b - print dest - else: - print 'sucess' + if not np.allclose(a-b, dest): + print 'fail' + print a-b + print dest + else: + print 'sucess' - print 'testing matrixmul...', + print 'testing matrixmul...', - matrixmul(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) + matrixmul(cuda.In(a), cuda.In(b), cuda.Out(dest), **size) - if not np.allclose(np.dot(a.reshape(3,3),b.reshape(3,3)).flatten(), dest): - print 'fail' - print np.dot(a.reshape(3,3),b.reshape(3,3)).flatten() - print dest - else: - print 'success' + if not np.allclose(np.dot(a.reshape(3,3),b.reshape(3,3)).flatten(), dest): + print 'fail' + print np.dot(a.reshape(3,3),b.reshape(3,3)).flatten() + print dest + else: + print 'success' - print 'testing multiply...', + print 'testing multiply...', - x_cpu = np.random.random_sample(size=3).astype(np.float32) - x_gpu = np.array((x_cpu[0], x_cpu[1], x_cpu[2]), dtype=float3) + x_cpu = np.random.random_sample(size=3).astype(np.float32) + x_gpu = np.array((x_cpu[0], x_cpu[1], x_cpu[2]), dtype=float3) - dest = np.empty(1, dtype=float3) - - multiply(cuda.In(a), cuda.In(x_gpu), cuda.Out(dest), **size) + dest = np.empty(1, dtype=float3) + + multiply(cuda.In(a), cuda.In(x_gpu), cuda.Out(dest), **size) - m = a.reshape(3,3) + m = a.reshape(3,3) - if not np.allclose(np.dot(x_cpu,m[0]), dest[0]['x']) or \ - not np.allclose(np.dot(x_cpu,m[1]), dest[0]['y']) or \ - not np.allclose(np.dot(x_cpu,m[2]), dest[0]['z']): - print 'fail' - print np.dot(x_cpu,m[0]) - print np.dot(x_cpu,m[1]) - print np.dot(x_cpu,m[2]) - print dest[0]['x'] - print dest[0]['y'] - print dest[0]['z'] - else: - print 'success' + if not np.allclose(np.dot(x_cpu,m[0]), dest[0]['x']) or \ + not np.allclose(np.dot(x_cpu,m[1]), dest[0]['y']) or \ + not np.allclose(np.dot(x_cpu,m[2]), dest[0]['z']): + print 'fail' + print np.dot(x_cpu,m[0]) + print np.dot(x_cpu,m[1]) + print np.dot(x_cpu,m[2]) + print dest[0]['x'] + print dest[0]['y'] + print dest[0]['z'] + else: + print 'success' - print 'testing matrixaddfloat...', + print 'testing matrixaddfloat...', - dest = np.empty(9, dtype=np.float32) + dest = np.empty(9, dtype=np.float32) - matrixaddfloat(cuda.In(a), c, cuda.Out(dest), **size) + matrixaddfloat(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(a+c, dest): - print 'fail' - print a+c - print dest - else: - print 'success' + if not np.allclose(a+c, dest): + print 'fail' + print a+c + print dest + else: + print 'success' - print 'testing matrixsubfloat...', + print 'testing matrixsubfloat...', - matrixsubfloat(cuda.In(a), c, cuda.Out(dest), **size) + matrixsubfloat(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(a-c, dest): - print 'fail' - print a-c - print dest - else: - print 'success' + if not np.allclose(a-c, dest): + print 'fail' + print a-c + print dest + else: + print 'success' - print 'testing matrixmulfloat...', + print 'testing matrixmulfloat...', - matrixmulfloat(cuda.In(a), c, cuda.Out(dest), **size) + matrixmulfloat(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(a*c, dest): - print 'fail' - print a-c - print dest - else: - print 'success' + if not np.allclose(a*c, dest): + print 'fail' + print a-c + print dest + else: + print 'success' - print 'testing matrixdivfloat...', + print 'testing matrixdivfloat...', - matrixdivfloat(cuda.In(a), c, cuda.Out(dest), **size) + matrixdivfloat(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(a/c, dest): - print 'fail' - print a/c - print dest - else: - print 'success' + if not np.allclose(a/c, dest): + print 'fail' + print a/c + print dest + else: + print 'success' - print 'testing floataddmatrix...', + print 'testing floataddmatrix...', - floataddmatrix(cuda.In(a), c, cuda.Out(dest), **size) + floataddmatrix(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(c+a, dest): - print 'fail' - print c+a - print dest - else: - print 'success' + if not np.allclose(c+a, dest): + print 'fail' + print c+a + print dest + else: + print 'success' - print 'testing floatsubmatrix...', + print 'testing floatsubmatrix...', - floatsubmatrix(cuda.In(a), c, cuda.Out(dest), **size) + floatsubmatrix(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(c-a, dest): - print 'fail' - print c-a - print dest - else: - print 'success' + if not np.allclose(c-a, dest): + print 'fail' + print c-a + print dest + else: + print 'success' - print 'testing floatmulmatrix...', + print 'testing floatmulmatrix...', - floatmulmatrix(cuda.In(a), c, cuda.Out(dest), **size) + floatmulmatrix(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(c*a, dest): - print 'fail' - print c*a - print dest - else: - print 'success' + if not np.allclose(c*a, dest): + print 'fail' + print c*a + print dest + else: + print 'success' - print 'testing floatdivmatrix...', + print 'testing floatdivmatrix...', - floatdivmatrix(cuda.In(a), c, cuda.Out(dest), **size) + floatdivmatrix(cuda.In(a), c, cuda.Out(dest), **size) - if not np.allclose(c/a, dest): - print 'fail' - print c/a - print dest - else: - print 'success' + if not np.allclose(c/a, dest): + print 'fail' + print c/a + print dest + else: + print 'success' - print 'testing matrixaddequals...', + print 'testing matrixaddequals...', - dest = np.copy(a) + dest = np.copy(a) - matrixaddequals(cuda.InOut(dest), cuda.In(b), **size) + matrixaddequals(cuda.InOut(dest), cuda.In(b), **size) - if not np.allclose(a+b, dest): - print 'fail' - print a+b - print dest - else: - print 'success' + if not np.allclose(a+b, dest): + print 'fail' + print a+b + print dest + else: + print 'success' - print 'testing matrixsubequals...', + print 'testing matrixsubequals...', - dest = np.copy(a) + dest = np.copy(a) - matrixsubequals(cuda.InOut(dest), cuda.In(b), **size) + matrixsubequals(cuda.InOut(dest), cuda.In(b), **size) - if not np.allclose(a-b, dest): - print 'fail' - print a-b - print dest - else: - print 'success' + if not np.allclose(a-b, dest): + print 'fail' + print a-b + print dest + else: + print 'success' - print 'testing matrixaddequalsfloat...', + print 'testing matrixaddequalsfloat...', - dest = np.copy(a) + dest = np.copy(a) - matrixaddequalsfloat(cuda.InOut(dest), c, **size) + matrixaddequalsfloat(cuda.InOut(dest), c, **size) - if not np.allclose(a+c, dest): - print 'fail' - print a+c - print dest - else: - print 'success' + if not np.allclose(a+c, dest): + print 'fail' + print a+c + print dest + else: + print 'success' - print 'testing matrixsubequalsfloat...', + print 'testing matrixsubequalsfloat...', - dest = np.copy(a) + dest = np.copy(a) - matrixsubequalsfloat(cuda.InOut(dest), c, **size) + matrixsubequalsfloat(cuda.InOut(dest), c, **size) - if not np.allclose(a-c, dest): - print 'fail' - print a-c - print dest - else: - print 'success' + if not np.allclose(a-c, dest): + print 'fail' + print a-c + print dest + else: + print 'success' - print 'testing matrixmulequalsfloat...', + print 'testing matrixmulequalsfloat...', - dest = np.copy(a) + dest = np.copy(a) - matrixmulequalsfloat(cuda.InOut(dest), c, **size) + matrixmulequalsfloat(cuda.InOut(dest), c, **size) - if not np.allclose(a*c, dest): - print 'fail' - print a*c - print dest - else: - print 'success' + if not np.allclose(a*c, dest): + print 'fail' + print a*c + print dest + else: + print 'success' - print 'testing matrixdivequalsfloat...', + print 'testing matrixdivequalsfloat...', - dest = np.copy(a) + dest = np.copy(a) - matrixdivequalsfloat(cuda.InOut(dest), c, **size) + matrixdivequalsfloat(cuda.InOut(dest), c, **size) - if not np.allclose(a/c, dest): - print 'fail' - print a*c - print dest - else: - print 'success' + if not np.allclose(a/c, dest): + print 'fail' + print a*c + print dest + else: + print 'success' - print 'testing outer...', + print 'testing outer...', - x1_cpu = np.random.random_sample(size=3).astype(np.float32) - x2_cpu = np.random.random_sample(size=3).astype(np.float32) + x1_cpu = np.random.random_sample(size=3).astype(np.float32) + x2_cpu = np.random.random_sample(size=3).astype(np.float32) - x1_gpu = np.array((x1_cpu[0], x1_cpu[1], x1_cpu[2]), dtype=float3) - x2_gpu = np.array((x2_cpu[0], x2_cpu[1], x2_cpu[2]), dtype=float3) + x1_gpu = np.array((x1_cpu[0], x1_cpu[1], x1_cpu[2]), dtype=float3) + x2_gpu = np.array((x2_cpu[0], x2_cpu[1], x2_cpu[2]), dtype=float3) - outer(x1_gpu, x2_gpu, cuda.Out(dest), **size) + outer(x1_gpu, x2_gpu, cuda.Out(dest), **size) - if not np.allclose(np.outer(x1_cpu, x2_cpu).flatten(), dest): - print 'fail' - print np.outer(x1_cpu, x2_cpu).flatten() - print dest - else: - print 'success' + if not np.allclose(np.outer(x1_cpu, x2_cpu).flatten(), dest): + print 'fail' + print np.outer(x1_cpu, x2_cpu).flatten() + print dest + else: + print 'success' - print 'testing minus matrix...', + print 'testing minus matrix...', - dest = np.copy(a) + dest = np.copy(a) - minusmatrix(cuda.In(a), cuda.Out(dest), **size) + minusmatrix(cuda.In(a), cuda.Out(dest), **size) - if not np.allclose(-a, dest): - print 'fail' - print -a - print dest - else: - print 'success' + if not np.allclose(-a, dest): + print 'fail' + print -a + print dest + else: + print 'success' |