我们需要看看你是如何启动你的内核的,上面的代码应该运行得很好。我创建了一个运行良好的测试类,并为您提供了如何准备内核网格/块/线程维度的示例。如果您想查看出色的示例,请下载 Cudafy 源代码并编译 CudafyExamples 项目,查看它们如何准备和使用 CUDAfy 的功能。
** 注意:我在发布第一堂课之前一定抽了一些不错的东西,我忽略了验证它没有产生内存访问冲突!!
固定类以下,没有违规。
在Codeproject和StackOverflow上查找很好的示例。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Cudafy;
using Cudafy.Host;
using Cudafy.Translator;
namespace FxKernelTest
{
public class FxKernTest
{
public GPGPU fxgpu;
public const int N = 1024 * 64;
public void ExeTestKernel()
{
GPGPU gpu = CudafyHost.GetDevice(CudafyModes.Target, 0);
eArchitecture arch = gpu.GetArchitecture();
CudafyModule km = CudafyTranslator.Cudafy(arch);
gpu.LoadModule(km);
int[] host_results = new int[N];
// Either assign a new block of memory to hold results on device
var dev_results = gpu.Allocate<int>(N);
gpu.Set<int>(dev_results);
// Or fill your array with values first and then
for (int i = 0; i < N; i++) host_results[i] = i * 3;
// Copy array with ints to device
//var dev_filled_results = gpu.CopyToDevice(host_results);
// 64*16 = 1024 threads per block (which is max for sm_30)
dim3 threadsPerBlock = new dim3(64, 16);
// 8*8 = 64 blocks per grid, 1024 threads per block = kernel launched 65536 times
dim3 blocksPerGrid = new dim3(8, 8);
//var threadsPerBlock = 1024; // this will only give you blockDim.x = 1024, .y = 0, .z = 0
//var blocksPerGrid = 1; // just for show
gpu.Launch(blocksPerGrid, threadsPerBlock, "GenerateRipples", dev_results);
gpu.CopyFromDevice(dev_results, host_results);
// Test our results
for (int index = 0; index < N; index++)
if (host_results[index] != index)
throw new Exception("Check your indexing math, genius!!!");
}
[Cudafy]
public static void GenerateRipples(GThread thread, int[] results)
{
var blockSize = thread.blockDim.x * thread.blockDim.y;
var offsetToGridY = blockSize * thread.gridDim.x;
// This took me a few tries, I've never used 4 dimensions into a 1D array beofre :)
var tid = thread.blockIdx.y * offsetToGridY + // each Grid Y is 8192 in size
thread.blockIdx.x * blockSize + // each Grid X is 1024 in size
thread.threadIdx.y * thread.blockDim.x + // each Block Y is 64 in size
thread.threadIdx.x; // index into block
var threadPosInBlockX = thread.threadIdx.x;
var threadPosInBlockY = thread.threadIdx.y;
var blockPosInGridX = thread.blockIdx.x;
var blockPosInGridY = thread.blockIdx.y;
var gridSizeX = thread.gridDim.x;
var gridSizeY = thread.gridDim.y;
var blockSizeX = thread.blockDim.x;
var blockSizeY = thread.blockDim.y;
// this is your code, see how I calculate the actual thread ID above!
var threadX = blockSizeX * blockPosInGridX + threadPosInBlockX;
//if i use only one variable, everything is fine:
var threadY = blockSizeY;
// this calculates just fine
threadY = blockSizeY * blockPosInGridY + threadPosInBlockY;
// hint: use NSight for Visual Studio and look at the NSight output,
// it reports access violations and tells you where...
// if our threadId is within bounds of array size
// we cause access violation if not
// (class constants are automatically passed to kernels)
if (tid < N)
results[tid] = tid;
}
}
}
ptxas 信息:0 字节 gmem ptxas 信息:为“sm_30”编译入口函数“GenerateRipples” ptxas 信息:GenerateRipples 的函数属性 0 字节堆栈帧,0 字节溢出存储,0 字节溢出加载 ptxas 信息:使用 5 个寄存器,328 字节 cmem [0]