我刚刚开始使用openCL C编程。工作组的所有工作项都会更新本地内存的唯一位置。稍后,工作项的私有变量将根据其他两个工作项更新的本地数据进行更新。像这样:
__kernel MyKernel(__global int *in_ptr)
{
/* Define a variable in private address space */
int priv_data;
/* Define two indices in private address space */
int index1, index2;
/* index1 and index2 are legitimate local work group indices */
index1 = SOME_CORRECT_VALUE;
index2 = ANOTHER_CORRECT_VALUE;
/* Define storage in local memory large enough to cater to all work items of this work group */
__local int tempPtr[WORK_GROUP_SIZE];
tempPtr[get_local_id(0)] = SOME_RANDOM_VALUE;
/* Do not proceed until the update of tempPtr by this WI has completed */
mem_fence(CLK_LOCAL_MEM_FENCE);
/* Do not proceed until all WI of this WG have updated tempPtr */
barrier(CLK_LOCAL_MEM_FENCE);
/* Update private data */
priv_data = tempPtr[index1] + tempPtr[index2];
}
虽然上面的代码片段是保守的,但是它没有完成这项工作,因为它在内部做了围栏吗?
答案 0 :(得分:1)
是,屏障已经进行了屏蔽。
屏障将同步该点的执行。因此,必须执行所有先前的指令,因此在那时存储器是一致的。 围栅只会确保在执行任何进一步的读/写操作之前完成所有读/写操作,但工作人员可能正在执行不同的指令。
在某些情况下,您可以使用单个围栏。如果您不关心本地工作人员不同步,并且您只想完成以前的内存写入/读取。在你的情况下,围栏就足够了。 (除非该代码在循环中运行,并且您没有在示例中添加额外的代码)。