【问题标题】:OpenCL kernel cannot work as expected (pyopencl)OpenCL 内核无法按预期工作 (pyopencl)
【发布时间】:2018-06-07 07:06:11
【问题描述】:

我编写了一个 OpenCL 函数来增加数组中的 64 位浮点值。 但是 CPU 和 GPU 的结果不同。

import numpy as np
import pyopencl as cl

CL_INC = '''
__kernel void inc_f64(__global const double *a_g, __global double *res_g)
{
  int gid = get_global_id(0);
  res_g[gid] = a_g[gid] + 1.0;
}
'''

def test(dev_type):
    ctx = cl.Context(dev_type=dev_type)
    queue = cl.CommandQueue(ctx)
    mf = cl.mem_flags
    prg = cl.Program(ctx, CL_INC).build()

    in_py = np.array([1.0, 2.0, 3.0, 4.0, 5.0])
    out_py = np.empty_like(in_py)
    in_cl = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=in_py)
    out_cl = cl.Buffer(ctx, mf.WRITE_ONLY, in_py.nbytes)
    prg.inc_f64(queue, in_py.shape, None, in_cl, out_cl)
    cl.enqueue_copy(queue, out_py, out_cl)

    queue.finish()
    return out_py

print('Run inc_f64() on CPU: ', end='')
print(test(cl.device_type.CPU))
print('Run inc_f64() on GPU: ', end='')
print(test(cl.device_type.GPU))

输出:

Run inc_f64() on CPU: [2. 3. 4. 5. 6.]
Run inc_f64() on GPU: [2.40000038e+001 3.20000076e+001 5.26354425e-315 0.00000000e+000
 0.00000000e+000]

硬件信息:

[0] Apple / OpenCL 1.2 (Oct 31 2017 18:30:00)
  |- [0:0] CPU / OpenCL 1.2 / Intel(R) Core(TM) i7-3667U CPU @ 2.00GHz
  |- [0:1] GPU / OpenCL 1.2 / HD Graphics 4000

这是硬件限制还是只是源代码中的错误?

【问题讨论】:

    标签: python-3.x opencl pyopencl


    【解决方案1】:

    您的 GPU 可能不支持双精度浮点数。您是否检查了对 cl_khr_fp64 扩展的支持?

    您的内核还必须声明它的要求:

    #pragma OPENCL EXTENSION cl_khr_fp64 : enable
    

    更多详情,see the cl_khr_fp64 extension documentation

    【讨论】:

    • @RaymondWu 这是否意味着您已经为您的设备检查了CL_DEVICE_EXTENSIONS 字符串,并且它包含cl_khr_fp64 扩展名? (请参阅clGetDeviceInfo)另外,您是否检查过缓冲区副本、内核执行等的任何返回值?他们返回什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-01
    • 2023-03-28
    • 2016-11-28
    • 2013-12-23
    • 2014-12-09
    • 2016-01-13
    • 2020-09-21
    相关资源
    最近更新 更多