From f07c2a29b8fdeffceece3fd5e47f5dc28f3f48d8 Mon Sep 17 00:00:00 2001 From: kodonell Date: Tue, 27 Mar 2018 08:30:58 +1300 Subject: moved override_parameters example out of sgemm example --- src/pyclblast/samples/override_parameters.py | 42 ++++++++++++++++++++++++++++ src/pyclblast/samples/sgemm.py | 19 ++----------- 2 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 src/pyclblast/samples/override_parameters.py (limited to 'src/pyclblast/samples') diff --git a/src/pyclblast/samples/override_parameters.py b/src/pyclblast/samples/override_parameters.py new file mode 100644 index 00000000..1cced4e5 --- /dev/null +++ b/src/pyclblast/samples/override_parameters.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python + +# This file is part of the CLBlast project. The project is licensed under Apache Version 2.0. +# This file follows the PEP8 Python style guide and uses a max-width of 100 characters per line. +# +# Author(s): +# Cedric Nugteren + +import numpy as np +import pyopencl as cl +from pyopencl.array import Array +import pyclblast +from datetime import datetime + +if __name__ == "__main__": + + # Set up pyopencl: + ctx = cl.create_some_context() + queue = cl.CommandQueue(ctx) + + # Set up a basic sgemm example: + m, n, k = 2, 3, 4 + a = np.random.rand(m, k).astype(dtype=np.float32) + b = np.random.rand(k, n).astype(dtype=np.float32) + c = np.empty((m, n), np.float32) + cla = Array(queue, a.shape, a.dtype) + clb = Array(queue, b.shape, b.dtype) + clc = Array(queue, c.shape, c.dtype) + cla.set(a) + clb.set(b) + clc.set(c) + + # Perform sgemm on these matrices, overriding the CLBlast parameters. In this example, we'll + # just change the 'MWG' parameter a couple of times: + params = { "KWG": 32, "KWI": 2, "MDIMA": 8, "MDIMC": 8, "MWG": 64, "NDIMB": 8, "NDIMC": 8, + "NWG": 64, "SA": 0, "SB": 0, "STRM": 0, "STRN": 0, "VWM": 4, "VWN": 1 } + for mwg in (32, 64, 256): + print("Running sgemm tuned with MWG = %d" % mwg) + params["MWG"] = mwg + pyclblast.override_parameters(ctx.devices[0], 'Xgemm', 32, params) + pyclblast.gemm(queue, m, n, k, cla, clb, clc, a_ld=k, b_ld=n, c_ld=n) + assert np.allclose(clc.get(), a.dot(b)), "uh-oh, xgemm isn't behaving correctly" diff --git a/src/pyclblast/samples/sgemm.py b/src/pyclblast/samples/sgemm.py index 3f149159..c872553f 100644 --- a/src/pyclblast/samples/sgemm.py +++ b/src/pyclblast/samples/sgemm.py @@ -10,7 +10,6 @@ import numpy as np import pyopencl as cl from pyopencl.array import Array import pyclblast -from datetime import datetime # Settings for this sample dtype = 'float32' @@ -20,7 +19,7 @@ ctx = cl.create_some_context() queue = cl.CommandQueue(ctx) print("# Setting up Numpy arrays") -m, n, k = 128, 256, 512 +m, n, k = 2, 3, 4 a = np.random.rand(m, k).astype(dtype=dtype) b = np.random.rand(k, n).astype(dtype=dtype) c = np.random.rand(m, n).astype(dtype=dtype) @@ -35,17 +34,5 @@ clc.set(c) print("# Example level-3 operation: GEMM") pyclblast.gemm(queue, m, n, k, cla, clb, clc, a_ld=k, b_ld=n, c_ld=n) -print("# PyCLBlast matrix result is correct?:", np.allclose(clc.get(), np.dot(a, b))) - -print("# GFLOPS when tuned with different values of MWG:") -params = { "KWG": 32, "KWI": 2, "MDIMA": 8, "MDIMC": 8, "MWG": 64, "NDIMB": 8, "NDIMC": 8, "NWG": 64, "SA": 0, "SB": 0, "STRM": 0, "STRN": 0, "VWM": 4, "VWN": 1 } -mwg = 1 -while mwg <= 256: - params["MWG"] = mwg - pyclblast.override_parameters(ctx.devices[0], 'Xgemm', 32, params) - for i in range(100): - if i == 10: - t0 = datetime.now() - pyclblast.gemm(queue, m, n, k, cla, clb, clc, a_ld=k, b_ld=n, c_ld=n) - print("#\tMWG = %-3d : %4d" % (mwg, int(2 * m * n * k / ((datetime.now() - t0).total_seconds() / 100) / 1024 ** 3))) - mwg *= 4 +print("# Matrix C result: %s" % clc.get()) +print("# Expected result: %s" % (np.dot(a, b))) -- cgit v1.2.3