【问题标题】:Use OpenCl Global ID as an integer in the kernel在内核中使用 OpenCl Global ID 作为整数
【发布时间】:2017-04-21 18:46:47
【问题描述】:

我刚开始使用 pyopencl 模块在 python 中研究 OpenCl。

我有兴趣在没有任何输入的情况下生成东西,例如生成正弦波的样本。

为此,我只需要全局 id 来进行计算,但返回全局 id 会产生一些花哨的数字。我使用下面的代码:

import numpy as np
import pyopencl as cl

Size = Width*Height

# Get platforms, both CPU and GPU
plat = cl.get_platforms()
GPU = plat[0].get_devices()

#Create context for GPU
ctx = cl.Context(GPU)

# Create queue for each kernel execution
queue = cl.CommandQueue(ctx)

mf = cl.mem_flags

# Kernel function
src = '''
__kernel void shader(__global float *result, __global int *width){

int w = *width;
size_t gid = get_global_id(0);

result[gid] = gid;
}
'''

#Kernel function instantiation
prg = cl.Program(ctx, src).build()

#Allocate memory for variables on the device
width_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=np.int32(Width))
result_g = cl.Buffer(ctx, mf.WRITE_ONLY, Size*8)

# Call Kernel. Automatically takes care of block/grid distribution
prg.shader(queue, (Size,), None , result_g, width_g)
result = np.empty((Size,))
cl.enqueue_copy(queue, result, result_g)

Image = result

我所做的只是将全局 id 复制到缓冲区对象 result_d,但是当我检查 result 时,我得到一些甚至不是整数的数字。我也尝试将缓冲区设置为整数而不是浮点数,但结果仍然相同。

我做错了什么?

【问题讨论】:

    标签: python opencl pyopencl


    【解决方案1】:

    问题是OpenCL内核中的resultfloat类型,而主机端的resultdouble类型。

    将主机缓冲区指定为float 以解决问题:

    result = np.empty((Size,),dtype=np.float32)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      • 2016-05-28
      • 2012-11-02
      • 2011-12-06
      • 2012-02-05
      • 1970-01-01
      相关资源
      最近更新 更多