我一直在尝试学习 OpenCL,但偶然发现了一个问题。在下面的代码中,我创建了一个空的 write_only opencl 图像对象并尝试让一个简单的内核变黑(或者至少以某种方式更改它),但是它只返回一个空图像。(我正在做一个图像卷积练习,它一直返回一个空图像,下面的代码只是试图隔离问题。)
我已经搞砸了2个多小时了,我很确定我被卡住了。
import matplotlib.pyplot as plt
import scipy.ndimage as si
import pyopencl as cl
import numpy as np
import os
kernel = """
__kernel void black(__write_only image2d_t dst,
int rows,
int columns)
{
const int column = get_global_id(0);
const int row = get_global_id(1);
if (column < columns && row < rows)
{
write_imagef(dst, (int2)(column, row),
(float4)(1.0f, 1.0f, 1.0f, 1.0f));
}
}
"""
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
f = cl.ImageFormat(cl.channel_order.R, cl.channel_type.UNSIGNED_INT8)
dst_image = cl.Image(ctx, mf.WRITE_ONLY , f, shape=(100,100,4))
prg = cl.Program(ctx, kernel).build()
prg.black(queue, (100,100), None, dst_image,
np.int32(100),
np.int32(100))
postimage = np.zeros((100,100,4), dtype=np.uint8)
cl.enqueue_copy(queue, postimage, dst_image,
origin=(0, 0, 0),
region=(100,100,4))
plt.imshow(postimage)
plt.show()