【问题标题】:Passing more than 16 kernel arguments in Alea.GPU在 Alea.GPU 中传递超过 16 个内核参数
【发布时间】:2015-05-23 19:36:19
【问题描述】:

我正在尝试编写一个相当复杂的内核。事实证明,我需要传递超过 16 个参数,显然 Alea GPU 有 16 个参数的限制。 (http://quantalea.com/static/app/manual/reference/alea_cuda_il/alea-cuda-il-ilgpumodule.html)

我知道 16 个参数一开始听起来是个坏主意……还有哪些其他选择?在普通代码中,我当然会将这些东西包装到它自己的类中,但是在 GPU 代码中我能做什么呢?

【问题讨论】:

  • 你能传递一个数组或列表吗?
  • 你通过deviceptr<T>s 获取矢量数据,但这里是关于传递不同的东西,例如2个数字,3个数组等

标签: c# .net gpu aleagpu


【解决方案1】:

在这种情况下,您可以通过GPUModule.GPUEntities 属性检索一个无类型的内核对象,然后将这些参数放入Object 类型的列表中,然后您可以启动它。

您还可以为此目的创建一些扩展方法并使其类型安全,这是一个示例,为了简单起见,我只使用了 3 个参数:

public static class GPUModuleExtensions
{
    public static void MyGPULaunch<T1, T2, T3>(
        this ILGPUModule module,
        Action<T1, T2, T3> kernelD, LaunchParam lp,
        T1 arg1, T2 arg2, T3 arg3)
    {
        // get the kernel object by method name
        var kernel = module.GPUEntities.GetKernel(kernelD.Method.Name).Kernel;
        // create parameter list (which is FSharpList)
        var parameterArray = new object[] {arg1, arg2, arg3};
        var parameterList = ListModule.OfArray(parameterArray);
        // use untyped LaunchRaw to launch the kernel
        kernel.LaunchRaw(lp, parameterList);
    }
}

public class GPUModule : ILGPUModule
{
    public GPUModule() : base(GPUModuleTarget.DefaultWorker)
    {
    }

    [Kernel]
    public void Kernel(deviceptr<int> outputs, int arg1, int arg2)
    {
        var tid = threadIdx.x;
        outputs[tid] = arg1 + arg2;
    }

    [Test]
    public void Test()
    {
        const int n = 32;
        var lp = new LaunchParam(1, n);
        using (var outputs = GPUWorker.Malloc<int>(n))
        {
            this.MyGPULaunch(Kernel, lp, outputs.Ptr, 1, 3);
            Console.WriteLine("{0}", (outputs.Gather())[4]);
        }
    }
}

注意,在这个例子中,我使用Action&lt;T1,T2,T3&gt;,但Action 类型最多有16 种类型,因此您可能需要定义自己的委托来传递超过16 种参数类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-14
    • 2018-01-09
    • 2013-12-02
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多