summaryrefslogtreecommitdiff
path: root/src/pyclblast
diff options
context:
space:
mode:
authorCedric Nugteren <web@cedricnugteren.nl>2018-02-18 17:59:43 +0100
committerCedric Nugteren <web@cedricnugteren.nl>2018-02-18 17:59:43 +0100
commit76c21a95c29bd3645213b870de61e012204cc844 (patch)
tree31e5bc235d917bcd930c5e600d7aa450742d64a5 /src/pyclblast
parenta66e24a009fe6f23c6231ec3b7c1a4698f831435 (diff)
Added PyCLBlast samples
Diffstat (limited to 'src/pyclblast')
-rw-r--r--src/pyclblast/samples/saxpy.py36
-rw-r--r--src/pyclblast/samples/sgemm.py38
-rw-r--r--src/pyclblast/samples/sgemv.py40
3 files changed, 114 insertions, 0 deletions
diff --git a/src/pyclblast/samples/saxpy.py b/src/pyclblast/samples/saxpy.py
new file mode 100644
index 00000000..098e44d5
--- /dev/null
+++ b/src/pyclblast/samples/saxpy.py
@@ -0,0 +1,36 @@
+#!/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
+
+# Settings for this sample
+dtype = 'float32'
+alpha = 1.5
+n = 4
+
+print("# Setting up OpenCL")
+ctx = cl.create_some_context()
+queue = cl.CommandQueue(ctx)
+
+print("# Setting up Numpy arrays")
+x = np.random.rand(n).astype(dtype=dtype)
+y = np.random.rand(n).astype(dtype=dtype)
+
+print("# Setting up OpenCL arrays")
+clx = Array(queue, x.shape, x.dtype)
+cly = Array(queue, y.shape, y.dtype)
+clx.set(x)
+cly.set(y)
+
+print("# Example level-1 operation: AXPY")
+pyclblast.axpy(queue, n, clx, cly, alpha=alpha)
+print("# Result for vector y: %s" % cly.get())
+print("# Expected result: %s" % (alpha * x + y))
diff --git a/src/pyclblast/samples/sgemm.py b/src/pyclblast/samples/sgemm.py
new file mode 100644
index 00000000..c872553f
--- /dev/null
+++ b/src/pyclblast/samples/sgemm.py
@@ -0,0 +1,38 @@
+#!/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
+
+# Settings for this sample
+dtype = 'float32'
+
+print("# Setting up OpenCL")
+ctx = cl.create_some_context()
+queue = cl.CommandQueue(ctx)
+
+print("# Setting up Numpy arrays")
+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)
+
+print("# Setting up OpenCL arrays")
+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)
+
+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("# Matrix C result: %s" % clc.get())
+print("# Expected result: %s" % (np.dot(a, b)))
diff --git a/src/pyclblast/samples/sgemv.py b/src/pyclblast/samples/sgemv.py
new file mode 100644
index 00000000..196c838d
--- /dev/null
+++ b/src/pyclblast/samples/sgemv.py
@@ -0,0 +1,40 @@
+#!/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
+
+# Settings for this sample
+dtype = 'float32'
+m, n = 4, 3
+alpha = 1.0
+beta = 0.0
+
+print("# Setting up OpenCL")
+ctx = cl.create_some_context()
+queue = cl.CommandQueue(ctx)
+
+print("# Setting up Numpy arrays")
+a = np.random.rand(m, n).astype(dtype=dtype)
+x = np.random.rand(n).astype(dtype=dtype)
+y = np.random.rand(m).astype(dtype=dtype)
+
+print("# Setting up OpenCL arrays")
+cla = Array(queue, a.shape, a.dtype)
+clx = Array(queue, x.shape, x.dtype)
+cly = Array(queue, y.shape, y.dtype)
+cla.set(a)
+clx.set(x)
+cly.set(y)
+
+print("# Example level-2 operation: GEMV")
+pyclblast.gemv(queue, m, n, cla, clx, cly, a_ld=n, alpha=alpha, beta=beta)
+print("# Result for vector y: %s" % cly.get())
+print("# Expected result: %s" % (alpha * np.dot(a, x) + beta * y))