Image processing 提高OpenCL图像处理速度
我认为我的内核执行时间太长了。它的工作就是用加法、减法、除法或乘法将两幅图像混合在一起Image processing 提高OpenCL图像处理速度,image-processing,kernel,opencl,Image Processing,Kernel,Opencl,我认为我的内核执行时间太长了。它的工作就是用加法、减法、除法或乘法将两幅图像混合在一起 #define SETUP_KERNEL(name, operator)\ __kernel void name(__read_only image2d_t image1,\ __read_only image2d_t image2,\ __write_only image2d_t output,\
#define SETUP_KERNEL(name, operator)\
__kernel void name(__read_only image2d_t image1,\
__read_only image2d_t image2,\
__write_only image2d_t output,\
const sampler_t sampler1,\
const sampler_t sampler2,\
float opacity)\
{\
int2 xy = (int2) (get_global_id(0), get_global_id(1));\
float2 normalizedCoords = convert_float2(xy) / (float2) (get_image_width(output), get_image_height(output));\
float4 pixel1 = read_imagef(image1, sampler1, normalizedCoords);\
float4 pixel2 = read_imagef(image2, sampler2, normalizedCoords);\
write_imagef(output, xy, (pixel1 * opacity) operator pixel2);\
}
SETUP_KERNEL(div, /)
SETUP_KERNEL(add, +)
SETUP_KERNEL(mult, *)
SETUP_KERNEL(sub, -)
如您所见,我使用宏来快速定义不同的内核。(我应该更好地使用函数吗?)
不知怎的,内核在GTX970上运行了3ms。
如何提高这个特定内核的性能?
我应该把它分成不同的项目吗?分工通常很昂贵 我建议将
normalizedCoords
的计算移到主机端
在主机端:
float normalized_x[output_width]; // initialize with [0..output_width-1]/output_width
float normalized_y[output_height]; // initialize with [0..output_height-1]/output_height
将内核更改为:
#define SETUP_KERNEL(name, operator)\
__kernel void name(__read_only image2d_t image1,\
__read_only image2d_t image2,\
__write_only image2d_t output,\
global float *normalized_x, \
global float *normalized_y, \
const sampler_t sampler1,\
const sampler_t sampler2,\
float opacity)\
{\
int2 xy = (int2) (get_global_id(0), get_global_id(1));\
float2 normalizedCoords = (float2) (normalized_x[xy.x],normalized_y[xy.y] );\
float4 pixel1 = read_imagef(image1, sampler1, normalizedCoords);\
float4 pixel2 = read_imagef(image2, sampler2, normalizedCoords);\
write_imagef(output, xy, (pixel1 * opacity) operator pixel2);\
}
您也可以尝试使用相同的技术不使用标准化的cooardinates。
如果输入图像的大小不经常改变,这将更加有益。双线性插值比最近邻法慢2-3倍。您确定没有在opengl中使用最近邻吗 它在后台(通过采样器)所做的工作如下:
R1 = ((x2 – x)/(x2 – x1))*Q11 + ((x – x1)/(x2 – x1))*Q21
R2 = ((x2 – x)/(x2 – x1))*Q12 + ((x – x1)/(x2 – x1))*Q22
After the two R values are calculated, the value of P can finally be calculated by a weighted average of R1 and R2.
P = ((y2 – y)/(y2 – y1))*R1 + ((y – y1)/(y2 – y1))*R2
The calculation will have to be repeated for the red, green, blue, and optionally the alpha component of.
或者它只是Nvidia实现的opengl快速路径和opencl图像访问的完整路径。例如,对于amd,图像写入是完整路径,小于32位的数据访问是完整路径,图像读取是快速路径
另一种选择:Z顺序更适合计算这些图像数据的散度,opencl的非Z顺序(可疑,可能不是)更差。图像的大小是多少?2048 x 2048像素。这是opengl opencl共享图像吗?未从主机复制或复制?没有共享映像。它只是上传到gpu,但我在内核执行之前就这样做了。内核是通过EnqueuenRangeKernelis执行的。3ms计时仅用于内核或包含复制?是的,线性采样是一个问题。我的内核计算现在花费的时间更少了。不调用finish()也有助于lot@DaOnlyOwner对于像素数据计算的散度,它也可以是Z阶与线性1-d阶或平方2-d阶。但仅当数据具有非规范化结果时。