如何在C#中使用ALEA库的递归

时间:2017-01-29 08:17:52

标签: c# recursion gpu aleagpu

我正在尝试使用ALEA库将递归算法从CPU转换为GPU。如果我构建代码,我会收到以下错误:

" Fody / Alea.CUDA:AOTCompileServer意外退出,退出代码为-1073741571"

public class GPUModule : ILGPUModule
{
 public GPUModule (GPUModuleTarget target) : base(target)
 {
 }

 [Kernel]  //Same Error whether RecursionTest is another Kernel or not.
 public void RecursionTest(deviceptr<int> a)
 {
   ...
   RecursionTest(a);
 }

 [Kernel]
 public MyKernel(deviceptr<int> a, ...)
 {
   ...
   var a = __shared__.Array<int>(10);
   RecursionTest(Intrinsic.__array_to_ptr<int>(a)); //Error here
 }
 ...
}

如果您使用ALEA Library在C#中提供递归示例的任何文档或链接,我将不胜感激。

提前致谢

1 个答案:

答案 0 :(得分:1)

您使用的是Alea GPU 2.x,最新版本是Alea GPU 3.x. (见www.aleagpu.com)。使用3.0,我做了一个测试,它的工作原理:

using Alea;
using Alea.CSharp;
using NUnit.Framework;

    public static void RecursionTestFunc(deviceptr<int> a)
    {
        if (a[0] == 0)
        {
            a[0] = -1;
        }
        else
        {
            a[0] -= 1;
            RecursionTestFunc(a);
        }
    }

    public static void RecursionTestKernel(int[] a)
    {
        var tid = threadIdx.x;
        var ptr = DeviceFunction.AddressOfArray(a);
        ptr += tid;
        RecursionTestFunc(ptr);
    }

    [Test]
    public static void RecursionTest()
    {
        var gpu = Gpu.Default;
        var host = new[] {1, 2, 3, 4, 5};
        var length = host.Length;
        var dev = gpu.Allocate(host);
        gpu.Launch(RecursionTestKernel, new LaunchParam(1, length), dev);
        var actual = Gpu.CopyToHost(dev);
        var expected = new[] {-1, -1, -1, -1, -1};
        Assert.AreEqual(expected, actual);
        Gpu.Free(dev);
    }