【发布时间】:2017-10-13 07:16:32
【问题描述】:
试图了解 cuda,在没有掌握类似的 stackoverflow 问题后,我决定测试一个示例(我在 c# 中使用 cudafy.net,但底层 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 的深度列)
我发现这可能与 Why is z always zero in CUDA kernel 相关,如果这让我感到困惑,我仍然会很感激纯 cuda 的答案来整理我的大脑
【问题讨论】:
标签: c# cuda gpgpu cudafy.net