以下内核计算声压场,每个线程计算它自己的pressure
向量的私有实例,然后需要将其归结为全局内存。
我很确定计算pressure
向量的代码是正确的,但是我仍然无法使它产生预期的结果。
int gid = get_global_id(0);
int lid = get_local_id(0);
int nGroups = get_num_groups(0);
int groupSize = get_local_size(0);
int groupID = get_group_id(0);
/* Each workitem gets private storage for the pressure field.
* The private instances are then summed into local storage at the end.*/
private float2 pressure[HYD_DIM_TOTAL];
local float2 pressure_local[HYD_DIM_TOTAL];
/* Code which computes value of 'pressure' */
//wait for all workgroups to finish accessing any memory
barrier(CLK_GLOBAL_MEM_FENCE | CLK_LOCAL_MEM_FENCE);
/// sum all results in a workgroup into local buffer:
for(i=0; i<groupSize; i++){
//each thread sums its own private instance into the local buffer
if (i == lid){
for(iHyd=0; iHyd<HYD_DIM_TOTAL; iHyd++){
pressure_local[iHyd] += pressure[iHyd];
}
}
//make sure all threads in workgroup get updated values of the local buffer
barrier(CLK_LOCAL_MEM_FENCE);
}
/// copy all the results into global storage
//1st thread in each workgroup writes the group's local buffer to global memory
if(lid == 0){
for(iHyd=0; iHyd<HYD_DIM_TOTAL; iHyd++){
pressure_global[groupID +nGroups*iHyd] = pressure_local[iHyd];
}
}
barrier(CLK_GLOBAL_MEM_FENCE);
/// sum the various instances in global memory into a single one
// 1st thread sums global instances
if(gid == 0){
for(iGroup=1; iGroup<nGroups; iGroup++){
//we only need to sum the results from the 1st group onward
for(iHyd=0; iHyd<HYD_DIM_TOTAL; iHyd++){
pressure_global[iHyd] += pressure_global[iGroup*HYD_DIM_TOTAL +iHyd];
barrier(CLK_GLOBAL_MEM_FENCE);
}
}
}
有关数据维度的一些注意事项:
线程总数将在100到2000之间变化,但有时可能会超出此间隔
groupSize
将取决于硬件,但我目前使用的值介于1(cpu)和32(gpu)之间。
HYD_DIM_TOTAL
在编译时是已知的,并且在4到32之间变化(通常但不一定是2的幂)。
此缩减代码有什么明显错误吗?
PS:我使用AMD APP SDK 2.8和NVIDIA GTX580在i7 3930k上运行。
答案 0 :(得分:4)
我注意到这里有两个问题,一个是大问题,一个是小问题:
希望这有帮助。