Python OpenCL Example: pyOpenCL
Example of a program written in the OpenCL programming language.
Below is an example of a program written in OpenCL. This language is written to allow highly parallel processes to be computed on any type of highly parallel processor(s). This is an example from the ‘examples’ folder of pyOpenCL.
About OpenCL
OpenCL stands for Open Computing Language. Its goal is to allow writing of programs across multiple platforms. One of the advantages of OpenCL is its ability to allow programs to be written for the GPU. The GPU is one of the most parallel processors to date and offers large amounts of parallel processing power. You will find OpenCL support in Linux, Windows, and MacOS (Snow Leopard or newer only unfortunately). You will find full support for the GPU using NVidia or ATI video cards and any of the previous operating systems. Also OpenCL is supported for some multi-core or multiple CPU set ups.
Alternatives
Other similar packages are NVidia’s Compute Unified Device Architecture or CUDA and Microsoft’s DirectCompute. None of these are quite as cross-platform as OpenCL.
Overview
We create 2 arrays of random numbers (using the numpy package) then write an OpenCL ‘kernel’ that will sum the 2 arrays, and write to an output buffer the results. We then read that buffer back into python’s memory and print out statistics on that array.
Source Code
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];
}
""").build()
prg.sum(queue, a.shape, a_buf, b_buf, dest_buf)
a_plus_b = numpy.empty_like(a)
cl.enqueue_read_buffer(queue, dest_buf, a_plus_b).wait()
print la.norm(a_plus_b - (a+b)), la.norm(a_plus_b)
Links:
PyOpenCL
Python
Boost C++ Libraries (required for PyOpenCL)
More info on OpenCL
buy@generic.LEVITRA” rel=”nofollow”>……
Need cheap generic LEVITRA?…
Hi…
http://www.webcamgirls4.com/...
medium hairstyles…
Hello there, just became aware of your blog through Google, and found that it’s really informative. I’m gonna watch out for brussels. I’ll appreciate if you continue this in future. Many people will be benefited from your writing. Cheers!…