您的代码无法编译的原因sum
是在您的类中声明而不是包装在array_view
. 本质上,您正在尝试this->sum
从 AMP 受限代码访问。sum
在将其传递给 之前,您需要使用以下内容进行包装parallel_for_each
,然后应该使用avSum
.
int sum = 0;
array_view<int, 1> avSum(1, &sum);
您还需要使用原子操作来增加sum
跨多个线程的值,这在很大程度上否定了 GPU 提供的并行性。这不是正确的做法。
减少
我认为你想要实现的是减少。您正在尝试对输入数组中的所有值求和并返回一个结果。这是 GPU 编程中一个有据可查的问题。NVidia 已经制作了几份关于它的白皮书。C++ AMP Book也详细介绍了这一点。
这是最简单的可能实现。它不使用平铺,效率相对较低但易于理解。循环的每次迭代都会stride
添加数组的连续元素,直到最终结果在元素 0 中。对于 8 个元素的数组:
stride = 4: a[0] += a[4]; a[1] += a[5]; a[2] += a[6]; a[3] += a[7]
stride = 2: a[0] += a[2]; a[1] += a[1];
零元素现在包含总数。
class SimpleReduction
{
public:
int Reduce(accelerator_view& view, const std::vector<int>& source,
double& computeTime) const
{
assert(source.size() <= UINT_MAX);
int elementCount = static_cast<int>(source.size());
// Copy data
array<int, 1> a(elementCount, source.cbegin(), source.cend(), view);
std::vector<int> result(1);
int tailResult = (elementCount % 2) ? source[elementCount - 1] : 0;
array_view<int, 1> tailResultView(1, &tailResult);
for (int stride = (elementCount / 2); stride > 0; stride /= 2)
{
parallel_for_each(view, extent<1>(stride), [=, &a] (index<1> idx)
restrict(amp)
{
a[idx] += a[idx + stride];
// If there are an odd number of elements then the
// first thread adds the last element.
if ((idx[0] == 0) && (stride & 0x1) && (stride != 1))
tailResultView[idx] += a[stride - 1];
});
}
// Only copy out the first element in the array as this
// contains the final answer.
copy(a.section(0, 1), result.begin());
tailResultView.synchronize();
return result[0] + tailResult;
}
};
您可以将其平铺,其中平铺中的每个线程负责为其元素生成结果,然后将所有平铺的结果相加。
template <int TileSize>
class TiledReduction
{
public:
int Reduce(accelerator_view& view, const std::vector<int>& source,
double& computeTime) const
{
int elementCount = static_cast<int>(source.size());
// Copy data
array<int, 1> arr(elementCount, source.cbegin(), source.cend(), view);
int result;
computeTime = TimeFunc(view, [&]()
{
while (elementCount >= TileSize)
{
extent<1> e(elementCount);
array<int, 1> tmpArr(elementCount / TileSize);
parallel_for_each(view, e.tile<TileSize>(),
[=, &arr, &tmpArr] (tiled_index<TileSize> tidx) restrict(amp)
{
// For each tile do the reduction on the first thread of the tile.
// This isn't expected to be very efficient as all the other
// threads in the tile are idle.
if (tidx.local[0] == 0)
{
int tid = tidx.global[0];
int tempResult = arr[tid];
for (int i = 1; i < TileSize; ++i)
tempResult += arr[tid + i];
// Take the result from each tile and create a new array.
// This will be used in the next iteration. Use temporary
// array to avoid race condition.
tmpArr[tidx.tile[0]] = tempResult;
}
});
elementCount /= TileSize;
std::swap(tmpArr, arr);
}
// Copy the final results from each tile to the CPU and accumulate them
std::vector<int> partialResult(elementCount);
copy(arr.section(0, elementCount), partialResult.begin());
result = std::accumulate(partialResult.cbegin(), partialResult.cend(), 0);
});
return result;
}
};
这仍然不是最有效的解决方案,因为它没有良好的内存访问模式。您可以在本书的 Codeplex 网站上看到对此的进一步改进。