Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Edit: extending the answer, making it maximally detailed.</p> <p>There are two ways to do that:</p> <ol> <li><p>(metaprogramming) Add your preprocessor directives directly to the string with the source code, or even run your own preprocessor using some templating engine.</p> <pre><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) defines = """ #define AXIS 0 #define COEFF 1 """ prg = cl.Program(ctx, defines + """ __kernel void sum(__global const float *a, __global const float *b, __global float *c) { int gid = get_global_id(AXIS); c[gid] = a[gid] + b[gid] + COEFF; } """).build() 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+1)), la.norm(a_plus_b)) </code></pre></li> <li><p>(C-way) use <code>options</code> keyword of <code>Program.build</code> to pass build options directly to <a href="http://www.khronos.org/registry/cl/sdk/1.0/docs/man/xhtml/clBuildProgram.html" rel="nofollow">clBuildProgram()</a>:</p> <pre><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(AXIS); c[gid] = a[gid] + b[gid] + COEFF; } """).build(options=['-D', 'AXIS=0', '-D', 'COEFF=1']) 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+1)), la.norm(a_plus_b)) </code></pre></li> </ol> <p>(I have used the modifed source code from the main page of PyOpenCL docs. Tested on pyopencl 2013.1).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload