4

我在我的项目中使用 Alea GPU 时遇到了异常NullReferenceException,一切正常,直到我添加Parallel.For了一些代码之后Gpu.Default.For,这是我可以重现错误的最少代码:

[GpuManaged]
void fun()
{
    int[] gpu_arr = new int[5];
    Gpu.Default.For(0, 5, x =>
    {
        gpu_arr[x] = 1;
    });

    int[] arr = new int[10]; //moving this line above Gpu.Default.For solves the problem
    Parallel.For(0, 10, y =>
    {
        arr[y] = 1; //the exception won't be thrown after deleting this line
    });
}

异常详情:

System.NullReferenceException was unhandled by user code
  HResult=-2147467261
  Message=Object reference not set to an instance of an object.
  Source=Alea
  StackTrace:
       at Alea.Builders.ToUnmanagedMarshaler.ca3c0a8545c9ef8bb625e1642f06cc3b0(ToUnmanagedMarshalContext c51dcb01051c4c98d3eeb37994cb59840, IRType cced00522c9c21aa0da268f3a19ebc3a4, Object cfc437ec92b1c7022feaad7b85c894b89, IntPtr c935f4057f51a7fe14b62c80b19464d48)
       at Alea.Builders.clo@4253-523.Invoke(ToUnmanagedMarshalContext ctx, IRType irType, Object clrObject, IntPtr buffer)
       at Alea.Constructs.IRStructType.MarshalToUnmanaged(ToUnmanagedMarshalContext ctx, Object[] clrFields, IntPtr buffer, FSharpFunc`2 marshal)
       at Alea.Builders.ToUnmanagedMarshaler.ca3c0a8545c9ef8bb625e1642f06cc3b0(ToUnmanagedMarshalContext c51dcb01051c4c98d3eeb37994cb59840, IRType cced00522c9c21aa0da268f3a19ebc3a4, Object cfc437ec92b1c7022feaad7b85c894b89, IntPtr c935f4057f51a7fe14b62c80b19464d48)
       at Alea.Builders.Marshal@4304.Invoke(IRType irType, Object clrObject, IntPtr buffer)
       at Alea.Builders.ToUnmanagedMarshaler.Marshal[b](ToUnmanagedMarshalContext ctx, IRType irType, b clrObject, IntPtr buffer)
       at A.cf5aded17df9f7cc4c132234dda010fa7.clo@2275-622.Invoke(Int32 i, Object param)
       at Microsoft.FSharp.Primitives.Basics.List.loop@504-16[T](FSharpFunc`3 f, Int32 n, FSharpList`1 x)
       at Microsoft.FSharp.Primitives.Basics.List.iteri[T](FSharpFunc`2 action, FSharpList`1 x)
       at A.cf5aded17df9f7cc4c132234dda010fa7.clo@2272-621.Invoke(Unit _arg59)
       at Alea.Context.LockEval[e,f](e l, FSharpFunc`2 f)
       at Alea.Kernel.c771089de7170797e4f5abe71ebafe73a(FSharpOption`1 ca0018dd2e778e1acd84b6ef7eb063a47, LaunchParam cecd3f8288b62aa7a1097708339c27039, FSharpList`1 ca4856957094a49b77a445a80fc96b563)
       at Alea.Kernel.LaunchRaw(LaunchParam lp, FSharpOption`1 instanceOpt, FSharpList`1 args)
       at Alea.Parallel.Device.DeviceFor.For(Gpu gpu, Int32 fromInclusive, Int32 toExclusive, Action`1 op)
       at Alea.Parallel.GpuExtension.For(Gpu gpu, Int32 fromInclusive, Int32 toExclusive, Action`1 op)

我已经使用控制台和 Windows 窗体对其进行了测试,结果相同。那么,我做错了什么?或者这是一个错误?

4

0 回答 0