【问题标题】:pyopencl global_work_offset kernel argumentpyopencl global_work_offset 内核参数
【发布时间】:2016-06-25 15:20:41
【问题描述】:

我想使用来自 OpenCL API 函数 clEnqueueNDRangeKernelglobal_work_offset 参数。我不知道如何在pyopencl API 中做到这一点。这是一个演示代码,我想在内核调用中添加 2 的偏移量,因此 get_global_id(0) 从 2 而不是 0 开始:

import pyopencl as cl 
import pyopencl.array 
import numpy as np

platform = cl.get_platforms()[0]
devices = platform.get_devices()[1] #gpu
context = cl.Context(devices=[devices])
queue =  cl.CommandQueue(context)

kernel = cl.Program(context, """
    __kernel void derp(global char* a) {
        a[get_global_id(0)] = 1;
    }""").build()

buffarr = cl.array.zeros(queue, 4, dtype=np.uint8)
kernel.derp(queue, (2,), None, buffarr.data)

np_data = buffarr.get()

# within this demo the buffer contains currently [1,1,0,0]
assert np.array_equal(np_data, [0,0,1,1])

如何更改代码以使断言不会失败?我不想在内核代码中添加额外的参数。

【问题讨论】:

    标签: python pyopencl


    【解决方案1】:

    作为documentation,您可以将global_offset 作为命名参数传递。

    内核的调用变成:

    kernel.derp(queue, (4, 1), None, buffarr.data, global_offset=[2, 0])
    

    有变化的程序:

    import pyopencl as cl
    import pyopencl.array
    import numpy as np
    
    
    platform = cl.get_platforms()[2]
    print(platform)
    devices = platform.get_devices()[0] #gpu
    context = cl.Context(devices=[devices])
    queue =  cl.CommandQueue(context)
    
    kernel = cl.Program(context, """
        __kernel void derp(global char* a) {
            a[get_global_id(0)] = 1;
        }""").build()
    
    
    buffarr = cl.array.zeros(queue, 4, dtype=np.uint8)
    
    # (4, 1) ==> shape of the buffer
    kernel.derp(queue, (4, 1), None, buffarr.data, global_offset=[2, 0])
    
    np_data = buffarr.get()
    print(np_data)
    # within this demo the buffer contains currently [1,1,0,0]
    assert np.array_equal(np_data, [0,0,1,1])
    print("Ok")
    

    执行后:

    在设备 0 上

    <pyopencl.Platform 'Intel(R) OpenCL' at 0x60bdc0>
    [0 0 1 1]
    Ok
    

    在设备 1 上

    <pyopencl.Platform 'Experimental OpenCL 2.0 CPU Only Platform' at 0xb60a20>
    [0 0 1 1]
    Ok
    

    在设备 2 上

    <pyopencl.Platform 'NVIDIA CUDA' at 0xff0440>
    [0 0 1 1]
    Ok
    

    使用 python 2.7.11 [MSC v.1500 64 bit (AMD64)] 测试 - pyopencl (2015, 1)

    【讨论】:

    • 是否可以偏移多个全局尺寸维度?例如 global_offset=[5, 5,0] 来偏移 3-d 工作空间的前两个维度?
    • 根据文档,是的。最多 3 个维度。
    猜你喜欢
    • 2018-10-26
    • 2021-07-21
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    相关资源
    最近更新 更多