Last active
January 19, 2018 00:50
-
-
Save kaushikcfd/c6a95d6d52db3cc4e15f5aa2bb37efbd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import loopy as lp | |
import pyopencl as cl | |
import pyopencl.array | |
import pyopencl.clrandom as cl_random | |
from time import time | |
ctx = cl.create_some_context() | |
queue = cl.CommandQueue(ctx) | |
n = 2**8 | |
a_mat_dev = cl_random.rand(queue, (n, n), dtype=np.float32) | |
b_mat_dev = cl_random.rand(queue, (n, n), dtype=np.float32) | |
c_mat_dev = cl.array.zeros(queue, (n, n), dtype=np.float32) | |
queue.finish() | |
knl = lp.make_kernel( | |
"{ [i, j, k, k1]: 0<=i, j, k, k1<256 }", | |
""" | |
temp_cnst[k] = 2.0 {id=insn_1} | |
temp_cnst_2[k1] = 2*temp_cnst[k1] {id=insn_4} | |
c[i, j] = reduce(sum, k, a[i,k]*b[k,j]) {id=insn_2} | |
c[i, j] = reduce(sum, k1, temp_cnst_2[k1]*a[i,k1]*b[k1,j]) {id=insn_3} | |
""", | |
[lp.TemporaryVariable("temp_cnst", | |
dtype=np.float32, | |
shape=lp.auto, | |
base_indices=lp.auto, | |
scope=lp.temp_var_scope.PRIVATE), | |
lp.TemporaryVariable("temp_cnst_2", | |
dtype=np.float32, | |
shape=lp.auto, | |
base_indices=lp.auto, | |
scope=lp.temp_var_scope.PRIVATE), | |
'...'] | |
) | |
knl = lp.add_and_infer_dtypes(knl, dict(a=np.float32, | |
b=np.float32, | |
c=np.float32)) | |
knl = lp.set_options(knl, "write_cl") | |
knl = lp.split_iname(knl, "i", 16, outer_tag="g.0", inner_tag="l.0") | |
processed_knl = lp.preprocess.preprocess_kernel(knl) | |
print(processed_knl) | |
print(lp.has_schedulable_iname_nesting(processed_knl)) | |
kernel_args = {} | |
kernel_args['a'] = a_mat_dev | |
kernel_args['b'] = b_mat_dev | |
kernel_args['c'] = c_mat_dev | |
evt, (out,) = knl(queue, **kernel_args) | |
a = a_mat_dev.get() | |
b = b_mat_dev.get() | |
c = out.get() | |
print('Error =', np.linalg.norm(4*a.dot(b)-c)/np.linalg.norm(4*a.dot(b))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment