Skip to content

Instantly share code, notes, and snippets.

@fjarri
Created February 19, 2015 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fjarri/09d25c4355d3a3a41041 to your computer and use it in GitHub Desktop.
Save fjarri/09d25c4355d3a3a41041 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
import pyopencl as cl
from mako.template import Template
a_np = np.random.rand(50000).astype(np.float32)
b_np = np.random.rand(50000).astype(np.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np)
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np)
class Parameters:
loops = 2
factor = 3
parameters = Parameters()
tmpl = Template("""
__kernel void sum(__global const float *a_g, __global const float *b_g, __global float *res_g) {
int gid = get_global_id(0);
float a = a_g[gid];
float b = b_g[gid];
%for i in range(parameters.loops):
a += b * ${parameters.factor};
%endfor
res_g[gid] = a;
}
""")
src = tmpl.render(parameters=parameters)
prg = cl.Program(ctx, src).build()
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes)
prg.sum(queue, a_np.shape, None, a_g, b_g, res_g)
res_np = np.empty_like(a_np)
cl.enqueue_copy(queue, res_np, res_g)
# Check on CPU with Numpy:
print(np.allclose(res_np, a_np + b_np * parameters.factor * parameters.loops))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment