gpt4 book ai didi

algorithm - 并行前缀和 CUDAfy

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:41:57 28 4
gpt4 key购买 nike

我需要一种算法来计算数组的并行前缀和而不使用共享内存。如果除了使用共享内存别无选择,解决冲突问题的最佳方法是什么?

最佳答案

此链接包含对并行前缀和的顺序和并行算法的详分割析:

Parallel Prefix Sum (Scan) with CUDA

它还包含用于实现并行前缀算法的 C 代码片段以及避免共享内存冲突的详细说明。

您可以将代码移植到 CUDAfy 或简单地定义 C 区域并将它们用作应用程序中的非托管代码。但是 CUDA C 代码中有几个错误。我正在 Cudafy.NET 中编写代码的更正版本

[Cudafy]
public static void prescan(GThread thread, int[] g_odata, int[] g_idata, int[] n)
{
int[] temp = thread.AllocateShared<int>("temp", threadsPerBlock);//threadsPerBlock is user defined
int thid = thread.threadIdx.x;
int offset = 1;
if (thid < n[0]/2)
{
temp[2 * thid] = g_idata[2 * thid]; // load input into shared memory
temp[2 * thid + 1] = g_idata[2 * thid + 1];

for (int d = n[0] >> 1; d > 0; d >>= 1) // build sum in place up the tree
{
thread.SyncThreads();
if (thid < d)
{
int ai = offset * (2 * thid + 1) - 1;
int bi = offset * (2 * thid + 2) - 1;
temp[bi] += temp[ai];
}
offset *= 2;
}
if (thid == 0)
{
temp[n[0] - 1] = 0;
} // clear the last element


for (int d = 1; d < n[0]; d *= 2) // traverse down tree & build scan
{
offset >>= 1;
thread.SyncThreads();
if (thid < d)
{
int ai = offset * (2 * thid + 1) - 1;
int bi = offset * (2 * thid + 2) - 1;
int t = temp[ai];
temp[ai] = temp[bi];
temp[bi] += t;
}
}
thread.SyncThreads();
g_odata[2 * thid] = temp[2 * thid]; // write results to device memory
g_odata[2 * thid + 1] = temp[2 * thid + 1];
}
}

您可以使用上面修改后的代码代替链接中的代码。

关于algorithm - 并行前缀和 CUDAfy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34705785/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com