moved override_parameters example out of sgemm example

pull/255/head
kodonell 2018-03-27 08:30:58 +13:00
parent 58e70c56f1
commit f07c2a29b8
2 changed files with 45 additions and 16 deletions

View File

@ -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 <www.cedricnugteren.nl>
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"

View File

@ -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)))