试图了解 cuda,在没有掌握类似的 stackoverflow 问题后,我决定测试一个示例(我使用 cudafy.net 进行 c#,但底层 cuda 应该是可解析的)
我想做以下事情。发送一个 4x4x4 的矩阵到内核,按照这个逻辑得到一个 4x4x4 的输出:
if(input[x,y,z] == 1)
output[x+1, y, z]++;
if(input[x,y,z] == 2)
output[x-1, y, z]++;
我研究了以下 cudafy 示例。
public const int N = 1 * 1024;
//Omissions
gpu.Launch(128, 1, function, dev_a, dev_b, dev_c);
核心:
[Cudafy]
public static void add_0(GThread thread, int[] a, int[] b, int[] c)
{
int tid = thread.blockIdx.x; // (tid 0 -> 127, from my understanding)
while (tid < N)
{
c[tid] = a[tid] + b[tid];
tid += thread.gridDim.x;
}
}
然后尝试将其转移到3d。我无法正确建立索引。说我有以下。(这里的三个数组只是为了测试索引)
int size = 4;
int[] dev_delta = gpu.Allocate<int>(size * size * size);
int[] dev_space = gpu.Allocate<int>(size * size * size);
int[] dev_result = gpu.Allocate<int>(size * size * size);
gpu.Launch(new dim3(4, 4, 4), 1, "testIndex", dev_delta, dev_space, dev_result);
和内核:
[Cudafy]
public static void testIndex(GThread thread, int[] delta, int[] space, int[] result)
{
int x = thread.blockIdx.x;
int y = thread.blockIdx.y;
int z = thread.blockIdx.z;
delta[x]++;
space[y]++;
result[z]++;
}
我天真地期待以下内容:
delta = {4,4,4,4,0,0,0,0,0, ... 0,0}
space = {4,4,4,4,0,0,0,0,0, ... 0,0}
result = {4,4,4,4,0,0,0,0,0 ... 0,0}
但我得到:
delta = {1,1,1,1,0,0,0,0,0, ... 0,0}
space = {1,1,1,1,0,0,0,0,0, ... 0,0}
result = {1,0,0,0,0,0,0,0,0 ... 0,0}
这对我来说毫无意义,显然我错过了一些东西。
问题:
我开始了多少线程?
您如何在 3 维中“索引”我的示例问题(启动 4x4x4 线程并获取 flat3DArray[x * sizeY * sizeZ + y * sizeZ + z] 的变量)?
您如何在二维中“索引”我的示例问题?(启动 4x4 线程,然后让每个线程处理长度为 4 的深度列)
我发现这可能是相关的为什么 CUDA 内核中的 z 总是为零,如果这让我很困惑,我仍然很感激纯 cuda 的答案来整理我的大脑