GPU Computing with Python: PyOpenCL and PyCUDA Updated

GPU Computing with Python

PyOpenCL and PyCUDA, two wrappers for OpenCL and CUDA APIs, have been updated. These wrappers allow to call OpenCL and CUDA functions from a Python code.

PyOpenCL: OpenCL + Python

PyCUDA: CUDA + Python

PyOpenCL sample:

import pyopencl as cl
import numpy
import numpy.linalg as la
a = numpy.random.rand(50000).astype(numpy.float32)
b = numpy.random.rand(50000).astype(numpy.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
prg = cl.Program(ctx, """
    __kernel void sum(__global const float *a,
    __global const float *b, __global float *c)
      int gid = get_global_id(0);
      c[gid] = a[gid] + b[gid];
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_copy(queue, a_plus_b, dest_buf)
print la.norm(a_plus_b - (a+b))

[ Subscribe to Geeks3D latest news by email ]

↑ Grab this Headline Animator