【发布时间】:2015-10-16 20:28:25
【问题描述】:
我正在尝试使用 Python 和 PyOpenCL 为我在网上找到的代码中的图像实现高斯过滤器。我的原始图像是 numpy 数组,但我很困惑应该使用哪个将图像传递给 GPU。
最初,内核接收 OpenCL 图像作为输入。这工作正常,内核运行正常,但是,我还没有找到将 GPU 计算(也是 OpenCL 图像)的输出转换为 numpy 数组的方法。这是必需的,因为在运行 GPU 过滤器后我必须执行其他计算。
我尝试使用 pyOpenCL Array,但在这种情况下遇到了 2 个问题:
- 不知道如何告诉内核输入将是一个数组,因为它是一个 pyOpenCL 数据结构,而不是 OpenCL 数据结构。
- 找不到在 pyOpenCL 数组上使用的
read_imagef等效项,我在内核中使用了该函数。 - 无法将 GPU 结果复制回主机。我会不断收到“
cl_array没有模块 get()”错误。
我想知道:
- 有没有办法告诉内核它将接收一个数组,就像我使用
image2d_t说输入是一个图像一样? - 对于 pyOpenCL 数组,我可以使用什么来等效于 OpenCL 的
read_imagef?
提前非常感谢。内核代码如下:
内核:
__kernel void gaussian(__read_only image2d_t inputImage,
__read_only image2d_t filterImage,
__write_only image2d_t outputImage,
const int nInWidth,
const int nFilterWidth){
const sampler_t sampler = CLK_NORMALIZED_COORDS_FALSE | CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;
const int xOut = get_global_id(0);
const int yOut = get_global_id(1);
float4 sum = (float4)(0.0, 0.0, 0.0, 1.0);
for(int r = 0; r < nFilterWidth; r++){
for(int c = 0; c < nFilterWidth; c++){
int2 location = (xOut + r, yOut + c);
float4 filterVal = read_imagef(filterImage, sampler, location);
float4 inputVal = read_imagef(inputImage, sampler, location);
sum.x += filterVal.x * inputVal.x;
sum.y += filterVal.y * inputVal.y;
sum.z += filterVal.z * inputVal.z;
sum.w = 1.0;
}
}
int2 outLocation = (xOut, yOut);
write_imagef(outputImage, outLocation, sum);
}
【问题讨论】: