【发布时间】:2015-06-18 04:01:38
【问题描述】:
您好,我创建了两个内核来执行一个简单的匹配碎纸机程序,以使用 OpenCL 运行并定时。这两个内核做了它们应该做的事情,但一个运行得比另一个慢得多,原因我无法破译:/ 唯一真正的区别是我如何存储发送的数据以及如何进行匹配。
__kernel void Horizontal_Match_Orig(
__global int* allShreds,
__global int* matchOut,
const unsigned int shredCount,
const unsigned int pixelCount)
{
int match = 0;
int GlobalID = get_global_id(0);
int currShred = GlobalID/pixelCount;
int thisPixel = GlobalID - (currShred * pixelCount);
int matchPixel = allShreds[GlobalID];//currShred*pixelCount+thisPixel];
for (int i = 0; i < shredCount; i++)
{
match = 0;
if (matchPixel == allShreds[(i * pixelCount) + thisPixel])
{
if (matchPixel == 0)
{
match = match + 150;
}
else match = match + 1;
}
else match = match - 50;
atomic_add(&matchOut[(currShred * shredCount) + i], match);
}
}
这个内核水平获取碎片边缘,因此一个碎片的像素占据数组allShreds中的pos 0到n,然后下一个碎片的像素从pos n+1存储到m(其中n =像素,m = 添加的像素数)。 GPU 的每个线程都获得一个像素来处理,并将其与所有其他碎片(包括其自身)的相应像素进行匹配
__kernel void Vertical(
__global int* allShreds,
__global int* matchOut,
const int numShreds,
const int pixelsPerEdge)
{
int GlobalID = get_global_id(0);
int myMatch = allShreds[GlobalID];
int myShred = GlobalID % numShreds;
int thisRow = GlobalID / numShreds;
for (int matchShred = 0; matchShred < numShreds; matchShred++)
{
int match = 0;
int matchPixel = allShreds[(thisRow * numShreds) + matchShred];
if (myMatch == matchPixel)
{
if (myMatch == 0)
match = 150;
else
match = 1;
}
else match = -50;
atomic_add(&matchOut[(myShred * numShreds) + matchShred], match);
}
}
这个内核垂直获取碎片边缘,因此所有碎片的第一个像素存储在 pos 0 到 n 中,然后所有碎片的第 2 个像素存储在 pos n+1 ot m 中(其中 n = 碎片数, m = 添加到 n) 的碎片数。该过程类似于前一个线程,每个线程获取一个像素并将其与其他每个碎片的相应像素进行匹配。
两者都给出了相同的结果,针对纯顺序程序测试的正确结果。从理论上讲,它们应该在大致相同的时间内运行,垂直的可能运行得更快,因为原子添加不应该对其产生太大影响......但是它运行得慢得多......有什么想法吗?
这是我用来启动它的代码(我正在使用 C# 包装器):
theContext.EnqueueNDRangeKernel(1, null, new int[] { minRows * shredcount }, null, out clEvent);
全球总工作量等于像素总数(每个像素中的#Shreds X #Pixels)。
任何帮助将不胜感激
【问题讨论】: