我正在编写一些OpenCL代码。我的内核应基于输入图像创建一个特殊的“累加器”输出。我尝试了两个概念,但两个概念都同样慢,尽管第二个概念使用本地内存。您能帮我确定为什么本地内存版本这么慢吗?内核的目标GPU是AMD Radeon Pro 450。
// version one
__kernel void find_points(__global const unsigned char* input, __global unsigned int* output) {
const unsigned int x = get_global_id(0);
const unsigned int y = get_global_id(1);
int ind;
for(k = SOME_BEGINNING; k <= SOME_END; k++) {
// some pretty wild calculation
// ind is not linear and accesses different areas of the output
ind = ...
if(input[y * WIDTH + x] == 255) {
atomic_inc(&output[ind]);
}
}
}
// variant two
__kernel void find_points(__global const unsigned char* input, __global unsigned int* output) {
const unsigned int x = get_global_id(0);
const unsigned int y = get_global_id(1);
__local int buf[7072];
if(y < 221 && x < 32) {
buf[y * 32 + x] = 0;
}
barrier(CLK_LOCAL_MEM_FENCE);
int ind;
int k;
for(k = SOME_BEGINNING; k <= SOME_END; k++) {
// some pretty wild calculation
// ind is not linear and access different areas of the output
ind = ...
if(input[y * WIDTH + x] == 255) {
atomic_inc(&buf[ind]);
}
}
barrier(CLK_LOCAL_MEM_FENCE);
if(get_local_id(0) == get_local_size(0) - 1)
for(k = 0; k < 7072; k++)
output[k] = buf[k];
}
}
我希望第二个变种比第一个变种快,但事实并非如此。有时甚至更慢。
答案 0 :(得分:5)
本地缓冲区大小__local int buf[7072]
(28288字节)太大。我不知道AMD Radeon Pro 450有多大的共享内存,但每个计算单元的共享内存可能是32kB或64kB。
32768/28288 = 1
,65536/28288 = 2
表示只能同时运行1个或最多2个波前(64个工作项),因此计算单元的占用率非常低,因此性能较差。
您的目标应该是尽可能减少本地缓冲区,以便可以同时处理更多的波前。
使用CodeXL
来分析您的内核-有一些工具可以向您展示所有这些内容。
另外,如果您不想运行探查器以更好地了解问题所在,则可以查看CUDA occupancy calculator
excel电子表格。