【发布时间】:2015-03-09 15:19:47
【问题描述】:
使用 Cudafy 开始我的第一步,并尝试编写一个函数,该函数将获取其线程的位置,并在此基础上将一些 int 值保存到数组元素中。 我的代码:
[Cudafy]
public static void GenerateRipples(GThread thread, int[] results)
{
int threadPosInBlockX = thread.threadIdx.x;
int threadPosInBlockY = thread.threadIdx.y;
int blockPosInGridX = thread.blockIdx.x;
int blockPosInGridY = thread.blockIdx.y;
int gridSizeX = thread.gridDim.x;
int gridSizeY = thread.gridDim.y;
int blockSizeX = thread.blockDim.x;
int blockSizeY = thread.blockDim.y;
//int threadX = blockSizeX*blockPosInGridX + threadPosInBlockX;
//if i use only one variable, everything is fine:
int threadY = blockSizeY;
//if i add or multiply anything, it cannot compile:
//int threadY = blockSizeY*blockPosInGridY + threadPosInBlockY;
// results[gridSizeX*blockSizeX*threadY + threadX] = 255;
}
所以我不能在这里计算threadY。如果我在计算中使用多个变量,Cudafy 翻译类会引发错误(CudafyModule cm = CudafyTranslator.Cudafy(); 引发 Cudafy.CudafyLanguageException)。
我做错了什么?
更新: 这是在 GPU 上运行内核的代码:
public void RunTest2()
{
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, CudafyModes.DeviceId);
CudafyModule km = CudafyTranslator.Cudafy();
gpu.LoadModule(km);
int size = 20 * 20;
int[] allPixels = new int[size];
int[] dev_result = gpu.Allocate<int>(size);
dim3 blocksInGrid = new dim3(5, 5);
dim3 threadsPerBlock = new dim3(4, 4);
gpu.Launch(blocksInGrid, threadsPerBlock).GenerateRipples(dev_result);
gpu.CopyFromDevice(dev_result, allPixels);
gpu.FreeAll();
}
【问题讨论】:
标签: c# cuda cudafy.net