繁体   English   中英

使用pyopenCL填充2D数组

[英]Fill a 2D array with pyopenCL

我正在尝试使用pyOpenCL填充2D数组。 计算内核及其调用发布如下:

ctx = cl.Context([cl.get_platforms()[0].get_devices()[0]])
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
x_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=x)
y_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=y)
a_buf = cl.Buffer(ctx, mf.WRITE_ONLY, a.nbytes)

prg = cl.Program(ctx, """
//#define PYOPENCL_DEFINE_CDOUBLE
#include "pyopencl-complex.h"    
__kernel void makeA(const unsigned int ySize, const float cov,
                  const int x0, const int y0, __global const float *x, __global const float *y,
                  __global cfloat_t *a)
{
  int gid0 = get_global_id(0);
  int gid1 = get_global_id(1);

  a[gid1 + ySize*gid0] = (cfloat_t)(1, 0);
}
""").build()

prg.makeA(queue, a.shape, None, np.int32(ySize),
    np.float32(c), np.int32(x0), np.int32(y0), x_buf, y_buf, a_dest_buf)  
cl.enqueue_copy(queue, a, a_dest_buf)

现在,这似乎工作正常。 今天我了解到,使用workgroups非常有用。 这个,我不能正常工作。 我试过替换函数调用

prg.makeA(queue, a.shape, (16,16), np.int32(ySize),
    np.float32(c), np.int32(x0), np.int32(y0), x_buf, y_buf, a_dest_buf)

但我不知道如何正确计算数组的新x和y指标,因此,我无法改变

a[yIdx + ySize*xIdx] = (cfloat)( x[xIdx] , 0);  

我现在尝试了以下工作:

__kernel void makea(const unsigned int ySize, const float cov,
                  const int x0, const int y0, __global const float *x, __global const float *y,
                  __global cfloat_t *a)
{

  int xIdx = get_local_id(0)+get_group_id(0)*get_local_size(0);
  int yIdx = get_local_id(1)+get_group_id(1)*get_local_size(1);

  a[yIdx + ySize*xIdx] = (cfloat_t)(1, 0);
}
""").build()




prg.makeA(queue, a.shape, (32,32), np.int32(ySize),
    np.float32(c), np.int32(x0), np.int32(y0), x_buf, y_buf, a_dest_buf)

通过这个我介绍了一个大小为32x32的工作组[函数调用参数: (32,32) ]。 看来这现在使用了几个多流处理器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM