~ubuntu-branches/debian/squeeze/pyopencl/squeeze

« back to all changes in this revision

Viewing changes to examples/demo.py

  • Committer: Bazaar Package Importer
  • Author(s): Tomasz Rybak
  • Date: 2010-05-31 19:29:00 UTC
  • Revision ID: james.westby@ubuntu.com-20100531192900-ll7guuro37nntr4y
Tags: upstream-0.92~beta+git20100709
ImportĀ upstreamĀ versionĀ 0.92~beta+git20100709

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import pyopencl as cl
 
2
import numpy
 
3
import numpy.linalg as la
 
4
 
 
5
a = numpy.random.rand(50000).astype(numpy.float32)
 
6
b = numpy.random.rand(50000).astype(numpy.float32)
 
7
 
 
8
ctx = cl.create_some_context()
 
9
queue = cl.CommandQueue(ctx)
 
10
 
 
11
mf = cl.mem_flags
 
12
a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
 
13
b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
 
14
dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, b.nbytes)
 
15
 
 
16
prg = cl.Program(ctx, """
 
17
    __kernel void sum(__global const float *a,
 
18
    __global const float *b, __global float *c)
 
19
    {
 
20
      int gid = get_global_id(0);
 
21
      c[gid] = a[gid] + b[gid];
 
22
    }
 
23
    """).build()
 
24
 
 
25
prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
 
26
 
 
27
a_plus_b = numpy.empty_like(a)
 
28
cl.enqueue_read_buffer(queue, dest_buf, a_plus_b).wait()
 
29
 
 
30
print la.norm(a_plus_b - (a+b)), la.norm(a_plus_b)