2016-04-19 7 views
0

私はOpenCLのためのPythonバインディングを使用していると私はスカラー(float型)の引数を期待するカーネルを書かれているが、私はそれを渡すために適切な方法を把握することはできません。Pythonでは、どのようにしてOpenCLカーネルにスカラー引数を渡すのですか?

私は単に

を起動した場合
prg.bloop(queue, [width,height], None, centers_g, 16/9, result_g) 

私はこのエラーを取得する:

pyopencl.cffi_cl.LogicError: when processing argument #2 (1-based): 'float' does not support the buffer interface 

私はnumpy.float32(16/9)でそれをラップした場合、それが0の代わりに、1.7777777を渡されたかのようにカーネルが動作します。

私がしていることを理解するのに役立つソースコードがあります。

def mission2(cells, width, height): 
    ctx = cl.create_some_context() 
    queue = cl.CommandQueue(ctx) 

    centers = numpy.array(list(cells), dtype=numpy.float32) 

    centers_g = cl.Buffer(ctx, cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR, hostbuf = centers) 

    prg = cl.Program(ctx, """ 
__kernel void test1(__global char *out) { 
    out[0] = 77; 
} 

float sphere(float r, float x, float y) 
{ 
    float q = r*r-(x*x+y*y); 
    if (q<0) 
     return 0; 
    return sqrt(q); 
} 

__kernel void bloop(__global const float centers[][6], float aspect, __global char * out) 
{ 
    int u = get_global_id(0); 
    int v = get_global_id(1); 
    int width = get_global_size(0); 
    int height = get_global_size(1); 

    float x = u/(float)width * aspect; 
    float y = v/(float)height; 

    float max = sphere(0.3, x-centers[0][0], y-centers[0][1]); 

    int idx = u+v*width; 
    out[idx] = 255*max; 

} 

    """).build() 

    result_g = cl.Buffer(ctx, cl.mem_flags.WRITE_ONLY, width*height) 


    if False: 
     prg.test1(queue, [width,height], None, result_g) 
    else: 
     prg.bloop(queue, [width,height], None, centers_g, numpy.float32(16/9), result_g) 


    result = numpy.zeros([height,width], dtype=numpy.uint8) 
    future = cl.enqueue_copy(queue, result, result_g) 
    future.wait() 

    print(result) 

    imsave("/tmp/bloop.png", result, cmap = matplotlib.pyplot.get_cmap('gray')) 
+0

1と同じように動作していないのですか? 16/9はPython 2で1を返します。numpy.float32でのラッピングは、PyOpenCLを使用するときに行います。ここで動作します。 –

+0

センターがカーネルに定義されていません。それはあなたのホストコードで定義されています。エラーはそれを言っています:浮動小数点のためのバッファインターフェイスが定義されていません。 'センター'はフロートです。したがって、センター[6]は違法です。 – Dschoni

+0

私はpython 2を使用していません。これはPython 3のアプリケーションで、 'print(16/9)'を実行すると、明らかに1.77777が得られます。 –

答えて

1

あなたはこのようset_scalar_arg_dtypes機能を使用することができるのOpenCLカーネルにスカラを渡すために:

:はっきりあなたはこのようにそれをコーディングする場合、それは動作しませんと述べて

kernel = prg.bloop 
kernel.set_scalar_arg_dtypes([None, numpy.float32, None]) 
kernel(queue, [width, height], None, centers_g, aspect, result_g) 

The manual page

prg.bloop.set_scalar_arg_dtypes([None, numpy.float32, None]) 
# this will fail: 
prg.bloop(queue, [width, height], None, centers_g, 16/9, result_g) 

"このrountineによって設定された情報は単一のカーネルインスタンスに関連付けられているため、新しいカーネルインスタンスはprogram.kernel属性acセス "

関連する問題