【发布时间】:2021-04-20 17:34:10
【问题描述】:
我无法理解工作项约束的含义。我正在使用pyopencl 并查看max_work_item_sizes,它给出了我假设的每个维度的最大全局工作线程数。
import pyopencl as cl
import numpy as np
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
queue.device.max_work_item_sizes # [1024, 1024, 64]
我可以通过以下方式模拟np.arange 函数:
prg = cl.Program(ctx, """
__kernel void arange(__global int *res_g)
{
int gid = get_global_id(0);
res_g[gid] = gid;
}
""").build()
res_g = cl.Buffer(ctx, cl.mem_flags.READ_WRITE, 4 * 4096)
prg.arange(queue, [4096], None, res_g)
# transfer back to cpu
res_np = np.empty(4096).astype(np.int32)
cl.enqueue_copy(queue, res_np, res_g)
assert (res_np == np.arange(4096)).all() # this is true
如何为第一个维度指定超过 1024 个工作项? max_work_item_sizes 是什么意思?
与此相关的另一个问题是使用尽可能多的工作维度是否有益?据我了解,最多可以使用 3 个维度。使用 2 个工作项维度模拟 np.arange 的方法可以通过以下方式完成:
prg = cl.Program(ctx, """
__kernel void arange(__global int *res_g)
{
int gid = get_global_id(0) * get_global_id(1);
barrier(CLK_GLOBAL_MEM_FENCE);
res_g[gid] = gid;
}
""").build()
res_g = cl.Buffer(ctx, cl.mem_flags.READ_WRITE, 4 * 4096)
prg.arange(queue, [64, 64], [1,1], res_g)
# transfer back to cpu
res_np = np.empty(4096).astype(np.int32)
cl.enqueue_copy(queue, res_np, res_g)
assert (res_np == np.arange(4096)).all()
由于某种原因,断言并不总是正确的
但我的问题是,在处理大型数组时,是否最好使用全部 3 个work_item_dimensions?还是将数组视为一维连续数组并仅使用get_global_id(0) 更好?
【问题讨论】:
标签: python arrays numpy opencl pyopencl